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, isDefaultKey=False):
36 if isDefaultKey and self.configElements.has_key(key):
37 del self.configElements[key]
39 self.configElements[key] = value
42 if self.changed == 0: #no changes, so no write to disk needed
45 fileHandle = open(resolveFilename(SCOPE_CONFIG, "config"), "w")
47 keys = self.configElements.keys()
50 wstr = x + "=" + self.configElements[x] + "\n"
52 fileHandle.write(wstr)
56 def currentConfigSelectionElement(element):
57 return element.vals[element.value][0]
59 def getConfigSelectionElement(element, value):
61 for x in element.vals:
67 class configSelection:
68 def __init__(self, parent):
71 def checkValues(self):
72 if self.parent.value < 0:
73 self.parent.value = len(self.parent.vals) - 1
74 elif(self.parent.value > (len(self.parent.vals) - 1)):
83 def handleKey(self, key):
84 if key == config.key["prevElement"]:
85 self.parent.value = self.parent.value - 1
86 if key == config.key["nextElement"]:
87 self.parent.value = self.parent.value + 1
93 def __call__(self, selected): #needed by configlist
96 returnValue = _(self.parent.vals[self.parent.value])
97 if not isinstance(returnValue, str):
98 returnValue = returnValue[1]
100 # FIXME: it's not really nice to translate this here.
101 # however, configSelections are persistent.
103 # WORKAROUND: don't translate ""
105 returnValue = _(returnValue)
107 return ("text", returnValue)
109 class configDateTime:
110 def __init__(self, parent):
113 def checkValues(self):
115 # if self.parent.value < 0:
116 #self.parent.value = 0
118 #if(self.parent.value >= (len(self.parent.vals) - 1)):
119 #self.parent.value = len(self.parent.vals) - 1
127 def handleKey(self, key):
128 if key == config.key["prevElement"]:
129 self.parent.value = self.parent.value - self.parent.vals[1]
130 if key == config.key["nextElement"]:
131 self.parent.value = self.parent.value + self.parent.vals[1]
137 def __call__(self, selected): #needed by configlist
139 return ("text", strftime(self.parent.vals[0], localtime(self.parent.value)))
142 def __init__(self, parent):
145 def checkValues(self):
146 if self.parent.value < 0:
147 self.parent.value = 0
149 if(self.parent.value >= (len(self.parent.vals) - 1)):
150 self.parent.value = len(self.parent.vals) - 1
158 def handleKey(self, key):
159 if key == config.key["prevElement"]:
160 self.parent.value = self.parent.value - 1
161 if key == config.key["nextElement"]:
162 self.parent.value = self.parent.value + 1
168 def __call__(self, selected): #needed by configlist
171 return ("text", str(self.parent.vals[self.parent.value][0]))
173 class configSequenceArg:
174 def get(self, type, args = ()):
175 # configsequencearg.get ("IP")
177 return (("."), [(0,255),(0,255),(0,255),(0,255)], "")
178 # configsequencearg.get ("MAC")
180 return ((":"), [(1,255),(1,255),(1,255),(1,255),(1,255),(1,255)], "")
181 # configsequencearg.get ("CLOCK")
182 if (type == "CLOCK"):
183 return ((":"), [(0,23),(0,59)], "")
184 # configsequencearg.get("INTEGER", (min, max)) => x with min <= x <= max
185 if (type == "INTEGER"):
186 return ((":"), [args], "")
187 # configsequencearg.get("PINCODE", (number, "*")) => pin with number = length of pincode and "*" as numbers shown as stars
188 # configsequencearg.get("PINCODE", (number, "")) => pin with number = length of pincode and numbers shown
189 if (type == "PINCODE"):
190 return ((":"), [(0, (10**args[0])-1)], args[1])
191 # configsequencearg.get("FLOAT", [(min,max),(min1,max1)]) => x.y with min <= x <= max and min1 <= y <= max1
192 if (type == "FLOAT"):
193 return (("."), args, "")
195 def getFloat(self, element):
196 return float(("%d.%0" + str(len(str(element.vals[1][1][1]))) + "d") % (element.value[0], element.value[1]))
198 configsequencearg = configSequenceArg()
200 class configSequence:
201 def __init__(self, parent):
204 self.seperator = self.parent.vals[0]
205 self.valueBounds = self.parent.vals[1]
206 self.censorChar = self.parent.vals[2]
208 def checkValues(self):
211 for i in self.parent.value:
212 maxPos += len(str(self.valueBounds[num][1]))
213 while (self.valueBounds[num][0] > self.parent.value[num]):
214 self.parent.value[num] += 1
216 while (self.valueBounds[num][1] < self.parent.value[num]):
217 self.parent.value[num] -= 1
219 # if (self.valueBounds[num][0] <= i <= self.valueBounds[num][1]):
222 #self.parent.value[num] = self.valueBounds[num][0]
225 if self.markedPos >= maxPos:
226 self.markedPos = maxPos - 1
227 if self.markedPos < 0:
236 def handleKey(self, key):
237 #this will no change anything on the value itself
238 #so we can handle it here in gui element
239 if key == config.key["prevElement"]:
241 if key == config.key["nextElement"]:
244 if key >= config.key["0"] and key <= config.key["9"]:
246 for x in self.valueBounds:
247 self.blockLen.append(len(str(x[1])))
251 self.blockLenTotal = [0,]
252 for x in self.blockLen:
253 pos += self.blockLen[blocknumber]
254 self.blockLenTotal.append(pos)
255 if (pos - 1 >= self.markedPos):
260 number = 9 - config.key["9"] + key
261 # length of numberblock
262 numberLen = len(str(self.valueBounds[blocknumber][1]))
263 # position in the block
264 posinblock = self.markedPos - self.blockLenTotal[blocknumber]
266 oldvalue = self.parent.value[blocknumber]
267 olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
268 newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
270 self.parent.value[blocknumber] = newvalue
275 #FIXME: dont call when press left/right
278 def __call__(self, selected): #needed by configlist
280 mPos = self.markedPos
282 for i in self.parent.value:
283 if len(value): #fixme no heading separator possible
284 value += self.seperator
285 if mPos >= len(value) - 1:
288 #diff = self.valueBounds - len(str(i))
292 if (self.censorChar == ""):
293 value += ("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i
295 value += (self.censorChar * len(str(self.valueBounds[num][1])))
297 # only mark cursor when we are selected
298 # (this code is heavily ink optimized!)
299 if (self.parent.enabled == True):
300 return ("mtext"[1-selected:], value, [mPos])
302 return ("text", value)
305 def __init__(self, parent):
316 self.parent.vals[1](self.parent.getConfigPath())
318 def handleKey(self, key):
321 def __call__(self, selected): #needed by configlist
325 # used as first parameter
326 # is the text of a fixed size or is the user able to extend the length of the text
330 def __init__(self, parent):
333 self.mode = self.parent.vals[0]
334 self.textInput = NumericalTextInput(self.nextEntry)
336 def checkValues(self):
337 if (self.markedPos < 0):
339 if (self.markedPos >= len(self.parent.value)):
340 self.markedPos = len(self.parent.value) - 1
349 self.parent.vals[1](self.parent.getConfigPath())
351 def handleKey(self, key):
352 #this will no change anything on the value itself
353 #so we can handle it here in gui element
354 if key == config.key["delete"]:
355 self.parent.value = self.parent.value[0:self.markedPos] + self.parent.value[self.markedPos + 1:]
356 if key == config.key["prevElement"]:
357 self.textInput.nextKey()
360 if key == config.key["nextElement"]:
361 self.textInput.nextKey()
363 if (self.mode == self.extendableSize):
364 if (self.markedPos >= len(self.parent.value)):
365 self.parent.value = self.parent.value.ljust(len(self.parent.value) + 1)
368 if key >= config.key["0"] and key <= config.key["9"]:
369 number = 9 - config.key["9"] + key
371 self.parent.value = self.parent.value[0:self.markedPos] + str(self.textInput.getKey(number)) + self.parent.value[self.markedPos + 1:]
377 def __call__(self, selected): #needed by configlist
378 return ("mtext"[1-selected:], str(self.parent.value), [self.markedPos])
381 def __init__(self, obj):
389 self.key = { "choseElement": 0,
406 configfile = configFile()
409 def __init__(self, parent):
418 def checkValues(self):
419 if self.parent.value < 0:
420 self.parent.value = 0
422 if self.parent.value > self.parent.vals[1]:
423 self.parent.value = self.parent.vals[1]
425 def handleKey(self, key):
426 if key == config.key["prevElement"]:
427 self.parent.value = self.parent.value - self.parent.vals[0]
428 if key == config.key["nextElement"]:
429 self.parent.value = self.parent.value + self.parent.vals[0]
434 def __call__(self, selected): #needed by configlist
436 return ("slider", self.parent.value, self.parent.vals[1])
438 class ConfigSubsection:
444 def getIndexbyEntry(self, data):
446 tcnt = -1; #for defaultval
448 if int(x[1]) == int(data):
450 if int(x[1]) == int(self.defaultValue):
455 return 0 #prevent bigger then array
457 def datafromFile(self, control, data):
458 if control == configSlider:
460 elif control == configSelection:
464 for x in data.split(":"):
471 return self.defaultValue
472 elif control == configDateTime:
474 elif control == configText:
476 elif control == configSequence:
478 part = data.split(self.vals[0])
482 elif control == configSatlist:
483 return self.getIndexbyEntry(data)
487 def datatoFile(self, control, data):
488 if control == configSlider:
490 elif control == configSelection:
491 if len(self.vals) < data + 1:
493 if isinstance(self.vals[data], str):
500 confList.append("*" + str(x[0] + "*"))
502 confList.append(x[0])
504 return ":".join(confList)
506 elif control == configDateTime:
508 elif control == configText:
509 return str(data.strip())
510 elif control == configSequence:
518 value += self.vals[0]
519 value += (("%0" + str(len(str(self.vals[1][count][1]))) + "d") % i)
521 #value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
525 elif control == configSatlist:
526 return str(self.vals[self.value][1]);
531 #print "load:" + self.configPath
533 value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
538 #print "value not found - using default"
539 if self.controlType == configSatlist:
540 self.value = self.getIndexbyEntry(self.defaultValue)
541 elif self.controlType == configSequence:
542 self.value = self.defaultValue[:]
544 self.value = self.defaultValue
546 self.save() #add missing value to dict
548 #print "set val:" + str(value)
551 #is this right? activate settings after load/cancel and use default
554 def __init__(self, configPath, control, defaultValue, vals, saveDefaults = True):
555 self.configPath = configPath
556 self.defaultValue = defaultValue
557 self.controlType = control
559 self.notifierList = [ ]
561 self.saveDefaults = saveDefaults
564 def getConfigPath(self):
565 return self.configPath
567 def addNotifier(self, notifier):
568 self.notifierList.append(notifier);
572 for notifier in self.notifierList:
579 if self.controlType == configSatlist:
580 defaultValue = self.getIndexbyEntry(self.defaultValue)
582 defaultValue = self.defaultValue
583 if self.value != defaultValue or self.saveDefaults:
584 configfile.setKey(self.configPath, self.datatoFile(self.controlType, self.value))
587 oldValue = configfile.getKey(self.configPath)
590 if oldValue is not None and oldValue != defaultValue:
591 configfile.setKey(self.configPath, self.datatoFile(self.controlType, self.value), True)
593 class configElement_nonSave(configElement):
594 def __init__(self, configPath, control, defaultValue, vals):
595 configElement.__init__(self, configPath, control, defaultValue, vals)
600 def getConfigListEntry(description, element):
602 item = b.controlType(b)
603 return ((description, item))
605 def configElementBoolean(name, default, texts=(_("Enable"), _("Disable"))):
606 return configElement(name, configSelection, default, texts)
608 config.misc = ConfigSubsection()