2 from time import strftime
5 from re import compile as re_compile
7 class AutoTimerComponent(object):
8 """AutoTimer Component which also handles validity checks"""
10 def __init__(self, id, *args, **kwargs):
13 self.setValues(*args, **kwargs)
15 def __eq__(self, other):
17 return self.id == other.id
18 except AttributeError:
21 def __ne__(self, other):
22 return not self.__eq__(other)
24 def setValues(self, name, match, enabled, timespan = None, services = None, offset = None, \
25 afterevent = [], exclude = None, maxduration = None, destination = None, \
26 include = None, matchCount = 0, matchLeft = 0, matchLimit = '', matchFormatString = '', \
27 lastBegin = 0, justplay = False, avoidDuplicateDescription = False, bouquets = None, \
31 self.timespan = timespan
32 self.services = services
34 self.afterevent = afterevent
35 self.exclude = exclude
36 self.include = include
37 self.maxduration = maxduration
38 self.enabled = enabled
39 self.destination = destination
40 self.matchCount = matchCount
41 self.matchLeft = matchLeft
42 self.matchLimit = matchLimit
43 self.matchFormatString = matchFormatString
44 self.lastBegin = lastBegin
45 self.justplay = justplay
46 self.avoidDuplicateDescription = avoidDuplicateDescription
47 self.bouquets = bouquets
48 self.tags = tags or []
50 def calculateDayspan(self, begin, end):
51 if end[0] < begin[0] or (end[0] == begin[0] and end[1] <= begin[1]):
52 return (begin, end, True)
54 return (begin, end, False)
56 def setTimespan(self, timespan):
58 self._timespan = (None,)
60 self._timespan = self.calculateDayspan(*timespan)
62 def getTimespan(self):
65 timespan = property(getTimespan, setTimespan)
67 def setExclude(self, exclude):
70 [re_compile(x) for x in exclude[0]],
71 [re_compile(x) for x in exclude[1]],
72 [re_compile(x) for x in exclude[2]],
76 self._exclude = ([], [], [], [])
81 exclude = property(getExclude, setExclude)
83 def setInclude(self, include):
86 [re_compile(x) for x in include[0]],
87 [re_compile(x) for x in include[1]],
88 [re_compile(x) for x in include[2]],
92 self._include = ([], [], [], [])
97 include = property(getInclude, setInclude)
99 def setServices(self, services):
101 self._services = services
105 def getServices(self):
106 return self._services
108 services = property(getServices, setServices)
110 def setBouquets(self, bouquets):
112 self._bouquets = bouquets
116 def getBouquets(self):
117 return self._bouquets
119 bouquets = property(getBouquets, setBouquets)
121 def setAfterEvent(self, afterevent):
122 del self._afterevent[:]
124 for definition in afterevent:
125 action, timespan = definition
127 self._afterevent.append((action, (None,)))
129 self._afterevent.append((action, self.calculateDayspan(*timespan)))
131 def getCompleteAfterEvent(self):
132 return self._afterevent
134 afterevent = property(getCompleteAfterEvent, setAfterEvent)
136 def hasTimespan(self):
137 return self.timespan[0] is not None
139 def getTimespanBegin(self):
140 return '%02d:%02d' % (self.timespan[0][0], self.timespan[0][1])
142 def getTimespanEnd(self):
143 return '%02d:%02d' % (self.timespan[1][0], self.timespan[1][1])
145 def checkAnyTimespan(self, time, begin = None, end = None, haveDayspan = False):
149 # Check if we span a day
151 # Check if begin of event is later than our timespan starts
152 if time.tm_hour > begin[0] or (time.tm_hour == begin[0] and time.tm_min >= begin[1]):
153 # If so, event is in our timespan
155 # Check if begin of event is earlier than our timespan end
156 if time.tm_hour < end[0] or (time.tm_hour == end[0] and time.tm_min <= end[1]):
157 # If so, event is in our timespan
161 # Check if event begins earlier than our timespan starts
162 if time.tm_hour < begin[0] or (time.tm_hour == begin[0] and time.tm_min < begin[1]):
163 # Its out of our timespan then
165 # Check if event begins later than our timespan ends
166 if time.tm_hour > end[0] or (time.tm_hour == end[0] and time.tm_min > end[1]):
167 # Its out of our timespan then
171 def checkTimespan(self, begin):
172 return self.checkAnyTimespan(begin, *self.timespan)
174 def hasDuration(self):
175 return self.maxduration is not None
177 def getDuration(self):
178 return self.maxduration/60
180 def checkDuration(self, length):
181 if self.maxduration is None:
183 return length > self.maxduration
185 def getFullServices(self):
186 list = self.services[:]
188 from enigma import eServiceReference, eServiceCenter
189 serviceHandler = eServiceCenter.getInstance()
190 for bouquet in self.bouquets:
191 myref = eServiceReference(str(bouquet))
192 mylist = serviceHandler.list(myref)
193 if mylist is not None:
196 # TODO: I wonder if its sane to assume we get services here (and not just new lists)
197 # We can ignore markers & directorys here because they won't match any event's service :-)
199 # strip all after last :
201 pos = value.rfind(':')
203 value = value[:pos+1]
211 def checkServices(self, service):
212 if len(self.services) or len(self.bouquets):
213 return service not in self.getFullServices()
216 def getExcludedTitle(self):
217 return [x.pattern for x in self.exclude[0]]
219 def getExcludedShort(self):
220 return [x.pattern for x in self.exclude[1]]
222 def getExcludedDescription(self):
223 return [x.pattern for x in self.exclude[2]]
225 def getExcludedDays(self):
226 return self.exclude[3]
228 def getIncludedTitle(self):
229 return [x.pattern for x in self.include[0]]
231 def getIncludedShort(self):
232 return [x.pattern for x in self.include[1]]
234 def getIncludedDescription(self):
235 return [x.pattern for x in self.include[2]]
237 def getIncludedDays(self):
238 return self.include[3]
240 def checkExcluded(self, title, short, extended, dayofweek):
241 if len(self.exclude[3]):
242 list = [x for x in self.exclude[3]]
243 if "weekend" in list:
244 list.extend(["5", "6"])
245 if "weekday" in list:
246 list.extend(["0", "1", "2", "3", "4"])
247 if dayofweek in list:
250 for exclude in self.exclude[0]:
251 if exclude.search(title):
253 for exclude in self.exclude[1]:
254 if exclude.search(short):
256 for exclude in self.exclude[2]:
257 if exclude.search(extended):
261 def checkIncluded(self, title, short, extended, dayofweek):
262 if len(self.include[3]):
263 list = [x for x in self.include[3]]
264 if "weekend" in list:
265 list.extend(["5", "6"])
266 if "weekday" in list:
267 list.extend(["0", "1", "2", "3", "4"])
268 if dayofweek not in list:
271 for include in self.include[0]:
272 if not include.search(title):
274 for include in self.include[1]:
275 if not include.search(short):
277 for include in self.include[2]:
278 if not include.search(extended):
283 def checkFilter(self, title, short, extended, dayofweek):
284 if self.checkExcluded(title, short, extended, dayofweek):
287 return self.checkIncluded(title, short, extended, dayofweek)
290 return self.offset is not None
292 def isOffsetEqual(self):
293 return self.offset[0] == self.offset[1]
295 def applyOffset(self, begin, end):
296 if self.offset is None:
298 return (begin - self.offset[0], end + self.offset[1])
300 def getOffsetBegin(self):
301 return self.offset[0]/60
303 def getOffsetEnd(self):
304 return self.offset[1]/60
306 def hasAfterEvent(self):
307 return len(self.afterevent)
309 def hasAfterEventTimespan(self):
310 for afterevent in self.afterevent:
311 if afterevent[1][0] is not None:
315 def getAfterEventTimespan(self, end):
316 for afterevent in self.afterevent:
317 if not self.checkAnyTimespan(end, *afterevent[1]):
321 def getAfterEvent(self):
322 for afterevent in self.afterevent:
323 if afterevent[1][0] is None:
327 def getEnabled(self):
328 return self.enabled and "yes" or "no"
330 def getJustplay(self):
331 return self.justplay and "1" or "0"
333 def hasDestination(self):
334 return self.destination is not None
336 def hasCounter(self):
337 return self.matchCount != 0
339 def hasCounterFormatString(self):
340 return self.matchFormatString != ''
342 def getCounter(self):
343 return self.matchCount
345 def getCounterLeft(self):
346 return self.matchLeft
348 def getCounterLimit(self):
349 return self.matchLimit
351 def getCounterFormatString(self):
352 return self.matchFormatString
354 def checkCounter(self, timestamp):
355 # 0-Count is considered "unset"
356 if self.matchCount == 0:
359 # Check if event is in current timespan (we can only manage one!)
360 limit = strftime(self.matchFormatString, timestamp)
361 if limit != self.matchLimit:
364 if self.matchLeft > 0:
369 def update(self, begin, timestamp):
370 # Only update limit when we have new begin
371 if begin > self.lastBegin:
372 self.lastBegin = begin
375 # %m is Month, %U is week (sunday), %W is week (monday)
376 newLimit = strftime(self.matchFormatString, timestamp)
378 if newLimit != self.matchLimit:
379 self.matchLeft = self.matchCount
380 self.matchLimit = newLimit
382 def getLastBegin(self):
383 return self.lastBegin
385 def getAvoidDuplicateDescription(self):
386 return self.avoidDuplicateDescription
389 return len(self.tags) > 0
401 str(self.afterevent),
403 str(self.maxduration),
405 str(self.destination),
406 str(self.matchCount),
408 str(self.matchLimit),
409 str(self.matchFormatString),