4072858aeaa2cc73079356995eb4dd58d8615b87
[vuplus_dvbapp-plugin] / 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         print "[AutoTimer] Trying to parse old config"
7
8         # Iterate Timers
9         for timer in configuration.getElementsByTagName("timer"):
10                 # Increment uniqueTimerId
11                 uniqueTimerId += 1
12
13                 # Get name (V2+)
14                 if timer.hasAttribute("name"):
15                         name = timer.getAttribute("name").encode("UTF-8")
16                 # Get name (= match) (V1)
17                 else:
18                         # Read out name
19                         name = getValue(timer.getElementsByTagName("name"), "").encode("UTF-8")
20
21                 if not name:
22                         print '[AutoTimer] Erroneous config is missing attribute "name", skipping entry'
23                         continue
24
25                 # Read out match (V3+)
26                 if timer.hasAttribute("match"):
27                         # Read out match
28                         match = timer.getAttribute("match").encode("UTF-8")
29                         if not match:
30                                 print '[AutoTimer] Erroneous config contains empty attribute "match", skipping entry'
31                                 continue
32                 # V2-
33                 else:
34                         # Setting name to match
35                         name = match
36
37
38                 # See if Timer is ensabled (V2+)
39                 if timer.hasAttribute("enabled"):
40                         enabled = timer.getAttribute("enabled") or "yes"
41                         if enabled == "no":
42                                 enabled = False
43                         elif enabled == "yes":
44                                 enabled = True
45                         else:
46                                 print '[AutoTimer] Erroneous config contains invalid value for "enabled":', enabled,', skipping entry'
47                                 enabled = False
48                 # V1
49                 else:
50                         elements = timer.getElementsByTagName("enabled")
51                         if len(elements):
52                                 if getValue(elements, "yes") == "no":
53                                         enabled = False
54                                 else:
55                                         enabled = True
56                         else:
57                                 enabled = True
58                         
59
60                 # Read out timespan (V4+; Falling back on missing definition should be OK)
61                 if timer.hasAttribute("from") and timer.hasAttribute("to"):
62                         start = timer.getAttribute("from")
63                         end = timer.getAttribute("to")
64                         if start and end:
65                                 start = [int(x) for x in start.split(':')]
66                                 end = [int(x) for x in end.split(':')]
67                                 timetuple = (start, end)
68                         else:
69                                 timetuple = None
70                 # V3-
71                 else:
72                         elements = timer.getElementsByTagName("timespan")
73                         Len = len(elements)
74                         if Len:
75                                 # Read out last definition
76                                 start = elements[Len-1].getAttribute("from")
77                                 end = elements[Len-1].getAttribute("to")
78                                 if start and end:
79                                         start = [int(x) for x in start.split(':')]
80                                         end = [int(x) for x in end.split(':')]
81                                         timetuple = (start, end)
82                                 else:
83                                         print '[AutoTimer] Erroneous config contains invalid definition of "timespan", ignoring definition'
84                                         timetuple = None
85                         else:
86                                 timetuple = None
87
88                 # Read out allowed services (V*)
89                 elements = timer.getElementsByTagName("serviceref")
90                 if len(elements):
91                         servicelist = []
92                         for service in elements:
93                                 value = getValue(service, None)
94                                 if value:
95                                         # strip all after last :
96                                         pos = value.rfind(':')
97                                         if pos != -1:
98                                                 value = value[:pos+1]
99
100                                         servicelist.append(value)
101                 else:
102                         servicelist = None
103
104                 # Read out allowed bouquets (V* though officially supported since V4)
105                 bouquets = []
106                 for bouquet in timer.getElementsByTagName("bouquet"):
107                         value = getValue(bouquet, None)
108                         if value:
109                                 bouquets.append(value)
110
111                 # Read out offset (V4+)
112                 if timer.hasAttribute("offset"):
113                         offset = timer.getAttribute("offset") or None
114                         if offset:
115                                 offset = offset.split(",")
116                                 if len(offset) == 1:
117                                         before = after = int(offset[0] or 0) * 60
118                                 else:
119                                         before = int(offset[0] or 0) * 60
120                                         after = int(offset[1] or 0) * 60
121                                 offset = (before, after)
122                 # V3-
123                 else:
124                         elements = timer.getElementsByTagName("offset")
125                         Len = len(elements)
126                         if Len:
127                                 value = elements[Len-1].getAttribute("both")
128                                 if value == '':
129                                         before = int(elements[Len-1].getAttribute("before") or 0) * 60
130                                         after = int(elements[Len-1].getAttribute("after") or 0) * 60
131                                 else:
132                                         before = after = int(value) * 60
133                                 offset = (before, after)
134                         else:
135                                 offset = None
136
137                 # Read out counter
138                 counter = int(timer.getAttribute("counter") or '0')
139                 counterLeft = int(timer.getAttribute("left") or counter)
140                 counterLimit = timer.getAttribute("lastActivation")
141                 counterFormat = timer.getAttribute("counterFormat")
142                 lastBegin = int(timer.getAttribute("lastBegin") or 0)
143
144                 # Read out justplay
145                 justplay = int(timer.getAttribute("justplay") or '0')
146
147                 # Read out avoidDuplicateDescription
148                 avoidDuplicateDescription = bool(timer.getAttribute("avoidDuplicateDescription") or False)
149
150                 # Read out afterevent (compatible to V* though behaviour for V3- is different as V4+ allows multiple afterevents while the last definication was chosen before)
151                 idx = {"none": AFTEREVENT.NONE, "standby": AFTEREVENT.STANDBY, "shutdown": AFTEREVENT.DEEPSTANDBY, "deepstandby": AFTEREVENT.DEEPSTANDBY}
152                 afterevent = []
153                 for element in timer.getElementsByTagName("afterevent"):
154                         value = getValue(element, None)
155
156                         try:
157                                 value = idx[value]
158                                 start = element.getAttribute("from")
159                                 end = element.getAttribute("to")
160                                 if start and end:
161                                         start = [int(x) for x in start.split(':')]
162                                         end = [int(x) for x in end.split(':')]
163                                         afterevent.append((value, (start, end)))
164                                 else:
165                                         afterevent.append((value, None))
166                         except KeyError, ke:
167                                 print '[AutoTimer] Erroneous config contains invalid value for "afterevent":', afterevent,', ignoring definition'
168                                 continue
169
170                 # Read out exclude (V*)
171                 idx = {"title": 0, "shortdescription": 1, "description": 2, "dayofweek": 3}
172                 excludes = ([], [], [], []) 
173                 for exclude in timer.getElementsByTagName("exclude"):
174                         where = exclude.getAttribute("where")
175                         value = getValue(exclude, None)
176                         if not (value and where):
177                                 continue
178
179                         try:
180                                 excludes[idx[where]].append(value.encode("UTF-8"))
181                         except KeyError, ke:
182                                 pass
183
184                 # Read out includes (use same idx) (V4+ feature, should not harm V3-)
185                 includes = ([], [], [], []) 
186                 for include in timer.getElementsByTagName("include"):
187                         where = include.getAttribute("where")
188                         value = getValue(include, None)
189                         if not (value and where):
190                                 continue
191
192                         try:
193                                 includes[idx[where]].append(value.encode("UTF-8"))
194                         except KeyError, ke:
195                                 pass
196
197                 # Read out max length (V4+)
198                 if timer.hasAttribute("maxduration"):
199                         maxlen = timer.getAttribute("maxduration") or None
200                         if maxlen:
201                                 maxlen = int(maxlen)*60
202                 # V3-
203                 else:
204                         elements = timer.getElementsByTagName("maxduration")
205                         if len(elements):
206                                 maxlen = getValue(elements, None)
207                                 if maxlen is not None:
208                                         maxlen = int(maxlen)*60
209                         else:
210                                 maxlen = None
211
212                 # Read out recording path
213                 destination = timer.getAttribute("destination").encode("UTF-8") or None
214
215                 # Read out recording tags (needs my enhanced tag support patch)
216                 tags = []
217                 for tag in timer.getElementsByTagName("tag"):
218                         value = getValue(tag, None)
219                         if not value:
220                                 continue
221
222                         tags.append(value.encode("UTF-8"))
223
224                 # Finally append timer
225                 list.append(AutoTimerComponent(
226                                 uniqueTimerId,
227                                 name,
228                                 match,
229                                 enabled,
230                                 timespan = timetuple,
231                                 services = servicelist,
232                                 offset = offset,
233                                 afterevent = afterevent,
234                                 exclude = excludes,
235                                 include = includes,
236                                 maxduration = maxlen,
237                                 destination = destination,
238                                 matchCount = counter,
239                                 matchLeft = counterLeft,
240                                 matchLimit = counterLimit,
241                                 matchFormatString = counterFormat,
242                                 lastBegin = lastBegin,
243                                 justplay = justplay,
244                                 avoidDuplicateDescription = avoidDuplicateDescription,
245                                 bouquets = bouquets,
246                                 tags = tags
247                 ))