4 |
4 |
5 import conf |
5 import conf |
6 |
6 |
7 import itertools |
7 import itertools |
8 |
8 |
|
9 class Comment (conf.ConfObject) : |
|
10 """ |
|
11 A comment, is, well, a comment :) |
|
12 |
|
13 Currently, comments are only one line, and look like the following: |
|
14 "#" <comment> |
|
15 """ |
|
16 |
|
17 def __init__ (self, comment) : |
|
18 """ |
|
19 @param comment the comment string |
|
20 """ |
|
21 |
|
22 self.comment = comment |
|
23 |
|
24 def fmt_lines (self) : |
|
25 """ |
|
26 Yield a single line with the comment |
|
27 """ |
|
28 |
|
29 yield "# %s" % (self.comment, ) |
|
30 |
9 class Section (conf.ConfObject) : |
31 class Section (conf.ConfObject) : |
10 """ |
32 """ |
11 A section holds a list of params and a list of decls |
33 A section holds a list of params and a list of decls |
12 """ |
34 """ |
13 |
35 |
14 def __init__ (self, params=None, decls=None) : |
36 def __init__ (self, params=None, decls=None, comment=None) : |
15 """ |
37 """ |
16 If params/decls are given, those are the used as the initial contents of this section |
38 If params/decls are given, those are the used as the initial contents of this section |
|
39 |
|
40 If a comment is given, then it will be formatted before the section's stuff |
17 """ |
41 """ |
18 |
42 |
19 self.params = params or [] |
43 self.params = params or [] |
20 self.decls = decls or [] |
44 self.decls = decls or [] |
|
45 self.comment = comment |
21 |
46 |
22 def add_param (self, param) : |
47 def add_param (self, param) : |
23 """ |
48 """ |
24 Add the given Parameter to the end of this section's params |
49 Add the given Parameter to the end of this section's params |
25 """ |
50 """ |
39 |
64 |
40 def add_decls (self, decls) : |
65 def add_decls (self, decls) : |
41 for decl in decls : |
66 for decl in decls : |
42 self.add_decl(decl) |
67 self.add_decl(decl) |
43 |
68 |
|
69 def _fmt_comment (self) : |
|
70 """ |
|
71 Format our comment line |
|
72 """ |
|
73 |
|
74 return "# %s" % (self.comment, ) |
|
75 |
|
76 |
44 def fmt_lines (self) : |
77 def fmt_lines (self) : |
45 """ |
78 """ |
46 Format all of our params and decls, in that order |
79 Format all of our params and decls, in that order |
47 """ |
80 """ |
|
81 |
|
82 # comment? |
|
83 if self.comment : |
|
84 yield self._fmt_comment() |
48 |
85 |
49 # then output each content line |
86 # then output each content line |
50 for stmt in itertools.chain(self.params, self.decls) : |
87 for stmt in itertools.chain(self.params, self.decls) : |
51 # skip Nones |
88 # skip Nones |
52 if stmt is None : |
89 if stmt is None : |
72 A statement is a single line in the config file |
109 A statement is a single line in the config file |
73 """ |
110 """ |
74 |
111 |
75 def __init__ (self, name, *args) : |
112 def __init__ (self, name, *args) : |
76 """ |
113 """ |
77 The statement will be formatted like this: |
|
78 <name> [ <arg> [ ... ] ] ";" |
|
79 |
|
80 Arguments given as None will be ignored. |
114 Arguments given as None will be ignored. |
81 """ |
115 """ |
82 |
116 |
83 self.name = name |
117 self.name = name |
84 self.args = args |
118 self.args = [arg for arg in args if arg is not None] |
85 |
119 |
86 def _fmt_arg (self, arg) : |
120 def _fmt_arg (self, arg) : |
87 """ |
121 """ |
88 Formats a arg for use in output, the following types are supported: |
122 Formats a arg for use in output, the following types are supported: |
89 |
123 |
119 def _fmt_data (self) : |
153 def _fmt_data (self) : |
120 """ |
154 """ |
121 Formats the statement name/params as a single line, ignoring None |
155 Formats the statement name/params as a single line, ignoring None |
122 """ |
156 """ |
123 |
157 |
124 return "%s%s" % (self.name, (' ' + ' '.join(self._fmt_arg(a) for a in self.args if a is not None)) if self.args else '') |
158 return "%s%s" % (self.name, (' ' + ' '.join(self._fmt_arg(a) for a in self.args)) if self.args else '') |
125 |
159 |
126 class Literal (Statement) : |
160 class Literal (Statement) : |
127 """ |
161 """ |
128 A literal is something that goes into the config file as-is, with no formatting or escaping applied. |
162 A literal is something that goes into the config file as-is, with no formatting or escaping applied. |
129 """ |
163 """ |
141 """ |
175 """ |
142 A parameter is a single statement that configures the behaviour of something. |
176 A parameter is a single statement that configures the behaviour of something. |
143 |
177 |
144 Parameters have a name, and optionally, a number of arguments, and are formatted as statements terminated with |
178 Parameters have a name, and optionally, a number of arguments, and are formatted as statements terminated with |
145 a semicolon. For convenience, params/decls that are None are ignored. |
179 a semicolon. For convenience, params/decls that are None are ignored. |
|
180 |
|
181 The parameter will be formatted like this: |
|
182 <name> [ <arg> [ ... ] ] ";" |
146 """ |
183 """ |
147 |
184 |
148 def fmt_lines (self) : |
185 def fmt_lines (self) : |
149 """ |
186 """ |
150 Yields a single ;-terminated line |
187 Yields a single ;-terminated line |