author | Tero Marttila <terom@fixme.fi> |
Thu, 02 Apr 2009 22:52:26 +0300 | |
changeset 4 | 8b633782f02d |
parent 3 | ff98fa9b84ce |
permissions | -rw-r--r-- |
0 | 1 |
""" |
2 |
Generic configuration file output |
|
3 |
""" |
|
4 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
5 |
import os, os.path, tempfile, shutil |
0 | 6 |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
7 |
class ConfObject (object) : |
0 | 8 |
""" |
9 |
An object that can be written to a ConfFile, as multiple lines of text. |
|
10 |
""" |
|
11 |
||
3 | 12 |
def __init__ (self, comments=None) : |
13 |
""" |
|
14 |
Initialize with the given list of comments. Comments that are None should be ignore. |
|
15 |
""" |
|
16 |
||
17 |
# init the comments list |
|
18 |
self.comments = comments or [] |
|
19 |
||
20 |
def add_comment (self, comment) : |
|
21 |
""" |
|
22 |
Add a comment to be rendered in the output. |
|
23 |
""" |
|
24 |
||
25 |
# add it |
|
26 |
self.comments.append(comment) |
|
27 |
||
0 | 28 |
def fmt_lines (self) : |
29 |
""" |
|
30 |
Yield a series of lines to be output in the file |
|
31 |
""" |
|
32 |
||
33 |
abstract |
|
34 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
35 |
class File (ConfObject) : |
0 | 36 |
""" |
37 |
A single configuration file on the filesystem. |
|
38 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
39 |
Configuration files are themselves ConfObject's, although this must be implemented in the inheriting class. |
0 | 40 |
""" |
41 |
||
42 |
def __init__ (self, name, path, backup_suffix='.bak', mode=0644) : |
|
43 |
""" |
|
44 |
Initialize the config file, but don't open it yet |
|
45 |
||
46 |
@param name the human-readable friendly name for the config file |
|
47 |
@param path the full filesystem path for the file |
|
48 |
@param backup_suffix rename the old file by adding this suffix when replacing it |
|
49 |
@param mode the permission bits for the new file |
|
50 |
""" |
|
51 |
||
52 |
self.name = name |
|
53 |
self.path = path |
|
54 |
self.backup_suffix = backup_suffix |
|
55 |
self.mode = mode |
|
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 |
def write_file (self, file) : |
0 | 58 |
""" |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
59 |
Write out this config's stuff into the given file |
0 | 60 |
""" |
61 |
||
62 |
writer = Writer(file) |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
63 |
writer.write_obj(self) |
0 | 64 |
|
1
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 write (self) : |
0 | 66 |
""" |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
67 |
Write out a new config file with this config's stuff using the following procedure: |
4 | 68 |
|
0 | 69 |
* lock the real file |
70 |
* open a new temporary file, and write out the objects into that, closing it |
|
71 |
* move the real file out of the way |
|
72 |
* move the temporary file into the real file's place |
|
73 |
* unlock the real file |
|
74 |
""" |
|
75 |
||
76 |
# XXX: how to aquire the lock? |
|
77 |
# XXX: this needs checking over |
|
78 |
||
79 |
# open the new temporary file |
|
80 |
# XXX: ensure fd is closed |
|
81 |
tmp_fd, tmp_path = tempfile.mkstemp(prefix=self.name) |
|
82 |
||
83 |
# ...as a file |
|
84 |
tmp_file = os.fdopen(tmp_fd, "w") |
|
85 |
||
86 |
# fix the permissions |
|
87 |
os.chmod(tmp_path, self.mode) |
|
88 |
||
89 |
# write it |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
90 |
self.write_file(tmp_file) |
0 | 91 |
|
92 |
# close it |
|
93 |
tmp_file.close() |
|
94 |
||
95 |
# move the old file out of the way |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
96 |
if os.path.exists(self.path) : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
97 |
os.rename(self.path, self.path + self.backup_suffix) |
0 | 98 |
|
99 |
# move the new file in |
|
100 |
shutil.move(tmp_path, self.path) |
|
101 |
||
102 |
class Writer (object) : |
|
103 |
""" |
|
104 |
A conf.Writer is used to write out a new conf.File (as a temporary file) |
|
105 |
""" |
|
106 |
||
107 |
def __init__ (self, file) : |
|
108 |
""" |
|
109 |
@param file the temporary file object |
|
110 |
""" |
|
111 |
||
112 |
self.file = file |
|
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 |
def write_line (self, line) : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
115 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
116 |
Write a single line to the file |
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 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
119 |
self.file.write("%s\n" % (line, )) |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
120 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
121 |
def write_lines (self, lines) : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
122 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
123 |
Write a series of lines into the file |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
124 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
125 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
126 |
for line in lines : |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
127 |
self.write_line(line) |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
128 |
|
0 | 129 |
def write_obj (self, obj) : |
130 |
""" |
|
131 |
Write a single object to the file |
|
132 |
""" |
|
133 |
||
134 |
# just write out all the lines |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
135 |
self.write_lines(obj.fmt_lines()) |
0 | 136 |