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)
25 return self.__class__(
30 timespan = self.timespan,
31 services = self.services,
33 afterevent = self.afterevent,
34 exclude = (self.getExcludedTitle(), self.getExcludedShort(), self.getExcludedDescription(), self.getExcludedDays()),
35 include = (self.getIncludedTitle(), self.getIncludedShort(), self.getIncludedDescription(), self.getIncludedDays()),
36 maxduration = self.maxduration,
37 destination = self.destination,
38 matchCount = self.matchCount,
39 matchLeft = self.matchLeft,
40 matchLimit = self.matchLimit,
41 matchFormatString = self.matchFormatString,
42 lastBegin = self.lastBegin,
43 justplay = self.justplay,
44 avoidDuplicateDescription = self.avoidDuplicateDescription,
45 bouquets = self.bouquets,
49 def __deepcopy__(self, memo):
50 return self.__class__(
55 timespan = self.timespan,
56 services = self.services[:],
57 offset = self.offset and self.offset[:],
58 afterevent = self.afterevent[:],
59 exclude = (self.getExcludedTitle(), self.getExcludedShort(), self.getExcludedDescription(), self.exclude[3][:]),
60 include = (self.getIncludedTitle(), self.getIncludedShort(), self.getIncludedDescription(), self.include[3][:]),
61 maxduration = self.maxduration,
62 destination = self.destination,
63 matchCount = self.matchCount,
64 matchLeft = self.matchLeft,
65 matchLimit = self.matchLimit,
66 matchFormatString = self.matchFormatString,
67 lastBegin = self.lastBegin,
68 justplay = self.justplay,
69 avoidDuplicateDescription = self.avoidDuplicateDescription,
70 bouquets = self.bouquets[:],
75 return self.__deepcopy__({})
77 def setValues(self, name, match, enabled, timespan = None, services = None, offset = None, \
78 afterevent = [], exclude = None, maxduration = None, destination = None, \
79 include = None, matchCount = 0, matchLeft = 0, matchLimit = '', matchFormatString = '', \
80 lastBegin = 0, justplay = False, avoidDuplicateDescription = False, bouquets = None, \
84 self.timespan = timespan
85 self.services = services
87 self.afterevent = afterevent
88 self.exclude = exclude
89 self.include = include
90 self.maxduration = maxduration
91 self.enabled = enabled
92 self.destination = destination
93 self.matchCount = matchCount
94 self.matchLeft = matchLeft
95 self.matchLimit = matchLimit
96 self.matchFormatString = matchFormatString
97 self.lastBegin = lastBegin
98 self.justplay = justplay
99 self.avoidDuplicateDescription = avoidDuplicateDescription
100 self.bouquets = bouquets
101 self.tags = tags or []
106 def setName(self, name):
112 def setMatch(self, match):
118 def calculateDayspan(self, begin, end, ignore = None):
119 if end[0] < begin[0] or (end[0] == begin[0] and end[1] <= begin[1]):
120 return (begin, end, True)
122 return (begin, end, False)
124 def setTimespan(self, timespan):
125 if timespan is None or len(timespan) and timespan[0] is None:
126 self._timespan = (None,)
128 self._timespan = self.calculateDayspan(*timespan)
130 def getTimespan(self):
131 return self._timespan
133 timespan = property(getTimespan, setTimespan)
135 def setExclude(self, exclude):
138 [re_compile(x) for x in exclude[0]],
139 [re_compile(x) for x in exclude[1]],
140 [re_compile(x) for x in exclude[2]],
144 self._exclude = ([], [], [], [])
146 def getExclude(self):
149 exclude = property(getExclude, setExclude)
151 def setInclude(self, include):
154 [re_compile(x) for x in include[0]],
155 [re_compile(x) for x in include[1]],
156 [re_compile(x) for x in include[2]],
160 self._include = ([], [], [], [])
162 def getInclude(self):
165 include = property(getInclude, setInclude)
167 def setServices(self, services):
169 self._services = services
173 def getServices(self):
174 return self._services
176 services = property(getServices, setServices)
178 def setBouquets(self, bouquets):
180 self._bouquets = bouquets
184 def getBouquets(self):
185 return self._bouquets
187 bouquets = property(getBouquets, setBouquets)
189 def setAfterEvent(self, afterevent):
190 del self._afterevent[:]
192 for definition in afterevent:
193 action, timespan = definition
195 self._afterevent.append((action, (None,)))
197 self._afterevent.append((action, self.calculateDayspan(*timespan)))
199 def getCompleteAfterEvent(self):
200 return self._afterevent
202 afterevent = property(getCompleteAfterEvent, setAfterEvent)
204 def hasTimespan(self):
205 return self.timespan[0] is not None
207 def getTimespanBegin(self):
208 return '%02d:%02d' % (self.timespan[0][0], self.timespan[0][1])
210 def getTimespanEnd(self):
211 return '%02d:%02d' % (self.timespan[1][0], self.timespan[1][1])
213 def checkAnyTimespan(self, time, begin = None, end = None, haveDayspan = False):
217 # Check if we span a day
219 # Check if begin of event is later than our timespan starts
220 if time.tm_hour > begin[0] or (time.tm_hour == begin[0] and time.tm_min >= begin[1]):
221 # If so, event is in our timespan
223 # Check if begin of event is earlier than our timespan end
224 if time.tm_hour < end[0] or (time.tm_hour == end[0] and time.tm_min <= end[1]):
225 # If so, event is in our timespan
229 # Check if event begins earlier than our timespan starts
230 if time.tm_hour < begin[0] or (time.tm_hour == begin[0] and time.tm_min < begin[1]):
231 # Its out of our timespan then
233 # Check if event begins later than our timespan ends
234 if time.tm_hour > end[0] or (time.tm_hour == end[0] and time.tm_min > end[1]):
235 # Its out of our timespan then
239 def checkTimespan(self, begin):
240 return self.checkAnyTimespan(begin, *self.timespan)
242 def hasDuration(self):
243 return self.maxduration is not None
245 def getDuration(self):
246 return self.maxduration/60
248 def checkDuration(self, length):
249 if self.maxduration is None:
251 return length > self.maxduration
253 def getFullServices(self):
254 list = self.services[:]
256 from enigma import eServiceReference, eServiceCenter
257 serviceHandler = eServiceCenter.getInstance()
258 for bouquet in self.bouquets:
259 myref = eServiceReference(str(bouquet))
260 mylist = serviceHandler.list(myref)
261 if mylist is not None:
264 # TODO: I wonder if its sane to assume we get services here (and not just new lists)
265 # We can ignore markers & directorys here because they won't match any event's service :-)
267 # strip all after last :
269 pos = value.rfind(':')
271 value = value[:pos+1]
279 def checkServices(self, service):
280 if len(self.services) or len(self.bouquets):
281 return service not in self.getFullServices()
284 def getExcludedTitle(self):
285 return [x.pattern for x in self.exclude[0]]
287 def getExcludedShort(self):
288 return [x.pattern for x in self.exclude[1]]
290 def getExcludedDescription(self):
291 return [x.pattern for x in self.exclude[2]]
293 def getExcludedDays(self):
294 return self.exclude[3]
296 def getIncludedTitle(self):
297 return [x.pattern for x in self.include[0]]
299 def getIncludedShort(self):
300 return [x.pattern for x in self.include[1]]
302 def getIncludedDescription(self):
303 return [x.pattern for x in self.include[2]]
305 def getIncludedDays(self):
306 return self.include[3]
308 def checkExcluded(self, title, short, extended, dayofweek):
309 if len(self.exclude[3]):
310 list = [x for x in self.exclude[3]]
311 if "weekend" in list:
312 list.extend(["5", "6"])
313 if "weekday" in list:
314 list.extend(["0", "1", "2", "3", "4"])
315 if dayofweek in list:
318 for exclude in self.exclude[0]:
319 if exclude.search(title):
321 for exclude in self.exclude[1]:
322 if exclude.search(short):
324 for exclude in self.exclude[2]:
325 if exclude.search(extended):
329 def checkIncluded(self, title, short, extended, dayofweek):
330 if len(self.include[3]):
331 list = [x for x in self.include[3]]
332 if "weekend" in list:
333 list.extend(["5", "6"])
334 if "weekday" in list:
335 list.extend(["0", "1", "2", "3", "4"])
336 if dayofweek not in list:
339 for include in self.include[0]:
340 if not include.search(title):
342 for include in self.include[1]:
343 if not include.search(short):
345 for include in self.include[2]:
346 if not include.search(extended):
351 def checkFilter(self, title, short, extended, dayofweek):
352 if self.checkExcluded(title, short, extended, dayofweek):
355 return self.checkIncluded(title, short, extended, dayofweek)
358 return self.offset is not None
360 def isOffsetEqual(self):
361 return self.offset[0] == self.offset[1]
363 def applyOffset(self, begin, end):
364 if self.offset is None:
366 return (begin - self.offset[0], end + self.offset[1])
368 def getOffsetBegin(self):
369 return self.offset[0]/60
371 def getOffsetEnd(self):
372 return self.offset[1]/60
374 def hasAfterEvent(self):
375 return len(self.afterevent)
377 def hasAfterEventTimespan(self):
378 for afterevent in self.afterevent:
379 if afterevent[1][0] is not None:
383 def getAfterEventTimespan(self, end):
384 for afterevent in self.afterevent:
385 if not self.checkAnyTimespan(end, *afterevent[1]):
389 def getAfterEvent(self):
390 for afterevent in self.afterevent:
391 if afterevent[1][0] is None:
395 def getEnabled(self):
396 return self.enabled and "yes" or "no"
398 def getJustplay(self):
399 return self.justplay and "1" or "0"
401 def hasDestination(self):
402 return self.destination is not None
404 def getDestination(self):
405 return self.destination is not None
407 def hasCounter(self):
408 return self.matchCount != 0
410 def hasCounterFormatString(self):
411 return self.matchFormatString != ''
413 def getCounter(self):
414 return self.matchCount
416 def getCounterLeft(self):
417 return self.matchLeft
419 def getCounterLimit(self):
420 return self.matchLimit
422 def getCounterFormatString(self):
423 return self.matchFormatString
425 def checkCounter(self, timestamp):
426 # 0-Count is considered "unset"
427 if self.matchCount == 0:
430 # Check if event is in current timespan (we can only manage one!)
431 limit = strftime(self.matchFormatString, timestamp)
432 if limit != self.matchLimit:
435 if self.matchLeft > 0:
440 def update(self, begin, timestamp):
441 # Only update limit when we have new begin
442 if begin > self.lastBegin:
443 self.lastBegin = begin
446 # %m is Month, %U is week (sunday), %W is week (monday)
447 newLimit = strftime(self.matchFormatString, timestamp)
449 if newLimit != self.matchLimit:
450 self.matchLeft = self.matchCount
451 self.matchLimit = newLimit
453 def getLastBegin(self):
454 return self.lastBegin
456 def getAvoidDuplicateDescription(self):
457 return self.avoidDuplicateDescription
460 return len(self.tags) > 0
475 str(self.afterevent),
476 str(([x.pattern for x in self.exclude[0]],
477 [x.pattern for x in self.exclude[1]],
478 [x.pattern for x in self.exclude[2]],
481 str(([x.pattern for x in self.include[0]],
482 [x.pattern for x in self.include[1]],
483 [x.pattern for x in self.include[2]],
486 str(self.maxduration),
488 str(self.destination),
489 str(self.matchCount),
491 str(self.matchLimit),
492 str(self.matchFormatString),
495 str(self.avoidDuplicateDescription),