- adding netmask to network setup
[enigma2.git] / lib / python / Components / config.py
index e0c878f6a0edb4ee65a9c56fed268c2cf3a4b5e7..2fd0ff15a3653e7e8f19dbf73ed68b89b12b69d1 100644 (file)
@@ -1,5 +1,6 @@
 class configFile:
        def __init__(self):
+               self.changed = 0
                self.configElements = { }
                try:
                        self.file = open("config")
@@ -11,6 +12,10 @@ class configFile:
                        line = self.file.readline()
                        if line == "":
                                break
+                       
+                       if line.startswith("#"):                #skip comments
+                               continue        
+                               
                        self.addElement(line)
                self.file.close()
 
@@ -23,17 +28,26 @@ class configFile:
                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:
-                       fileHandle.write(x + "=" + self.configElements[x] + "\n")
+                       wstr = x + "=" + self.configElements[x]
+                       
+                       if wstr[len(wstr) - 1] != '\n':
+                               wstr = wstr + "\n"
+
+                       fileHandle.write(wstr)
 
                fileHandle.close()              
 
-class configBoolean:
+class configSelection:
        def __init__(self, parent):
                self.parent = parent
                
@@ -51,9 +65,9 @@ class configBoolean:
                self.parent.save()
 
        def handleKey(self, key):
-               if key == 1:
+               if key == config.prevElement:
                        self.parent.value = self.parent.value - 1
-               if key == 2:
+               if key == config.nextElement:
                        self.parent.value = self.parent.value + 1
                
                self.checkValues()                      
@@ -63,6 +77,61 @@ class configBoolean:
        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
+               self.markedPos = 0
+               
+       def checkValues(self):
+               maxPos = len(self.parent.value) * self.parent.vals[1] 
+               print maxPos
+                       
+               if self.markedPos >= maxPos:
+                       self.markedPos = maxPos - 1
+               if self.markedPos < 0:
+                       self.markedPos = 0
+                       
+       def cancel(self):
+               self.parent.reload()
+
+       def save(self):
+               self.parent.save()
+
+       def handleKey(self, key):
+               #this will no change anything on the value itself
+               #so we can handle it here in gui element
+               if key == config.prevElement:
+                       self.markedPos -= 1
+               if key == config.nextElement:
+                       self.markedPos += 1
+               
+               self.checkValues()                      
+               
+               print "markPos:",
+               print self.markedPos
+
+               #FIXME: dont call when press left/right
+               self.parent.change()    
+
+       def __call__(self):                     #needed by configlist
+               value = ""
+               mPos = self.markedPos
+               print mPos
+               for i in self.parent.value:
+                       if value != "": #fixme no heading separator possible
+                               value += self.parent.vals[0]
+                               if mPos >= len(value) - 1:
+                                       mPos += 1
+                               
+                       diff =  self.parent.vals[1] - len(str(i))
+                       if diff > 0:
+                               value += " " * diff
+                       value +=        str(i)
+# or the above code if you have to spare ink
+#              value = ((len(self.parent.value) * ("%0" + str(self.parent.vals[1]) + "d" + self.parent.vals[0]))[0:-1]) % tuple(self.parent.value)
+               value = value[0:mPos] + "_" + value[mPos + 1:]
+               return ("text", value)
 
 class configValue:
        def __init__(self, obj):
@@ -73,8 +142,10 @@ class configValue:
 
 class Config:
        def __init__(self):
-               pass
-               
+               self.choseElement = 0
+               self.prevElement = 1
+               self.nextElement = 2
+                                               
 config = Config();
 configfile = configFile()
 
@@ -96,9 +167,9 @@ class ConfigSlider:
                        self.parent.value = 10  
 
        def handleKey(self, key):
-               if key == 1:
+               if key == config.prevElement:
                        self.parent.value = self.parent.value - 1
-               if key == 2:
+               if key == config.nextElement:
                        self.parent.value = self.parent.value + 1
                                        
                self.checkValues()      
@@ -116,16 +187,31 @@ class configElement:
        def datafromFile(self, control, data):
                if control == ConfigSlider:
                        return int(data);
-               elif control == configBoolean:
+               elif control == configSelection:
                        return int(data);
+               elif control == configSequence:
+                       list = [ ]
+                       part = data.split(self.vals[0])
+                       for x in part:
+                               list.append(int(x))
+                       return list
                else: 
                        return ""       
 
        def datatoFile(self, control, data):
                if control == ConfigSlider:
                        return str(data);
-               elif control == configBoolean:
+               elif control == configSelection:
                        return str(data);
+               elif control == configSequence:
+                       value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
+#                      just in case you don't understand the above, here an equivalent:
+#                      value = ""
+#                      for i in data:
+#                              if value !="":
+#                                      value += self.vals[0]
+#                              value += str(i)
+                       return value
                else: 
                        return ""       
 
@@ -141,7 +227,6 @@ class configElement:
                        self.save()             #add missing value to dict
                else:
                        self.value = value
-                       print "value ok"
 
        def __init__(self, configPath, control, defaultValue, vals):
                self.configPath = configPath
@@ -149,6 +234,7 @@ class configElement:
                self.controlType = control
                self.vals = vals
                self.notifierList = [ ]
+               self.enabled = True
                self.loadData()         
        def addNotifier(self, notifier):
                self.notifierList.append(notifier);