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"""
13 def __init__(self, id, name, match, enabled, *args, **kwargs):
16 self.setValues(name, match, enabled, *args, **kwargs)
21 def clear(self, id = -1, enabled = False):
23 self.setValues('', '', enabled)
26 Create a deep copy of this instance
29 return self.__deepcopy__({})
38 Keeps init small and helps setting many values at once
40 def setValues(self, name, match, enabled, timespan = None, services = None, offset = None, \
41 afterevent = [], exclude = None, maxduration = None, destination = None, \
42 include = None, matchCount = 0, matchLeft = 0, matchLimit = '', matchFormatString = '', \
43 lastBegin = 0, justplay = False, avoidDuplicateDescription = 0, bouquets = None, \
47 self.enabled = enabled
48 self.timespan = timespan
49 self.services = services
51 self.afterevent = afterevent
52 self.exclude = exclude
53 self.maxduration = maxduration
54 self.destination = destination
55 self.include = include
56 self.matchCount = matchCount
57 self.matchLeft = matchLeft
58 self.matchLimit = matchLimit
59 self.matchFormatString = matchFormatString
60 self.lastBegin = lastBegin
61 self.justplay = justplay
62 self.avoidDuplicateDescription = avoidDuplicateDescription
63 self.bouquets = bouquets
64 self.tags = tags or []
66 ### Attributes / Properties
68 def getCompleteAfterEvent(self):
69 return self._afterevent
71 def setAfterEvent(self, afterevent):
72 del self._afterevent[:]
74 for definition in afterevent:
75 action, timespan = definition
77 self._afterevent.append((action, (None,)))
79 self._afterevent.append((action, self.calculateDayspan(*timespan)))
81 afterevent = property(getCompleteAfterEvent, setAfterEvent)
83 def setBouquets(self, bouquets):
85 self._bouquets = bouquets
89 def getBouquets(self):
92 bouquets = property(getBouquets, setBouquets)
94 def setExclude(self, exclude):
97 [re_compile(x) for x in exclude[0]],
98 [re_compile(x) for x in exclude[1]],
99 [re_compile(x) for x in exclude[2]],
103 self._exclude = ([], [], [], [])
105 def getExclude(self):
108 exclude = property(getExclude, setExclude)
110 def setInclude(self, include):
113 [re_compile(x) for x in include[0]],
114 [re_compile(x) for x in include[1]],
115 [re_compile(x) for x in include[2]],
119 self._include = ([], [], [], [])
121 def getInclude(self):
124 include = property(getInclude, setInclude)
126 def setName(self, name):
132 def setMatch(self, match):
133 # XXX: a sanity check might be useful...
136 match = property(getMatch, setMatch)
138 def getServices(self):
139 return self._services
141 def setServices(self, services):
143 self._services = services
147 services = property(getServices, setServices)
149 def getTimespan(self):
150 return self._timespan
152 def setTimespan(self, timespan):
153 if timespan is None or len(timespan) and timespan[0] is None:
154 self._timespan = (None,)
156 self._timespan = self.calculateDayspan(*timespan)
158 timespan = property(getTimespan, setTimespan)
160 ### See if Attributes are set
162 def hasAfterEvent(self):
163 return len(self.afterevent)
165 def hasAfterEventTimespan(self):
166 for afterevent in self.afterevent:
167 if afterevent[1][0] is not None:
171 def hasCounter(self):
172 return self.matchCount != 0
174 def hasCounterFormatString(self):
175 return self.matchFormatString != ''
177 def hasDestination(self):
178 return self.destination is not None
180 def hasDuration(self):
181 return self.maxduration is not None
184 return len(self.tags) > 0
186 def hasTimespan(self):
187 return self.timespan[0] is not None
190 return self.offset is not None
195 Returns a tulple of (input begin, input end, begin earlier than end)
197 def calculateDayspan(self, begin, end, ignore = None):
198 if end[0] < begin[0] or (end[0] == begin[0] and end[1] <= begin[1]):
199 return (begin, end, True)
201 return (begin, end, False)
204 Returns if a given timestruct is in a timespan
206 def checkAnyTimespan(self, time, begin = None, end = None, haveDayspan = False):
210 # Check if we span a day
212 # Check if begin of event is later than our timespan starts
213 if time.tm_hour > begin[0] or (time.tm_hour == begin[0] and time.tm_min >= begin[1]):
214 # If so, event is in our timespan
216 # Check if begin of event is earlier than our timespan end
217 if time.tm_hour < end[0] or (time.tm_hour == end[0] and time.tm_min <= end[1]):
218 # If so, event is in our timespan
222 # Check if event begins earlier than our timespan starts
223 if time.tm_hour < begin[0] or (time.tm_hour == begin[0] and time.tm_min < begin[1]):
224 # Its out of our timespan then
226 # Check if event begins later than our timespan ends
227 if time.tm_hour > end[0] or (time.tm_hour == end[0] and time.tm_min > end[1]):
228 # Its out of our timespan then
233 Returns a list of all allowed services by listing the bouquets
235 def getFullServices(self):
236 list = self.services[:]
238 from enigma import eServiceReference, eServiceCenter
239 serviceHandler = eServiceCenter.getInstance()
240 for bouquet in self.bouquets:
241 myref = eServiceReference(str(bouquet))
242 mylist = serviceHandler.list(myref)
243 if mylist is not None:
246 # TODO: I wonder if its sane to assume we get services here (and not just new lists)
247 # We can ignore markers & directorys here because they won't match any event's service :-)
249 # strip all after last :
251 pos = value.rfind(':')
253 value = value[:pos+1]
262 Called when a timer based on this component was added
264 def update(self, begin, timestamp):
265 # Only update limit when we have new begin
266 if begin > self.lastBegin:
267 self.lastBegin = begin
270 # %m is Month, %U is week (sunday), %W is week (monday)
271 newLimit = strftime(self.matchFormatString, timestamp)
273 if newLimit != self.matchLimit:
274 self.matchLeft = self.matchCount
275 self.matchLimit = newLimit
277 ### Makes saving Config easier
279 def getAvoidDuplicateDescription(self):
280 return self.avoidDuplicateDescription
282 def getCounter(self):
283 return self.matchCount
285 def getCounterFormatString(self):
286 return self.matchFormatString
288 def getCounterLeft(self):
289 return self.matchLeft
291 def getCounterLimit(self):
292 return self.matchLimit
294 def getDestination(self):
295 # XXX: as this function was not added by me (ritzMo) i'll leave it like this but i'm not really sure if this is right ;-)
296 return self.destination is not None
298 def getDuration(self):
299 return self.maxduration/60
301 def getEnabled(self):
302 return self.enabled and "yes" or "no"
304 def getExcludedDays(self):
305 return self.exclude[3]
307 def getExcludedDescription(self):
308 return [x.pattern for x in self.exclude[2]]
310 def getExcludedShort(self):
311 return [x.pattern for x in self.exclude[1]]
313 def getExcludedTitle(self):
314 return [x.pattern for x in self.exclude[0]]
316 def getIncludedTitle(self):
317 return [x.pattern for x in self.include[0]]
319 def getIncludedShort(self):
320 return [x.pattern for x in self.include[1]]
322 def getIncludedDescription(self):
323 return [x.pattern for x in self.include[2]]
325 def getIncludedDays(self):
326 return self.include[3]
328 def getJustplay(self):
329 return self.justplay and "1" or "0"
331 def getLastBegin(self):
332 return self.lastBegin
337 def getOffsetBegin(self):
338 return self.offset[0]/60
340 def getOffsetEnd(self):
341 return self.offset[1]/60
346 def getTimespanBegin(self):
347 return '%02d:%02d' % (self.timespan[0][0], self.timespan[0][1])
349 def getTimespanEnd(self):
350 return '%02d:%02d' % (self.timespan[1][0], self.timespan[1][1])
352 def isOffsetEqual(self):
353 return self.offset[0] == self.offset[1]
355 ### Actual functionality
357 def applyOffset(self, begin, end):
358 if self.offset is None:
360 return (begin - self.offset[0], end + self.offset[1])
362 def checkCounter(self, timestamp):
363 # 0-Count is considered "unset"
364 if self.matchCount == 0:
367 # Check if event is in current timespan (we can only manage one!)
368 limit = strftime(self.matchFormatString, timestamp)
369 if limit != self.matchLimit:
372 if self.matchLeft > 0:
377 def checkDuration(self, length):
378 if self.maxduration is None:
380 return length > self.maxduration
382 def checkExcluded(self, title, short, extended, dayofweek):
383 if len(self.exclude[3]):
384 list = [x for x in self.exclude[3]]
385 if "weekend" in list:
386 list.extend(["5", "6"])
387 if "weekday" in list:
388 list.extend(["0", "1", "2", "3", "4"])
389 if dayofweek in list:
392 for exclude in self.exclude[0]:
393 if exclude.search(title):
395 for exclude in self.exclude[1]:
396 if exclude.search(short):
398 for exclude in self.exclude[2]:
399 if exclude.search(extended):
403 def checkFilter(self, title, short, extended, dayofweek):
404 if self.checkExcluded(title, short, extended, dayofweek):
407 return self.checkIncluded(title, short, extended, dayofweek)
409 def checkIncluded(self, title, short, extended, dayofweek):
410 if len(self.include[3]):
411 list = [x for x in self.include[3]]
412 if "weekend" in list:
413 list.extend(["5", "6"])
414 if "weekday" in list:
415 list.extend(["0", "1", "2", "3", "4"])
416 if dayofweek not in list:
419 for include in self.include[0]:
420 if not include.search(title):
422 for include in self.include[1]:
423 if not include.search(short):
425 for include in self.include[2]:
426 if not include.search(extended):
431 def checkServices(self, service):
432 if len(self.services) or len(self.bouquets):
433 return service not in self.getFullServices()
436 def checkTimespan(self, begin):
437 return self.checkAnyTimespan(begin, *self.timespan)
439 def getAfterEvent(self):
440 for afterevent in self.afterevent:
441 if afterevent[1][0] is None:
445 def getAfterEventTimespan(self, end):
446 for afterevent in self.afterevent:
447 if not self.checkAnyTimespan(end, *afterevent[1]):
454 return self.__class__(
459 timespan = self.timespan,
460 services = self.services,
461 offset = self.offset,
462 afterevent = self.afterevent,
463 exclude = (self.getExcludedTitle(), self.getExcludedShort(), self.getExcludedDescription(), self.getExcludedDays()),
464 maxduration = self.maxduration,
465 destination = self.destination,
466 include = (self.getIncludedTitle(), self.getIncludedShort(), self.getIncludedDescription(), self.getIncludedDays()),
467 matchCount = self.matchCount,
468 matchLeft = self.matchLeft,
469 matchLimit = self.matchLimit,
470 matchFormatString = self.matchFormatString,
471 lastBegin = self.lastBegin,
472 justplay = self.justplay,
473 avoidDuplicateDescription = self.avoidDuplicateDescription,
474 bouquets = self.bouquets,
478 def __deepcopy__(self, memo):
479 return self.__class__(
484 timespan = self.timespan,
485 services = self.services[:],
486 offset = self.offset and self.offset[:],
487 afterevent = self.afterevent[:],
488 exclude = (self.getExcludedTitle(), self.getExcludedShort(), self.getExcludedDescription(), self.exclude[3][:]),
489 maxduration = self.maxduration,
490 destination = self.destination,
491 include = (self.getIncludedTitle(), self.getIncludedShort(), self.getIncludedDescription(), self.include[3][:]),
492 matchCount = self.matchCount,
493 matchLeft = self.matchLeft,
494 matchLimit = self.matchLimit,
495 matchFormatString = self.matchFormatString,
496 lastBegin = self.lastBegin,
497 justplay = self.justplay,
498 avoidDuplicateDescription = self.avoidDuplicateDescription,
499 bouquets = self.bouquets[:],
503 def __eq__(self, other):
504 if isinstance(other, AutoTimerComponent):
505 return self.id == other.id
508 def __lt__(self, other):
509 if isinstance(other, AutoTimerComponent):
510 return self.name.lower() < other.name.lower()
513 def __ne__(self, other):
514 return not self.__eq__(other)
526 str(self.afterevent),
527 str(([x.pattern for x in self.exclude[0]],
528 [x.pattern for x in self.exclude[1]],
529 [x.pattern for x in self.exclude[2]],
532 str(([x.pattern for x in self.include[0]],
533 [x.pattern for x in self.include[1]],
534 [x.pattern for x in self.include[2]],
537 str(self.maxduration),
539 str(self.destination),
540 str(self.matchCount),
542 str(self.matchLimit),
543 str(self.matchFormatString),
546 str(self.avoidDuplicateDescription),