2 from Tools.NumericalTextInput import *
7 self.configElements = { }
9 self.file = open("config")
11 print "cannot open config file"
15 line = self.file.readline()
19 if line.startswith("#"): #skip comments
25 def addElement(self, line):
28 self.configElements[line[:x]] = line[x + 1:]
30 def getKey(self, key):
31 return self.configElements[key]
33 def setKey(self, key, value):
35 self.configElements[key] = value
38 if self.changed == 0: #no changes, so no write to disk needed
41 fileHandle = open("config", "w")
43 keys = self.configElements.keys()
46 wstr = x + "=" + self.configElements[x]
48 if wstr[len(wstr) - 1] != '\n':
51 fileHandle.write(wstr)
55 class configSelection:
56 def __init__(self, parent):
59 def checkValues(self):
60 if self.parent.value < 0:
63 if(self.parent.value >= (len(self.parent.vals) - 1)):
64 self.parent.value = len(self.parent.vals) - 1
72 def handleKey(self, key):
73 if key == config.key["prevElement"]:
74 self.parent.value = self.parent.value - 1
75 if key == config.key["nextElement"]:
76 self.parent.value = self.parent.value + 1
82 def __call__(self, selected): #needed by configlist
84 return ("text", self.parent.vals[self.parent.value])
87 def __init__(self, parent):
90 def checkValues(self):
92 # if self.parent.value < 0:
93 #self.parent.value = 0
95 #if(self.parent.value >= (len(self.parent.vals) - 1)):
96 #self.parent.value = len(self.parent.vals) - 1
104 def handleKey(self, key):
105 if key == config.key["prevElement"]:
106 self.parent.value = self.parent.value - self.parent.vals[1]
107 if key == config.key["nextElement"]:
108 self.parent.value = self.parent.value + self.parent.vals[1]
114 def __call__(self, selected): #needed by configlist
116 return ("text", strftime(self.parent.vals[0], localtime(self.parent.value)))
119 def __init__(self, parent):
122 def checkValues(self):
123 if self.parent.value < 0:
124 self.parent.value = 0
126 if(self.parent.value >= (len(self.parent.vals) - 1)):
127 self.parent.value = len(self.parent.vals) - 1
135 def handleKey(self, key):
136 if key == config.key["prevElement"]:
137 self.parent.value = self.parent.value - 1
138 if key == config.key["nextElement"]:
139 self.parent.value = self.parent.value + 1
145 def __call__(self, selected): #needed by configlist
148 return ("text", str(self.parent.vals[self.parent.value][0]))
150 class configSequenceArg:
151 def get(self, type, args = ()):
152 # configsequencearg.get ("IP")
154 return (("."), [(1,255),(0,255),(0,255),(0,255)], "")
155 # configsequencearg.get ("MAC")
157 return ((":"), [(1,255),(1,255),(1,255),(1,255),(1,255),(1,255)], "")
158 # configsequencearg.get ("CLOCK")
159 if (type == "CLOCK"):
160 return ((":"), [(0,23),(0,59)], "")
161 # configsequencearg.get("INTEGER", (min, max)) => x with min <= x <= max
162 if (type == "INTEGER"):
163 return ((":"), [args], "")
164 # configsequencearg.get("PINCODE", (number, "*")) => pin with number = length of pincode and "*" as numbers shown as stars
165 # configsequencearg.get("PINCODE", (number, "")) => pin with number = length of pincode and numbers shown
166 if (type == "PINCODE"):
167 return ((":"), [(0, (10**args[0])-1)], args[1])
168 # configsequencearg.get("FLOAT", [(min,max),(min1,max1)]) => x.y with min <= x <= max and min1 <= y <= max1
169 if (type == "FLOAT"):
170 return (("."), args, "")
172 configsequencearg = configSequenceArg()
174 class configSequence:
175 def __init__(self, parent):
178 self.seperator = self.parent.vals[0]
179 self.valueBounds = self.parent.vals[1]
180 self.censorChar = self.parent.vals[2]
182 def checkValues(self):
185 for i in self.parent.value:
186 maxPos += len(str(self.valueBounds[num][1]))
187 while (self.valueBounds[num][0] > self.parent.value[num]):
188 self.parent.value[num] += 1
190 while (self.valueBounds[num][1] < self.parent.value[num]):
191 self.parent.value[num] -= 1
193 # if (self.valueBounds[num][0] <= i <= self.valueBounds[num][1]):
196 #self.parent.value[num] = self.valueBounds[num][0]
199 if self.markedPos >= maxPos:
200 self.markedPos = maxPos - 1
201 if self.markedPos < 0:
210 def handleKey(self, key):
211 #this will no change anything on the value itself
212 #so we can handle it here in gui element
213 if key == config.key["prevElement"]:
215 if key == config.key["nextElement"]:
218 if key >= config.key["0"] and key <= config.key["9"]:
219 number = 9 - config.key["9"] + key
220 # length of numberblock
221 numberLen = len(str(self.valueBounds[0][1]))
222 # position in the block
223 posinblock = self.markedPos % numberLen
225 blocknumber = self.markedPos / numberLen
227 oldvalue = self.parent.value[blocknumber]
228 olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
229 newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
231 print "You actually pressed a number (" + str(number) + ") which will be added at block number " + str(blocknumber) + " on position " + str(posinblock)
232 print "Old value: " + str(oldvalue) + " olddec: " + str(olddec) + " newvalue: " + str(newvalue)
233 self.parent.value[blocknumber] = newvalue
241 #FIXME: dont call when press left/right
244 def __call__(self, selected): #needed by configlist
246 mPos = self.markedPos
247 print "Positon: " + str(mPos)
249 for i in self.parent.value:
250 if len(value): #fixme no heading separator possible
251 value += self.seperator
252 if mPos >= len(value) - 1:
255 #diff = self.valueBounds - len(str(i))
259 print (("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i)
260 if (self.censorChar == ""):
261 value += ("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i
263 value += (self.censorChar * len(str(self.valueBounds[num][1])))
265 # only mark cursor when we are selected
266 # (this code is heavily ink optimized!)
267 return ("mtext"[1-selected:], value, [mPos])
270 # used as first parameter
271 # is the text of a fixed size or is the user able to extend the length of the text
275 def __init__(self, parent):
278 self.mode = self.parent.vals[0]
279 self.textInput = NumericalTextInput(self.nextEntry)
281 def checkValues(self):
282 if (self.markedPos < 0):
284 if (self.markedPos >= len(self.parent.value)):
285 self.markedPos = len(self.parent.value) - 1
295 self.handleKey(config.key["nextElement"])
297 def handleKey(self, key):
298 #this will no change anything on the value itself
299 #so we can handle it here in gui element
300 if key == config.key["prevElement"]:
301 self.textInput.nextKey()
304 if key == config.key["nextElement"]:
305 self.textInput.nextKey()
307 if (self.mode == self.extendableSize):
308 if (self.markedPos >= len(self.parent.value)):
309 self.parent.value = self.parent.value.ljust(len(self.parent.value) + 1)
312 if key >= config.key["0"] and key <= config.key["9"]:
313 number = 9 - config.key["9"] + key
315 self.parent.value = self.parent.value[0:self.markedPos] + str(self.textInput.getKey(number)) + self.parent.value[self.markedPos + 1:]
321 def __call__(self, selected): #needed by configlist
322 return ("mtext"[1-selected:], str(self.parent.value), [self.markedPos])
325 def __init__(self, obj):
333 self.key = { "choseElement": 0,
348 configfile = configFile()
351 def __init__(self, parent):
360 def checkValues(self):
361 if self.parent.value < 0:
362 self.parent.value = 0
364 if self.parent.value > 10:
365 self.parent.value = 10
367 def handleKey(self, key):
368 if key == config.key["prevElement"]:
369 self.parent.value = self.parent.value - 1
370 if key == config.key["nextElement"]:
371 self.parent.value = self.parent.value + 1
376 def __call__(self, selected): #needed by configlist
378 return ("slider", self.parent.value * 10)
380 class ConfigSubsection:
386 def getIndexbyEntry(self, data):
388 tcnt = -1; #for defaultval
390 if int(x[1]) == int(data):
392 if int(x[1]) == int(self.defaultValue):
397 return 0 #prevent bigger then array
399 def datafromFile(self, control, data):
400 if control == ConfigSlider:
402 elif control == configSelection:
404 elif control == configDateTime:
406 elif control == configText:
408 elif control == configSequence:
410 part = data.split(self.vals[0])
414 elif control == configSatlist:
415 return self.getIndexbyEntry(data)
419 def datatoFile(self, control, data):
420 if control == ConfigSlider:
422 elif control == configSelection:
424 elif control == configDateTime:
426 elif control == configText:
427 return str(data.strip())
429 elif control == configSequence:
430 value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
431 # just in case you don't understand the above, here an equivalent:
435 # value += self.vals[0]
438 elif control == configSatlist:
439 return str(self.vals[self.value][1]);
445 value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
450 #print "value not found - using default"
452 if self.controlType == configSatlist:
453 self.value = self.getIndexbyEntry(self.defaultValue)
455 self.value = self.defaultValue
457 self.save() #add missing value to dict
461 #is this right? activate settings after load/cancel and use default
464 def __init__(self, configPath, control, defaultValue, vals):
465 self.configPath = configPath
466 self.defaultValue = defaultValue
467 self.controlType = control
469 self.notifierList = [ ]
473 def getConfigPath(self):
474 return self.configPath
476 def addNotifier(self, notifier):
477 self.notifierList.append(notifier);
480 for notifier in self.notifierList:
485 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))
487 class configElement_nonSave(configElement):
488 def __init__(self, configPath, control, defaultValue, vals):
489 configElement.__init__(self, configPath, control, defaultValue, vals)
494 def getConfigListEntry(description, element):
496 item = b.controlType(b)
497 return ((description, item))