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
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
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):
347 self.mode = self.parent.vals[0]
348 self.textInput = NumericalTextInput(self.nextEntry)
350 def checkValues(self):
351 if (self.markedPos < 0):
353 if (self.markedPos >= len(self.parent.value)):
354 self.markedPos = len(self.parent.value) - 1
363 self.parent.vals[1](self.parent.getConfigPath())
365 def handleKey(self, key):
366 #this will no change anything on the value itself
367 #so we can handle it here in gui element
368 if key == config.key["delete"]:
369 self.parent.value = self.parent.value[0:self.markedPos] + self.parent.value[self.markedPos + 1:]
370 if key == config.key["prevElement"]:
371 self.textInput.nextKey()
374 if key == config.key["nextElement"]:
375 self.textInput.nextKey()
377 if (self.mode == self.extendableSize):
378 if (self.markedPos >= len(self.parent.value)):
379 self.parent.value = self.parent.value.ljust(len(self.parent.value) + 1)
382 if key >= config.key["0"] and key <= config.key["9"]:
383 number = 9 - config.key["9"] + key
385 self.parent.value = self.parent.value[0:self.markedPos] + str(self.textInput.getKey(number)) + self.parent.value[self.markedPos + 1:]
391 def __call__(self, selected): #needed by configlist
392 return ("mtext"[1-selected:], str(self.parent.value), [self.markedPos])
395 def __init__(self, obj):
403 self.key = { "choseElement": 0,
420 configfile = configFile()
423 def __init__(self, parent):
432 def checkValues(self):
433 if self.parent.value < 0:
434 self.parent.value = 0
436 if self.parent.value > self.parent.vals[1]:
437 self.parent.value = self.parent.vals[1]
439 def handleKey(self, key):
440 if key == config.key["prevElement"]:
441 self.parent.value = self.parent.value - self.parent.vals[0]
442 if key == config.key["nextElement"]:
443 self.parent.value = self.parent.value + self.parent.vals[0]
448 def __call__(self, selected): #needed by configlist
450 return ("slider", self.parent.value, self.parent.vals[1])
452 class ConfigSubsection:
458 def getIndexbyEntry(self, data):
460 tcnt = -1; #for defaultval
462 if int(x[1]) == int(data):
464 if int(x[1]) == int(self.defaultValue):
469 return 0 #prevent bigger then array
471 def datafromFile(self, control, data):
472 if control == configSlider:
474 elif control == configSelection:
478 for x in data.split(":"):
485 return self.defaultValue
486 elif control == configDateTime:
488 elif control == configText:
490 elif control == configSequence:
492 part = data.split(self.vals[0])
496 elif control == configSatlist:
497 return self.getIndexbyEntry(data)
501 def datatoFile(self, control, data):
502 if control == configSlider:
504 elif control == configSelection:
505 if len(self.vals) < data + 1:
507 if isinstance(self.vals[data], str):
514 confList.append("*" + str(x[0] + "*"))
516 confList.append(x[0])
518 return ":".join(confList)
520 elif control == configDateTime:
522 elif control == configText:
523 return str(data.strip())
524 elif control == configSequence:
532 value += self.vals[0]
533 value += (("%0" + str(len(str(self.vals[1][count][1]))) + "d") % i)
535 #value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
539 elif control == configSatlist:
540 return str(self.vals[self.value][1]);
545 #print "load:" + self.configPath
547 value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
552 #print "value not found - using default"
553 if self.controlType == configSatlist:
554 self.value = self.getIndexbyEntry(self.defaultValue)
555 elif self.controlType == configSequence:
556 self.value = self.defaultValue[:]
558 self.value = self.defaultValue
560 self.save() #add missing value to dict
562 #print "set val:" + str(value)
565 #is this right? activate settings after load/cancel and use default
568 def __init__(self, configPath, control, defaultValue, vals, saveDefaults = True):
569 self.configPath = configPath
570 self.defaultValue = defaultValue
571 self.controlType = control
573 self.notifierList = [ ]
575 self.saveDefaults = saveDefaults
578 def getConfigPath(self):
579 return self.configPath
581 def addNotifier(self, notifier):
582 self.notifierList.append(notifier);
586 for notifier in self.notifierList:
593 if self.controlType == configSatlist:
594 defaultValue = self.getIndexbyEntry(self.defaultValue)
596 defaultValue = self.defaultValue
597 if self.value != defaultValue or self.saveDefaults:
598 configfile.setKey(self.configPath, self.datatoFile(self.controlType, self.value))
601 oldValue = configfile.getKey(self.configPath)
604 if oldValue is not None and oldValue != defaultValue:
605 configfile.setKey(self.configPath, self.datatoFile(self.controlType, self.value), True)
607 class configElement_nonSave(configElement):
608 def __init__(self, configPath, control, defaultValue, vals):
609 configElement.__init__(self, configPath, control, defaultValue, vals)
614 def getConfigListEntry(description, element):
616 item = b.controlType(b)
617 return ((description, item))
619 def configElementBoolean(name, default, texts=(_("Disable"), _("Enable"))):
620 return configElement(name, configSelection, default, texts)
622 config.misc = ConfigSubsection()