intial commit of autotimer, epgrefresh and werbezapper to enigma2-plugins
[enigma2-plugins.git] / autotimer / src / OldConfigurationParser.py
1 from AutoTimerComponent import AutoTimerComponent
2 from AutoTimer import getValue
3 from RecordTimer import AFTEREVENT
4
5 def parseConfig(configuration, list, version = None, uniqueTimerId = 0):
6         if version == "3":
7                 print "[AutoTimer] Trying to parse config version 3"
8                 parseConfig_v3(configuration, list, uniqueTimerId)
9         elif version == "2":
10                 print "[AutoTimer] Trying to parse config version 2"
11                 parseConfig_v2(configuration, list, uniqueTimerId)
12         else:
13                 print "[AutoTimer] Trying to parse unversioned config file"
14                 parseConfig_v1(configuration, list, uniqueTimerId)
15
16 def parseConfig_v3(configuration, list, uniqueTimerId = 0):
17         # Iterate Timers
18         for timer in configuration.getElementsByTagName("timer"):
19                 # Increment uniqueTimerId
20                 uniqueTimerId += 1
21
22                 # Read out match
23                 match = timer.getAttribute("match").encode("UTF-8")
24                 if not match:
25                         print '[AutoTimer] Erroneous config is missing attribute "match", skipping entry'
26                         continue
27
28                 # Read out name
29                 name = timer.getAttribute("name").encode("UTF-8")
30                 if not name:
31                         print '[AutoTimer] Timer is missing attribute "name", defaulting to match'
32                         name = match
33
34                 # Read out enabled
35                 enabled = timer.getAttribute("enabled") or "yes"
36                 if enabled == "no":
37                         enabled = False
38                 elif enabled == "yes":
39                         enabled = True
40                 else:
41                         print '[AutoTimer] Erroneous config contains invalid value for "enabled":', enabled,', disabling'
42                         enabled = False
43
44                 # Guess allowedtime
45                 elements = timer.getElementsByTagName("timespan")
46                 Len = len(elements)
47                 if Len:
48                         # Read out last definition
49                         start = elements[Len-1].getAttribute("from")
50                         end = elements[Len-1].getAttribute("to")
51                         if start and end:
52                                 start = [int(x) for x in start.split(':')]
53                                 end = [int(x) for x in end.split(':')]
54                                 timetuple = (start, end)
55                         else:
56                                 print '[AutoTimer] Erroneous config contains invalid definition of "timespan", ignoring definition'
57                                 timetuple = None
58                 else:
59                         timetuple = None
60
61                 # Read out allowed services
62                 servicelist = []                        
63                 for service in timer.getElementsByTagName("serviceref"):
64                         value = getValue(service, None, False)
65                         if value:
66                                 # strip all after last :
67                                 pos = value.rfind(':')
68                                 if pos != -1:
69                                         value = value[:pos+1]
70
71                                 servicelist.append(value)
72
73                 # Read out offset
74                 elements = timer.getElementsByTagName("offset")
75                 Len = len(elements)
76                 if Len:
77                         value = elements[Len-1].getAttribute("both")
78                         if value:
79                                 before = after = int(value) * 60
80                         else:
81                                 before = int(elements[Len-1].getAttribute("before") or 0) * 60
82                                 after = int(elements[Len-1].getAttribute("after") or 0) * 60
83                         offset = (before, after)
84                 else:
85                         offset = None
86
87                 # Read out afterevent
88                 elements = timer.getElementsByTagName("afterevent")
89                 Len = len(elements)
90                 if Len:
91                         idx = {"none": AFTEREVENT.NONE, "standby": AFTEREVENT.STANDBY, "shutdown": AFTEREVENT.DEEPSTANDBY, "deepstandby": AFTEREVENT.DEEPSTANDBY}
92                         value = getValue(elements[Len-1], None, False)
93
94                         try:
95                                 value = idx[value]
96                                 start = elements[Len-1].getAttribute("from")
97                                 end = elements[Len-1].getAttribute("to")
98                                 if start and end:
99                                         start = [int(x) for x in start.split(':')]
100                                         end = [int(x) for x in end.split(':')]
101                                         afterevent = [(value, (start, end))]
102                                 else:
103                                         afterevent = [(value, None)]
104                         except KeyError, ke:
105                                 print '[AutoTimer] Erroneous config contains invalid value for "afterevent":', afterevent,', ignoring definition'
106                                 afterevent = []
107                 else:
108                         afterevent = []
109
110                 # Read out exclude
111                 idx = {"title": 0, "shortdescription": 1, "description": 2, "dayofweek": 3}
112                 excludes = ([], [], [], []) 
113                 for exclude in timer.getElementsByTagName("exclude"):
114                         where = exclude.getAttribute("where")
115                         value = getValue(exclude, None, False)
116                         if not (value and where):
117                                 continue
118
119                         try:
120                                 excludes[idx[where]].append(value.encode("UTF-8"))
121                         except KeyError, ke:
122                                 pass
123
124                 # Read out max length
125                 # TODO: this item is unique, shouldn't it be an attribute then?
126                 maxlen = getValue(timer.getElementsByTagName("maxduration"), None)
127                 if maxlen is not None:
128                         maxlen = int(maxlen)*60
129                 else:
130                         maxlen = None
131
132                 # Finally append tuple
133                 list.append(AutoTimerComponent(
134                         uniqueTimerId,
135                         name,
136                         match,
137                         enabled,
138                         timespan = timetuple,
139                         services = servicelist,
140                         offset = offset,
141                         afterevent = afterevent,
142                         exclude = excludes,
143                         maxduration = maxlen
144                 ))
145                 
146
147 def parseConfig_v2(configuration, list, uniqueTimerId = 0):
148         # Iterate Timers
149         for timer in configuration.getElementsByTagName("timer"):
150                 # Increment uniqueTimerId
151                 uniqueTimerId += 1
152
153                 # Read out match/name
154                 match = timer.getAttribute("name").encode("UTF-8")
155                 if not match:
156                         print '[AutoTimer] Erroneous config is missing attribute "name", skipping entry'
157                         continue
158
159                 # Setting name to match
160                 name = match
161
162                 enabled = timer.getAttribute("enabled") or "yes"
163                 if enabled == "no":
164                         enabled = False
165                 elif enabled == "yes":
166                         enabled = True
167                 else:
168                         print '[AutoTimer] Erroneous config contains invalid value for "enabled":', enabled,', disabling entry'
169                         enabled = False
170
171                 # Guess allowedtime
172                 elements = timer.getElementsByTagName("timespan")
173                 Len = len(elements)
174                 if Len:
175                         # Read out last definition
176                         start = elements[Len-1].getAttribute("from")
177                         end = elements[Len-1].getAttribute("to")
178                         if start and end:
179                                 start = [int(x) for x in start.split(':')]
180                                 end = [int(x) for x in end.split(':')]
181                                 timetuple = (start, end)
182                         else:
183                                 print '[AutoTimer] Erroneous config contains invalid definition of "timespan", ignoring definition'
184                                 timetuple = None
185                 else:
186                         timetuple = None
187
188                 # Read out allowed services
189                 elements = timer.getElementsByTagName("serviceref")
190                 if len(elements):
191                         servicelist = []
192                         for service in elements:
193                                 value = getValue(service, None, False)
194                                 if value:
195                                         # strip all after last :
196                                         pos = value.rfind(':')
197                                         if pos != -1:
198                                                 value = value[:pos+1]
199
200                                         servicelist.append(value)
201                 else:
202                         servicelist = None
203
204                 # Read out offset
205                 elements = timer.getElementsByTagName("offset")
206                 Len = len(elements)
207                 if Len:
208                         value = elements[Len-1].getAttribute("both")
209                         if value == '':
210                                 before = int(elements[Len-1].getAttribute("before") or 0) * 60
211                                 after = int(elements[Len-1].getAttribute("after") or 0) * 60
212                         else:
213                                 before = after = int(value) * 60
214                         offset = (before, after)
215                 else:
216                         offset = None
217
218                 # Read out afterevent
219                 elements = timer.getElementsByTagName("afterevent")
220                 Len = len(elements)
221                 if Len:
222                         idx = {"none": AFTEREVENT.NONE, "standby": AFTEREVENT.STANDBY, "shutdown": AFTEREVENT.DEEPSTANDBY, "deepstandby": AFTEREVENT.DEEPSTANDBY}
223                         value = getValue(elements[Len-1], None, False)
224                         try:
225                                 value = idx[value]
226                                 start = elements[Len-1].getAttribute("from")
227                                 end = elements[Len-1].getAttribute("to")
228                                 if start and end:
229                                         start = [int(x) for x in start.split(':')]
230                                         end = [int(x) for x in end.split(':')]
231                                         afterevent = [(value, (start, end))]
232                                 else:
233                                         afterevent = [(value, None)]
234                         except KeyError, ke:
235                                 print '[AutoTimer] Erroneous config contains invalid value for "afterevent":', afterevent,', ignoring definition'
236                                 afterevent = []
237                 else:
238                         afterevent = []
239
240                 # Read out exclude
241                 elements = timer.getElementsByTagName("exclude")
242                 if len(elements):
243                         excludes = ([], [], [], [])
244                         idx = {"title": 0, "shortdescription": 1, "description": 2, "dayofweek": 3}
245                         for exclude in elements:
246                                 where = exclude.getAttribute("where")
247                                 value = getValue(exclude, None, False)
248                                 if not (value and where):
249                                         continue
250
251                                 try:
252                                         excludes[idx[where]].append(value.encode("UTF-8"))
253                                 except KeyError, ke:
254                                         pass
255                 else:
256                         excludes = None
257
258                 # Read out max length
259                 elements = timer.getElementsByTagName("maxduration")
260                 if len(elements):
261                         maxlen = getValue(elements, None)
262                         if maxlen is not None:
263                                 maxlen = int(maxlen)*60
264                 else:
265                         maxlen = None
266
267                 # Finally append tuple
268                 list.append(AutoTimerComponent(
269                         uniqueTimerId,
270                         name,
271                         match,
272                         enabled,
273                         timespan = timetuple,
274                         services = servicelist,
275                         offset = offset,
276                         afterevent = afterevent,
277                         exclude = excludes,
278                         maxduration = maxlen
279                 ))
280
281 def parseConfig_v1(configuration, list, uniqueTimerId = 0):
282         # Iterate Timers
283         for timer in configuration.getElementsByTagName("timer"):
284                 # Timers are saved as tuple (name, allowedtime (from, to) or None, list of services or None, timeoffset in m (before, after) or None, afterevent)
285
286                 # Increment uniqueTimerId
287                 uniqueTimerId += 1
288
289                 # Read out name
290                 name = getValue(timer.getElementsByTagName("name"), None)
291                 if name is None:
292                         print "[AutoTimer] Erroneous config, skipping entry"
293                         continue
294
295                 # Encode name
296                 name = name.encode('UTF-8')
297
298                 # Setting match to name
299                 match = name
300
301                 # Guess allowedtime
302                 elements = timer.getElementsByTagName("timespan")
303                 if len(elements):
304                         # We only support 1 Timespan so far
305                         start = getValue(elements[0].getElementsByTagName("from"), None)
306                         end = getValue(elements[0].getElementsByTagName("to"), None)
307                         if start and end:
308                                 start = [int(x) for x in start.split(':')]
309                                 end = [int(x) for x in end.split(':')]
310                                 timetuple = (start, end)
311                         else:
312                                 timetuple = None
313                 else:
314                         timetuple = None
315
316                 # Read out allowed services
317                 elements = timer.getElementsByTagName("serviceref")
318                 if len(elements):
319                         servicelist = []
320                         for service in elements:
321                                 value = getValue(service, None, False)
322                                 if value:
323                                         # strip all after last :
324                                         pos = value.rfind(':')
325                                         if pos != -1:
326                                                 value = value[:pos+1]
327
328                                         servicelist.append(value)
329                 else:
330                         servicelist = None
331
332                 # Read out offset
333                 elements = timer.getElementsByTagName("offset")
334                 if len(elements):
335                         value = getValue(elements, None)
336                         if value is None:
337                                 before = int(getValue(elements[0].getElementsByTagName("before"), 0)) * 60
338                                 after = int(getValue(elements[0].getElementsByTagName("after"), 0)) * 60
339                         else:
340                                 before = after = int(value) * 60
341                         offset = (before, after)
342                 else:
343                         offset = None
344
345                 # Read out afterevent
346                 idx = {"standby": AFTEREVENT.STANDBY, "shutdown": AFTEREVENT.DEEPSTANDBY, "deepstandby": AFTEREVENT.DEEPSTANDBY}
347                 afterevent = getValue(timer.getElementsByTagName("afterevent"), None)
348                 try:
349                         afterevent = (idx[afterevent], None)
350                 except KeyError, ke:
351                         # TODO: do we really want to copy this behaviour?
352                         afterevent = (AFTEREVENT.NONE, None)
353
354                 # Read out exclude
355                 elements = timer.getElementsByTagName("exclude")
356                 if len(elements):
357                         excludes = ([], [], [], [])
358                         idx = {"title": 0, "shortdescription": 1, "description": 2, "dayofweek": 3}
359                         for exclude in elements:
360                                 where = exclude.getAttribute("where")
361                                 value = getValue(exclude, None, False)
362                                 if not (value and where):
363                                         continue
364
365                                 try:
366                                         excludes[idx[where]].append(value.encode("UTF-8"))
367                                 except KeyError, ke:
368                                         pass
369                 else:
370                         excludes = None
371
372                 # Read out max length
373                 elements = timer.getElementsByTagName("maxduration")
374                 if len(elements):
375                         maxlen = getValue(elements, None)
376                         if maxlen is not None:
377                                 maxlen = int(maxlen)*60
378                 else:
379                         maxlen = None
380
381                 # Read out enabled status
382                 elements = timer.getElementsByTagName("enabled")
383                 if len(elements):
384                         if getValue(elements, "yes") == "no":
385                                 enabled = False
386                         else:
387                                 enabled = True
388                 else:
389                         enabled = True
390
391                 # Finally append tuple
392                 list.append(AutoTimerComponent(
393                                 uniqueTimerId,
394                                 name,
395                                 match,
396                                 enabled,
397                                 timespan = timetuple,
398                                 services = servicelist,
399                                 offset = offset,
400                                 afterevent = [afterevent],
401                                 exclude = excludes,
402                                 maxduration = maxlen
403                 ))