+class Config:
+ def __init__(self):
+ self.key = { "choseElement": 0,
+ "prevElement": 1,
+ "nextElement": 2,
+ "delete": 3,
+ "0": 10,
+ "1": 11,
+ "2": 12,
+ "3": 13,
+ "4": 14,
+ "5": 15,
+ "6": 16,
+ "7": 17,
+ "8": 18,
+ "9": 19 }
+
+config = Config();
+
+configfile = configFile()
+
+class ConfigSlider:
+ def __init__(self, parent):
+ self.parent = parent
+
+ def cancel(self):
+ self.parent.reload()
+
+ def save(self):
+ self.parent.save()
+
+ def checkValues(self):
+ if self.parent.value < 0:
+ self.parent.value = 0
+
+ if self.parent.value > 10:
+ self.parent.value = 10
+
+ def handleKey(self, key):
+ if key == config.key["prevElement"]:
+ self.parent.value = self.parent.value - 1
+ if key == config.key["nextElement"]:
+ self.parent.value = self.parent.value + 1
+
+ self.checkValues()
+ self.parent.change()
+
+ def __call__(self, selected): #needed by configlist
+ self.checkValues()
+ return ("slider", self.parent.value * 10)
+
+class ConfigSubsection:
+ def __init__(self):
+ pass
+
+class configElement:
+
+ def getIndexbyEntry(self, data):
+ cnt = 0;
+ tcnt = -1; #for defaultval
+ for x in self.vals:
+ if int(x[1]) == int(data):
+ return cnt
+ if int(x[1]) == int(self.defaultValue):
+ tcnt = cnt
+ cnt += 1
+ if tcnt != -1:
+ return tcnt
+ return 0 #prevent bigger then array
+
+ def datafromFile(self, control, data):
+ if control == ConfigSlider:
+ return int(data)
+ elif control == configSelection:
+ try:
+ return int(data)
+ except:
+ for x in data.split(":"):
+ if x[0] == "*":
+ count = 0
+ for y in self.vals:
+ if y[0] == x[1:-1]:
+ return count
+ count += 1
+ return self.defaultValue
+ elif control == configDateTime:
+ return int(data)
+ elif control == configText:
+ return str(data)
+ elif control == configSequence:
+ list = [ ]
+ part = data.split(self.vals[0])
+ for x in part:
+ list.append(int(x))
+ return list
+ elif control == configSatlist:
+ return self.getIndexbyEntry(data)
+ else:
+ return ""
+
+ def datatoFile(self, control, data):
+ if control == ConfigSlider:
+ return str(data)
+ elif control == configSelection:
+ if len(self.vals) < data + 1:
+ return "0"
+ if isinstance(self.vals[data], str):
+ return str(data)
+ else:
+ confList = []
+ count = 0
+ for x in self.vals:
+ if count == data:
+ confList.append("*" + str(x[0] + "*"))
+ else:
+ confList.append(x[0])
+ count += 1
+ return ":".join(confList)
+ return str(data)
+ elif control == configDateTime:
+ return str(data)
+ elif control == configText:
+ return str(data.strip())
+ elif control == configSequence:
+# print self.vals
+# print self.value
+ try:
+ value = ""
+ count = 0
+ for i in data:
+ if value !="":
+ value += self.vals[0]
+ value += (("%0" + str(len(str(self.vals[1][count][1]))) + "d") % i)
+ count += 1
+ #value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
+ except:
+ value = str(data)
+ return value
+ elif control == configSatlist:
+ return str(self.vals[self.value][1]);
+ else:
+ return ""
+
+ def loadData(self):
+ #print "load:" + self.configPath
+ try:
+ value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
+ except:
+ value = ""
+
+ if value == "":
+ #print "value not found - using default"
+ if self.controlType == configSatlist:
+ self.value = self.getIndexbyEntry(self.defaultValue)
+ elif self.controlType == configSequence:
+ self.value = self.defaultValue[:]
+ else:
+ self.value = self.defaultValue
+
+ self.save() #add missing value to dict
+ else:
+ #print "set val:" + str(value)
+ self.value = value
+
+ #is this right? activate settings after load/cancel and use default
+ self.change()
+
+ def __init__(self, configPath, control, defaultValue, vals, saveDefaults = True):
+ self.configPath = configPath
+ self.defaultValue = defaultValue
+ self.controlType = control
+ self.vals = vals
+ self.notifierList = [ ]
+ self.enabled = True
+ self.saveDefaults = saveDefaults
+ self.loadData()
+
+ def getConfigPath(self):
+ return self.configPath
+
+ def addNotifier(self, notifier):
+ self.notifierList.append(notifier);
+ notifier(self);
+
+ def change(self):
+ for notifier in self.notifierList:
+ notifier(self)
+
+ def reload(self):
+ self.loadData()
+
+ def save(self):
+ if self.controlType == configSatlist:
+ defaultValue = self.getIndexbyEntry(self.defaultValue)
+ else:
+ defaultValue = self.defaultValue
+ if (defaultValue != self.value) or (self.saveDefaults == True):
+ configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))
+
+class configElement_nonSave(configElement):
+ def __init__(self, configPath, control, defaultValue, vals):
+ configElement.__init__(self, configPath, control, defaultValue, vals)
+
+ def save(self):
+ pass
+
+def getConfigListEntry(description, element):
+ b = element
+ item = b.controlType(b)
+ return ((description, item))
+
+def configElementBoolean(name, default, texts=(_("Enable"), _("Disable"))):
+ return configElement(name, configSelection, default, texts)