add digitCount to init
[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                 print "__CALL__"
119                 value = ""
120                 mPos = self.markedPos
121                 print mPos
122                 for i in self.parent.value:
123                         if value != "": #fixme no heading separator possible
124                                 value += self.parent.vals[0]
125                                 if mPos >= len(value) - 1:
126                                         mPos += 1
127                                 
128                         diff =  self.parent.vals[1] - len(str(i))
129                         if diff > 0:
130                                 #how about alignment?
131                                 value += "           "[0:diff]          #how is this done correct?
132                         value +=        str(i)
133                 
134                 value = value[0:mPos] + "_" + value[mPos + 1:]
135                 return ("text", value)
136
137 class configValue:
138         def __init__(self, obj):
139                 self.obj = obj
140                 
141         def __str__(self):
142                 return self.obj
143
144 class Config:
145         def __init__(self):
146                 self.choseElement = 0
147                 self.prevElement = 1
148                 self.nextElement = 2
149                                                 
150 config = Config();
151 configfile = configFile()
152
153 class ConfigSlider:
154         def __init__(self, parent):
155                 self.parent = parent
156
157         def cancel(self):
158                 self.parent.reload()
159
160         def save(self):
161                 self.parent.save()
162
163         def checkValues(self):
164                 if self.parent.value < 0:
165                         self.parent.value = 0   
166
167                 if self.parent.value > 10:
168                         self.parent.value = 10  
169
170         def handleKey(self, key):
171                 if key == config.prevElement:
172                         self.parent.value = self.parent.value - 1
173                 if key == config.nextElement:
174                         self.parent.value = self.parent.value + 1
175                                         
176                 self.checkValues()      
177                 self.parent.change()    
178
179         def __call__(self):                     #needed by configlist
180                 self.checkValues()      
181                 return ("slider", self.parent.value * 10)
182
183 class ConfigSubsection:
184         def __init__(self):
185                 pass
186
187 class configElement:
188         def datafromFile(self, control, data):
189                 if control == ConfigSlider:
190                         return int(data);
191                 elif control == configBoolean:
192                         return int(data);
193                 elif control == configSequence:
194                         list = [ ]
195                         part = data.split(self.vals[0])
196                         for x in part:
197                                 list.append(int(x))
198                         return list
199                 else: 
200                         return ""       
201
202         def datatoFile(self, control, data):
203                 if control == ConfigSlider:
204                         return str(data);
205                 elif control == configBoolean:
206                         return str(data);
207                 elif control == configSequence:
208                         value = ""
209                         for i in data:
210                                 if value !="":
211                                         value += self.vals[0]
212                                 value += str(i)
213                         return value
214                 else: 
215                         return ""       
216
217         def loadData(self):
218                 try:
219                         value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
220                 except:         
221                         value = ""
222
223                 if value == "":
224                         print "value not found - using default"
225                         self.value = self.defaultValue
226                         self.save()             #add missing value to dict
227                 else:
228                         self.value = value
229
230         def __init__(self, configPath, control, defaultValue, vals):
231                 self.configPath = configPath
232                 self.defaultValue = defaultValue
233                 self.controlType = control
234                 self.vals = vals
235                 self.notifierList = [ ]
236                 self.loadData()         
237         def addNotifier(self, notifier):
238                 self.notifierList.append(notifier);
239                 notifier(self);
240         def change(self):
241                 for notifier in self.notifierList:
242                         notifier(self)
243         def reload(self):
244                 self.loadData()
245         def save(self):
246                 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))