removed debug
[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 == config.prevElement:
69                         self.parent.value = self.parent.value - 1
70                 if key == config.nextElement:
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 configSequence:
82         def __init__(self, parent):
83                 self.parent = parent
84                 self.markedPos = 0
85                 
86         def checkValues(self):
87                 maxPos = len(self.parent.value) * self.parent.vals[1] 
88                 print maxPos
89                         
90                 if self.markedPos >= maxPos:
91                         self.markedPos = maxPos - 1
92                 if self.markedPos < 0:
93                         self.markedPos = 0
94                         
95         def cancel(self):
96                 self.parent.reload()
97
98         def save(self):
99                 self.parent.save()
100
101         def handleKey(self, key):
102                 #this will no change anything on the value itself
103                 #so we can handle it here in gui element
104                 if key == config.prevElement:
105                         self.markedPos -= 1
106                 if key == config.nextElement:
107                         self.markedPos += 1
108                 
109                 self.checkValues()                      
110                 
111                 print "markPos:",
112                 print self.markedPos
113
114                 #FIXME: dont call when press left/right
115                 self.parent.change()    
116
117         def __call__(self):                     #needed by configlist
118                 value = ""
119                 mPos = self.markedPos
120                 print mPos
121                 for i in self.parent.value:
122                         if value != "": #fixme no heading separator possible
123                                 value += self.parent.vals[0]
124                                 if mPos >= len(value) - 1:
125                                         mPos += 1
126                                 
127                         diff =  self.parent.vals[1] - len(str(i))
128                         if diff > 0:
129                                 value += " " * diff
130                         value +=        str(i)
131                 
132                 value = value[0:mPos] + "_" + value[mPos + 1:]
133                 return ("text", value)
134
135 class configValue:
136         def __init__(self, obj):
137                 self.obj = obj
138                 
139         def __str__(self):
140                 return self.obj
141
142 class Config:
143         def __init__(self):
144                 self.choseElement = 0
145                 self.prevElement = 1
146                 self.nextElement = 2
147                                                 
148 config = Config();
149 configfile = configFile()
150
151 class ConfigSlider:
152         def __init__(self, parent):
153                 self.parent = parent
154
155         def cancel(self):
156                 self.parent.reload()
157
158         def save(self):
159                 self.parent.save()
160
161         def checkValues(self):
162                 if self.parent.value < 0:
163                         self.parent.value = 0   
164
165                 if self.parent.value > 10:
166                         self.parent.value = 10  
167
168         def handleKey(self, key):
169                 if key == config.prevElement:
170                         self.parent.value = self.parent.value - 1
171                 if key == config.nextElement:
172                         self.parent.value = self.parent.value + 1
173                                         
174                 self.checkValues()      
175                 self.parent.change()    
176
177         def __call__(self):                     #needed by configlist
178                 self.checkValues()      
179                 return ("slider", self.parent.value * 10)
180
181 class ConfigSubsection:
182         def __init__(self):
183                 pass
184
185 class configElement:
186         def datafromFile(self, control, data):
187                 if control == ConfigSlider:
188                         return int(data);
189                 elif control == configBoolean:
190                         return int(data);
191                 elif control == configSequence:
192                         list = [ ]
193                         part = data.split(self.vals[0])
194                         for x in part:
195                                 list.append(int(x))
196                         return list
197                 else: 
198                         return ""       
199
200         def datatoFile(self, control, data):
201                 if control == ConfigSlider:
202                         return str(data);
203                 elif control == configBoolean:
204                         return str(data);
205                 elif control == configSequence:
206                         value = ""
207                         for i in data:
208                                 if value !="":
209                                         value += self.vals[0]
210                                 value += str(i)
211                         return value
212                 else: 
213                         return ""       
214
215         def loadData(self):
216                 try:
217                         value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
218                 except:         
219                         value = ""
220
221                 if value == "":
222                         print "value not found - using default"
223                         self.value = self.defaultValue
224                         self.save()             #add missing value to dict
225                 else:
226                         self.value = value
227
228         def __init__(self, configPath, control, defaultValue, vals):
229                 self.configPath = configPath
230                 self.defaultValue = defaultValue
231                 self.controlType = control
232                 self.vals = vals
233                 self.notifierList = [ ]
234                 self.enabled = True
235                 self.loadData()         
236         def addNotifier(self, notifier):
237                 self.notifierList.append(notifier);
238                 notifier(self);
239         def change(self):
240                 for notifier in self.notifierList:
241                         notifier(self)
242         def reload(self):
243                 self.loadData()
244         def save(self):
245                 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))