1 from AutoTimerComponent import AutoTimerComponent
2 from AutoTimer import getValue
3 from RecordTimer import AFTEREVENT
5 def parseConfig(configuration, list, version = None, uniqueTimerId = 0):
7 print "[AutoTimer] Trying to parse config version 3"
8 parseConfig_v3(configuration, list, uniqueTimerId)
10 print "[AutoTimer] Trying to parse config version 2"
11 parseConfig_v2(configuration, list, uniqueTimerId)
13 print "[AutoTimer] Trying to parse unversioned config file"
14 parseConfig_v1(configuration, list, uniqueTimerId)
16 def parseConfig_v3(configuration, list, uniqueTimerId = 0):
18 for timer in configuration.getElementsByTagName("timer"):
19 # Increment uniqueTimerId
23 match = timer.getAttribute("match").encode("UTF-8")
25 print '[AutoTimer] Erroneous config is missing attribute "match", skipping entry'
29 name = timer.getAttribute("name").encode("UTF-8")
31 print '[AutoTimer] Timer is missing attribute "name", defaulting to match'
35 enabled = timer.getAttribute("enabled") or "yes"
38 elif enabled == "yes":
41 print '[AutoTimer] Erroneous config contains invalid value for "enabled":', enabled,', disabling'
45 elements = timer.getElementsByTagName("timespan")
48 # Read out last definition
49 start = elements[Len-1].getAttribute("from")
50 end = elements[Len-1].getAttribute("to")
52 start = [int(x) for x in start.split(':')]
53 end = [int(x) for x in end.split(':')]
54 timetuple = (start, end)
56 print '[AutoTimer] Erroneous config contains invalid definition of "timespan", ignoring definition'
61 # Read out allowed services
63 for service in timer.getElementsByTagName("serviceref"):
64 value = getValue(service, None)
66 # strip all after last :
67 pos = value.rfind(':')
71 servicelist.append(value)
74 elements = timer.getElementsByTagName("offset")
77 value = elements[Len-1].getAttribute("both")
79 before = after = int(value) * 60
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)
88 elements = timer.getElementsByTagName("afterevent")
91 idx = {"none": AFTEREVENT.NONE, "standby": AFTEREVENT.STANDBY, "shutdown": AFTEREVENT.DEEPSTANDBY, "deepstandby": AFTEREVENT.DEEPSTANDBY}
92 value = getValue(elements[Len-1], None)
96 start = elements[Len-1].getAttribute("from")
97 end = elements[Len-1].getAttribute("to")
99 start = [int(x) for x in start.split(':')]
100 end = [int(x) for x in end.split(':')]
101 afterevent = [(value, (start, end))]
103 afterevent = [(value, None)]
105 print '[AutoTimer] Erroneous config contains invalid value for "afterevent":', afterevent,', ignoring definition'
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)
116 if not (value and where):
120 excludes[idx[where]].append(value.encode("UTF-8"))
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
132 # Finally append tuple
133 list.append(AutoTimerComponent(
138 timespan = timetuple,
139 services = servicelist,
141 afterevent = afterevent,
147 def parseConfig_v2(configuration, list, uniqueTimerId = 0):
149 for timer in configuration.getElementsByTagName("timer"):
150 # Increment uniqueTimerId
153 # Read out match/name
154 match = timer.getAttribute("name").encode("UTF-8")
156 print '[AutoTimer] Erroneous config is missing attribute "name", skipping entry'
159 # Setting name to match
162 enabled = timer.getAttribute("enabled") or "yes"
165 elif enabled == "yes":
168 print '[AutoTimer] Erroneous config contains invalid value for "enabled":', enabled,', disabling entry'
172 elements = timer.getElementsByTagName("timespan")
175 # Read out last definition
176 start = elements[Len-1].getAttribute("from")
177 end = elements[Len-1].getAttribute("to")
179 start = [int(x) for x in start.split(':')]
180 end = [int(x) for x in end.split(':')]
181 timetuple = (start, end)
183 print '[AutoTimer] Erroneous config contains invalid definition of "timespan", ignoring definition'
188 # Read out allowed services
189 elements = timer.getElementsByTagName("serviceref")
192 for service in elements:
193 value = getValue(service, None)
195 # strip all after last :
196 pos = value.rfind(':')
198 value = value[:pos+1]
200 servicelist.append(value)
205 elements = timer.getElementsByTagName("offset")
208 value = elements[Len-1].getAttribute("both")
210 before = int(elements[Len-1].getAttribute("before") or 0) * 60
211 after = int(elements[Len-1].getAttribute("after") or 0) * 60
213 before = after = int(value) * 60
214 offset = (before, after)
218 # Read out afterevent
219 elements = timer.getElementsByTagName("afterevent")
222 idx = {"none": AFTEREVENT.NONE, "standby": AFTEREVENT.STANDBY, "shutdown": AFTEREVENT.DEEPSTANDBY, "deepstandby": AFTEREVENT.DEEPSTANDBY}
223 value = getValue(elements[Len-1], None)
226 start = elements[Len-1].getAttribute("from")
227 end = elements[Len-1].getAttribute("to")
229 start = [int(x) for x in start.split(':')]
230 end = [int(x) for x in end.split(':')]
231 afterevent = [(value, (start, end))]
233 afterevent = [(value, None)]
235 print '[AutoTimer] Erroneous config contains invalid value for "afterevent":', afterevent,', ignoring definition'
241 elements = timer.getElementsByTagName("exclude")
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)
248 if not (value and where):
252 excludes[idx[where]].append(value.encode("UTF-8"))
258 # Read out max length
259 elements = timer.getElementsByTagName("maxduration")
261 maxlen = getValue(elements, None)
262 if maxlen is not None:
263 maxlen = int(maxlen)*60
267 # Finally append tuple
268 list.append(AutoTimerComponent(
273 timespan = timetuple,
274 services = servicelist,
276 afterevent = afterevent,
281 def parseConfig_v1(configuration, list, uniqueTimerId = 0):
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)
286 # Increment uniqueTimerId
290 name = getValue(timer.getElementsByTagName("name"), None)
292 print "[AutoTimer] Erroneous config, skipping entry"
296 name = name.encode('UTF-8')
298 # Setting match to name
302 elements = timer.getElementsByTagName("timespan")
304 # We only support 1 Timespan so far
305 start = getValue(elements[0].getElementsByTagName("from"), None)
306 end = getValue(elements[0].getElementsByTagName("to"), None)
308 start = [int(x) for x in start.split(':')]
309 end = [int(x) for x in end.split(':')]
310 timetuple = (start, end)
316 # Read out allowed services
317 elements = timer.getElementsByTagName("serviceref")
320 for service in elements:
321 value = getValue(service, None)
323 # strip all after last :
324 pos = value.rfind(':')
326 value = value[:pos+1]
328 servicelist.append(value)
333 elements = timer.getElementsByTagName("offset")
335 value = getValue(elements, None)
337 before = int(getValue(elements[0].getElementsByTagName("before"), 0)) * 60
338 after = int(getValue(elements[0].getElementsByTagName("after"), 0)) * 60
340 before = after = int(value) * 60
341 offset = (before, after)
345 # Read out afterevent
346 idx = {"standby": AFTEREVENT.STANDBY, "shutdown": AFTEREVENT.DEEPSTANDBY, "deepstandby": AFTEREVENT.DEEPSTANDBY}
347 afterevent = getValue(timer.getElementsByTagName("afterevent"), None)
349 afterevent = (idx[afterevent], None)
351 # TODO: do we really want to copy this behaviour?
352 afterevent = (AFTEREVENT.NONE, None)
355 elements = timer.getElementsByTagName("exclude")
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)
362 if not (value and where):
366 excludes[idx[where]].append(value.encode("UTF-8"))
372 # Read out max length
373 elements = timer.getElementsByTagName("maxduration")
375 maxlen = getValue(elements, None)
376 if maxlen is not None:
377 maxlen = int(maxlen)*60
381 # Read out enabled status
382 elements = timer.getElementsByTagName("enabled")
384 if getValue(elements, "yes") == "no":
391 # Finally append tuple
392 list.append(AutoTimerComponent(
397 timespan = timetuple,
398 services = servicelist,
400 afterevent = [afterevent],