added skip comment
[enigma2.git] / lib / python / Components / config.py
1 class configFile:
2         def __init__(self):
3                 self.changed = 0
4                 self.configElements = { }
5                 try:
6                         self.file = open("config")
7                 except IOError:
8                         print "cannot open config file"
9                         return 
10                 
11                 while 1:
12                         line = self.file.readline()
13                         if line == "":
14                                 break
15                         
16                         if line.startswith("#"):                #skip comments
17                                 continue        
18                                 
19                         self.addElement(line)
20                 self.file.close()
21
22         def addElement(self, line):
23                 x = line.find("=")
24                 if x > -1:
25                         self.configElements[line[:x]] = line[x + 1:]
26         
27         def getKey(self, key):
28                 return self.configElements[key]
29
30         def setKey(self, key, value):
31                 self.changed = 1
32                 self.configElements[key] = value
33
34         def save(self):
35                 if self.changed == 0:           #no changes, so no write to disk needed
36                         return
37                         
38                 fileHandle = open("config", "w")
39                 
40                 for x in self.configElements:
41                         wstr = x + "=" + self.configElements[x]
42                         
43                         if wstr[len(wstr) - 1] != '\n':
44                                 wstr = wstr + "\n"
45
46                         fileHandle.write(wstr)
47
48                 fileHandle.close()              
49
50 class configBoolean:
51         def __init__(self, parent):
52                 self.parent = parent
53                 
54         def checkValues(self):
55                 if self.parent.value < 0:
56                         self.parent.value = 0   
57
58                 if(self.parent.value >= (len(self.parent.vals) - 1)):
59                         self.parent.value = len(self.parent.vals) - 1
60
61         def cancel(self):
62                 self.parent.reload()
63
64         def save(self):
65                 self.parent.save()
66
67         def handleKey(self, key):
68                 if key == 1:
69                         self.parent.value = self.parent.value - 1
70                 if key == 2:
71                         self.parent.value = self.parent.value + 1
72                 
73                 self.checkValues()                      
74
75                 self.parent.change()    
76
77         def __call__(self):                     #needed by configlist
78                 self.checkValues()                      
79                 return ("text", self.parent.vals[self.parent.value])
80
81 class configValue:
82         def __init__(self, obj):
83                 self.obj = obj
84                 
85         def __str__(self):
86                 return self.obj
87
88 class Config:
89         def __init__(self):
90                 pass
91                 
92 config = Config();
93 configfile = configFile()
94
95 class ConfigSlider:
96         def __init__(self, parent):
97                 self.parent = parent
98
99         def cancel(self):
100                 self.parent.reload()
101
102         def save(self):
103                 self.parent.save()
104
105         def checkValues(self):
106                 if self.parent.value < 0:
107                         self.parent.value = 0   
108
109                 if self.parent.value > 10:
110                         self.parent.value = 10  
111
112         def handleKey(self, key):
113                 if key == 1:
114                         self.parent.value = self.parent.value - 1
115                 if key == 2:
116                         self.parent.value = self.parent.value + 1
117                                         
118                 self.checkValues()      
119                 self.parent.change()    
120
121         def __call__(self):                     #needed by configlist
122                 self.checkValues()      
123                 return ("slider", self.parent.value * 10)
124
125 class ConfigSubsection:
126         def __init__(self):
127                 pass
128
129 class configElement:
130         def datafromFile(self, control, data):
131                 if control == ConfigSlider:
132                         return int(data);
133                 elif control == configBoolean:
134                         return int(data);
135                 else: 
136                         return ""       
137
138         def datatoFile(self, control, data):
139                 if control == ConfigSlider:
140                         return str(data);
141                 elif control == configBoolean:
142                         return str(data);
143                 else: 
144                         return ""       
145
146         def loadData(self):
147                 try:
148                         value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
149                 except:         
150                         value = ""
151
152                 if value == "":
153                         print "value not found - using default"
154                         self.value = self.defaultValue
155                         self.save()             #add missing value to dict
156                 else:
157                         self.value = value
158
159         def __init__(self, configPath, control, defaultValue, vals):
160                 self.configPath = configPath
161                 self.defaultValue = defaultValue
162                 self.controlType = control
163                 self.vals = vals
164                 self.notifierList = [ ]
165                 self.loadData()         
166         def addNotifier(self, notifier):
167                 self.notifierList.append(notifier);
168                 notifier(self);
169         def change(self):
170                 for notifier in self.notifierList:
171                         notifier(self)
172         def reload(self):
173                 self.loadData()
174         def save(self):
175                 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))