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.Text = self.parent.value.decode("utf-8")
351 except UnicodeDecodeError:
352 self.Text = self.parent.value
355 def checkValues(self):
356 if (self.markedPos < 0):
358 if (self.markedPos >= len(self.Text)):
359 self.markedPos = len(self.Text) - 1
368 self.parent.vals[1](self.parent.getConfigPath())
370 def handleKey(self, key):
371 #this will no change anything on the value itself
372 #so we can handle it here in gui element
373 if key == config.key["delete"]:
374 self.Text = self.Text[0:self.markedPos] + self.Text[self.markedPos + 1:]
375 self.parent.value = self.Text.encode("utf-8")
376 elif key == config.key["prevElement"]:
379 elif key == config.key["nextElement"]:
382 if (self.mode == self.extendableSize):
383 if (self.markedPos >= len(self.Text)):
384 self.Text = self.Text.ljust(len(self.Text) + 1)
385 self.parent.value = self.Text.encode("utf-8")
386 elif key >= config.key["0"] and key <= config.key["9"]:
387 number = 9 - config.key["9"] + key
388 self.Text = self.Text[0:self.markedPos] + self.getKey(number) + self.Text[self.markedPos + 1:]
389 self.parent.value = self.Text.encode("utf-8")
393 def __call__(self, selected): #needed by configlist
394 return ("mtext"[1-selected:], self.parent.value, [self.markedPos])
397 def __init__(self, obj):
405 self.key = { "choseElement": 0,
422 configfile = configFile()
425 def __init__(self, parent):
434 def checkValues(self):
435 if self.parent.value < 0:
436 self.parent.value = 0
438 if self.parent.value > self.parent.vals[1]:
439 self.parent.value = self.parent.vals[1]
441 def handleKey(self, key):
442 if key == config.key["prevElement"]:
443 self.parent.value = self.parent.value - self.parent.vals[0]
444 if key == config.key["nextElement"]:
445 self.parent.value = self.parent.value + self.parent.vals[0]
450 def __call__(self, selected): #needed by configlist
452 return ("slider", self.parent.value, self.parent.vals[1])
454 class ConfigSubsection:
460 def getIndexbyEntry(self, data):
462 tcnt = -1; #for defaultval
464 if int(x[1]) == int(data):
466 if int(x[1]) == int(self.defaultValue):
471 return 0 #prevent bigger then array
473 def datafromFile(self, control, data):
474 if control == configSlider:
476 elif control == configSelection:
480 for x in data.split(":"):
487 return self.defaultValue
488 elif control == configDateTime:
490 elif control == configText:
492 elif control == configSequence:
494 part = data.split(self.vals[0])
498 elif control == configSatlist:
499 return self.getIndexbyEntry(data)
503 def datatoFile(self, control, data):
504 if control == configSlider:
506 elif control == configSelection:
507 if len(self.vals) < data + 1:
509 if isinstance(self.vals[data], str):
516 confList.append("*" + str(x[0] + "*"))
518 confList.append(x[0])
520 return ":".join(confList)
522 elif control == configDateTime:
524 elif control == configText:
525 return str(data.strip())
526 elif control == configSequence:
534 value += self.vals[0]
535 value += (("%0" + str(len(str(self.vals[1][count][1]))) + "d") % i)
537 #value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
541 elif control == configSatlist:
542 return str(self.vals[self.value][1]);
547 #print "load:" + self.configPath
549 value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
554 #print "value not found - using default"
555 if self.controlType == configSatlist:
556 self.value = self.getIndexbyEntry(self.defaultValue)
557 elif self.controlType == configSequence:
558 self.value = self.defaultValue[:]
560 self.value = self.defaultValue
562 self.save() #add missing value to dict
564 #print "set val:" + str(value)
567 #is this right? activate settings after load/cancel and use default
570 def __init__(self, configPath, control, defaultValue, vals, saveDefaults = True):
571 self.configPath = configPath
572 self.defaultValue = defaultValue
573 self.controlType = control
575 self.notifierList = [ ]
577 self.saveDefaults = saveDefaults
580 def getConfigPath(self):
581 return self.configPath
583 def addNotifier(self, notifier):
584 self.notifierList.append(notifier);
588 for notifier in self.notifierList:
595 if self.controlType == configSatlist:
596 defaultValue = self.getIndexbyEntry(self.defaultValue)
598 defaultValue = self.defaultValue
599 if self.value != defaultValue or self.saveDefaults:
600 configfile.setKey(self.configPath, self.datatoFile(self.controlType, self.value))
603 oldValue = configfile.getKey(self.configPath)
606 if oldValue is not None and oldValue != defaultValue:
607 configfile.setKey(self.configPath, self.datatoFile(self.controlType, self.value), True)
609 class configElement_nonSave(configElement):
610 def __init__(self, configPath, control, defaultValue, vals):
611 configElement.__init__(self, configPath, control, defaultValue, vals)
616 def getConfigListEntry(description, element):
618 item = b.controlType(b)
619 return ((description, item))
621 def configElementBoolean(name, default, texts=(_("Disable"), _("Enable"))):
622 return configElement(name, configSelection, default, texts)
624 config.misc = ConfigSubsection()