-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfgfile.py
121 lines (102 loc) · 2.32 KB
/
cfgfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-
#
# Scribbington config file
import string
def _load_config(filename):
"""
Load a configuration that returns variables.
"""
try:
f = open(filename, "r")
except IOError, e:
return None
stuff = {}
line = 0
while 1:
line = line + 1
s = f.readline()
if s=="":
break
if s[0]=="#":
continue
while s.find("#") == -1:
lecture = f.readline()
if lecture == "":
break
if s[-2] == '\\':
s = s[:-2]
s = s[:s.rfind("\n")] + lecture
line = line + 1
s = string.split(s, "=")
try:
stuff[string.strip(s[0])] = eval(string.strip(string.join(s[1:], "=")))
except:
print "Malformed line in %s line %d" % (filename, line)
print "\t%s" %s
continue
return stuff
def _save_config(filename, fields):
"""
Should be a dictionary;
Keys as names of variables containing tuple.
"""
f = open(filename, "w")
for key in fields.keys():
f.write("# "+fields[key][0]+" #\n")
s = repr(fields[key][1])
f.write(key+"\t= ")
#Create newline after each entry
if s.find("],") != -1:
cut_string = ""
while s.find("],") != -1:
position = s.find("],")+3
cut_string = cut_string + s[:position] + "\n\t"
s = s[position:]
s = cut_string + s
f.write(s+"\n")
continue
#cut at 80 col
if len(s) > 80:
cut_string = ""
while len(s) > 80:
position = s.rfind(",",0,80)+1
cut_string = cut_string + s[:position] + "\n\t\t"
s = s[position:]
s = cut_string + s
f.write(s+"\n")
f.write("# End of configuration #")
f.close()
class cfgset:
def load(self, filename, defaults):
"""
Defaults should be key=variable name, value=
tuple of (comment, default value)
"""
self._defaults = defaults
self._filename = filename
for i in defaults.keys():
self.__dict__[i] = defaults[i][1]
# try to laad saved ones
vars = _load_config(filename)
if vars == None:
# none found. this is new
self.save()
return
for i in vars.keys():
self.__dict__[i] = vars[i]
def save(self):
"""
Save borg settings
"""
keys = {}
for i in self.__dict__.keys():
# reserved
if i == "_defaults" or i == "_filename":
continue
if self._defaults.has_key(i):
comment = self._defaults[i][0]
else:
comment = ""
keys[i] = (comment, self.__dict__[i])
# save to config file
_save_config(self._filename, keys)