2 from Tools.NumericalTextInput import *
7 self.configElements = { }
9 self.file = open("config")
11 print "cannot open config file"
15 line = self.file.readline()
19 if line.startswith("#"): #skip comments
25 def addElement(self, line):
28 self.configElements[line[:x]] = line[x + 1:]
30 def getKey(self, key):
31 return self.configElements[key]
33 def setKey(self, key, value):
35 self.configElements[key] = value
38 if self.changed == 0: #no changes, so no write to disk needed
41 fileHandle = open("config", "w")
43 keys = self.configElements.keys()
46 wstr = x + "=" + self.configElements[x]
48 if wstr[len(wstr) - 1] != '\n':
51 fileHandle.write(wstr)
55 class configSelection:
56 def __init__(self, parent):
59 def checkValues(self):
60 if self.parent.value < 0:
63 if(self.parent.value >= (len(self.parent.vals) - 1)):
64 self.parent.value = len(self.parent.vals) - 1
72 def handleKey(self, key):
73 if key == config.key["prevElement"]:
74 self.parent.value = self.parent.value - 1
75 if key == config.key["nextElement"]:
76 self.parent.value = self.parent.value + 1
82 def __call__(self, selected): #needed by configlist
84 return ("text", self.parent.vals[self.parent.value])
87 def __init__(self, parent):
90 def checkValues(self):
92 # if self.parent.value < 0:
93 #self.parent.value = 0
95 #if(self.parent.value >= (len(self.parent.vals) - 1)):
96 #self.parent.value = len(self.parent.vals) - 1
104 def handleKey(self, key):
105 if key == config.key["prevElement"]:
106 self.parent.value = self.parent.value - self.parent.vals[1]
107 if key == config.key["nextElement"]:
108 self.parent.value = self.parent.value + self.parent.vals[1]
114 def __call__(self, selected): #needed by configlist
116 return ("text", strftime(self.parent.vals[0], localtime(self.parent.value)))
119 def __init__(self, parent):
122 def checkValues(self):
123 if self.parent.value < 0:
124 self.parent.value = 0
126 if(self.parent.value >= (len(self.parent.vals) - 1)):
127 self.parent.value = len(self.parent.vals) - 1
135 def handleKey(self, key):
136 if key == config.key["prevElement"]:
137 self.parent.value = self.parent.value - 1
138 if key == config.key["nextElement"]:
139 self.parent.value = self.parent.value + 1
145 def __call__(self, selected): #needed by configlist
148 return ("text", str(self.parent.vals[self.parent.value][0]))
150 class configSequenceArg:
151 def get(self, type, args = ()):
152 # configsequencearg.get ("IP")
154 return (("."), [(1,255),(0,255),(0,255),(0,255)], "")
155 # configsequencearg.get ("MAC")
157 return ((":"), [(1,255),(1,255),(1,255),(1,255),(1,255),(1,255)], "")
158 # configsequencearg.get ("CLOCK")
159 if (type == "CLOCK"):
160 return ((":"), [(0,23),(0,59)], "")
161 # configsequencearg.get("INTEGER", (min, max)) => x with min <= x <= max
162 if (type == "INTEGER"):
163 return ((":"), [args], "")
164 # configsequencearg.get("PINCODE", (number, "*")) => pin with number = length of pincode and "*" as numbers shown as stars
165 # configsequencearg.get("PINCODE", (number, "")) => pin with number = length of pincode and numbers shown
166 if (type == "PINCODE"):
167 return ((":"), [(0, (10**args[0])-1)], args[1])
168 # configsequencearg.get("FLOAT", [(min,max),(min1,max1)]) => x.y with min <= x <= max and min1 <= y <= max1
169 if (type == "FLOAT"):
170 return (("."), args, "")
172 configsequencearg = configSequenceArg()
174 class configSequence:
175 def __init__(self, parent):
178 self.seperator = self.parent.vals[0]
179 self.valueBounds = self.parent.vals[1]
180 self.censorChar = self.parent.vals[2]
182 def checkValues(self):
185 for i in self.parent.value:
186 maxPos += len(str(self.valueBounds[num][1]))
187 while (self.valueBounds[num][0] > self.parent.value[num]):
188 self.parent.value[num] += 1
190 while (self.valueBounds[num][1] < self.parent.value[num]):
191 self.parent.value[num] -= 1
193 # if (self.valueBounds[num][0] <= i <= self.valueBounds[num][1]):
196 #self.parent.value[num] = self.valueBounds[num][0]
199 if self.markedPos >= maxPos:
200 self.markedPos = maxPos - 1
201 if self.markedPos < 0:
210 def handleKey(self, key):
211 #this will no change anything on the value itself
212 #so we can handle it here in gui element
213 if key == config.key["prevElement"]:
215 if key == config.key["nextElement"]:
218 if key >= config.key["0"] and key <= config.key["9"]:
219 number = 9 - config.key["9"] + key
220 # length of numberblock
221 numberLen = len(str(self.valueBounds[0][1]))
222 # position in the block
223 posinblock = self.markedPos % numberLen
225 blocknumber = self.markedPos / numberLen
227 oldvalue = self.parent.value[blocknumber]
228 olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
229 newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
231 print "You actually pressed a number (" + str(number) + ") which will be added at block number " + str(blocknumber) + " on position " + str(posinblock)
232 print "Old value: " + str(oldvalue) + " olddec: " + str(olddec) + " newvalue: " + str(newvalue)
233 self.parent.value[blocknumber] = newvalue
241 #FIXME: dont call when press left/right
244 def __call__(self, selected): #needed by configlist
246 mPos = self.markedPos
247 print "Positon: " + str(mPos)
249 for i in self.parent.value:
250 if len(value): #fixme no heading separator possible
251 value += self.seperator
252 if mPos >= len(value) - 1:
255 #diff = self.valueBounds - len(str(i))
259 print (("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i)
260 if (self.censorChar == ""):
261 value += ("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i
263 value += (self.censorChar * len(str(self.valueBounds[num][1])))
265 # only mark cursor when we are selected
266 # (this code is heavily ink optimized!)
267 return ("mtext"[1-selected:], value, [mPos])
270 # used as first parameter
271 # is the text of a fixed size or is the user able to extend the length of the text
275 def __init__(self, parent):
278 self.mode = self.parent.vals[0]
279 self.textInput = NumericalTextInput(self.nextEntry)
281 def checkValues(self):
282 if (self.markedPos < 0):
284 if (self.markedPos >= len(self.parent.value)):
285 self.markedPos = len(self.parent.value) - 1
295 self.handleKey(config.key["nextElement"])
297 def handleKey(self, key):
298 #this will no change anything on the value itself
299 #so we can handle it here in gui element
300 if key == config.key["prevElement"]:
302 if key == config.key["nextElement"]:
304 if (self.mode == self.extendableSize):
305 if (self.markedPos >= len(self.parent.value)):
306 self.parent.value = self.parent.value.ljust(len(self.parent.value) + 1)
308 if key >= config.key["0"] and key <= config.key["9"]:
309 number = 9 - config.key["9"] + key
311 self.parent.value = self.parent.value[0:self.markedPos] + str(self.textInput.getKey(number)) + self.parent.value[self.markedPos + 1:]
317 def __call__(self, selected): #needed by configlist
318 return ("mtext"[1-selected:], str(self.parent.value), [self.markedPos])
321 def __init__(self, obj):
329 self.key = { "choseElement": 0,
344 configfile = configFile()
347 def __init__(self, parent):
356 def checkValues(self):
357 if self.parent.value < 0:
358 self.parent.value = 0
360 if self.parent.value > 10:
361 self.parent.value = 10
363 def handleKey(self, key):
364 if key == config.key["prevElement"]:
365 self.parent.value = self.parent.value - 1
366 if key == config.key["nextElement"]:
367 self.parent.value = self.parent.value + 1
372 def __call__(self, selected): #needed by configlist
374 return ("slider", self.parent.value * 10)
376 class ConfigSubsection:
382 def getIndexbyEntry(self, data):
384 tcnt = -1; #for defaultval
386 if int(x[1]) == int(data):
388 if int(x[1]) == int(self.defaultValue):
393 return 0 #prevent bigger then array
395 def datafromFile(self, control, data):
396 if control == ConfigSlider:
398 elif control == configSelection:
400 elif control == configDateTime:
402 elif control == configText:
404 elif control == configSequence:
406 part = data.split(self.vals[0])
410 elif control == configSatlist:
411 return self.getIndexbyEntry(data)
415 def datatoFile(self, control, data):
416 if control == ConfigSlider:
418 elif control == configSelection:
420 elif control == configDateTime:
422 elif control == configText:
423 return str(data.strip())
425 elif control == configSequence:
426 value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
427 # just in case you don't understand the above, here an equivalent:
431 # value += self.vals[0]
434 elif control == configSatlist:
435 return str(self.vals[self.value][1]);
441 value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
446 #print "value not found - using default"
448 if self.controlType == configSatlist:
449 self.value = self.getIndexbyEntry(self.defaultValue)
451 self.value = self.defaultValue
453 self.save() #add missing value to dict
457 #is this right? activate settings after load/cancel and use default
460 def __init__(self, configPath, control, defaultValue, vals):
461 self.configPath = configPath
462 self.defaultValue = defaultValue
463 self.controlType = control
465 self.notifierList = [ ]
468 def addNotifier(self, notifier):
469 self.notifierList.append(notifier);
472 for notifier in self.notifierList:
477 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))
479 class configElement_nonSave(configElement):
480 def __init__(self, configPath, control, defaultValue, vals):
481 configElement.__init__(self, configPath, control, defaultValue, vals)
486 def getConfigListEntry(description, element):
488 item = b.controlType(b)
489 return ((description, item))