-
Notifications
You must be signed in to change notification settings - Fork 451
/
configreader.py
160 lines (128 loc) · 4.86 KB
/
configreader.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import os
import sys
import wx
from string import lower
from traceback import print_exc
from cStringIO import StringIO
from ConfigParser import ConfigParser, MissingSectionHeaderError, NoSectionError
class ConfigReader(ConfigParser):
def __init__(self, filename, section, defaults = {}):
# if defaults is None:
# ConfigParser.__init__(self)
# else:
# ConfigParser.__init__(self, defaults)
ConfigParser.__init__(self)
self.defaults = defaults
self.filename = filename
self.section = section
try:
self.read(self.filename)
except MissingSectionHeaderError:
# Old file didn't have the section header
# (add it in manually)
oldfile = open(self.filename, "r")
oldconfig = oldfile.readlines()
oldfile.close()
newfile = open(self.filename, "w")
newfile.write("[" + self.section + "]\n")
newfile.writelines(oldconfig)
newfile.close()
self.read(self.filename)
def setSection(self, section):
self.section = section
def Read(self, param, type = "string", section = None):
if section is None:
section = self.section
if param is None or param == "":
return ""
defaults = { "string" : "",
"int" : 0,
"float" : 0.0,
"boolean" : False }
value = defaults[type]
try:
value = self.get(section, param)
value = value.strip("\"")
value = value.strip("'")
# if self.has_option(section, param):
# value = self.get(section, param)
# value = value.strip("\"")
# value = value.strip("'")
# else:
# param = lower(param)
# if self.defaults.has_key(param):
# value = self.defaults[param]
except:
param = lower(param)
if self.defaults.has_key(param):
value = self.defaults[param]
# sys.stderr.write("Error while reading parameter: (" + str(param) + ")\n")
# data = StringIO()
# print_exc(file = data)
# sys.stderr.write(data.getvalue())
pass
try:
if type == "boolean":
if value == "1":
value = True
else:
value = False
elif type == "int":
value = int(value)
elif type == "float":
value = float(value)
except:
value = defaults[type]
return value
def Exists(self, param, section = None):
if section is None:
section = self.section
return self.has_option(section, param)
def Write(self, param, value, type = "string", section = None):
if section is None:
section = self.section
if param is None or param == "":
return False
param = lower(param)
if not self.has_section(section):
self.add_section(section)
if type == "boolean":
if value:
text = "1"
else:
text = "0"
else:
text = str(value)
while True:
try:
oldtext = self.Read(param)
self.set(section, param, text)
# Return True if we actually changed something
if oldtext != text:
return True
break
except NoSectionError:
self.add_section(section)
except:
# sys.stderr.write("Error while writing parameter: (" + str(param) + ") with value: (" + str(text) + ")\n")
# data = StringIO()
# print_exc(file = data)
# sys.stderr.write(data.getvalue())
break
return False
def DeleteEntry(self, param, section = None):
if section is None:
section = self.section
try:
return self.remove_option(section, param)
except:
return False
def DeleteGroup(self, section = None):
if section is None:
section = self.section
try:
return self.remove_section(section)
except:
return False
def Flush(self):
self.write(open(self.filename, "w"))