2 from Tools.NumericalTextInput import *
3 from Tools.Directories import *
8 self.configElements = { }
10 self.file = open(resolveFilename(SCOPE_CONFIG, "config"))
12 print "cannot open config file"
16 line = self.file.readline()
20 if line.startswith("#"): #skip comments
26 def addElement(self, line):
29 self.configElements[line[:x]] = line[x + 1:-1]
31 def getKey(self, key):
32 return self.configElements[key]
34 def setKey(self, key, value):
36 self.configElements[key] = value
39 if self.changed == 0: #no changes, so no write to disk needed
42 fileHandle = open(resolveFilename(SCOPE_CONFIG, "config"), "w")
44 keys = self.configElements.keys()
47 wstr = x + "=" + self.configElements[x] + "\n"
49 fileHandle.write(wstr)
53 class configSelection:
54 def __init__(self, parent):
57 def checkValues(self):
58 if self.parent.value < 0:
59 self.parent.value = len(self.parent.vals) - 1
60 elif(self.parent.value > (len(self.parent.vals) - 1)):
69 def handleKey(self, key):
70 if key == config.key["prevElement"]:
71 self.parent.value = self.parent.value - 1
72 if key == config.key["nextElement"]:
73 self.parent.value = self.parent.value + 1
79 def __call__(self, selected): #needed by configlist
81 return ("text", _(self.parent.vals[self.parent.value]))
84 def __init__(self, parent):
87 def checkValues(self):
89 # if self.parent.value < 0:
90 #self.parent.value = 0
92 #if(self.parent.value >= (len(self.parent.vals) - 1)):
93 #self.parent.value = len(self.parent.vals) - 1
101 def handleKey(self, key):
102 if key == config.key["prevElement"]:
103 self.parent.value = self.parent.value - self.parent.vals[1]
104 if key == config.key["nextElement"]:
105 self.parent.value = self.parent.value + self.parent.vals[1]
111 def __call__(self, selected): #needed by configlist
113 return ("text", strftime(self.parent.vals[0], localtime(self.parent.value)))
116 def __init__(self, parent):
119 def checkValues(self):
120 if self.parent.value < 0:
121 self.parent.value = 0
123 if(self.parent.value >= (len(self.parent.vals) - 1)):
124 self.parent.value = len(self.parent.vals) - 1
132 def handleKey(self, key):
133 if key == config.key["prevElement"]:
134 self.parent.value = self.parent.value - 1
135 if key == config.key["nextElement"]:
136 self.parent.value = self.parent.value + 1
142 def __call__(self, selected): #needed by configlist
145 return ("text", str(self.parent.vals[self.parent.value][0]))
147 class configSequenceArg:
148 def get(self, type, args = ()):
149 # configsequencearg.get ("IP")
151 return (("."), [(1,255),(0,255),(0,255),(0,255)], "")
152 # configsequencearg.get ("MAC")
154 return ((":"), [(1,255),(1,255),(1,255),(1,255),(1,255),(1,255)], "")
155 # configsequencearg.get ("CLOCK")
156 if (type == "CLOCK"):
157 return ((":"), [(0,23),(0,59)], "")
158 # configsequencearg.get("INTEGER", (min, max)) => x with min <= x <= max
159 if (type == "INTEGER"):
160 return ((":"), [args], "")
161 # configsequencearg.get("PINCODE", (number, "*")) => pin with number = length of pincode and "*" as numbers shown as stars
162 # configsequencearg.get("PINCODE", (number, "")) => pin with number = length of pincode and numbers shown
163 if (type == "PINCODE"):
164 return ((":"), [(0, (10**args[0])-1)], args[1])
165 # configsequencearg.get("FLOAT", [(min,max),(min1,max1)]) => x.y with min <= x <= max and min1 <= y <= max1
166 if (type == "FLOAT"):
167 return (("."), args, "")
169 configsequencearg = configSequenceArg()
171 class configSequence:
172 def __init__(self, parent):
175 self.seperator = self.parent.vals[0]
176 self.valueBounds = self.parent.vals[1]
177 self.censorChar = self.parent.vals[2]
179 def checkValues(self):
182 for i in self.parent.value:
183 maxPos += len(str(self.valueBounds[num][1]))
184 while (self.valueBounds[num][0] > self.parent.value[num]):
185 self.parent.value[num] += 1
187 while (self.valueBounds[num][1] < self.parent.value[num]):
188 self.parent.value[num] -= 1
190 # if (self.valueBounds[num][0] <= i <= self.valueBounds[num][1]):
193 #self.parent.value[num] = self.valueBounds[num][0]
196 if self.markedPos >= maxPos:
197 self.markedPos = maxPos - 1
198 if self.markedPos < 0:
207 def handleKey(self, key):
208 #this will no change anything on the value itself
209 #so we can handle it here in gui element
210 if key == config.key["prevElement"]:
212 if key == config.key["nextElement"]:
215 if key >= config.key["0"] and key <= config.key["9"]:
216 number = 9 - config.key["9"] + key
217 # length of numberblock
218 numberLen = len(str(self.valueBounds[0][1]))
219 # position in the block
220 posinblock = self.markedPos % numberLen
222 blocknumber = self.markedPos / numberLen
224 oldvalue = self.parent.value[blocknumber]
225 olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
226 newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
228 print "You actually pressed a number (" + str(number) + ") which will be added at block number " + str(blocknumber) + " on position " + str(posinblock)
229 print "Old value: " + str(oldvalue) + " olddec: " + str(olddec) + " newvalue: " + str(newvalue)
230 self.parent.value[blocknumber] = newvalue
238 #FIXME: dont call when press left/right
241 def __call__(self, selected): #needed by configlist
243 mPos = self.markedPos
244 print "Positon: " + str(mPos)
246 for i in self.parent.value:
247 if len(value): #fixme no heading separator possible
248 value += self.seperator
249 if mPos >= len(value) - 1:
252 #diff = self.valueBounds - len(str(i))
256 print (("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i)
257 if (self.censorChar == ""):
258 value += ("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i
260 value += (self.censorChar * len(str(self.valueBounds[num][1])))
262 # only mark cursor when we are selected
263 # (this code is heavily ink optimized!)
264 if (self.parent.enabled == True):
265 return ("mtext"[1-selected:], value, [mPos])
267 return ("text", value)
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.parent.vals[1](self.parent.getConfigPath())
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["delete"]:
301 self.parent.value = self.parent.value[0:self.markedPos] + self.parent.value[self.markedPos + 1:]
302 if key == config.key["prevElement"]:
303 self.textInput.nextKey()
306 if key == config.key["nextElement"]:
307 self.textInput.nextKey()
309 if (self.mode == self.extendableSize):
310 if (self.markedPos >= len(self.parent.value)):
311 self.parent.value = self.parent.value.ljust(len(self.parent.value) + 1)
314 if key >= config.key["0"] and key <= config.key["9"]:
315 number = 9 - config.key["9"] + key
317 self.parent.value = self.parent.value[0:self.markedPos] + str(self.textInput.getKey(number)) + self.parent.value[self.markedPos + 1:]
323 def __call__(self, selected): #needed by configlist
324 return ("mtext"[1-selected:], str(self.parent.value), [self.markedPos])
327 def __init__(self, obj):
335 self.key = { "choseElement": 0,
352 configfile = configFile()
355 def __init__(self, parent):
364 def checkValues(self):
365 if self.parent.value < 0:
366 self.parent.value = 0
368 if self.parent.value > 10:
369 self.parent.value = 10
371 def handleKey(self, key):
372 if key == config.key["prevElement"]:
373 self.parent.value = self.parent.value - 1
374 if key == config.key["nextElement"]:
375 self.parent.value = self.parent.value + 1
380 def __call__(self, selected): #needed by configlist
382 return ("slider", self.parent.value * 10)
384 class ConfigSubsection:
390 def getIndexbyEntry(self, data):
392 tcnt = -1; #for defaultval
394 if int(x[1]) == int(data):
396 if int(x[1]) == int(self.defaultValue):
401 return 0 #prevent bigger then array
403 def datafromFile(self, control, data):
404 if control == ConfigSlider:
406 elif control == configSelection:
408 elif control == configDateTime:
410 elif control == configText:
412 elif control == configSequence:
414 part = data.split(self.vals[0])
418 elif control == configSatlist:
419 return self.getIndexbyEntry(data)
423 def datatoFile(self, control, data):
424 if control == ConfigSlider:
426 elif control == configSelection:
428 elif control == configDateTime:
430 elif control == configText:
431 return str(data.strip())
433 elif control == configSequence:
436 value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
439 # just in case you don't understand the above, here an equivalent:
443 # value += self.vals[0]
446 elif control == configSatlist:
447 return str(self.vals[self.value][1]);
452 #print "load:" + self.configPath
454 value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
459 #print "value not found - using default"
461 if self.controlType == configSatlist:
462 self.value = self.getIndexbyEntry(self.defaultValue)
464 self.value = self.defaultValue
466 self.save() #add missing value to dict
468 #print "set val:" + str(value)
471 #is this right? activate settings after load/cancel and use default
474 def __init__(self, configPath, control, defaultValue, vals):
475 self.configPath = configPath
476 self.defaultValue = defaultValue
477 self.controlType = control
479 self.notifierList = [ ]
483 def getConfigPath(self):
484 return self.configPath
486 def addNotifier(self, notifier):
487 self.notifierList.append(notifier);
490 for notifier in self.notifierList:
495 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))
497 class configElement_nonSave(configElement):
498 def __init__(self, configPath, control, defaultValue, vals):
499 configElement.__init__(self, configPath, control, defaultValue, vals)
504 def getConfigListEntry(description, element):
506 item = b.controlType(b)
507 return ((description, item))
510 def configElementBoolean(name, default, texts=(_("Enable"), _("Disable"))):
511 return configElement(name, configSelection, default, texts)
513 config.misc = ConfigSubsection()