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 if isinstance(self.parent.vals[self.parent.value], str):
82 returnValue = _(self.parent.vals[self.parent.value])
84 returnValue = _(self.parent.vals[self.parent.value][1])
87 print self.parent.vals[self.parent.value]
89 return ("text", returnValue)
92 def __init__(self, parent):
95 def checkValues(self):
97 # if self.parent.value < 0:
98 #self.parent.value = 0
100 #if(self.parent.value >= (len(self.parent.vals) - 1)):
101 #self.parent.value = len(self.parent.vals) - 1
109 def handleKey(self, key):
110 if key == config.key["prevElement"]:
111 self.parent.value = self.parent.value - self.parent.vals[1]
112 if key == config.key["nextElement"]:
113 self.parent.value = self.parent.value + self.parent.vals[1]
119 def __call__(self, selected): #needed by configlist
121 return ("text", strftime(self.parent.vals[0], localtime(self.parent.value)))
124 def __init__(self, parent):
127 def checkValues(self):
128 if self.parent.value < 0:
129 self.parent.value = 0
131 if(self.parent.value >= (len(self.parent.vals) - 1)):
132 self.parent.value = len(self.parent.vals) - 1
140 def handleKey(self, key):
141 if key == config.key["prevElement"]:
142 self.parent.value = self.parent.value - 1
143 if key == config.key["nextElement"]:
144 self.parent.value = self.parent.value + 1
150 def __call__(self, selected): #needed by configlist
153 return ("text", str(self.parent.vals[self.parent.value][0]))
155 class configSequenceArg:
156 def get(self, type, args = ()):
157 # configsequencearg.get ("IP")
159 return (("."), [(0,255),(0,255),(0,255),(0,255)], "")
160 # configsequencearg.get ("MAC")
162 return ((":"), [(1,255),(1,255),(1,255),(1,255),(1,255),(1,255)], "")
163 # configsequencearg.get ("CLOCK")
164 if (type == "CLOCK"):
165 return ((":"), [(0,23),(0,59)], "")
166 # configsequencearg.get("INTEGER", (min, max)) => x with min <= x <= max
167 if (type == "INTEGER"):
168 return ((":"), [args], "")
169 # configsequencearg.get("PINCODE", (number, "*")) => pin with number = length of pincode and "*" as numbers shown as stars
170 # configsequencearg.get("PINCODE", (number, "")) => pin with number = length of pincode and numbers shown
171 if (type == "PINCODE"):
172 return ((":"), [(0, (10**args[0])-1)], args[1])
173 # configsequencearg.get("FLOAT", [(min,max),(min1,max1)]) => x.y with min <= x <= max and min1 <= y <= max1
174 if (type == "FLOAT"):
175 return (("."), args, "")
177 configsequencearg = configSequenceArg()
179 class configSequence:
180 def __init__(self, parent):
183 self.seperator = self.parent.vals[0]
184 self.valueBounds = self.parent.vals[1]
185 self.censorChar = self.parent.vals[2]
187 def checkValues(self):
190 for i in self.parent.value:
191 maxPos += len(str(self.valueBounds[num][1]))
192 while (self.valueBounds[num][0] > self.parent.value[num]):
193 self.parent.value[num] += 1
195 while (self.valueBounds[num][1] < self.parent.value[num]):
196 self.parent.value[num] -= 1
198 # if (self.valueBounds[num][0] <= i <= self.valueBounds[num][1]):
201 #self.parent.value[num] = self.valueBounds[num][0]
204 if self.markedPos >= maxPos:
205 self.markedPos = maxPos - 1
206 if self.markedPos < 0:
215 def handleKey(self, key):
216 #this will no change anything on the value itself
217 #so we can handle it here in gui element
218 if key == config.key["prevElement"]:
220 if key == config.key["nextElement"]:
223 if key >= config.key["0"] and key <= config.key["9"]:
225 for x in self.valueBounds:
226 self.blockLen.append(len(str(x[1])))
230 self.blockLenTotal = [0,]
231 for x in self.blockLen:
232 pos += self.blockLen[blocknumber]
233 self.blockLenTotal.append(pos)
234 if (pos - 1 >= self.markedPos):
239 number = 9 - config.key["9"] + key
240 # length of numberblock
241 numberLen = len(str(self.valueBounds[blocknumber][1]))
242 # position in the block
243 posinblock = self.markedPos - self.blockLenTotal[blocknumber]
245 oldvalue = self.parent.value[blocknumber]
246 olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
247 newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
249 print "You actually pressed a number (" + str(number) + ") which will be added at block number " + str(blocknumber) + " on position " + str(posinblock)
250 print "Old value: " + str(oldvalue) + " olddec: " + str(olddec) + " newvalue: " + str(newvalue)
251 self.parent.value[blocknumber] = newvalue
259 #FIXME: dont call when press left/right
262 def __call__(self, selected): #needed by configlist
264 mPos = self.markedPos
265 print "Positon: " + str(mPos)
267 for i in self.parent.value:
268 if len(value): #fixme no heading separator possible
269 value += self.seperator
270 if mPos >= len(value) - 1:
273 #diff = self.valueBounds - len(str(i))
277 print (("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i)
278 if (self.censorChar == ""):
279 value += ("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i
281 value += (self.censorChar * len(str(self.valueBounds[num][1])))
283 # only mark cursor when we are selected
284 # (this code is heavily ink optimized!)
285 if (self.parent.enabled == True):
286 return ("mtext"[1-selected:], value, [mPos])
288 return ("text", value)
291 # used as first parameter
292 # is the text of a fixed size or is the user able to extend the length of the text
296 def __init__(self, parent):
299 self.mode = self.parent.vals[0]
300 self.textInput = NumericalTextInput(self.nextEntry)
302 def checkValues(self):
303 if (self.markedPos < 0):
305 if (self.markedPos >= len(self.parent.value)):
306 self.markedPos = len(self.parent.value) - 1
316 self.parent.vals[1](self.parent.getConfigPath())
318 def handleKey(self, key):
319 #this will no change anything on the value itself
320 #so we can handle it here in gui element
321 if key == config.key["delete"]:
322 self.parent.value = self.parent.value[0:self.markedPos] + self.parent.value[self.markedPos + 1:]
323 if key == config.key["prevElement"]:
324 self.textInput.nextKey()
327 if key == config.key["nextElement"]:
328 self.textInput.nextKey()
330 if (self.mode == self.extendableSize):
331 if (self.markedPos >= len(self.parent.value)):
332 self.parent.value = self.parent.value.ljust(len(self.parent.value) + 1)
335 if key >= config.key["0"] and key <= config.key["9"]:
336 number = 9 - config.key["9"] + key
338 self.parent.value = self.parent.value[0:self.markedPos] + str(self.textInput.getKey(number)) + self.parent.value[self.markedPos + 1:]
344 def __call__(self, selected): #needed by configlist
345 return ("mtext"[1-selected:], str(self.parent.value), [self.markedPos])
348 def __init__(self, obj):
356 self.key = { "choseElement": 0,
373 configfile = configFile()
376 def __init__(self, parent):
385 def checkValues(self):
386 if self.parent.value < 0:
387 self.parent.value = 0
389 if self.parent.value > 10:
390 self.parent.value = 10
392 def handleKey(self, key):
393 if key == config.key["prevElement"]:
394 self.parent.value = self.parent.value - 1
395 if key == config.key["nextElement"]:
396 self.parent.value = self.parent.value + 1
401 def __call__(self, selected): #needed by configlist
403 return ("slider", self.parent.value * 10)
405 class ConfigSubsection:
411 def getIndexbyEntry(self, data):
413 tcnt = -1; #for defaultval
415 if int(x[1]) == int(data):
417 if int(x[1]) == int(self.defaultValue):
422 return 0 #prevent bigger then array
424 def datafromFile(self, control, data):
425 if control == ConfigSlider:
427 elif control == configSelection:
431 for x in data.split(":"):
438 return self.defaultValue
439 elif control == configDateTime:
441 elif control == configText:
443 elif control == configSequence:
445 part = data.split(self.vals[0])
449 elif control == configSatlist:
450 return self.getIndexbyEntry(data)
454 def datatoFile(self, control, data):
455 if control == ConfigSlider:
457 elif control == configSelection:
458 if isinstance(self.vals[data], str):
465 confList.append("*" + str(x[0] + "*"))
467 confList.append(x[0])
469 return ":".join(confList)
471 elif control == configDateTime:
473 elif control == configText:
474 return str(data.strip())
476 elif control == configSequence:
479 value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
482 # just in case you don't understand the above, here an equivalent:
486 # value += self.vals[0]
489 elif control == configSatlist:
490 return str(self.vals[self.value][1]);
495 #print "load:" + self.configPath
497 value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
502 #print "value not found - using default"
504 if self.controlType == configSatlist:
505 self.value = self.getIndexbyEntry(self.defaultValue)
507 self.value = self.defaultValue
509 self.save() #add missing value to dict
511 #print "set val:" + str(value)
514 #is this right? activate settings after load/cancel and use default
517 def __init__(self, configPath, control, defaultValue, vals):
518 self.configPath = configPath
519 self.defaultValue = defaultValue
520 self.controlType = control
522 self.notifierList = [ ]
526 def getConfigPath(self):
527 return self.configPath
529 def addNotifier(self, notifier):
530 self.notifierList.append(notifier);
533 for notifier in self.notifierList:
538 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))
540 class configElement_nonSave(configElement):
541 def __init__(self, configPath, control, defaultValue, vals):
542 configElement.__init__(self, configPath, control, defaultValue, vals)
547 def getConfigListEntry(description, element):
549 item = b.controlType(b)
550 return ((description, item))
553 def configElementBoolean(name, default, texts=(_("Enable"), _("Disable"))):
554 return configElement(name, configSelection, default, texts)
556 config.misc = ConfigSubsection()