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),(1,255),(1,255),(1,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("FLOAT", [(min,max),(min1,max1)]) => x.y with min <= x <= max and min1 <= y <= max1
127 if (type == "FLOAT"):
130 configsequencearg = configSequenceArg()
132 class configSequence:
133 def __init__(self, parent):
136 self.seperator = self.parent.vals[0]
137 self.valueBounds = self.parent.vals[1]
139 def checkValues(self):
140 maxPos = len(self.parent.value) * len(self.valueBounds) + len(self.parent.value)
142 if self.markedPos >= maxPos:
143 self.markedPos = maxPos - 1
144 if self.markedPos < 0:
153 def handleKey(self, key):
154 #this will no change anything on the value itself
155 #so we can handle it here in gui element
156 if key == config.key["prevElement"]:
158 if key == config.key["nextElement"]:
161 if key >= config.key["0"] and key <= config.key["9"]:
162 number = 9 - config.key["9"] + key
163 # length of numberblock
164 numberLen = len(str(self.valueBounds[0][1]))
165 # position in the block
166 posinblock = self.markedPos % numberLen
168 blocknumber = self.markedPos / numberLen
170 oldvalue = self.parent.value[blocknumber]
171 olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
172 newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
174 print "You actually pressed a number (" + str(number) + ") which will be added at block number " + str(blocknumber) + " on position " + str(posinblock)
175 print "Old value: " + str(oldvalue) + " olddec: " + str(olddec) + " newvalue: " + str(newvalue)
176 self.parent.value[blocknumber] = newvalue
184 #FIXME: dont call when press left/right
187 def __call__(self, selected): #needed by configlist
189 mPos = self.markedPos
190 print "Positon: " + str(mPos)
191 for i in self.parent.value:
192 if len(value): #fixme no heading separator possible
193 value += self.seperator
194 if mPos >= len(value) - 1:
197 #diff = self.valueBounds - len(str(i))
201 print (("%0" + str(len(str(self.valueBounds[0][1]))) + "d") % i)
202 value += ("%0" + str(len(str(self.valueBounds[0][1]))) + "d") % i
204 # only mark cursor when we are selected
205 # (this code is heavily ink optimized!)
206 return ("mtext"[1-selected:], value, [mPos])
209 def __init__(self, obj):
217 self.key = { "choseElement": 0,
232 configfile = configFile()
235 def __init__(self, parent):
244 def checkValues(self):
245 if self.parent.value < 0:
246 self.parent.value = 0
248 if self.parent.value > 10:
249 self.parent.value = 10
251 def handleKey(self, key):
252 if key == config.key["prevElement"]:
253 self.parent.value = self.parent.value - 1
254 if key == config.key["nextElement"]:
255 self.parent.value = self.parent.value + 1
260 def __call__(self, selected): #needed by configlist
262 return ("slider", self.parent.value * 10)
264 class ConfigSubsection:
270 def getIndexbyEntry(self, data):
272 tcnt = -1; #for defaultval
274 if int(x[1]) == int(data):
276 if int(x[1]) == int(self.defaultValue):
281 return 0 #prevent bigger then array
283 def datafromFile(self, control, data):
284 if control == ConfigSlider:
286 elif control == configSelection:
288 elif control == configSequence:
290 part = data.split(self.vals[0])
294 elif control == configSatlist:
295 return self.getIndexbyEntry(data)
299 def datatoFile(self, control, data):
300 if control == ConfigSlider:
302 elif control == configSelection:
304 elif control == configSequence:
305 value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
306 # just in case you don't understand the above, here an equivalent:
310 # value += self.vals[0]
313 elif control == configSatlist:
314 return str(self.vals[self.value][1]);
320 value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
325 print "value not found - using default"
327 if self.controlType == configSatlist:
328 self.value = self.getIndexbyEntry(self.defaultValue)
330 self.value = self.defaultValue
332 self.save() #add missing value to dict
336 #is this right? activate settings after load/cancel and use default
339 def __init__(self, configPath, control, defaultValue, vals):
340 self.configPath = configPath
341 self.defaultValue = defaultValue
342 self.controlType = control
344 self.notifierList = [ ]
347 def addNotifier(self, notifier):
348 self.notifierList.append(notifier);
351 for notifier in self.notifierList:
356 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))
358 class configElement_nonSave(configElement):
359 def __init__(self, configPath, control, defaultValue, vals):
360 configElement.__init__(self, configPath, control, defaultValue, vals)
365 def getConfigListEntry(description, element):
367 item = b.controlType(b)
368 return ((description, item))