X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/aa3e781f31a04223416f0a34b25ab95fc0bef429..c46216a8e07d97647287aeba0281f226b1ba05e9:/lib/python/Components/config.py diff --git a/lib/python/Components/config.py b/lib/python/Components/config.py index 5d582abb..14c03655 100644 --- a/lib/python/Components/config.py +++ b/lib/python/Components/config.py @@ -1,15 +1,118 @@ -# temp stuff :) -class configBoolean: - def __init__(self, reg): - self.reg = reg - self.val = 0 - - def toggle(self): - self.val += 1 - self.val %= 3 +class configFile: + def __init__(self): + self.changed = 0 + self.configElements = { } + try: + self.file = open("config") + except IOError: + print "cannot open config file" + return + + while 1: + line = self.file.readline() + if line == "": + break + + if line.startswith("#"): #skip comments + continue + + self.addElement(line) + self.file.close() + + def addElement(self, line): + x = line.find("=") + if x > -1: + self.configElements[line[:x]] = line[x + 1:] - def __str__(self): - return ("NO", "YES", "MAYBE")[self.val] + def getKey(self, key): + return self.configElements[key] + + def setKey(self, key, value): + self.changed = 1 + self.configElements[key] = value + + def save(self): + if self.changed == 0: #no changes, so no write to disk needed + return + + fileHandle = open("config", "w") + + for x in self.configElements: + wstr = x + "=" + self.configElements[x] + + if wstr[len(wstr) - 1] != '\n': + wstr = wstr + "\n" + + fileHandle.write(wstr) + + fileHandle.close() + +class configBoolean: + def __init__(self, parent): + self.parent = parent + + def checkValues(self): + if self.parent.value < 0: + self.parent.value = 0 + + if(self.parent.value >= (len(self.parent.vals) - 1)): + self.parent.value = len(self.parent.vals) - 1 + + def cancel(self): + self.parent.reload() + + def save(self): + self.parent.save() + + def handleKey(self, key): + if key == config.prevElement: + self.parent.value = self.parent.value - 1 + if key == config.nextElement: + self.parent.value = self.parent.value + 1 + + self.checkValues() + + self.parent.change() + + def __call__(self): #needed by configlist + self.checkValues() + return ("text", self.parent.vals[self.parent.value]) + +class configSequence: + def __init__(self, parent): + self.parent = parent + + def checkValues(self): + pass +# if self.parent.value < 0: +# self.parent.value = 0 +# +# if(self.parent.value >= (len(self.parent.vals) - 1)): +# self.parent.value = len(self.parent.vals) - 1 +# + def cancel(self): + self.parent.reload() + + def save(self): + self.parent.save() + + def handleKey(self, key): + if key == config.prevElement: + self.parent.value = self.parent.value - 1 + if key == config.nextElement: + self.parent.value = self.parent.value + 1 + + self.checkValues() + + self.parent.change() + + def __call__(self): #needed by configlist + value = "" + for i in self.parent.value: + if (value != ""): + value += self.parent.vals[0] + value += str(i) + return ("text", value) class configValue: def __init__(self, obj): @@ -18,12 +121,93 @@ class configValue: def __str__(self): return self.obj -def configEntry(obj): - # das hier ist ein zugriff auf die registry... - if obj == "HKEY_LOCAL_ENIGMA/IMPORTANT/USER_ANNOYING_STUFF/SDTV/FLASHES/GREEN": - return ("SDTV green flashes", configBoolean(obj)) - elif obj == "HKEY_LOCAL_ENIGMA/IMPORTANT/USER_ANNOYING_STUFF/HDTV/FLASHES/GREEN": - return ("HDTV reen flashes", configBoolean(obj)) - else: - return ("invalid", "") +class Config: + def __init__(self): + self.choseElement = 0 + self.prevElement = 1 + self.nextElement = 2 + +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.prevElement: + self.parent.value = self.parent.value - 1 + if key == config.nextElement: + self.parent.value = self.parent.value + 1 + + self.checkValues() + self.parent.change() + + def __call__(self): #needed by configlist + self.checkValues() + return ("slider", self.parent.value * 10) + +class ConfigSubsection: + def __init__(self): + pass + +class configElement: + def datafromFile(self, control, data): + if control == ConfigSlider: + return int(data); + elif control == configBoolean: + return int(data); + else: + return "" + + def datatoFile(self, control, data): + if control == ConfigSlider: + return str(data); + elif control == configBoolean: + return str(data); + else: + return "" + + def loadData(self): + try: + value = self.datafromFile(self.controlType, configfile.getKey(self.configPath)) + except: + value = "" + + if value == "": + print "value not found - using default" + self.value = self.defaultValue + self.save() #add missing value to dict + else: + self.value = value + def __init__(self, configPath, control, defaultValue, vals): + self.configPath = configPath + self.defaultValue = defaultValue + self.controlType = control + self.vals = vals + self.notifierList = [ ] + self.loadData() + 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): + configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))