2 from time import strftime
4 class AutoTimerComponent(object):
5 """AutoTimer Component which also handles validity checks"""
7 def __init__(self, id, *args, **kwargs):
10 self.setValues(*args, **kwargs)
12 def __eq__(self, other):
14 return self.id == other.id
15 except AttributeError:
18 def __ne__(self, other):
19 return not self.__eq__(other)
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, avoidDuplicateDescription = False):
24 self.timespan = timespan
25 self.services = services
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 self.avoidDuplicateDescription = avoidDuplicateDescription
41 def calculateDayspan(self, begin, end):
42 if end[0] < begin[0] or (end[0] == begin[0] and end[1] <= begin[1]):
43 return (begin, end, True)
45 return (begin, end, False)
47 def setTimespan(self, timespan):
49 self._timespan = (None,)
51 self._timespan = self.calculateDayspan(*timespan)
53 def getTimespan(self):
56 timespan = property(getTimespan, setTimespan)
58 def setExclude(self, exclude):
60 self._exclude = exclude
62 self._exclude = ([], [], [], [])
67 exclude = property(getExclude, setExclude)
69 def setInclude(self, include):
71 self._include = include
73 self._include = ([], [], [], [])
78 include = property(getInclude, setInclude)
80 def setServices(self, services):
82 self._services = services
86 def getServices(self):
89 services = property(getServices, setServices)
91 def setAfterEvent(self, afterevent):
92 del self._afterevent[:]
94 for definition in afterevent:
95 action, timespan = definition
97 self._afterevent.append((action, (None,)))
99 self._afterevent.append((action, self.calculateDayspan(*timespan)))
101 def getCompleteAfterEvent(self):
102 return self._afterevent
104 afterevent = property(getCompleteAfterEvent, setAfterEvent)
106 def hasTimespan(self):
107 return self.timespan[0] is not None
109 def getTimespanBegin(self):
110 return '%02d:%02d' % (self.timespan[0][0], self.timespan[0][1])
112 def getTimespanEnd(self):
113 return '%02d:%02d' % (self.timespan[1][0], self.timespan[1][1])
115 def checkAnyTimespan(self, time, begin = None, end = None, haveDayspan = False):
119 # Check if we span a day
121 # Check if begin of event is later than our timespan starts
122 if time[3] > begin[0] or (time[3] == begin[0] and time[4] >= begin[1]):
123 # If so, event is in our timespan
125 # Check if begin of event is earlier than our timespan end
126 if time[3] < end[0] or (time[3] == end[0] and time[4] <= end[1]):
127 # If so, event is in our timespan
131 # Check if event begins earlier than our timespan starts
132 if time[3] < begin[0] or (time[3] == begin[0] and time[4] < begin[1]):
133 # Its out of our timespan then
135 # Check if event begins later than our timespan ends
136 if time[3] > end[0] or (time[3] == end[0] and time[4] > end[1]):
137 # Its out of our timespan then
141 def checkTimespan(self, begin):
142 return self.checkAnyTimespan(begin, *self.timespan)
144 def hasDuration(self):
145 return self.maxduration is not None
147 def getDuration(self):
148 return self.maxduration/60
150 def checkDuration(self, length):
151 if self.maxduration is None:
153 return length > self.maxduration
155 def checkServices(self, service):
156 if not len(self.services):
158 return service not in self.services
160 def getExcludedTitle(self):
161 return self.exclude[0]
163 def getExcludedShort(self):
164 return self.exclude[1]
166 def getExcludedDescription(self):
167 return self.exclude[2]
169 def getExcludedDays(self):
170 return self.exclude[3]
172 def getIncludedTitle(self):
173 return self.include[0]
175 def getIncludedShort(self):
176 return self.include[1]
178 def getIncludedDescription(self):
179 return self.include[2]
181 def getIncludedDays(self):
182 return self.include[3]
184 def checkExcluded(self, title, short, extended, dayofweek):
185 if len(self.exclude[3]):
186 list = [x for x in self.exclude[3]]
187 if "weekend" in list:
188 list.extend(["5", "6"])
189 if "weekday" in list:
190 list.extend(["0", "1", "2", "3", "4"])
191 if dayofweek in list:
194 for exclude in self.exclude[0]:
197 for exclude in self.exclude[1]:
200 for exclude in self.exclude[2]:
201 if exclude in extended:
205 def checkIncluded(self, title, short, extended, dayofweek):
206 if len(self.include[3]):
207 list = [x for x in self.include[3]]
208 if "weekend" in list:
209 list.extend(["5", "6"])
210 if "weekday" in list:
211 list.extend(["0", "1", "2", "3", "4"])
212 if dayofweek not in list:
215 for include in self.include[0]:
216 if include not in title:
218 for include in self.include[1]:
219 if include not in short:
221 for include in self.include[2]:
222 if include not in extended:
227 def checkFilter(self, title, short, extended, dayofweek):
228 if self.checkExcluded(title, short, extended, dayofweek):
231 return self.checkIncluded(title, short, extended, dayofweek)
234 return self.offset is not None
236 def isOffsetEqual(self):
237 return self.offset[0] == self.offset[1]
239 def applyOffset(self, begin, end):
240 if self.offset is None:
242 return (begin - self.offset[0], end + self.offset[1])
244 def getOffsetBegin(self):
245 return self.offset[0]/60
247 def getOffsetEnd(self):
248 return self.offset[1]/60
250 def hasAfterEvent(self):
251 return len(self.afterevent)
253 def hasAfterEventTimespan(self):
254 for afterevent in self.afterevent:
255 if afterevent[1][0] is not None:
259 def getAfterEventTimespan(self, end):
260 for afterevent in self.afterevent:
261 if not self.checkAnyTimespan(end, *afterevent[1]):
265 def getAfterEvent(self):
266 for afterevent in self.afterevent:
267 if afterevent[1][0] is None:
271 def getEnabled(self):
272 return self.enabled and "yes" or "no"
274 def getJustplay(self):
275 return self.justplay and "1" or "0"
277 def hasDestination(self):
278 return self.destination is not None
280 def hasCounter(self):
281 return self.matchCount != 0
283 def hasCounterFormatString(self):
284 return self.matchFormatString != ''
286 def getCounter(self):
287 return self.matchCount
289 def getCounterLeft(self):
290 return self.matchLeft
292 def getCounterLimit(self):
293 return self.matchLimit
295 def getCounterFormatString(self):
296 return self.matchFormatString
298 def checkCounter(self, timestamp):
299 # 0-Count is considered "unset"
300 if self.matchCount == 0:
303 # Check if event is in current timespan (we can only manage one!)
304 limit = strftime(self.matchFormatString, timestamp)
305 if limit != self.matchLimit:
308 if self.matchLeft > 0:
313 def update(self, begin, timestamp):
314 # Only update limit when we have new begin
315 if begin > self.lastBegin:
316 self.lastBegin = begin
319 # %m is Month, %U is week (sunday), %W is week (monday)
320 newLimit = strftime(self.matchFormatString, timestamp)
322 if newLimit != self.matchLimit:
323 self.matchLeft = self.matchCount
324 self.matchLimit = newLimit
326 def getLastBegin(self):
327 return self.lastBegin
329 def getAvoidDuplicateDescription(self):
330 return self.avoidDuplicateDescription
342 str(self.afterevent),
344 str(self.maxduration),
346 str(self.destination),
347 str(self.matchCount),
349 str(self.matchLimit),
350 str(self.matchFormatString),