fix not-a-string when using english
[enigma2.git] / lib / python / Components / config.py
1 from time import *
2 from Tools.NumericalTextInput import *
3 from Tools.Directories import *
4
5 class configFile:
6         def __init__(self):
7                 self.changed = 0
8                 self.configElements = { }
9                 try:
10                         self.file = open(resolveFilename(SCOPE_CONFIG, "config"))
11                 except IOError:
12                         print "cannot open config file"
13                         return 
14                 
15                 while 1:
16                         line = self.file.readline()
17                         if line == "":
18                                 break
19                         
20                         if line.startswith("#"):                #skip comments
21                                 continue        
22                                 
23                         self.addElement(line)
24                 self.file.close()
25
26         def addElement(self, line):
27                 x = line.find("=")
28                 if x > -1:
29                         self.configElements[line[:x]] = line[x + 1:-1]
30         
31         def getKey(self, key):
32                 return self.configElements[key]
33
34         def setKey(self, key, value):
35                 self.changed = 1
36                 self.configElements[key] = value
37
38         def save(self):
39                 if self.changed == 0:           #no changes, so no write to disk needed
40                         return
41                         
42                 fileHandle = open(resolveFilename(SCOPE_CONFIG, "config"), "w")
43                 
44                 keys = self.configElements.keys()
45                 keys.sort()
46                 for x in keys:
47                         wstr = x + "=" + self.configElements[x] + "\n"
48
49                         fileHandle.write(wstr)
50
51                 fileHandle.close()              
52
53 class configSelection:
54         def __init__(self, parent):
55                 self.parent = parent
56                 
57         def checkValues(self):
58                 if self.parent.value < 0:
59                         self.parent.value = len(self.parent.vals) - 1   
60                 elif(self.parent.value > (len(self.parent.vals) - 1)):
61                         self.parent.value = 0
62
63         def cancel(self):
64                 self.parent.reload()
65
66         def save(self):
67                 self.parent.save()
68
69         def handleKey(self, key):
70                 if key == config.key["prevElement"]:
71                         self.parent.value = self.parent.value - 1
72                 if key == config.key["nextElement"]:
73                         self.parent.value = self.parent.value + 1
74                 
75                 self.checkValues()                      
76
77                 self.parent.change()
78
79         def __call__(self, selected):                   #needed by configlist
80                 self.checkValues()
81                 print "[config.py] orgstring: ", self.parent.vals[self.parent.value]
82                 print "[config.py] translation: ", _(self.parent.vals[self.parent.value])
83                 return ("text", _(self.parent.vals[self.parent.value]))
84
85 class configDateTime:
86         def __init__(self, parent):
87                 self.parent = parent
88                 
89         def checkValues(self):
90                 pass
91 #               if self.parent.value < 0:
92                         #self.parent.value = 0  
93
94                 #if(self.parent.value >= (len(self.parent.vals) - 1)):
95                         #self.parent.value = len(self.parent.vals) - 1
96
97         def cancel(self):
98                 self.parent.reload()
99
100         def save(self):
101                 self.parent.save()
102
103         def handleKey(self, key):
104                 if key == config.key["prevElement"]:
105                         self.parent.value = self.parent.value - self.parent.vals[1]
106                 if key == config.key["nextElement"]:
107                         self.parent.value = self.parent.value + self.parent.vals[1]
108                 
109                 self.checkValues()
110
111                 self.parent.change()    
112
113         def __call__(self, selected):                   #needed by configlist
114                 self.checkValues()
115                 return ("text", strftime(self.parent.vals[0], localtime(self.parent.value)))
116         
117 class configSatlist:
118         def __init__(self, parent):
119                 self.parent = parent
120
121         def checkValues(self):
122                 if self.parent.value < 0:
123                         self.parent.value = 0   
124
125                 if(self.parent.value >= (len(self.parent.vals) - 1)):
126                         self.parent.value = len(self.parent.vals) - 1
127                         
128         def cancel(self):
129                 self.parent.reload()
130
131         def save(self):
132                 self.parent.save()
133
134         def handleKey(self, key):
135                 if key == config.key["prevElement"]:
136                         self.parent.value = self.parent.value - 1
137                 if key == config.key["nextElement"]:
138                         self.parent.value = self.parent.value + 1
139                 
140                 self.checkValues()                      
141
142                 self.parent.change()    
143
144         def __call__(self, selected):                   #needed by configlist
145                 self.checkValues()
146                 #fixme
147                 return ("text", str(self.parent.vals[self.parent.value][0]))
148
149 class configSequenceArg:
150         def get(self, type, args = ()):
151                 # configsequencearg.get ("IP")
152                 if (type == "IP"):
153                         return (("."), [(0,255),(0,255),(0,255),(0,255)], "")
154                 # configsequencearg.get ("MAC")
155                 if (type == "MAC"):
156                         return ((":"), [(1,255),(1,255),(1,255),(1,255),(1,255),(1,255)], "")
157                 # configsequencearg.get ("CLOCK")
158                 if (type == "CLOCK"):
159                         return ((":"), [(0,23),(0,59)], "")
160                 # configsequencearg.get("INTEGER", (min, max)) => x with min <= x <= max
161                 if (type == "INTEGER"):
162                         return ((":"), [args], "")
163                 # configsequencearg.get("PINCODE", (number, "*")) => pin with number = length of pincode and "*" as numbers shown as stars
164                 # configsequencearg.get("PINCODE", (number, "")) => pin with number = length of pincode and numbers shown
165                 if (type == "PINCODE"):
166                         return ((":"), [(0, (10**args[0])-1)], args[1])
167                 # configsequencearg.get("FLOAT", [(min,max),(min1,max1)]) => x.y with min <= x <= max and min1 <= y <= max1
168                 if (type == "FLOAT"):
169                         return (("."), args, "")
170
171 configsequencearg = configSequenceArg()
172                 
173 class configSequence:
174         def __init__(self, parent):
175                 self.parent = parent
176                 self.markedPos = 0
177                 self.seperator = self.parent.vals[0]
178                 self.valueBounds = self.parent.vals[1]
179                 self.censorChar = self.parent.vals[2]
180
181         def checkValues(self):
182                 maxPos = 0
183                 num = 0
184                 for i in self.parent.value:
185                         maxPos += len(str(self.valueBounds[num][1]))
186                         while (self.valueBounds[num][0] > self.parent.value[num]):
187                                 self.parent.value[num] += 1
188
189                         while (self.valueBounds[num][1] < self.parent.value[num]):
190                                 self.parent.value[num] -= 1
191                                 
192 #                       if (self.valueBounds[num][0] <= i <= self.valueBounds[num][1]):
193                                 #pass
194                         #else:
195                                 #self.parent.value[num] = self.valueBounds[num][0]
196                         num += 1
197                 
198                 if self.markedPos >= maxPos:
199                         self.markedPos = maxPos - 1
200                 if self.markedPos < 0:
201                         self.markedPos = 0
202                         
203         def cancel(self):
204                 self.parent.reload()
205
206         def save(self):
207                 self.parent.save()
208
209         def handleKey(self, key):
210                 #this will no change anything on the value itself
211                 #so we can handle it here in gui element
212                 if key == config.key["prevElement"]:
213                         self.markedPos -= 1
214                 if key == config.key["nextElement"]:
215                         self.markedPos += 1
216                 
217                 if key >= config.key["0"] and key <= config.key["9"]:
218                         self.blockLen = []
219                         for x in self.valueBounds:
220                                 self.blockLen.append(len(str(x[1])))
221                                 
222                         pos = 0
223                         blocknumber = 0
224                         self.blockLenTotal = [0,]
225                         for x in self.blockLen:
226                                 pos += self.blockLen[blocknumber]
227                                 self.blockLenTotal.append(pos)
228                                 if (pos - 1 >= self.markedPos):
229                                         pass
230                                 else:
231                                         blocknumber += 1
232                                         
233                         number = 9 - config.key["9"] + key
234                         # length of numberblock
235                         numberLen = len(str(self.valueBounds[blocknumber][1]))
236                         # position in the block
237                         posinblock = self.markedPos - self.blockLenTotal[blocknumber]
238                         
239                         oldvalue = self.parent.value[blocknumber]
240                         olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
241                         newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
242                         
243                         print "You actually pressed a number (" + str(number) + ") which will be added at block number " + str(blocknumber) + " on position " + str(posinblock)
244                         print "Old value: " + str(oldvalue) + " olddec: " + str(olddec) + " newvalue: " + str(newvalue)
245                         self.parent.value[blocknumber] = newvalue
246                         self.markedPos += 1
247                 
248                 self.checkValues()                      
249                 
250                 print "markPos:",
251                 print self.markedPos
252
253                 #FIXME: dont call when press left/right
254                 self.parent.change()    
255
256         def __call__(self, selected):                   #needed by configlist
257                 value = ""
258                 mPos = self.markedPos
259                 print "Positon: " + str(mPos)
260                 num = 0;
261                 for i in self.parent.value:
262                         if len(value):  #fixme no heading separator possible
263                                 value += self.seperator
264                                 if mPos >= len(value) - 1:
265                                         mPos += 1
266                                 
267                         #diff =         self.valueBounds - len(str(i))
268                         #if diff > 0:
269                                 ## if this helps?!
270                                 #value += " " * diff
271                         print (("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i)
272                         if (self.censorChar == ""):
273                                 value += ("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i
274                         else:
275                                 value += (self.censorChar * len(str(self.valueBounds[num][1])))
276                         num += 1
277                         # only mark cursor when we are selected
278                         # (this code is heavily ink optimized!)
279                 if (self.parent.enabled == True):
280                         return ("mtext"[1-selected:], value, [mPos])
281                 else:
282                         return ("text", value)
283                 
284 class configText:
285         # used as first parameter
286         # is the text of a fixed size or is the user able to extend the length of the text
287         extendableSize = 1
288         fixedSize = 2
289
290         def __init__(self, parent):
291                 self.parent = parent
292                 self.markedPos = 0
293                 self.mode = self.parent.vals[0]
294                 self.textInput = NumericalTextInput(self.nextEntry)
295
296         def checkValues(self):
297                 if (self.markedPos < 0):
298                         self.markedPos = 0
299                 if (self.markedPos >= len(self.parent.value)):
300                         self.markedPos = len(self.parent.value) - 1
301                         
302         def cancel(self):
303                 self.parent.reload()
304
305         def save(self):
306                 self.parent.save()
307                 
308         def nextEntry(self):
309                 print self.parent
310                 self.parent.vals[1](self.parent.getConfigPath())
311
312         def handleKey(self, key):
313                 #this will no change anything on the value itself
314                 #so we can handle it here in gui element
315                 if key == config.key["delete"]:
316                         self.parent.value = self.parent.value[0:self.markedPos] + self.parent.value[self.markedPos + 1:]
317                 if key == config.key["prevElement"]:
318                         self.textInput.nextKey()
319                         self.markedPos -= 1
320
321                 if key == config.key["nextElement"]:
322                         self.textInput.nextKey()
323                         self.markedPos += 1
324                         if (self.mode == self.extendableSize):
325                                 if (self.markedPos >= len(self.parent.value)):
326                                         self.parent.value = self.parent.value.ljust(len(self.parent.value) + 1)
327                         
328                                 
329                 if key >= config.key["0"] and key <= config.key["9"]:
330                         number = 9 - config.key["9"] + key
331
332                         self.parent.value = self.parent.value[0:self.markedPos] + str(self.textInput.getKey(number)) + self.parent.value[self.markedPos + 1:]
333                 
334                 self.checkValues()                      
335                 
336                 self.parent.change()    
337
338         def __call__(self, selected):                   #needed by configlist
339                 return ("mtext"[1-selected:], str(self.parent.value), [self.markedPos])
340                 
341 class configValue:
342         def __init__(self, obj):
343                 self.obj = obj
344                 
345         def __str__(self):
346                 return self.obj
347
348 class Config:
349         def __init__(self):
350                 self.key = { "choseElement": 0,
351                                          "prevElement": 1,
352                                          "nextElement": 2,
353                                          "delete": 3,
354                                          "0": 10,
355                                          "1": 11,
356                                          "2": 12,
357                                          "3": 13,
358                                          "4": 14,
359                                          "5": 15,
360                                          "6": 16,
361                                          "7": 17,
362                                          "8": 18,
363                                          "9": 19 }
364                 
365 config = Config();
366
367 configfile = configFile()
368
369 class ConfigSlider:
370         def __init__(self, parent):
371                 self.parent = parent
372
373         def cancel(self):
374                 self.parent.reload()
375
376         def save(self):
377                 self.parent.save()
378
379         def checkValues(self):
380                 if self.parent.value < 0:
381                         self.parent.value = 0   
382
383                 if self.parent.value > 10:
384                         self.parent.value = 10  
385
386         def handleKey(self, key):
387                 if key == config.key["prevElement"]:
388                         self.parent.value = self.parent.value - 1
389                 if key == config.key["nextElement"]:
390                         self.parent.value = self.parent.value + 1
391                                         
392                 self.checkValues()      
393                 self.parent.change()    
394
395         def __call__(self, selected):                   #needed by configlist
396                 self.checkValues()
397                 return ("slider", self.parent.value * 10)
398
399 class ConfigSubsection:
400         def __init__(self):
401                 pass
402
403 class configElement:
404
405         def getIndexbyEntry(self, data):
406                 cnt = 0;
407                 tcnt = -1; #for defaultval
408                 for x in self.vals:
409                         if int(x[1]) == int(data):
410                                         return cnt
411                         if int(x[1]) == int(self.defaultValue):
412                                         tcnt = cnt
413                         cnt += 1
414                 if tcnt != -1:
415                         return tcnt
416                 return 0        #prevent bigger then array
417
418         def datafromFile(self, control, data):
419                 if control == ConfigSlider:
420                         return int(data)
421                 elif control == configSelection:
422                         return int(data)
423                 elif control == configDateTime:
424                         return int(data)
425                 elif control == configText:
426                         return str(data)
427                 elif control == configSequence:
428                         list = [ ]
429                         part = data.split(self.vals[0])
430                         for x in part:
431                                 list.append(int(x))
432                         return list
433                 elif control == configSatlist:
434                         return self.getIndexbyEntry(data)
435                 else: 
436                         return ""       
437
438         def datatoFile(self, control, data):
439                 if control == ConfigSlider:
440                         return str(data)
441                 elif control == configSelection:
442                         return str(data)
443                 elif control == configDateTime:
444                         return str(data)
445                 elif control == configText:
446                         return str(data.strip())
447
448                 elif control == configSequence:
449                         print data
450                         try:
451                                 value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
452                         except: 
453                                 value = str(data)       
454 #                       just in case you don't understand the above, here an equivalent:
455 #                       value = ""
456 #                       for i in data:
457 #                               if value !="":
458 #                                       value += self.vals[0]
459 #                               value += str(i)
460                         return value
461                 elif control == configSatlist:
462                         return str(self.vals[self.value][1]);
463                 else: 
464                         return ""       
465
466         def loadData(self):
467                 #print "load:" + self.configPath
468                 try:
469                         value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
470                 except:         
471                         value = ""
472
473                 if value == "":
474                         #print "value not found - using default"
475
476                         if self.controlType == configSatlist:
477                                 self.value = self.getIndexbyEntry(self.defaultValue)
478                         else:   
479                                 self.value = self.defaultValue
480
481                         self.save()             #add missing value to dict
482                 else:
483                         #print "set val:" + str(value)
484                         self.value = value
485                         
486                 #is this right? activate settings after load/cancel and use default     
487                 self.change()
488
489         def __init__(self, configPath, control, defaultValue, vals):
490                 self.configPath = configPath
491                 self.defaultValue = defaultValue
492                 self.controlType = control
493                 self.vals = vals
494                 self.notifierList = [ ]
495                 self.enabled = True
496                 self.loadData()         
497                 
498         def getConfigPath(self):
499                 return self.configPath
500         
501         def addNotifier(self, notifier):
502                 self.notifierList.append(notifier);
503                 notifier(self);
504         def change(self):
505                 for notifier in self.notifierList:
506                         notifier(self)
507         def reload(self):
508                 self.loadData()
509         def save(self):
510                 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))
511
512 class configElement_nonSave(configElement):
513         def __init__(self, configPath, control, defaultValue, vals):
514                 configElement.__init__(self, configPath, control, defaultValue, vals)
515
516         def save(self):
517                 pass
518                 
519 def getConfigListEntry(description, element):
520         b = element
521         item = b.controlType(b)
522         return ((description, item))
523
524
525 def configElementBoolean(name, default, texts=(_("Enable"), _("Disable"))):
526         return configElement(name, configSelection, default, texts)
527
528 config.misc = ConfigSubsection()