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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
class configFile:
def __init__(self):
self.changed = 0
self.configElements = { }
try:
self.file = open("config")
except IOError:
print "cannot open config file"
return
while 1:
line = self.file.readline()
if line == "":
break
if line.startswith("#"): #skip comments
continue
self.addElement(line)
self.file.close()
def addElement(self, line):
x = line.find("=")
if x > -1:
self.configElements[line[:x]] = line[x + 1:]
def getKey(self, key):
return self.configElements[key]
def setKey(self, key, value):
self.changed = 1
self.configElements[key] = value
def save(self):
if self.changed == 0: #no changes, so no write to disk needed
return
fileHandle = open("config", "w")
for x in self.configElements:
wstr = x + "=" + self.configElements[x]
if wstr[len(wstr) - 1] != '\n':
wstr = wstr + "\n"
fileHandle.write(wstr)
fileHandle.close()
class configBoolean:
def __init__(self, parent):
self.parent = parent
def checkValues(self):
if self.parent.value < 0:
self.parent.value = 0
if(self.parent.value >= (len(self.parent.vals) - 1)):
self.parent.value = len(self.parent.vals) - 1
def cancel(self):
self.parent.reload()
def save(self):
self.parent.save()
def handleKey(self, key):
if key == config.prevElement:
self.parent.value = self.parent.value - 1
if key == config.nextElement:
self.parent.value = self.parent.value + 1
self.checkValues()
self.parent.change()
def __call__(self): #needed by configlist
self.checkValues()
return ("text", self.parent.vals[self.parent.value])
class configSequence:
def __init__(self, parent):
self.parent = parent
def checkValues(self):
pass
# if self.parent.value < 0:
# self.parent.value = 0
#
# if(self.parent.value >= (len(self.parent.vals) - 1)):
# self.parent.value = len(self.parent.vals) - 1
#
def cancel(self):
self.parent.reload()
def save(self):
self.parent.save()
def handleKey(self, key):
if key == config.prevElement:
self.parent.value = self.parent.value - 1
if key == config.nextElement:
self.parent.value = self.parent.value + 1
self.checkValues()
self.parent.change()
def __call__(self): #needed by configlist
value = ""
for i in self.parent.value:
if (value != ""):
value += self.parent.vals[0]
value += str(i)
return ("text", value)
class configValue:
def __init__(self, obj):
self.obj = obj
def __str__(self):
return self.obj
class Config:
def __init__(self):
self.choseElement = 0
self.prevElement = 1
self.nextElement = 2
config = Config();
configfile = configFile()
class ConfigSlider:
def __init__(self, parent):
self.parent = parent
def cancel(self):
self.parent.reload()
def save(self):
self.parent.save()
def checkValues(self):
if self.parent.value < 0:
self.parent.value = 0
if self.parent.value > 10:
self.parent.value = 10
def handleKey(self, key):
if key == config.prevElement:
self.parent.value = self.parent.value - 1
if key == config.nextElement:
self.parent.value = self.parent.value + 1
self.checkValues()
self.parent.change()
def __call__(self): #needed by configlist
self.checkValues()
return ("slider", self.parent.value * 10)
class ConfigSubsection:
def __init__(self):
pass
class configElement:
def datafromFile(self, control, data):
if control == ConfigSlider:
return int(data);
elif control == configBoolean:
return int(data);
else:
return ""
def datatoFile(self, control, data):
if control == ConfigSlider:
return str(data);
elif control == configBoolean:
return str(data);
else:
return ""
def loadData(self):
try:
value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
except:
value = ""
if value == "":
print "value not found - using default"
self.value = self.defaultValue
self.save() #add missing value to dict
else:
self.value = value
def __init__(self, configPath, control, defaultValue, vals):
self.configPath = configPath
self.defaultValue = defaultValue
self.controlType = control
self.vals = vals
self.notifierList = [ ]
self.loadData()
def addNotifier(self, notifier):
self.notifierList.append(notifier);
notifier(self);
def change(self):
for notifier in self.notifierList:
notifier(self)
def reload(self):
self.loadData()
def save(self):
configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))
|