author | Tero Marttila <terom@fixme.fi> |
Thu, 02 Apr 2009 20:54:37 +0300 | |
changeset 2 | e66102ab7048 |
parent 1 | 2223ade4f259 |
child 3 | ff98fa9b84ce |
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 |
||
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
|
9 |
class Comment (conf.ConfObject) : |
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
|
10 |
""" |
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
|
11 |
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
|
12 |
|
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
|
13 |
Currently, comments are only one line, and look like the following: |
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
|
14 |
"#" <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
|
15 |
""" |
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
|
16 |
|
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
|
17 |
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
|
18 |
""" |
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
|
19 |
@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
|
20 |
""" |
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
|
21 |
|
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
|
22 |
self.comment = 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
|
23 |
|
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
|
24 |
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
|
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 |
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
|
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 |
yield "# %s" % (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 |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
31 |
class Section (conf.ConfObject) : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
32 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
33 |
A section holds a list of params and a list of decls |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
34 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
35 |
|
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
|
36 |
def __init__ (self, params=None, decls=None, comment=None) : |
0 | 37 |
""" |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
38 |
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
|
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 |
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
|
41 |
""" |
0 | 42 |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
43 |
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
|
44 |
self.decls = decls or [] |
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
|
45 |
self.comment = comment |
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 |
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
|
48 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
49 |
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
|
50 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
51 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
52 |
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
|
53 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
54 |
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
|
55 |
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
|
56 |
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
|
57 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
58 |
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
|
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 |
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
|
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 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
63 |
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
|
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 |
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
|
66 |
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
|
67 |
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
|
68 |
|
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
|
69 |
def _fmt_comment (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
|
70 |
""" |
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
|
71 |
Format our comment line |
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
|
72 |
""" |
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
|
73 |
|
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
|
74 |
return "# %s" % (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
|
75 |
|
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
|
76 |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
77 |
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
|
78 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
79 |
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
|
80 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
81 |
|
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
|
82 |
# 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
|
83 |
if 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
|
84 |
yield self._fmt_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
|
85 |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
86 |
# 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
|
87 |
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
|
88 |
# 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
|
89 |
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
|
90 |
continue |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
91 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
92 |
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
|
93 |
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
|
94 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
99 |
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
|
100 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
101 |
Initialize the dhcpd config file, but don't open it yet. |
0 | 102 |
""" |
103 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
104 |
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
|
105 |
Section.__init__(self, params, decls) |
0 | 106 |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
107 |
class Statement (conf.ConfObject) : |
0 | 108 |
""" |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
109 |
A statement is a single line in the config file |
0 | 110 |
""" |
111 |
||
112 |
def __init__ (self, name, *args) : |
|
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 |
Arguments given as None will be ignored. |
0 | 115 |
""" |
116 |
||
117 |
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
|
118 |
self.args = [arg for arg in args if arg is not None] |
0 | 119 |
|
120 |
def _fmt_arg (self, arg) : |
|
121 |
""" |
|
122 |
Formats a arg for use in output, the following types are supported: |
|
123 |
||
124 |
list/tuple/iter: results in a comma-and-space separated list of formatted values |
|
125 |
unicode: results in an encoded str |
|
126 |
str: results in the string itself, quoted if needed |
|
127 |
other: attempt to convert to a str, and then format that |
|
128 |
""" |
|
129 |
||
130 |
# format lists specially |
|
131 |
# XXX: iterators? |
|
132 |
if isinstance(arg, (list, tuple)) : |
|
133 |
# recurse as a comma-and-space separated list |
|
134 |
return ', '.join(self._fmt_arg(a) for a in arg) |
|
135 |
||
136 |
elif isinstance(arg, Literal) : |
|
137 |
# use what it specifies |
|
138 |
return arg.fmt_arg() |
|
139 |
||
140 |
elif isinstance(arg, unicode) : |
|
141 |
# recurse with the str version |
|
142 |
# XXX: what encoding to use? |
|
143 |
return self._fmt_arg(arg.encode('utf8')) |
|
144 |
||
145 |
elif isinstance(arg, str) : |
|
146 |
# XXX: quoting |
|
147 |
return arg |
|
148 |
||
149 |
else : |
|
150 |
# try and use it as a string |
|
151 |
return self._fmt_arg(str(arg)) |
|
152 |
||
153 |
def _fmt_data (self) : |
|
154 |
""" |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
155 |
Formats the statement name/params as a single line, ignoring None |
0 | 156 |
""" |
157 |
||
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
|
158 |
return "%s%s" % (self.name, (' ' + ' '.join(self._fmt_arg(a) for a in self.args)) if self.args else '') |
0 | 159 |
|
160 |
class Literal (Statement) : |
|
161 |
""" |
|
162 |
A literal is something that goes into the config file as-is, with no formatting or escaping applied. |
|
163 |
""" |
|
164 |
||
165 |
def __init__ (self, literal) : |
|
166 |
self.literal = literal |
|
167 |
||
168 |
def fmt_arg (self) : |
|
169 |
return self.literal |
|
170 |
||
171 |
def fmt_lines (self) : |
|
172 |
yield self.literal |
|
173 |
||
174 |
class Parameter (Statement) : |
|
175 |
""" |
|
176 |
A parameter is a single statement that configures the behaviour of something. |
|
177 |
||
178 |
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
|
179 |
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
|
180 |
|
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
|
181 |
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
|
182 |
<name> [ <arg> [ ... ] ] ";" |
0 | 183 |
""" |
184 |
||
185 |
def fmt_lines (self) : |
|
186 |
""" |
|
187 |
Yields a single ;-terminated line |
|
188 |
""" |
|
189 |
||
190 |
yield "%s;" % self._fmt_data() |
|
191 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
192 |
class Declaration (Section, Statement) : |
0 | 193 |
""" |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
194 |
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
|
195 |
that acts like a Section. |
0 | 196 |
|
197 |
<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
|
198 |
[ <Section> ] |
0 | 199 |
} |
200 |
||
201 |
""" |
|
202 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
203 |
def __init__ (self, name, args=[], params=None, decls=None) : |
0 | 204 |
""" |
205 |
The name/args will be formatted as in Statement, but params should be an iterable of Parameters, and decls |
|
206 |
an iterable of Declarations. |
|
207 |
""" |
|
208 |
||
209 |
# init the statement bit |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
210 |
Section.__init__(self, params, decls) |
0 | 211 |
Statement.__init__(self, name, *args) |
212 |
||
213 |
def fmt_lines (self) : |
|
214 |
""" |
|
215 |
Yields a header line, a series of indented body lines, and the footer line |
|
216 |
""" |
|
217 |
||
218 |
# the header to open the block |
|
219 |
yield "%s {" % self._fmt_data() |
|
220 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
221 |
# then output the section stuff, indented |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
222 |
for line in Section.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
|
223 |
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
|
224 |
|
0 | 225 |
# and then close the block |
226 |
yield "}" |
|
227 |
||
228 |
class SharedNetwork (Declaration) : |
|
229 |
""" |
|
230 |
A shared-network declaration is used to define a set of subnets that share the same physical network, |
|
231 |
optionally with some shared params. |
|
232 |
||
233 |
shared-network <name> { |
|
234 |
[ parameters ] |
|
235 |
[ declarations ] |
|
236 |
} |
|
237 |
""" |
|
238 |
||
239 |
def __init__ (self, name, params=[], decls=[]) : |
|
240 |
""" |
|
241 |
@param name the name of the shared-subnet |
|
242 |
@param params optional parameters |
|
243 |
@param decls the iterable of subnets or other declarations in the shared network |
|
244 |
""" |
|
245 |
||
246 |
super(SharedNetwork, self).__init__("shared-network", [name], params, decls) |
|
247 |
||
248 |
class Subnet (Declaration) : |
|
249 |
""" |
|
250 |
A subnet is used to provide the information about a subnet required to identify whether or not an IP address is |
|
251 |
on that subnet, and may also be used to specify parameters/declarations for that subnet. |
|
252 |
||
253 |
subnet <subnet-number> netmask <netmask> { |
|
254 |
[ parameters ] |
|
255 |
[ declarations ] |
|
256 |
} |
|
257 |
""" |
|
258 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
259 |
def __init__ (self, network, params=None, decls=None) : |
0 | 260 |
""" |
261 |
@param network the addr.Network for the subnet |
|
262 |
@param params optional parameters |
|
263 |
@param decls optional decls, e.g. subnets |
|
264 |
""" |
|
265 |
||
266 |
super(Subnet, self).__init__("subnet", [network.net(), "netmask", network.netmask()], params, decls) |
|
267 |
||
268 |
class Group (Declaration) : |
|
269 |
""" |
|
270 |
A group is simply used to apply a set of parameters to a set of declarations. |
|
271 |
||
272 |
group { |
|
273 |
[ parameters ] |
|
274 |
[ declarations ] |
|
275 |
} |
|
276 |
""" |
|
277 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
278 |
def __init__ (self, params=None, decls=None) : |
0 | 279 |
super(Group, self).__init__("group", [], params, decls) |
280 |
||
281 |
||
282 |
class Host (Declaration) : |
|
283 |
""" |
|
284 |
A host is used to match a request against specific host, and then apply settings for that host. |
|
285 |
||
286 |
The "hostname" is the DHCP name to identify the host. |
|
287 |
||
288 |
If no dhcp-client-identifier option is specified in the parameters, then the host is matched using the |
|
289 |
"hardware" parameter. |
|
290 |
||
291 |
host <hostname> { |
|
292 |
[ parameters ] |
|
293 |
[ declarations ] |
|
294 |
} |
|
295 |
""" |
|
296 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
297 |
def __init__ (self, hostname, params=None, decls=None) : |
0 | 298 |
super(Host, self).__init__("host", [hostname], params, decls) |
299 |
||
300 |
class Option (Parameter) : |
|
301 |
""" |
|
302 |
A generic 'option' parameter for a dhcpd.conf file |
|
303 |
""" |
|
304 |
||
305 |
def __init__ (self, name, *args) : |
|
306 |
""" |
|
307 |
Formatted as a Satement with a name of "option <name>". |
|
308 |
""" |
|
309 |
||
310 |
super(Option, self).__init__("option %s" % name, *args) |
|
311 |