author | Tero Marttila <terom@fixme.fi> |
Thu, 02 Apr 2009 21:11:01 +0300 | |
changeset 3 | ff98fa9b84ce |
parent 2 | e66102ab7048 |
child 4 | 8b633782f02d |
permissions | -rw-r--r-- |
0 | 1 |
""" |
2 |
Configuration file output for the ISC DHCP server |
|
3 |
""" |
|
4 |
||
5 |
import conf |
|
6 |
||
7 |
import itertools |
|
8 |
||
3 | 9 |
class Object (conf.ConfObject) : |
10 |
""" |
|
11 |
Our version of ConfObject |
|
12 |
""" |
|
13 |
||
14 |
def _fmt_comments (self) : |
|
15 |
""" |
|
16 |
Format our comment lines |
|
17 |
""" |
|
18 |
||
19 |
for comment in self.comments : |
|
20 |
if comment is not None : |
|
21 |
yield "# %s" % (comment, ) |
|
22 |
||
23 |
||
24 |
class Comment (Object) : |
|
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
25 |
""" |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
26 |
A comment, is, well, a comment :) |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
27 |
""" |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
28 |
|
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
29 |
def __init__ (self, comment) : |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
30 |
""" |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
31 |
@param comment the comment string |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
32 |
""" |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
33 |
|
3 | 34 |
Object.__init__(self, [comment]) |
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
35 |
|
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
36 |
def fmt_lines (self) : |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
37 |
""" |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
38 |
Yield a single line with the comment |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
39 |
""" |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
40 |
|
3 | 41 |
return self._fmt_comments() |
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
42 |
|
3 | 43 |
class _Section (Object) : |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
44 |
""" |
3 | 45 |
Base implementation of Section, but doesn't format comments in output (inheriting class can define how that happens) |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
46 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
47 |
|
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
48 |
def __init__ (self, params=None, decls=None, comment=None) : |
0 | 49 |
""" |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
50 |
If params/decls are given, those are the used as the initial contents of this section |
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
51 |
|
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
52 |
If a comment is given, then it will be formatted before the section's stuff |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
53 |
""" |
3 | 54 |
|
55 |
Object.__init__(self, [comment]) |
|
0 | 56 |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
57 |
self.params = params or [] |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
58 |
self.decls = decls or [] |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
59 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
60 |
def add_param (self, param) : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
61 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
62 |
Add the given Parameter to the end of this section's params |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
63 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
64 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
65 |
self.params.append(param) |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
66 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
67 |
def add_params (self, params) : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
68 |
for param in params : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
69 |
self.add_param(param) |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
70 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
71 |
def add_decl (self, decl) : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
72 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
73 |
Add the given Declaration to the end of this section's decls |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
74 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
75 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
76 |
self.decls.append(decl) |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
77 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
78 |
def add_decls (self, decls) : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
79 |
for decl in decls : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
80 |
self.add_decl(decl) |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
81 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
82 |
def fmt_lines (self) : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
83 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
84 |
Format all of our params and decls, in that order |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
85 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
86 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
87 |
# then output each content line |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
88 |
for stmt in itertools.chain(self.params, self.decls) : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
89 |
# skip Nones |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
90 |
if stmt is None : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
91 |
continue |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
92 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
93 |
for line in stmt.fmt_lines() : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
94 |
yield line |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
95 |
|
3 | 96 |
class Section (_Section) : |
97 |
""" |
|
98 |
A section holds a list of params and a list of decls, plus some comments at the beginning of the section |
|
99 |
""" |
|
100 |
||
101 |
def fmt_lines (self) : |
|
102 |
""" |
|
103 |
Format all of our comments, and then super |
|
104 |
""" |
|
105 |
||
106 |
# comments |
|
107 |
for line in self._fmt_comments() : |
|
108 |
yield line |
|
109 |
||
110 |
# section stuff |
|
111 |
for line in _Section.fmt_lines(self) : |
|
112 |
yield line |
|
113 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
114 |
class ConfFile (Section, conf.File) : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
115 |
DEFAULT_NAME = "dhcpd.conf" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
116 |
DEFAULT_PATH = "/etc/dhcp3/dhcpd.conf" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
117 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
118 |
def __init__ (self, name=DEFAULT_NAME, path=DEFAULT_PATH, params=None, decls=None) : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
119 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
120 |
Initialize the dhcpd config file, but don't open it yet. |
0 | 121 |
""" |
122 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
123 |
conf.File.__init__(self, name, path) |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
124 |
Section.__init__(self, params, decls) |
0 | 125 |
|
3 | 126 |
class Statement (Object) : |
0 | 127 |
""" |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
128 |
A statement is a single line in the config file |
0 | 129 |
""" |
130 |
||
3 | 131 |
def __init__ (self, name, *args, **kwargs) : |
0 | 132 |
""" |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
133 |
Arguments given as None will be ignored. |
3 | 134 |
|
135 |
A comment can be given as a keyword argument |
|
0 | 136 |
""" |
137 |
||
3 | 138 |
if kwargs : assert len(kwargs) == 1 and 'comment' in kwargs |
139 |
||
140 |
Object.__init__(self, [kwargs.get('comment')]) |
|
141 |
||
0 | 142 |
self.name = name |
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
143 |
self.args = [arg for arg in args if arg is not None] |
0 | 144 |
|
145 |
def _fmt_arg (self, arg) : |
|
146 |
""" |
|
147 |
Formats a arg for use in output, the following types are supported: |
|
148 |
||
149 |
list/tuple/iter: results in a comma-and-space separated list of formatted values |
|
150 |
unicode: results in an encoded str |
|
151 |
str: results in the string itself, quoted if needed |
|
152 |
other: attempt to convert to a str, and then format that |
|
153 |
""" |
|
154 |
||
155 |
# format lists specially |
|
156 |
# XXX: iterators? |
|
157 |
if isinstance(arg, (list, tuple)) : |
|
158 |
# recurse as a comma-and-space separated list |
|
159 |
return ', '.join(self._fmt_arg(a) for a in arg) |
|
160 |
||
161 |
elif isinstance(arg, Literal) : |
|
162 |
# use what it specifies |
|
163 |
return arg.fmt_arg() |
|
164 |
||
165 |
elif isinstance(arg, unicode) : |
|
166 |
# recurse with the str version |
|
167 |
# XXX: what encoding to use? |
|
168 |
return self._fmt_arg(arg.encode('utf8')) |
|
169 |
||
170 |
elif isinstance(arg, str) : |
|
171 |
# XXX: quoting |
|
172 |
return arg |
|
173 |
||
174 |
else : |
|
175 |
# try and use it as a string |
|
176 |
return self._fmt_arg(str(arg)) |
|
177 |
||
178 |
def _fmt_data (self) : |
|
179 |
""" |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
180 |
Formats the statement name/params as a single line, ignoring None |
0 | 181 |
""" |
182 |
||
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
183 |
return "%s%s" % (self.name, (' ' + ' '.join(self._fmt_arg(a) for a in self.args)) if self.args else '') |
0 | 184 |
|
185 |
class Literal (Statement) : |
|
186 |
""" |
|
187 |
A literal is something that goes into the config file as-is, with no formatting or escaping applied. |
|
188 |
""" |
|
189 |
||
190 |
def __init__ (self, literal) : |
|
191 |
self.literal = literal |
|
192 |
||
193 |
def fmt_arg (self) : |
|
194 |
return self.literal |
|
195 |
||
196 |
def fmt_lines (self) : |
|
197 |
yield self.literal |
|
198 |
||
199 |
class Parameter (Statement) : |
|
200 |
""" |
|
201 |
A parameter is a single statement that configures the behaviour of something. |
|
202 |
||
203 |
Parameters have a name, and optionally, a number of arguments, and are formatted as statements terminated with |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
204 |
a semicolon. For convenience, params/decls that are None are ignored. |
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
205 |
|
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
206 |
The parameter will be formatted like this: |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
207 |
<name> [ <arg> [ ... ] ] ";" |
0 | 208 |
""" |
209 |
||
210 |
def fmt_lines (self) : |
|
211 |
""" |
|
212 |
Yields a single ;-terminated line |
|
213 |
""" |
|
3 | 214 |
|
215 |
# comments |
|
216 |
for line in self._fmt_comments() : |
|
217 |
yield line |
|
218 |
||
219 |
# the line |
|
0 | 220 |
yield "%s;" % self._fmt_data() |
221 |
||
3 | 222 |
class Declaration (_Section, Statement) : |
0 | 223 |
""" |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
224 |
A declaration begins like a statement (with name and args), but then contains a curly-braces-delimited block |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
225 |
that acts like a Section. |
0 | 226 |
|
227 |
<name> [ <args> [ ... ] ] { |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
228 |
[ <Section> ] |
0 | 229 |
} |
230 |
||
231 |
""" |
|
232 |
||
3 | 233 |
def __init__ (self, name, args=[], params=None, decls=None, comment=None) : |
0 | 234 |
""" |
235 |
The name/args will be formatted as in Statement, but params should be an iterable of Parameters, and decls |
|
236 |
an iterable of Declarations. |
|
237 |
""" |
|
238 |
||
239 |
# init the statement bit |
|
3 | 240 |
_Section.__init__(self, params, decls) |
241 |
Statement.__init__(self, name, *args, **dict(comment=comment)) |
|
0 | 242 |
|
243 |
def fmt_lines (self) : |
|
244 |
""" |
|
245 |
Yields a header line, a series of indented body lines, and the footer line |
|
246 |
""" |
|
247 |
||
3 | 248 |
# comments |
249 |
for line in self._fmt_comments() : |
|
250 |
yield line |
|
251 |
||
0 | 252 |
# the header to open the block |
253 |
yield "%s {" % self._fmt_data() |
|
254 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
255 |
# then output the section stuff, indented |
3 | 256 |
for line in _Section.fmt_lines(self) : |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
257 |
yield "\t%s" % line |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
258 |
|
0 | 259 |
# and then close the block |
260 |
yield "}" |
|
261 |
||
262 |
class SharedNetwork (Declaration) : |
|
263 |
""" |
|
264 |
A shared-network declaration is used to define a set of subnets that share the same physical network, |
|
265 |
optionally with some shared params. |
|
266 |
||
267 |
shared-network <name> { |
|
268 |
[ parameters ] |
|
269 |
[ declarations ] |
|
270 |
} |
|
271 |
""" |
|
272 |
||
273 |
def __init__ (self, name, params=[], decls=[]) : |
|
274 |
""" |
|
275 |
@param name the name of the shared-subnet |
|
276 |
@param params optional parameters |
|
277 |
@param decls the iterable of subnets or other declarations in the shared network |
|
278 |
""" |
|
279 |
||
280 |
super(SharedNetwork, self).__init__("shared-network", [name], params, decls) |
|
281 |
||
282 |
class Subnet (Declaration) : |
|
283 |
""" |
|
284 |
A subnet is used to provide the information about a subnet required to identify whether or not an IP address is |
|
285 |
on that subnet, and may also be used to specify parameters/declarations for that subnet. |
|
286 |
||
287 |
subnet <subnet-number> netmask <netmask> { |
|
288 |
[ parameters ] |
|
289 |
[ declarations ] |
|
290 |
} |
|
291 |
""" |
|
292 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
293 |
def __init__ (self, network, params=None, decls=None) : |
0 | 294 |
""" |
295 |
@param network the addr.Network for the subnet |
|
296 |
@param params optional parameters |
|
297 |
@param decls optional decls, e.g. subnets |
|
298 |
""" |
|
299 |
||
300 |
super(Subnet, self).__init__("subnet", [network.net(), "netmask", network.netmask()], params, decls) |
|
301 |
||
302 |
class Group (Declaration) : |
|
303 |
""" |
|
304 |
A group is simply used to apply a set of parameters to a set of declarations. |
|
305 |
||
306 |
group { |
|
307 |
[ parameters ] |
|
308 |
[ declarations ] |
|
309 |
} |
|
310 |
""" |
|
311 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
312 |
def __init__ (self, params=None, decls=None) : |
0 | 313 |
super(Group, self).__init__("group", [], params, decls) |
314 |
||
315 |
||
316 |
class Host (Declaration) : |
|
317 |
""" |
|
318 |
A host is used to match a request against specific host, and then apply settings for that host. |
|
319 |
||
320 |
The "hostname" is the DHCP name to identify the host. |
|
321 |
||
322 |
If no dhcp-client-identifier option is specified in the parameters, then the host is matched using the |
|
323 |
"hardware" parameter. |
|
324 |
||
325 |
host <hostname> { |
|
326 |
[ parameters ] |
|
327 |
[ declarations ] |
|
328 |
} |
|
329 |
""" |
|
330 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
331 |
def __init__ (self, hostname, params=None, decls=None) : |
0 | 332 |
super(Host, self).__init__("host", [hostname], params, decls) |
333 |
||
334 |
class Option (Parameter) : |
|
335 |
""" |
|
336 |
A generic 'option' parameter for a dhcpd.conf file |
|
337 |
""" |
|
338 |
||
339 |
def __init__ (self, name, *args) : |
|
340 |
""" |
|
341 |
Formatted as a Satement with a name of "option <name>". |
|
342 |
""" |
|
343 |
||
344 |
super(Option, self).__init__("option %s" % name, *args) |
|
345 |