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:-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] + "\n"
48 fileHandle.write(wstr)
52 class configSelection:
53 def __init__(self, parent):
56 def checkValues(self):
57 if self.parent.value < 0:
58 self.parent.value = len(self.parent.vals) - 1
59 elif(self.parent.value > (len(self.parent.vals) - 1)):
68 def handleKey(self, key):
69 if key == config.key["prevElement"]:
70 self.parent.value = self.parent.value - 1
71 if key == config.key["nextElement"]:
72 self.parent.value = self.parent.value + 1
78 def __call__(self, selected): #needed by configlist
80 return ("text", _(self.parent.vals[self.parent.value]))
83 def __init__(self, parent):
86 def checkValues(self):
88 # if self.parent.value < 0:
89 #self.parent.value = 0
91 #if(self.parent.value >= (len(self.parent.vals) - 1)):
92 #self.parent.value = len(self.parent.vals) - 1
100 def handleKey(self, key):
101 if key == config.key["prevElement"]:
102 self.parent.value = self.parent.value - self.parent.vals[1]
103 if key == config.key["nextElement"]:
104 self.parent.value = self.parent.value + self.parent.vals[1]
110 def __call__(self, selected): #needed by configlist
112 return ("text", strftime(self.parent.vals[0], localtime(self.parent.value)))
115 def __init__(self, parent):
118 def checkValues(self):
119 if self.parent.value < 0:
120 self.parent.value = 0
122 if(self.parent.value >= (len(self.parent.vals) - 1)):
123 self.parent.value = len(self.parent.vals) - 1
131 def handleKey(self, key):
132 if key == config.key["prevElement"]:
133 self.parent.value = self.parent.value - 1
134 if key == config.key["nextElement"]:
135 self.parent.value = self.parent.value + 1
141 def __call__(self, selected): #needed by configlist
144 return ("text", str(self.parent.vals[self.parent.value][0]))
146 class configSequenceArg:
147 def get(self, type, args = ()):
148 # configsequencearg.get ("IP")
150 return (("."), [(1,255),(0,255),(0,255),(0,255)], "")
151 # configsequencearg.get ("MAC")
153 return ((":"), [(1,255),(1,255),(1,255),(1,255),(1,255),(1,255)], "")
154 # configsequencearg.get ("CLOCK")
155 if (type == "CLOCK"):
156 return ((":"), [(0,23),(0,59)], "")
157 # configsequencearg.get("INTEGER", (min, max)) => x with min <= x <= max
158 if (type == "INTEGER"):
159 return ((":"), [args], "")
160 # configsequencearg.get("PINCODE", (number, "*")) => pin with number = length of pincode and "*" as numbers shown as stars
161 # configsequencearg.get("PINCODE", (number, "")) => pin with number = length of pincode and numbers shown
162 if (type == "PINCODE"):
163 return ((":"), [(0, (10**args[0])-1)], args[1])
164 # configsequencearg.get("FLOAT", [(min,max),(min1,max1)]) => x.y with min <= x <= max and min1 <= y <= max1
165 if (type == "FLOAT"):
166 return (("."), args, "")
168 configsequencearg = configSequenceArg()
170 class configSequence:
171 def __init__(self, parent):
174 self.seperator = self.parent.vals[0]
175 self.valueBounds = self.parent.vals[1]
176 self.censorChar = self.parent.vals[2]
178 def checkValues(self):
181 for i in self.parent.value:
182 maxPos += len(str(self.valueBounds[num][1]))
183 while (self.valueBounds[num][0] > self.parent.value[num]):
184 self.parent.value[num] += 1
186 while (self.valueBounds[num][1] < self.parent.value[num]):
187 self.parent.value[num] -= 1
189 # if (self.valueBounds[num][0] <= i <= self.valueBounds[num][1]):
192 #self.parent.value[num] = self.valueBounds[num][0]
195 if self.markedPos >= maxPos:
196 self.markedPos = maxPos - 1
197 if self.markedPos < 0:
206 def handleKey(self, key):
207 #this will no change anything on the value itself
208 #so we can handle it here in gui element
209 if key == config.key["prevElement"]:
211 if key == config.key["nextElement"]:
214 if key >= config.key["0"] and key <= config.key["9"]:
215 number = 9 - config.key["9"] + key
216 # length of numberblock
217 numberLen = len(str(self.valueBounds[0][1]))
218 # position in the block
219 posinblock = self.markedPos % numberLen
221 blocknumber = self.markedPos / numberLen
223 oldvalue = self.parent.value[blocknumber]
224 olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
225 newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
227 print "You actually pressed a number (" + str(number) + ") which will be added at block number " + str(blocknumber) + " on position " + str(posinblock)
228 print "Old value: " + str(oldvalue) + " olddec: " + str(olddec) + " newvalue: " + str(newvalue)
229 self.parent.value[blocknumber] = newvalue
237 #FIXME: dont call when press left/right
240 def __call__(self, selected): #needed by configlist
242 mPos = self.markedPos
243 print "Positon: " + str(mPos)
245 for i in self.parent.value:
246 if len(value): #fixme no heading separator possible
247 value += self.seperator
248 if mPos >= len(value) - 1:
251 #diff = self.valueBounds - len(str(i))
255 print (("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i)
256 if (self.censorChar == ""):
257 value += ("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i
259 value += (self.censorChar * len(str(self.valueBounds[num][1])))
261 # only mark cursor when we are selected
262 # (this code is heavily ink optimized!)
263 if (self.parent.enabled == True):
264 return ("mtext"[1-selected:], value, [mPos])
266 return ("text", value)
269 # used as first parameter
270 # is the text of a fixed size or is the user able to extend the length of the text
274 def __init__(self, parent):
277 self.mode = self.parent.vals[0]
278 self.textInput = NumericalTextInput(self.nextEntry)
280 def checkValues(self):
281 if (self.markedPos < 0):
283 if (self.markedPos >= len(self.parent.value)):
284 self.markedPos = len(self.parent.value) - 1
294 self.parent.vals[1](self.parent.getConfigPath())
296 def handleKey(self, key):
297 #this will no change anything on the value itself
298 #so we can handle it here in gui element
299 if key == config.key["delete"]:
300 self.parent.value = self.parent.value[0:self.markedPos] + self.parent.value[self.markedPos + 1:]
301 if key == config.key["prevElement"]:
302 self.textInput.nextKey()
305 if key == config.key["nextElement"]:
306 self.textInput.nextKey()
308 if (self.mode == self.extendableSize):
309 if (self.markedPos >= len(self.parent.value)):
310 self.parent.value = self.parent.value.ljust(len(self.parent.value) + 1)
313 if key >= config.key["0"] and key <= config.key["9"]:
314 number = 9 - config.key["9"] + key
316 self.parent.value = self.parent.value[0:self.markedPos] + str(self.textInput.getKey(number)) + self.parent.value[self.markedPos + 1:]
322 def __call__(self, selected): #needed by configlist
323 return ("mtext"[1-selected:], str(self.parent.value), [self.markedPos])
326 def __init__(self, obj):
334 self.key = { "choseElement": 0,
351 configfile = configFile()
354 def __init__(self, parent):
363 def checkValues(self):
364 if self.parent.value < 0:
365 self.parent.value = 0
367 if self.parent.value > 10:
368 self.parent.value = 10
370 def handleKey(self, key):
371 if key == config.key["prevElement"]:
372 self.parent.value = self.parent.value - 1
373 if key == config.key["nextElement"]:
374 self.parent.value = self.parent.value + 1
379 def __call__(self, selected): #needed by configlist
381 return ("slider", self.parent.value * 10)
383 class ConfigSubsection:
389 def getIndexbyEntry(self, data):
391 tcnt = -1; #for defaultval
393 if int(x[1]) == int(data):
395 if int(x[1]) == int(self.defaultValue):
400 return 0 #prevent bigger then array
402 def datafromFile(self, control, data):
403 if control == ConfigSlider:
405 elif control == configSelection:
407 elif control == configDateTime:
409 elif control == configText:
411 elif control == configSequence:
413 part = data.split(self.vals[0])
417 elif control == configSatlist:
418 return self.getIndexbyEntry(data)
422 def datatoFile(self, control, data):
423 if control == ConfigSlider:
425 elif control == configSelection:
427 elif control == configDateTime:
429 elif control == configText:
430 return str(data.strip())
432 elif control == configSequence:
435 value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
438 # just in case you don't understand the above, here an equivalent:
442 # value += self.vals[0]
445 elif control == configSatlist:
446 return str(self.vals[self.value][1]);
451 #print "load:" + self.configPath
453 value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
458 #print "value not found - using default"
460 if self.controlType == configSatlist:
461 self.value = self.getIndexbyEntry(self.defaultValue)
463 self.value = self.defaultValue
465 self.save() #add missing value to dict
467 #print "set val:" + str(value)
470 #is this right? activate settings after load/cancel and use default
473 def __init__(self, configPath, control, defaultValue, vals):
474 self.configPath = configPath
475 self.defaultValue = defaultValue
476 self.controlType = control
478 self.notifierList = [ ]
482 def getConfigPath(self):
483 return self.configPath
485 def addNotifier(self, notifier):
486 self.notifierList.append(notifier);
489 for notifier in self.notifierList:
494 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))
496 class configElement_nonSave(configElement):
497 def __init__(self, configPath, control, defaultValue, vals):
498 configElement.__init__(self, configPath, control, defaultValue, vals)
503 def getConfigListEntry(description, element):
505 item = b.controlType(b)
506 return ((description, item))
509 def configElementBoolean(name, default, texts=(_("Enable"), _("Disable"))):
510 return configElement(name, configSelection, default, texts)
512 config.misc = ConfigSubsection()