2 from Tools.NumericalTextInput import NumericalTextInput
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
41 def getResolvedKey(self, key):
42 str = self.configElements[key]
54 if self.changed == 0: #no changes, so no write to disk needed
57 fileHandle = open(resolveFilename(SCOPE_CONFIG, "config"), "w")
59 keys = self.configElements.keys()
62 wstr = x + "=" + self.configElements[x] + "\n"
64 fileHandle.write(wstr)
68 def currentConfigSelectionElement(element):
69 return element.vals[element.value][0]
71 def getConfigSelectionElement(element, value):
73 for x in element.vals:
79 class configSelection:
80 def __init__(self, parent):
83 def checkValues(self):
84 if self.parent.value < 0:
85 self.parent.value = len(self.parent.vals) - 1
86 elif(self.parent.value > (len(self.parent.vals) - 1)):
95 def handleKey(self, key):
96 if key == config.key["prevElement"]:
97 self.parent.value = self.parent.value - 1
98 if key == config.key["nextElement"]:
99 self.parent.value = self.parent.value + 1
105 def __call__(self, selected): #needed by configlist
108 returnValue = _(self.parent.vals[self.parent.value])
109 if not isinstance(returnValue, str):
110 returnValue = returnValue[1]
112 # FIXME: it's not really nice to translate this here.
113 # however, configSelections are persistent.
115 # WORKAROUND: don't translate ""
117 returnValue = _(returnValue)
119 return ("text", returnValue)
121 class configDateTime:
122 def __init__(self, parent):
125 def checkValues(self):
127 # if self.parent.value < 0:
128 #self.parent.value = 0
130 #if(self.parent.value >= (len(self.parent.vals) - 1)):
131 #self.parent.value = len(self.parent.vals) - 1
139 def handleKey(self, key):
140 if key == config.key["prevElement"]:
141 self.parent.value = self.parent.value - self.parent.vals[1]
142 if key == config.key["nextElement"]:
143 self.parent.value = self.parent.value + self.parent.vals[1]
149 def __call__(self, selected): #needed by configlist
151 return ("text", strftime(self.parent.vals[0], localtime(self.parent.value)))
154 def __init__(self, parent):
157 def checkValues(self):
158 if self.parent.value < 0:
159 self.parent.value = 0
161 if(self.parent.value >= (len(self.parent.vals) - 1)):
162 self.parent.value = len(self.parent.vals) - 1
170 def handleKey(self, key):
171 if key == config.key["prevElement"]:
172 self.parent.value = self.parent.value - 1
173 if key == config.key["nextElement"]:
174 self.parent.value = self.parent.value + 1
180 def __call__(self, selected): #needed by configlist
183 return ("text", str(self.parent.vals[self.parent.value][0]))
185 class configSequenceArg:
186 def get(self, type, args = ()):
187 # configsequencearg.get ("IP")
189 return (("."), [(0,255),(0,255),(0,255),(0,255)], "")
190 # configsequencearg.get ("MAC")
191 if (type == "POSITION"):
192 return ((","), [(0,args[0]),(0,args[1]),(0,args[2]),(0,args[3])], "")
194 return ((":"), [(1,255),(1,255),(1,255),(1,255),(1,255),(1,255)], "")
195 # configsequencearg.get ("CLOCK")
196 if (type == "CLOCK"):
197 return ((":"), [(0,23),(0,59)], "")
198 # configsequencearg.get("INTEGER", (min, max)) => x with min <= x <= max
199 if (type == "INTEGER"):
200 return ((":"), [args], "")
201 # configsequencearg.get("PINCODE", (number, "*")) => pin with number = length of pincode and "*" as numbers shown as stars
202 # configsequencearg.get("PINCODE", (number, "")) => pin with number = length of pincode and numbers shown
203 if (type == "PINCODE"):
204 return ((":"), [(0, (10**args[0])-1)], args[1])
205 # configsequencearg.get("FLOAT", [(min,max),(min1,max1)]) => x.y with min <= x <= max and min1 <= y <= max1
206 if (type == "FLOAT"):
207 return (("."), args, "")
209 def getFloat(self, element):
210 return float(("%d.%0" + str(len(str(element.vals[1][1][1]))) + "d") % (element.value[0], element.value[1]))
212 configsequencearg = configSequenceArg()
214 class configSequence:
215 def __init__(self, parent):
218 self.seperator = self.parent.vals[0]
219 self.valueBounds = self.parent.vals[1]
220 self.censorChar = self.parent.vals[2]
222 def checkValues(self):
225 for i in self.parent.value:
226 maxPos += len(str(self.valueBounds[num][1]))
227 while (self.valueBounds[num][0] > self.parent.value[num]):
228 self.parent.value[num] += 1
230 while (self.valueBounds[num][1] < self.parent.value[num]):
231 self.parent.value[num] -= 1
233 # if (self.valueBounds[num][0] <= i <= self.valueBounds[num][1]):
236 #self.parent.value[num] = self.valueBounds[num][0]
239 if self.markedPos >= maxPos:
240 self.markedPos = maxPos - 1
241 if self.markedPos < 0:
250 def handleKey(self, key):
251 #this will no change anything on the value itself
252 #so we can handle it here in gui element
253 if key == config.key["prevElement"]:
255 if key == config.key["nextElement"]:
258 if key >= config.key["0"] and key <= config.key["9"]:
260 for x in self.valueBounds:
261 self.blockLen.append(len(str(x[1])))
265 self.blockLenTotal = [0,]
266 for x in self.blockLen:
267 pos += self.blockLen[blocknumber]
268 self.blockLenTotal.append(pos)
269 if (pos - 1 >= self.markedPos):
274 number = 9 - config.key["9"] + key
275 # length of numberblock
276 numberLen = len(str(self.valueBounds[blocknumber][1]))
277 # position in the block
278 posinblock = self.markedPos - self.blockLenTotal[blocknumber]
280 oldvalue = self.parent.value[blocknumber]
281 olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
282 newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
284 self.parent.value[blocknumber] = newvalue
289 #FIXME: dont call when press left/right
292 def __call__(self, selected): #needed by configlist
294 mPos = self.markedPos
296 for i in self.parent.value:
297 if len(value): #fixme no heading separator possible
298 value += self.seperator
299 if mPos >= len(value) - 1:
302 #diff = self.valueBounds - len(str(i))
306 if (self.censorChar == ""):
307 value += ("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i
309 value += (self.censorChar * len(str(self.valueBounds[num][1])))
311 # only mark cursor when we are selected
312 # (this code is heavily ink optimized!)
313 if (self.parent.enabled == True):
314 return ("mtext"[1-selected:], value, [mPos])
316 return ("text", value)
319 def __init__(self, parent):
330 self.parent.vals[1](self.parent.getConfigPath())
332 def handleKey(self, key):
335 def __call__(self, selected): #needed by configlist
338 class configText(NumericalTextInput):
339 # used as first parameter
340 # is the text of a fixed size or is the user able to extend the length of the text
344 def __init__(self, parent):
345 NumericalTextInput.__init__(self, self.nextEntry)
348 self.mode = self.parent.vals[0]
350 self.parent.value = self.parent.value.decode("utf-8")
351 except UnicodeDecodeError:
354 def checkValues(self):
355 if (self.markedPos < 0):
357 if (self.markedPos >= len(self.parent.value)):
358 self.markedPos = len(self.parent.value) - 1
367 self.parent.vals[1](self.parent.getConfigPath())
369 def handleKey(self, key):
370 #this will no change anything on the value itself
371 #so we can handle it here in gui element
372 if key == config.key["delete"]:
373 self.parent.value = self.parent.value[0:self.markedPos] + self.parent.value[self.markedPos + 1:]
374 elif key == config.key["prevElement"]:
377 elif key == config.key["nextElement"]:
380 if (self.mode == self.extendableSize):
381 if (self.markedPos >= len(self.parent.value)):
382 self.parent.value = self.parent.value.ljust(len(self.parent.value) + 1)
383 elif key >= config.key["0"] and key <= config.key["9"]:
384 number = 9 - config.key["9"] + key
385 self.parent.value = self.parent.value[0:self.markedPos] + self.getKey(number) + self.parent.value[self.markedPos + 1:]
390 def __call__(self, selected): #needed by configlist
391 return ("mtext"[1-selected:], self.parent.value.encode("utf-8"), [self.markedPos])
394 def __init__(self, obj):
402 self.key = { "choseElement": 0,
419 configfile = configFile()
422 def __init__(self, parent):
431 def checkValues(self):
432 if self.parent.value < 0:
433 self.parent.value = 0
435 if self.parent.value > self.parent.vals[1]:
436 self.parent.value = self.parent.vals[1]
438 def handleKey(self, key):
439 if key == config.key["prevElement"]:
440 self.parent.value = self.parent.value - self.parent.vals[0]
441 if key == config.key["nextElement"]:
442 self.parent.value = self.parent.value + self.parent.vals[0]
447 def __call__(self, selected): #needed by configlist
449 return ("slider", self.parent.value, self.parent.vals[1])
451 class ConfigSubsection:
457 def getIndexbyEntry(self, data):
459 tcnt = -1; #for defaultval
461 if int(x[1]) == int(data):
463 if int(x[1]) == int(self.defaultValue):
468 return 0 #prevent bigger then array
470 def datafromFile(self, control, data):
471 if control == configSlider:
473 elif control == configSelection:
477 for x in data.split(":"):
484 return self.defaultValue
485 elif control == configDateTime:
487 elif control == configText:
489 elif control == configSequence:
491 part = data.split(self.vals[0])
495 elif control == configSatlist:
496 return self.getIndexbyEntry(data)
500 def datatoFile(self, control, data):
501 if control == configSlider:
503 elif control == configSelection:
504 if len(self.vals) < data + 1:
506 if isinstance(self.vals[data], str):
513 confList.append("*" + str(x[0] + "*"))
515 confList.append(x[0])
517 return ":".join(confList)
519 elif control == configDateTime:
521 elif control == configText:
522 return str(data.strip())
523 elif control == configSequence:
531 value += self.vals[0]
532 value += (("%0" + str(len(str(self.vals[1][count][1]))) + "d") % i)
534 #value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
538 elif control == configSatlist:
539 return str(self.vals[self.value][1]);
544 #print "load:" + self.configPath
546 value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
551 #print "value not found - using default"
552 if self.controlType == configSatlist:
553 self.value = self.getIndexbyEntry(self.defaultValue)
554 elif self.controlType == configSequence:
555 self.value = self.defaultValue[:]
557 self.value = self.defaultValue
559 self.save() #add missing value to dict
561 #print "set val:" + str(value)
564 #is this right? activate settings after load/cancel and use default
567 def __init__(self, configPath, control, defaultValue, vals, saveDefaults = True):
568 self.configPath = configPath
569 self.defaultValue = defaultValue
570 self.controlType = control
572 self.notifierList = [ ]
574 self.saveDefaults = saveDefaults
577 def getConfigPath(self):
578 return self.configPath
580 def addNotifier(self, notifier):
581 self.notifierList.append(notifier);
585 for notifier in self.notifierList:
592 if self.controlType == configSatlist:
593 defaultValue = self.getIndexbyEntry(self.defaultValue)
595 defaultValue = self.defaultValue
596 if self.value != defaultValue or self.saveDefaults:
597 configfile.setKey(self.configPath, self.datatoFile(self.controlType, self.value))
600 oldValue = configfile.getKey(self.configPath)
603 if oldValue is not None and oldValue != defaultValue:
604 configfile.setKey(self.configPath, self.datatoFile(self.controlType, self.value), True)
606 class configElement_nonSave(configElement):
607 def __init__(self, configPath, control, defaultValue, vals):
608 configElement.__init__(self, configPath, control, defaultValue, vals)
613 def getConfigListEntry(description, element):
615 item = b.controlType(b)
616 return ((description, item))
618 def configElementBoolean(name, default, texts=(_("Disable"), _("Enable"))):
619 return configElement(name, configSelection, default, texts)
621 config.misc = ConfigSubsection()