add fileIO
authorRonny Strutz <ronny.strutz@multimedia-labs.de>
Thu, 1 Sep 2005 21:13:23 +0000 (21:13 +0000)
committerRonny Strutz <ronny.strutz@multimedia-labs.de>
Thu, 1 Sep 2005 21:13:23 +0000 (21:13 +0000)
lib/python/Components/config.py

index 457736f..015ac06 100644 (file)
@@ -1,4 +1,28 @@
-#  temp stuff :)
+class configFile:
+       def __init__(self):
+               pass
+
+       def openFile(self):
+               try:
+                       self.file = open("config")
+               except IOError:
+                       self.file = ""
+
+       def getKey(self, key, dataType):
+               self.openFile()                 #good idea? (open every time we need it?) else we have to seek
+               while 1:
+                       line = self.file.readline()
+                       if line == "":
+                               break
+                       if line.startswith(key):
+                               x = line.find("=")
+                               if x > -1:
+                                       self.file.close()
+                                       return dataType(line[x + 1:])
+
+               self.file.close()
+               return ""
+
 class configBoolean:
        def __init__(self, parent):
                self.parent = parent
@@ -14,7 +38,7 @@ class configBoolean:
                self.parent.reload()
 
        def save(self):
-               print "save"
+               print "save bool"
 
        def handleKey(self, key):
                if key == 1:
@@ -28,7 +52,6 @@ class configBoolean:
 
        def __call__(self):                     #needed by configlist
                self.checkValues()                      
-       
                return ("text", self.parent.vals[self.parent.value])
 
 class configValue:
@@ -38,20 +61,29 @@ 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
+
+       def saveLine(self, file, element):
+               #FIXME can handle INTs only
+               line = element.configPath + "=" + str(element.value) + "\n"
+               file.write(line)
+
+       def save(self):
+               fileHandle = open("config", "w")
+               
+               for groupElement in self.__dict__.items():
+                       for element in groupElement[1].__dict__.items():
+                               self.saveLine(fileHandle, element[1])
+               
+               fileHandle.close()              
+               
+               while 1:
+                       pass    
                
 config = Config();
+configfile = configFile()
 
 class ConfigSlider:
        def __init__(self, parent):
@@ -88,14 +120,34 @@ class ConfigSubsection:
                pass
 
 class configElement:
+       def dataType(self, control):
+               if control == ConfigSlider:
+                       return int;
+               elif control == configBoolean:
+                       return int;
+               else: 
+                       return ""       
+
+       def loadData(self):
+               try:
+                       value = configfile.getKey(self.configPath, self.dataType(self.controlType))
+               except:         
+                       value = ""
+
+               if value == "":
+                       print "value not found - using default"
+                       self.value = self.defaultValue
+               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 +155,4 @@ class configElement:
                for notifier in self.notifierList:
                        notifier(self)
        def reload(self):
-               self.value = self.defaultValue  #HACK :-)
+               self.loadData()