4 self.configElements = { }
6 self.file = open("config")
8 print "cannot open config file"
12 line = self.file.readline()
16 if line.startswith("#"): #skip comments
22 def addElement(self, line):
25 self.configElements[line[:x]] = line[x + 1:]
27 def getKey(self, key):
28 return self.configElements[key]
30 def setKey(self, key, value):
32 self.configElements[key] = value
35 if self.changed == 0: #no changes, so no write to disk needed
38 fileHandle = open("config", "w")
40 keys = self.configElements.keys()
43 wstr = x + "=" + self.configElements[x]
45 if wstr[len(wstr) - 1] != '\n':
48 fileHandle.write(wstr)
52 class configSelection:
53 def __init__(self, parent):
56 def checkValues(self):
57 if self.parent.value < 0:
60 if(self.parent.value >= (len(self.parent.vals) - 1)):
61 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 return ("text", self.parent.vals[self.parent.value])
84 def __init__(self, parent):
87 def checkValues(self):
88 if self.parent.value < 0:
91 if(self.parent.value >= (len(self.parent.vals) - 1)):
92 self.parent.value = len(self.parent.vals) - 1
100 def handleKey(self, key):
101 if key == config.key["prevElement"]:
102 self.parent.value = self.parent.value - 1
103 if key == config.key["nextElement"]:
104 self.parent.value = self.parent.value + 1
110 def __call__(self, selected): #needed by configlist
113 return ("text", str(self.parent.vals[self.parent.value][0]))
115 class configSequenceArg:
116 def get(self, type, args = ()):
117 # configsequencearg.get ("IP")
119 return (("."), [(1,255),(0,255),(0,255),(0,255)], "")
120 # configsequencearg.get ("MAC")
122 return ((":"), [(1,255),(1,255),(1,255),(1,255),(1,255),(1,255)], "")
123 # configsequencearg.get("INTEGER", (min, max)) => x with min <= x <= max
124 if (type == "INTEGER"):
125 return ((":"), [args], "")
126 # configsequencearg.get("PINCODE", (number, "*")) => pin with number = length of pincode and "*" as numbers shown as stars
127 # configsequencearg.get("PINCODE", (number, "")) => pin with number = length of pincode and numbers shown
128 if (type == "PINCODE"):
129 return ((":"), [(0, (10**args[0])-1)], args[1])
130 # configsequencearg.get("FLOAT", [(min,max),(min1,max1)]) => x.y with min <= x <= max and min1 <= y <= max1
131 if (type == "FLOAT"):
132 return (("."), args, "")
134 configsequencearg = configSequenceArg()
136 class configSequence:
137 def __init__(self, parent):
140 self.seperator = self.parent.vals[0]
141 self.valueBounds = self.parent.vals[1]
142 self.censorChar = self.parent.vals[2]
144 def checkValues(self):
147 for i in self.parent.value:
148 maxPos += len(str(self.valueBounds[num][1]))
149 while (self.valueBounds[num][0] > self.parent.value[num]):
150 self.parent.value[num] += 1
152 while (self.valueBounds[num][1] < self.parent.value[num]):
153 self.parent.value[num] -= 1
155 # if (self.valueBounds[num][0] <= i <= self.valueBounds[num][1]):
158 #self.parent.value[num] = self.valueBounds[num][0]
161 if self.markedPos >= maxPos:
162 self.markedPos = maxPos - 1
163 if self.markedPos < 0:
172 def handleKey(self, key):
173 #this will no change anything on the value itself
174 #so we can handle it here in gui element
175 if key == config.key["prevElement"]:
177 if key == config.key["nextElement"]:
180 if key >= config.key["0"] and key <= config.key["9"]:
181 number = 9 - config.key["9"] + key
182 # length of numberblock
183 numberLen = len(str(self.valueBounds[0][1]))
184 # position in the block
185 posinblock = self.markedPos % numberLen
187 blocknumber = self.markedPos / numberLen
189 oldvalue = self.parent.value[blocknumber]
190 olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
191 newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
193 print "You actually pressed a number (" + str(number) + ") which will be added at block number " + str(blocknumber) + " on position " + str(posinblock)
194 print "Old value: " + str(oldvalue) + " olddec: " + str(olddec) + " newvalue: " + str(newvalue)
195 self.parent.value[blocknumber] = newvalue
203 #FIXME: dont call when press left/right
206 def __call__(self, selected): #needed by configlist
208 mPos = self.markedPos
209 print "Positon: " + str(mPos)
211 for i in self.parent.value:
212 if len(value): #fixme no heading separator possible
213 value += self.seperator
214 if mPos >= len(value) - 1:
217 #diff = self.valueBounds - len(str(i))
221 print (("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i)
222 if (self.censorChar == ""):
223 value += ("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i
225 value += (self.censorChar * len(str(self.valueBounds[num][1])))
227 # only mark cursor when we are selected
228 # (this code is heavily ink optimized!)
229 return ("mtext"[1-selected:], value, [mPos])
232 def __init__(self, obj):
240 self.key = { "choseElement": 0,
255 configfile = configFile()
258 def __init__(self, parent):
267 def checkValues(self):
268 if self.parent.value < 0:
269 self.parent.value = 0
271 if self.parent.value > 10:
272 self.parent.value = 10
274 def handleKey(self, key):
275 if key == config.key["prevElement"]:
276 self.parent.value = self.parent.value - 1
277 if key == config.key["nextElement"]:
278 self.parent.value = self.parent.value + 1
283 def __call__(self, selected): #needed by configlist
285 return ("slider", self.parent.value * 10)
287 class ConfigSubsection:
293 def getIndexbyEntry(self, data):
295 tcnt = -1; #for defaultval
297 if int(x[1]) == int(data):
299 if int(x[1]) == int(self.defaultValue):
304 return 0 #prevent bigger then array
306 def datafromFile(self, control, data):
307 if control == ConfigSlider:
309 elif control == configSelection:
311 elif control == configSequence:
313 part = data.split(self.vals[0])
317 elif control == configSatlist:
318 return self.getIndexbyEntry(data)
322 def datatoFile(self, control, data):
323 if control == ConfigSlider:
325 elif control == configSelection:
327 elif control == configSequence:
328 value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
329 # just in case you don't understand the above, here an equivalent:
333 # value += self.vals[0]
336 elif control == configSatlist:
337 return str(self.vals[self.value][1]);
343 value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
348 #print "value not found - using default"
350 if self.controlType == configSatlist:
351 self.value = self.getIndexbyEntry(self.defaultValue)
353 self.value = self.defaultValue
355 self.save() #add missing value to dict
359 #is this right? activate settings after load/cancel and use default
362 def __init__(self, configPath, control, defaultValue, vals):
363 self.configPath = configPath
364 self.defaultValue = defaultValue
365 self.controlType = control
367 self.notifierList = [ ]
370 def addNotifier(self, notifier):
371 self.notifierList.append(notifier);
374 for notifier in self.notifierList:
379 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))
381 class configElement_nonSave(configElement):
382 def __init__(self, configPath, control, defaultValue, vals):
383 configElement.__init__(self, configPath, control, defaultValue, vals)
388 def getConfigListEntry(description, element):
390 item = b.controlType(b)
391 return ((description, item))