fix line break
[enigma2.git] / lib / python / Components / config.py
index 457736f54033b510ff2a2e1a067f24fa0240e069..7419eff54f2177319a37e70f88b8d73ed3ab8e0c 100644 (file)
@@ -1,4 +1,52 @@
-#  temp stuff :)
+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
+                       self.addElement(line)
+               self.file.close()
+
+       def addElement(self, line):
+               x = line.find("=")
+               if x > -1:
+                       self.configElements[line[:x]] = line[x + 1:]
+       
+       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)
+                       #else:
+                       #       fileHandle.write(wstr + "\n")
+
+                       fileHandle.write(wstr)
+
+               fileHandle.close()              
+
 class configBoolean:
        def __init__(self, parent):
                self.parent = parent
@@ -14,7 +62,7 @@ class configBoolean:
                self.parent.reload()
 
        def save(self):
-               print "save"
+               self.parent.save()
 
        def handleKey(self, key):
                if key == 1:
@@ -28,7 +76,6 @@ class configBoolean:
 
        def __call__(self):                     #needed by configlist
                self.checkValues()                      
-       
                return ("text", self.parent.vals[self.parent.value])
 
 class configValue:
@@ -38,20 +85,12 @@ 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):
                pass
                
 config = Config();
+configfile = configFile()
 
 class ConfigSlider:
        def __init__(self, parent):
@@ -61,7 +100,7 @@ class ConfigSlider:
                self.parent.reload()
 
        def save(self):
-               print "slider - save"
+               self.parent.save()
 
        def checkValues(self):
                if self.parent.value < 0:
@@ -88,14 +127,43 @@ class ConfigSubsection:
                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
+                       print "value ok"
+
        def __init__(self, configPath, control, defaultValue, vals):
                self.configPath = configPath
-#              self.value = 0  #read from registry else use default
-               self.value = defaultValue       #read from registry else use default
                self.defaultValue = defaultValue
                self.controlType = control
                self.vals = vals
                self.notifierList = [ ]
+               self.loadData()         
        def addNotifier(self, notifier):
                self.notifierList.append(notifier);
                notifier(self);
@@ -103,4 +171,6 @@ class configElement:
                for notifier in self.notifierList:
                        notifier(self)
        def reload(self):
-               self.value = self.defaultValue  #HACK :-)
+               self.loadData()
+       def save(self):
+               configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))