Configuration parser factored out to AutoTimerConfiguration, adjust to previous commit...
[enigma2-plugins.git] / autotimer / src / AutoTimer.py
1 # Plugins Config
2 from xml.dom.minidom import parse as minidom_parse
3 from os import path as os_path
4 from AutoTimerConfiguration import parseConfig, writeConfig
5
6 # Navigation (RecordTimer)
7 import NavigationInstance
8
9 # Timer
10 from ServiceReference import ServiceReference
11 from RecordTimer import RecordTimerEntry
12 from Components.TimerSanityCheck import TimerSanityCheck
13
14 # Timespan
15 from time import localtime, time
16
17 # EPGCache & Event
18 from enigma import eEPGCache, eServiceReference
19
20 # Enigma2 Config
21 from Components.config import config
22
23 # AutoTimer Component
24 from AutoTimerComponent import AutoTimerComponent
25
26 XML_CONFIG = "/etc/enigma2/autotimer.xml"
27
28 def getTimeDiff(timer, begin, end):
29         if begin <= timer.begin <= end:
30                 return end - timer.begin
31         elif timer.begin <= begin <= timer.end:
32                 return timer.end - begin
33         return 0
34
35 class AutoTimerIgnoreTimerException(Exception):
36         def __init__(self, cause):
37                 self.cause = cause
38
39         def __str__(self):
40                 return "[AutoTimer] " + str(self.cause)
41
42         def __repr__(self):
43                 return str(type(self))
44
45 class AutoTimer:
46         """Read and save xml configuration, query EPGCache"""
47
48         def __init__(self):
49                 # Keep EPGCache
50                 self.epgcache = eEPGCache.getInstance()
51
52                 # Initialize
53                 self.timers = []
54                 self.configMtime = -1
55                 self.uniqueTimerId = 0
56                 self.defaultTimer = AutoTimerComponent(
57                         0,              # Id
58                         "",             # Name
59                         "",             # Match
60                         True    # Enabled
61                 )
62
63 # Configuration
64
65         def readXml(self):
66                 # Abort if no config found
67                 if not os_path.exists(XML_CONFIG):
68                         print "[AutoTimer] No configuration file present"
69                         return
70
71                 # Parse if mtime differs from whats saved
72                 mtime = os_path.getmtime(XML_CONFIG)
73                 if mtime == self.configMtime:
74                         print "[AutoTimer] No changes in configuration, won't parse"
75                         return
76
77                 # Save current mtime
78                 self.configMtime = mtime
79
80                 # Parse Config
81                 dom = minidom_parse(XML_CONFIG)
82                 
83                 # Empty out timers and reset Ids
84                 del self.timers[:]
85                 self.uniqueTimerId = 0
86                 self.defaultTimer.clear(-1, True)
87
88                 # Get Config Element
89                 for configuration in dom.getElementsByTagName("autotimer"):
90                         parseConfig(
91                                 configuration,
92                                 self.timers,
93                                 configuration.getAttribute("version"),
94                                 self.uniqueTimerId,
95                                 self.defaultTimer
96                         )
97                         self.uniqueTimerId = len(self.timers)
98
99         def writeXml(self):
100                 writeConfig(XML_CONFIG, self.defaultTimer, self.timers)
101
102 # Manage List
103
104         def add(self, timer):
105                 self.timers.append(timer)
106
107         def getEnabledTimerList(self):
108                 return [x for x in self.timers if x.enabled]
109
110         def getTimerList(self):
111                 return self.timers
112
113         def getTupleTimerList(self):
114                 return [(x,) for x in self.timers]
115
116         def getUniqueId(self):
117                 self.uniqueTimerId += 1
118                 return self.uniqueTimerId
119
120         def remove(self, uniqueId):
121                 idx = 0
122                 for timer in self.timers:
123                         if timer.id == uniqueId:
124                                 self.timers.pop(idx)
125                                 return
126                         idx += 1
127
128         def set(self, timer):
129                 idx = 0
130                 for stimer in self.timers:
131                         if stimer == timer:
132                                 self.timers[idx] = timer
133                                 return
134                         idx += 1
135                 self.timers.append(timer)
136
137 # Main function
138
139         def parseEPG(self, simulateOnly = False):
140                 if NavigationInstance.instance is None:
141                         print "[AutoTimer] Navigation is not available, can't parse EPG"
142                         return (0, 0, 0, [])
143
144                 total = 0
145                 new = 0
146                 modified = 0
147                 timers = []
148
149                 self.readXml()
150
151                 # Save Recordings in a dict to speed things up a little
152                 # We include processed timers as we might search for duplicate descriptions
153                 recorddict = {}
154                 for timer in NavigationInstance.instance.RecordTimer.timer_list + NavigationInstance.instance.RecordTimer.processed_timers:
155                         if not recorddict.has_key(str(timer.service_ref)):
156                                 recorddict[str(timer.service_ref)] = [timer]
157                         else:
158                                 recorddict[str(timer.service_ref)].append(timer)
159
160                 # Iterate Timer
161                 for timer in self.getEnabledTimerList():
162                         # Search EPG, default to empty list
163                         ret = self.epgcache.search(('RI', 100, eEPGCache.PARTIAL_TITLE_SEARCH, timer.match, eEPGCache.NO_CASE_CHECK)) or []
164
165                         for serviceref, eit in ret:
166                                 eserviceref = eServiceReference(serviceref)
167
168                                 evt = self.epgcache.lookupEventId(eserviceref, eit)
169                                 if not evt:
170                                         print "[AutoTimer] Could not create Event!"
171                                         continue
172
173                                 # Try to determine real service (we always choose the last one)
174                                 n = evt.getNumOfLinkageServices()
175                                 if n > 0:
176                                         i = evt.getLinkageService(eserviceref, n-1)
177                                         serviceref = i.toString()
178
179                                 # Gather Information
180                                 name = evt.getEventName()
181                                 description = evt.getShortDescription()
182                                 begin = evt.getBeginTime()
183                                 duration = evt.getDuration()
184                                 end = begin + duration
185
186                                 # If event starts in less than 60 seconds skip it
187                                 if begin < time() + 60:
188                                         continue
189
190                                 # Convert begin time
191                                 timestamp = localtime(begin)
192
193                                 # Update timer
194                                 timer.update(begin, timestamp)
195
196                                 # Check Duration, Timespan and Excludes
197                                 if timer.checkServices(serviceref) \
198                                         or timer.checkDuration(duration) \
199                                         or timer.checkTimespan(timestamp) \
200                                         or timer.checkFilter(name, description,
201                                                 evt.getExtendedDescription(), str(timestamp.tm_wday)):
202                                         continue
203
204                                 if timer.hasOffset():
205                                         # Apply custom Offset
206                                         begin, end = timer.applyOffset(begin, end)
207                                 else:
208                                         # Apply E2 Offset
209                                         begin -= config.recording.margin_before.value * 60
210                                         end += config.recording.margin_after.value * 60
211
212
213                                 total += 1
214
215                                 # Append to timerlist and abort if simulating
216                                 timers.append((name, begin, end, serviceref, timer.name))
217                                 if simulateOnly:
218                                         continue
219
220                                 # Initialize
221                                 newEntry = None
222
223                                 # Check for double Timers
224                                 # We first check eit and if user wants us to guess event based on time
225                                 # we try this as backup. The allowed diff should be configurable though.
226                                 try:
227                                         for rtimer in recorddict.get(serviceref, []):
228                                                 if rtimer.eit == eit or config.plugins.autotimer.try_guessing.value and getTimeDiff(rtimer, begin, end) > ((duration/10)*8):
229                                                         newEntry = rtimer
230
231                                                         # Abort if we don't want to modify timers or timer is repeated
232                                                         if config.plugins.autotimer.refresh.value == "none" or newEntry.repeated:
233                                                                 raise AutoTimerIgnoreTimerException("Won't modify existing timer because either no modification allowed or repeated timer")
234
235                                                         try:
236                                                                 if newEntry.isAutoTimer:
237                                                                         print "[AutoTimer] Modifying existing AutoTimer!"
238                                                         except AttributeError, ae:
239                                                                 if config.plugins.autotimer.refresh.value != "all":
240                                                                         raise AutoTimerIgnoreTimerException("Won't modify existing timer because it's no timer set by us")
241                                                                 print "[AutoTimer] Warning, we're messing with a timer which might not have been set by us"
242
243                                                         func = NavigationInstance.instance.RecordTimer.timeChanged
244                                                         modified += 1
245
246                                                         # Modify values saved in timer
247                                                         newEntry.name = name
248                                                         newEntry.description = description
249                                                         newEntry.begin = int(begin)
250                                                         newEntry.end = int(end)
251                                                         newEntry.service_ref = ServiceReference(serviceref)
252
253                                                         break
254                                                 elif timer.getAvoidDuplicateDescription() and rtimer.description == description:
255                                                         raise AutoTimerIgnoreTimerException("We found a timer with same description, skipping event")
256
257                                 except AutoTimerIgnoreTimerException, etite:
258                                         print etite
259                                         continue
260
261                                 # Event not yet in Timers
262                                 if newEntry is None:
263                                         if timer.checkCounter(timestamp):
264                                                 continue
265
266                                         new += 1
267
268                                         print "[AutoTimer] Adding an event."
269                                         newEntry = RecordTimerEntry(ServiceReference(serviceref), begin, end, name, description, eit)
270                                         func = NavigationInstance.instance.RecordTimer.record
271
272                                         # Mark this entry as AutoTimer (only AutoTimers will have this Attribute set)
273                                         newEntry.isAutoTimer = True
274
275                                         if not recorddict.has_key(serviceref):
276                                                 recorddict[serviceref] = [newEntry]
277                                         else:
278                                                 recorddict[serviceref].append(newEntry)
279
280                                 # Apply afterEvent
281                                 if timer.hasAfterEvent():
282                                         afterEvent = timer.getAfterEventTimespan(localtime(end))
283                                         if afterEvent is None:
284                                                 afterEvent = timer.getAfterEvent()
285                                         if afterEvent is not None:
286                                                 newEntry.afterEvent = afterEvent
287
288                                 newEntry.dirname = timer.destination
289                                 newEntry.justplay = timer.justplay
290                                 newEntry.tags = timer.tags # This needs my enhanced tag support patch to work
291  
292                                 # Do a sanity check, although it does not do much right now
293                                 timersanitycheck = TimerSanityCheck(NavigationInstance.instance.RecordTimer.timer_list, newEntry)
294                                 if not timersanitycheck.check():
295                                         print "[Autotimer] Sanity check failed"
296                                 else:
297                                         print "[Autotimer] Sanity check passed"
298
299                                 # Either add to List or change time
300                                 func(newEntry)
301
302                 return (total, new, modified, timers)