intial commit of autotimer, epgrefresh and werbezapper to enigma2-plugins
[enigma2-plugins.git] / autotimer / src / AutoTimerComponent.py
1 # Format Counter
2 from time import strftime
3
4 class AutoTimerComponent(object):
5         """AutoTimer Component which also handles validity checks"""
6
7         def __init__(self, id, *args, **kwargs):
8                 self.id = id
9                 self._afterevent = []
10                 self.setValues(*args, **kwargs)
11
12         def __eq__(self, other):
13                 try:
14                         return self.id == other.id
15                 except AttributeError:
16                         return False
17
18         def __ne__(self, other):
19                 return not self.__eq__(other)
20
21         def setValues(self, name, match, enabled, timespan = None, services = None, offset = None, afterevent = [], exclude = None, maxduration = None, destination = None, include = None, matchCount = 0, matchLeft = 0, matchLimit = '', matchFormatString = '', lastBegin = 0, justplay = False):
22                 self.name = name
23                 self.match = match
24                 self.timespan = timespan
25                 self.services = services
26                 self.offset = offset
27                 self.afterevent = afterevent
28                 self.exclude = exclude
29                 self.include = include
30                 self.maxduration = maxduration
31                 self.enabled = enabled
32                 self.destination = destination
33                 self.matchCount = matchCount
34                 self.matchLeft = matchLeft
35                 self.matchLimit = matchLimit
36                 self.matchFormatString = matchFormatString
37                 self.lastBegin = lastBegin
38                 self.justplay = justplay
39
40         def calculateDayspan(self, begin, end):
41                 if end[0] < begin[0] or (end[0] == begin[0] and end[1] <= begin[1]):
42                         return (begin, end, True)
43                 else:
44                         return (begin, end, False)
45
46         def setTimespan(self, timespan):
47                 if timespan is None:
48                         self._timespan = (None,)
49                 else:
50                         self._timespan = self.calculateDayspan(*timespan)
51
52         def getTimespan(self):
53                 return self._timespan
54
55         timespan = property(getTimespan, setTimespan)
56
57         def setExclude(self, exclude):
58                 if exclude:
59                         self._exclude = exclude
60                 else:
61                         self._exclude = ([], [], [], [])
62
63         def getExclude(self):
64                 return self._exclude
65
66         exclude = property(getExclude, setExclude)
67
68         def setInclude(self, include):
69                 if include:
70                         self._include = include
71                 else:
72                         self._include = ([], [], [], [])
73
74         def getInclude(self):
75                 return self._include
76
77         include = property(getInclude, setInclude)
78
79         def setServices(self, services):
80                 if services:
81                         self._services = services
82                 else:
83                         self._services = []
84
85         def getServices(self):
86                 return self._services
87
88         services = property(getServices, setServices)
89
90         def setAfterEvent(self, afterevent):
91                 del self._afterevent[:]
92                 if len(afterevent):
93                         for definition in afterevent:
94                                 action, timespan = definition
95                                 if timespan is None:
96                                         self._afterevent.append((action, (None,)))
97                                 else:
98                                         self._afterevent.append((action, self.calculateDayspan(*timespan)))
99
100         def getCompleteAfterEvent(self):
101                 return self._afterevent
102
103         afterevent = property(getCompleteAfterEvent, setAfterEvent)
104
105         def hasTimespan(self):
106                 return self.timespan[0] is not None
107
108         def getTimespanBegin(self):
109                 return '%02d:%02d' % (self.timespan[0][0], self.timespan[0][1])
110
111         def getTimespanEnd(self):
112                 return '%02d:%02d' % (self.timespan[1][0], self.timespan[1][1])
113
114         def checkAnyTimespan(self, time, begin = None, end = None, haveDayspan = False):
115                 if begin is None:
116                         return False
117
118                 # Check if we span a day
119                 if haveDayspan:
120                         # Check if begin of event is later than our timespan starts
121                         if time[3] > begin[0] or (time[3] == begin[0] and time[4] >= begin[1]):
122                                 # If so, event is in our timespan
123                                 return False
124                         # Check if begin of event is earlier than our timespan end
125                         if time[3] < end[0] or (time[3] == end[0] and time[4] <= end[1]):
126                                 # If so, event is in our timespan
127                                 return False
128                         return True
129                 else:
130                         # Check if event begins earlier than our timespan starts 
131                         if time[3] < begin[0] or (time[3] == begin[0] and time[4] < begin[1]):
132                                 # Its out of our timespan then
133                                 return True
134                         # Check if event begins later than our timespan ends
135                         if time[3] > end[0] or (time[3] == end[0] and time[4] > end[1]):
136                                 # Its out of our timespan then
137                                 return True
138                         return False
139
140         def checkTimespan(self, begin):
141                 return self.checkAnyTimespan(begin, *self.timespan)
142
143         def hasDuration(self):
144                 return self.maxduration is not None
145
146         def getDuration(self):
147                 return self.maxduration/60
148
149         def checkDuration(self, length):
150                 if self.maxduration is None:
151                         return False
152                 return length > self.maxduration
153
154         def checkServices(self, service):
155                 if not len(self.services):
156                         return False
157                 return service not in self.services
158
159         def getExcludedTitle(self):
160                 return self.exclude[0]
161
162         def getExcludedShort(self):
163                 return self.exclude[1]
164
165         def getExcludedDescription(self):
166                 return self.exclude[2]
167
168         def getExcludedDays(self):
169                 return self.exclude[3]
170
171         def getIncludedTitle(self):
172                 return self.include[0]
173
174         def getIncludedShort(self):
175                 return self.include[1]
176
177         def getIncludedDescription(self):
178                 return self.include[2]
179
180         def getIncludedDays(self):
181                 return self.include[3]
182
183         def checkExcluded(self, title, short, extended, dayofweek):
184                 if len(self.exclude[3]):
185                         list = [x for x in self.exclude[3]]
186                         if "weekend" in list:
187                                 list.extend(["5", "6"])
188                         if "weekday" in list:
189                                 list.extend(["0", "1", "2", "3", "4"])
190                         if dayofweek in list:
191                                 return True
192
193                 for exclude in self.exclude[0]:
194                         if exclude in title:
195                                 return True
196                 for exclude in self.exclude[1]:
197                         if exclude in short:
198                                 return True
199                 for exclude in self.exclude[2]:
200                         if exclude in extended:
201                                 return True
202                 return False
203
204         def checkIncluded(self, title, short, extended, dayofweek):
205                 if len(self.include[3]):
206                         list = [x for x in self.include[3]]
207                         if "weekend" in list:
208                                 list.extend(["5", "6"])
209                         if "weekday" in list:
210                                 list.extend(["0", "1", "2", "3", "4"])
211                         if dayofweek not in list:
212                                 return True
213
214                 for include in self.include[0]:
215                         if include not in title:
216                                 return True
217                 for include in self.include[1]:
218                         if include not in short:
219                                 return True
220                 for include in self.include[2]:
221                         if include not in extended:
222                                 return True
223
224                 return False
225
226         def checkFilter(self, title, short, extended, dayofweek):
227                 if self.checkExcluded(title, short, extended, dayofweek):
228                         return True
229
230                 return self.checkIncluded(title, short, extended, dayofweek)
231
232         def hasOffset(self):
233                 return self.offset is not None
234
235         def isOffsetEqual(self):
236                 return self.offset[0] == self.offset[1]
237
238         def applyOffset(self, begin, end):
239                 if self.offset is None:
240                         return (begin, end)
241                 return (begin - self.offset[0], end + self.offset[1])
242
243         def getOffsetBegin(self):
244                 return self.offset[0]/60
245
246         def getOffsetEnd(self):
247                 return self.offset[1]/60
248
249         def hasAfterEvent(self):
250                 return len(self.afterevent)
251
252         def hasAfterEventTimespan(self):
253                 for afterevent in self.afterevent:
254                         if afterevent[1][0] is not None:
255                                 return True
256                 return False
257
258         def getAfterEventTimespan(self, end):
259                 for afterevent in self.afterevent:
260                         if not self.checkAnyTimespan(end, *afterevent[1]):
261                                 return afterevent[0]
262                 return None
263
264         def getAfterEvent(self):
265                 for afterevent in self.afterevent:
266                         if afterevent[1][0] is None:
267                                 return afterevent[0]
268                 return None
269
270         def getEnabled(self):
271                 return self.enabled and "yes" or "no"
272
273         def getJustplay(self):
274                 return self.justplay and "1" or "0"
275
276         def hasDestination(self):
277                 return self.destination is not None
278
279         def hasCounter(self):
280                 return self.matchCount != 0
281
282         def hasCounterFormatString(self):
283                 return self.matchFormatString != ''
284
285         def getCounter(self):
286                 return self.matchCount
287
288         def getCounterLeft(self):
289                 return self.matchLeft
290
291         def getCounterLimit(self):
292                 return self.matchLimit
293
294         def getCounterFormatString(self):
295                 return self.matchFormatString
296
297         def checkCounter(self, timestamp):
298                 # 0-Count is considered "unset"
299                 if self.matchCount == 0:
300                         return False
301
302                 # Check if event is in current timespan (we can only manage one!)
303                 limit = strftime(self.matchFormatString, timestamp)
304                 if limit != self.matchLimit:
305                         return True
306
307                 if self.matchLeft > 0:
308                         self.matchLeft -= 1
309                         return False
310                 return True
311
312         def update(self, begin, timestamp):
313                 # Only update limit when we have new begin
314                 if begin > self.lastBegin:
315                         self.lastBegin = begin
316
317                         # Update Counter:
318                         # %m is Month, %U is week (sunday), %W is week (monday)
319                         newLimit = strftime(self.matchFormatString, timestamp)
320
321                         if newLimit != self.matchLimit:
322                                 self.matchLeft = self.matchCount
323                                 self.matchLimit = newLimit
324
325         def __repr__(self):
326                 return ''.join([
327                         '<AutomaticTimer ',
328                         self.name,
329                         ' (',
330                         ', '.join([
331                                         str(self.match),
332                                         str(self.timespan),
333                                         str(self.services),
334                                         str(self.offset),
335                                         str(self.afterevent),
336                                         str(self.exclude),
337                                         str(self.maxduration),
338                                         str(self.enabled),
339                                         str(self.destination),
340                                         str(self.matchCount),
341                                         str(self.matchLeft),
342                                         str(self.matchLimit),
343                                         str(self.matchFormatString),
344                                         str(self.lastBegin),
345                                         str(self.justplay)
346                          ]),
347                          ")>"
348                 ])