4 self.configElements = { }
6 self.file = open("config")
8 print "cannot open config file"
12 line = self.file.readline()
16 if line.startswith("#"): #skip comments
22 def addElement(self, line):
25 self.configElements[line[:x]] = line[x + 1:]
27 def getKey(self, key):
28 return self.configElements[key]
30 def setKey(self, key, value):
32 self.configElements[key] = value
35 if self.changed == 0: #no changes, so no write to disk needed
38 fileHandle = open("config", "w")
40 for x in self.configElements:
41 wstr = x + "=" + self.configElements[x]
43 if wstr[len(wstr) - 1] != '\n':
46 fileHandle.write(wstr)
50 class configSelection:
51 def __init__(self, parent):
54 def checkValues(self):
55 if self.parent.value < 0:
58 if(self.parent.value >= (len(self.parent.vals) - 1)):
59 self.parent.value = len(self.parent.vals) - 1
67 def handleKey(self, key):
68 if key == config.key["prevElement"]:
69 self.parent.value = self.parent.value - 1
70 if key == config.key["nextElement"]:
71 self.parent.value = self.parent.value + 1
77 def __call__(self, selected): #needed by configlist
79 return ("text", self.parent.vals[self.parent.value])
82 def __init__(self, parent):
85 def checkValues(self):
86 if self.parent.value < 0:
89 if(self.parent.value >= (len(self.parent.vals) - 1)):
90 self.parent.value = len(self.parent.vals) - 1
92 print "value" + str(self.parent.value)
93 print "name " + self.parent.vals[self.parent.value][0]
101 def handleKey(self, key):
102 if key == config.key["prevElement"]:
103 self.parent.value = self.parent.value - 1
104 if key == config.key["nextElement"]:
105 self.parent.value = self.parent.value + 1
111 def __call__(self, selected): #needed by configlist
114 return ("text", str(self.parent.vals[self.parent.value][0]))
116 class configSequence:
117 def __init__(self, parent):
121 def checkValues(self):
122 maxPos = len(self.parent.value) * self.parent.vals[1]
125 if self.markedPos >= maxPos:
126 self.markedPos = maxPos - 1
127 if self.markedPos < 0:
136 def handleKey(self, key):
137 #this will no change anything on the value itself
138 #so we can handle it here in gui element
139 if key == config.key["prevElement"]:
141 if key == config.key["nextElement"]:
144 if key >= config.key["0"] and key <= config.key["9"]:
145 number = 9 - config.key["9"] + key
146 # length of numberblock
147 numberLen = len(str(self.parent.vals[1][1]))
148 # position in the block
149 posinblock = self.markedPos % numberLen
151 blocknumber = self.markedPos / numberLen
153 oldvalue = self.parent.value[blocknumber]
154 olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
155 newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
157 print "You actually pressed a number (" + str(number) + ") which will be added at block number " + str(blocknumber) + " on position " + str(posinblock)
158 print "Old value: " + str(oldvalue) + " olddec: " + str(olddec) + " newvalue: " + str(newvalue)
159 self.parent.value[blocknumber] = newvalue
167 #FIXME: dont call when press left/right
170 def __call__(self, selected): #needed by configlist
172 mPos = self.markedPos
173 print "Positon: " + str(mPos)
174 for i in self.parent.value:
175 if len(value): #fixme no heading separator possible
176 value += self.parent.vals[0]
177 if mPos >= len(value) - 1:
180 #diff = self.parent.vals[1] - len(str(i))
184 print (("%0" + str(len(str(self.parent.vals[1][1]))) + "d") % i)
185 value += ("%0" + str(len(str(self.parent.vals[1][1]))) + "d") % i
187 # only mark cursor when we are selected
188 # (this code is heavily ink optimized!)
189 return ("mtext"[1-selected:], value, [mPos])
192 def __init__(self, obj):
200 self.key = { "choseElement": 0,
215 configfile = configFile()
218 def __init__(self, parent):
227 def checkValues(self):
228 if self.parent.value < 0:
229 self.parent.value = 0
231 if self.parent.value > 10:
232 self.parent.value = 10
234 def handleKey(self, key):
235 if key == config.key["prevElement"]:
236 self.parent.value = self.parent.value - 1
237 if key == config.key["nextElement"]:
238 self.parent.value = self.parent.value + 1
243 def __call__(self, selected): #needed by configlist
245 return ("slider", self.parent.value * 10)
247 class ConfigSubsection:
252 def datafromFile(self, control, data):
253 if control == ConfigSlider:
255 elif control == configSelection:
257 elif control == configSequence:
259 part = data.split(self.vals[0])
266 def datatoFile(self, control, data):
267 if control == ConfigSlider:
269 elif control == configSelection:
271 elif control == configSequence:
272 value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
273 # just in case you don't understand the above, here an equivalent:
277 # value += self.vals[0]
285 value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
290 print "value not found - using default"
291 self.value = self.defaultValue
292 self.save() #add missing value to dict
296 def __init__(self, configPath, control, defaultValue, vals):
297 self.configPath = configPath
298 self.defaultValue = defaultValue
299 self.controlType = control
301 self.notifierList = [ ]
304 def addNotifier(self, notifier):
305 self.notifierList.append(notifier);
308 for notifier in self.notifierList:
313 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))