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