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):
30 self.timespan = timespan
31 self.services = services
33 self.afterevent = afterevent
34 self.exclude = exclude
35 self.include = include
36 self.maxduration = maxduration
37 self.enabled = enabled
38 self.destination = destination
39 self.matchCount = matchCount
40 self.matchLeft = matchLeft
41 self.matchLimit = matchLimit
42 self.matchFormatString = matchFormatString
43 self.lastBegin = lastBegin
44 self.justplay = justplay
45 self.avoidDuplicateDescription = avoidDuplicateDescription
46 self.bouquets = bouquets
48 def calculateDayspan(self, begin, end):
49 if end[0] < begin[0] or (end[0] == begin[0] and end[1] <= begin[1]):
50 return (begin, end, True)
52 return (begin, end, False)
54 def setTimespan(self, timespan):
56 self._timespan = (None,)
58 self._timespan = self.calculateDayspan(*timespan)
60 def getTimespan(self):
63 timespan = property(getTimespan, setTimespan)
65 def setExclude(self, exclude):
68 [re_compile(x) for x in exclude[0]],
69 [re_compile(x) for x in exclude[1]],
70 [re_compile(x) for x in exclude[2]],
74 self._exclude = ([], [], [], [])
79 exclude = property(getExclude, setExclude)
81 def setInclude(self, include):
84 [re_compile(x) for x in include[0]],
85 [re_compile(x) for x in include[1]],
86 [re_compile(x) for x in include[2]],
90 self._include = ([], [], [], [])
95 include = property(getInclude, setInclude)
97 def setServices(self, services):
99 self._services = services
103 def getServices(self):
104 return self._services
106 services = property(getServices, setServices)
108 def setBouquets(self, bouquets):
110 self._bouquets = bouquets
114 def getBouquets(self):
115 return self._bouquets
117 bouquets = property(getBouquets, setBouquets)
119 def setAfterEvent(self, afterevent):
120 del self._afterevent[:]
122 for definition in afterevent:
123 action, timespan = definition
125 self._afterevent.append((action, (None,)))
127 self._afterevent.append((action, self.calculateDayspan(*timespan)))
129 def getCompleteAfterEvent(self):
130 return self._afterevent
132 afterevent = property(getCompleteAfterEvent, setAfterEvent)
134 def hasTimespan(self):
135 return self.timespan[0] is not None
137 def getTimespanBegin(self):
138 return '%02d:%02d' % (self.timespan[0][0], self.timespan[0][1])
140 def getTimespanEnd(self):
141 return '%02d:%02d' % (self.timespan[1][0], self.timespan[1][1])
143 def checkAnyTimespan(self, time, begin = None, end = None, haveDayspan = False):
147 # Check if we span a day
149 # Check if begin of event is later than our timespan starts
150 if time.tm_hour > begin[0] or (time.tm_hour == begin[0] and time.tm_min >= begin[1]):
151 # If so, event is in our timespan
153 # Check if begin of event is earlier than our timespan end
154 if time.tm_hour < end[0] or (time.tm_hour == end[0] and time.tm_min <= end[1]):
155 # If so, event is in our timespan
159 # Check if event begins earlier than our timespan starts
160 if time.tm_hour < begin[0] or (time.tm_hour == begin[0] and time.tm_min < begin[1]):
161 # Its out of our timespan then
163 # Check if event begins later than our timespan ends
164 if time.tm_hour > end[0] or (time.tm_hour == end[0] and time.tm_min > end[1]):
165 # Its out of our timespan then
169 def checkTimespan(self, begin):
170 return self.checkAnyTimespan(begin, *self.timespan)
172 def hasDuration(self):
173 return self.maxduration is not None
175 def getDuration(self):
176 return self.maxduration/60
178 def checkDuration(self, length):
179 if self.maxduration is None:
181 return length > self.maxduration
183 def getFullServices(self):
184 list = self.services[:]
186 from enigma import eServiceReference, eServiceCenter
187 serviceHandler = eServiceCenter.getInstance()
188 for bouquet in self.bouquets:
189 myref = eServiceReference(str(bouquet))
190 mylist = serviceHandler.list(myref)
191 if mylist is not None:
194 # TODO: I wonder if its sane to assume we get services here (and not just new lists)
195 # We can ignore markers & directorys here because they won't match any event's service :-)
197 # TODO: see if we need to check on custom names here
198 list.append(s.toString())
204 def checkServices(self, service):
205 if len(self.services) or len(self.bouquets):
206 return service not in self.getFullServices()
209 def getExcludedTitle(self):
210 return [x.pattern for x in self.exclude[0]]
212 def getExcludedShort(self):
213 return [x.pattern for x in self.exclude[1]]
215 def getExcludedDescription(self):
216 return [x.pattern for x in self.exclude[2]]
218 def getExcludedDays(self):
219 return self.exclude[3]
221 def getIncludedTitle(self):
222 return [x.pattern for x in self.include[0]]
224 def getIncludedShort(self):
225 return [x.pattern for x in self.include[1]]
227 def getIncludedDescription(self):
228 return [x.pattern for x in self.include[2]]
230 def getIncludedDays(self):
231 return self.include[3]
233 def checkExcluded(self, title, short, extended, dayofweek):
234 if len(self.exclude[3]):
235 list = [x for x in self.exclude[3]]
236 if "weekend" in list:
237 list.extend(["5", "6"])
238 if "weekday" in list:
239 list.extend(["0", "1", "2", "3", "4"])
240 if dayofweek in list:
243 for exclude in self.exclude[0]:
244 if exclude.search(title):
246 for exclude in self.exclude[1]:
247 if exclude.search(short):
249 for exclude in self.exclude[2]:
250 if exclude.search(extended):
254 def checkIncluded(self, title, short, extended, dayofweek):
255 if len(self.include[3]):
256 list = [x for x in self.include[3]]
257 if "weekend" in list:
258 list.extend(["5", "6"])
259 if "weekday" in list:
260 list.extend(["0", "1", "2", "3", "4"])
261 if dayofweek not in list:
264 for include in self.include[0]:
265 if not include.search(title):
267 for include in self.include[1]:
268 if not include.search(short):
270 for include in self.include[2]:
271 if not include.search(extended):
276 def checkFilter(self, title, short, extended, dayofweek):
277 if self.checkExcluded(title, short, extended, dayofweek):
280 return self.checkIncluded(title, short, extended, dayofweek)
283 return self.offset is not None
285 def isOffsetEqual(self):
286 return self.offset[0] == self.offset[1]
288 def applyOffset(self, begin, end):
289 if self.offset is None:
291 return (begin - self.offset[0], end + self.offset[1])
293 def getOffsetBegin(self):
294 return self.offset[0]/60
296 def getOffsetEnd(self):
297 return self.offset[1]/60
299 def hasAfterEvent(self):
300 return len(self.afterevent)
302 def hasAfterEventTimespan(self):
303 for afterevent in self.afterevent:
304 if afterevent[1][0] is not None:
308 def getAfterEventTimespan(self, end):
309 for afterevent in self.afterevent:
310 if not self.checkAnyTimespan(end, *afterevent[1]):
314 def getAfterEvent(self):
315 for afterevent in self.afterevent:
316 if afterevent[1][0] is None:
320 def getEnabled(self):
321 return self.enabled and "yes" or "no"
323 def getJustplay(self):
324 return self.justplay and "1" or "0"
326 def hasDestination(self):
327 return self.destination is not None
329 def hasCounter(self):
330 return self.matchCount != 0
332 def hasCounterFormatString(self):
333 return self.matchFormatString != ''
335 def getCounter(self):
336 return self.matchCount
338 def getCounterLeft(self):
339 return self.matchLeft
341 def getCounterLimit(self):
342 return self.matchLimit
344 def getCounterFormatString(self):
345 return self.matchFormatString
347 def checkCounter(self, timestamp):
348 # 0-Count is considered "unset"
349 if self.matchCount == 0:
352 # Check if event is in current timespan (we can only manage one!)
353 limit = strftime(self.matchFormatString, timestamp)
354 if limit != self.matchLimit:
357 if self.matchLeft > 0:
362 def update(self, begin, timestamp):
363 # Only update limit when we have new begin
364 if begin > self.lastBegin:
365 self.lastBegin = begin
368 # %m is Month, %U is week (sunday), %W is week (monday)
369 newLimit = strftime(self.matchFormatString, timestamp)
371 if newLimit != self.matchLimit:
372 self.matchLeft = self.matchCount
373 self.matchLimit = newLimit
375 def getLastBegin(self):
376 return self.lastBegin
378 def getAvoidDuplicateDescription(self):
379 return self.avoidDuplicateDescription
391 str(self.afterevent),
393 str(self.maxduration),
395 str(self.destination),
396 str(self.matchCount),
398 str(self.matchLimit),
399 str(self.matchFormatString),