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 |
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 |
||
12 |
def fmt_lines (self) : |
|
13 |
""" |
|
14 |
Yield a series of lines to be output in the file |
|
15 |
""" |
|
16 |
||
17 |
abstract |
|
18 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
19 |
class File (ConfObject) : |
0 | 20 |
""" |
21 |
A single configuration file on the filesystem. |
|
22 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
23 |
Configuration files are themselves ConfObject's, although this must be implemented in the inheriting class. |
0 | 24 |
""" |
25 |
||
26 |
def __init__ (self, name, path, backup_suffix='.bak', mode=0644) : |
|
27 |
""" |
|
28 |
Initialize the config file, but don't open it yet |
|
29 |
||
30 |
@param name the human-readable friendly name for the config file |
|
31 |
@param path the full filesystem path for the file |
|
32 |
@param backup_suffix rename the old file by adding this suffix when replacing it |
|
33 |
@param mode the permission bits for the new file |
|
34 |
""" |
|
35 |
||
36 |
self.name = name |
|
37 |
self.path = path |
|
38 |
self.backup_suffix = backup_suffix |
|
39 |
self.mode = mode |
|
40 |
||
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 |
def write_file (self, file) : |
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 |
Write out this config's stuff into the given file |
0 | 44 |
""" |
45 |
||
46 |
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
|
47 |
writer.write_obj(self) |
0 | 48 |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
49 |
def write (self) : |
0 | 50 |
""" |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
51 |
Write out a new config file with this config's stuff using the following procedure: |
0 | 52 |
* lock the real file |
53 |
* open a new temporary file, and write out the objects into that, closing it |
|
54 |
* move the real file out of the way |
|
55 |
* move the temporary file into the real file's place |
|
56 |
* unlock the real file |
|
57 |
""" |
|
58 |
||
59 |
# XXX: how to aquire the lock? |
|
60 |
# XXX: this needs checking over |
|
61 |
||
62 |
# open the new temporary file |
|
63 |
# XXX: ensure fd is closed |
|
64 |
tmp_fd, tmp_path = tempfile.mkstemp(prefix=self.name) |
|
65 |
||
66 |
# ...as a file |
|
67 |
tmp_file = os.fdopen(tmp_fd, "w") |
|
68 |
||
69 |
# fix the permissions |
|
70 |
os.chmod(tmp_path, self.mode) |
|
71 |
||
72 |
# 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
|
73 |
self.write_file(tmp_file) |
0 | 74 |
|
75 |
# close it |
|
76 |
tmp_file.close() |
|
77 |
||
78 |
# 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
|
79 |
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
|
80 |
os.rename(self.path, self.path + self.backup_suffix) |
0 | 81 |
|
82 |
# move the new file in |
|
83 |
shutil.move(tmp_path, self.path) |
|
84 |
||
85 |
class Writer (object) : |
|
86 |
""" |
|
87 |
A conf.Writer is used to write out a new conf.File (as a temporary file) |
|
88 |
""" |
|
89 |
||
90 |
def __init__ (self, file) : |
|
91 |
""" |
|
92 |
@param file the temporary file object |
|
93 |
""" |
|
94 |
||
95 |
self.file = file |
|
96 |
||
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
97 |
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
|
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 |
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
|
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 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
102 |
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
|
103 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
104 |
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
|
105 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
106 |
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
|
107 |
""" |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
108 |
|
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
109 |
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
|
110 |
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
|
111 |
|
0 | 112 |
def write_obj (self, obj) : |
113 |
""" |
|
114 |
Write a single object to the file |
|
115 |
""" |
|
116 |
||
117 |
# 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
|
118 |
self.write_lines(obj.fmt_lines()) |
0 | 119 |