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, \
22 afterevent = [], exclude = None, maxduration = None, destination = None, \
23 include = None, matchCount = 0, matchLeft = 0, matchLimit = '', matchFormatString = '', \
24 lastBegin = 0, justplay = False, avoidDuplicateDescription = False):
27 self.timespan = timespan
28 self.services = services
30 self.afterevent = afterevent
31 self.exclude = exclude
32 self.include = include
33 self.maxduration = maxduration
34 self.enabled = enabled
35 self.destination = destination
36 self.matchCount = matchCount
37 self.matchLeft = matchLeft
38 self.matchLimit = matchLimit
39 self.matchFormatString = matchFormatString
40 self.lastBegin = lastBegin
41 self.justplay = justplay
42 self.avoidDuplicateDescription = avoidDuplicateDescription
44 def calculateDayspan(self, begin, end):
45 if end[0] < begin[0] or (end[0] == begin[0] and end[1] <= begin[1]):
46 return (begin, end, True)
48 return (begin, end, False)
50 def setTimespan(self, timespan):
52 self._timespan = (None,)
54 self._timespan = self.calculateDayspan(*timespan)
56 def getTimespan(self):
59 timespan = property(getTimespan, setTimespan)
61 def setExclude(self, exclude):
63 self._exclude = exclude
65 self._exclude = ([], [], [], [])
70 exclude = property(getExclude, setExclude)
72 def setInclude(self, include):
74 self._include = include
76 self._include = ([], [], [], [])
81 include = property(getInclude, setInclude)
83 def setServices(self, services):
85 self._services = services
89 def getServices(self):
92 services = property(getServices, setServices)
94 def setAfterEvent(self, afterevent):
95 del self._afterevent[:]
97 for definition in afterevent:
98 action, timespan = definition
100 self._afterevent.append((action, (None,)))
102 self._afterevent.append((action, self.calculateDayspan(*timespan)))
104 def getCompleteAfterEvent(self):
105 return self._afterevent
107 afterevent = property(getCompleteAfterEvent, setAfterEvent)
109 def hasTimespan(self):
110 return self.timespan[0] is not None
112 def getTimespanBegin(self):
113 return '%02d:%02d' % (self.timespan[0][0], self.timespan[0][1])
115 def getTimespanEnd(self):
116 return '%02d:%02d' % (self.timespan[1][0], self.timespan[1][1])
118 def checkAnyTimespan(self, time, begin = None, end = None, haveDayspan = False):
122 # Check if we span a day
124 # Check if begin of event is later than our timespan starts
125 if time.tm_hour > begin[0] or (time.tm_hour == begin[0] and time.tm_min >= begin[1]):
126 # If so, event is in our timespan
128 # Check if begin of event is earlier than our timespan end
129 if time.tm_hour < end[0] or (time.tm_hour == end[0] and time.tm_min <= end[1]):
130 # If so, event is in our timespan
134 # Check if event begins earlier than our timespan starts
135 if time.tm_hour < begin[0] or (time.tm_hour == begin[0] and time.tm_min < begin[1]):
136 # Its out of our timespan then
138 # Check if event begins later than our timespan ends
139 if time.tm_hour > end[0] or (time.tm_hour == end[0] and time.tm_min > end[1]):
140 # Its out of our timespan then
144 def checkTimespan(self, begin):
145 return self.checkAnyTimespan(begin, *self.timespan)
147 def hasDuration(self):
148 return self.maxduration is not None
150 def getDuration(self):
151 return self.maxduration/60
153 def checkDuration(self, length):
154 if self.maxduration is None:
156 return length > self.maxduration
158 def checkServices(self, service):
159 if not len(self.services):
161 return service not in self.services
163 def getExcludedTitle(self):
164 return self.exclude[0]
166 def getExcludedShort(self):
167 return self.exclude[1]
169 def getExcludedDescription(self):
170 return self.exclude[2]
172 def getExcludedDays(self):
173 return self.exclude[3]
175 def getIncludedTitle(self):
176 return self.include[0]
178 def getIncludedShort(self):
179 return self.include[1]
181 def getIncludedDescription(self):
182 return self.include[2]
184 def getIncludedDays(self):
185 return self.include[3]
187 def checkExcluded(self, title, short, extended, dayofweek):
188 if len(self.exclude[3]):
189 list = [x for x in self.exclude[3]]
190 if "weekend" in list:
191 list.extend(["5", "6"])
192 if "weekday" in list:
193 list.extend(["0", "1", "2", "3", "4"])
194 if dayofweek in list:
197 for exclude in self.exclude[0]:
200 for exclude in self.exclude[1]:
203 for exclude in self.exclude[2]:
204 if exclude in extended:
208 def checkIncluded(self, title, short, extended, dayofweek):
209 if len(self.include[3]):
210 list = [x for x in self.include[3]]
211 if "weekend" in list:
212 list.extend(["5", "6"])
213 if "weekday" in list:
214 list.extend(["0", "1", "2", "3", "4"])
215 if dayofweek not in list:
218 for include in self.include[0]:
219 if include not in title:
221 for include in self.include[1]:
222 if include not in short:
224 for include in self.include[2]:
225 if include not in extended:
230 def checkFilter(self, title, short, extended, dayofweek):
231 if self.checkExcluded(title, short, extended, dayofweek):
234 return self.checkIncluded(title, short, extended, dayofweek)
237 return self.offset is not None
239 def isOffsetEqual(self):
240 return self.offset[0] == self.offset[1]
242 def applyOffset(self, begin, end):
243 if self.offset is None:
245 return (begin - self.offset[0], end + self.offset[1])
247 def getOffsetBegin(self):
248 return self.offset[0]/60
250 def getOffsetEnd(self):
251 return self.offset[1]/60
253 def hasAfterEvent(self):
254 return len(self.afterevent)
256 def hasAfterEventTimespan(self):
257 for afterevent in self.afterevent:
258 if afterevent[1][0] is not None:
262 def getAfterEventTimespan(self, end):
263 for afterevent in self.afterevent:
264 if not self.checkAnyTimespan(end, *afterevent[1]):
268 def getAfterEvent(self):
269 for afterevent in self.afterevent:
270 if afterevent[1][0] is None:
274 def getEnabled(self):
275 return self.enabled and "yes" or "no"
277 def getJustplay(self):
278 return self.justplay and "1" or "0"
280 def hasDestination(self):
281 return self.destination is not None
283 def hasCounter(self):
284 return self.matchCount != 0
286 def hasCounterFormatString(self):
287 return self.matchFormatString != ''
289 def getCounter(self):
290 return self.matchCount
292 def getCounterLeft(self):
293 return self.matchLeft
295 def getCounterLimit(self):
296 return self.matchLimit
298 def getCounterFormatString(self):
299 return self.matchFormatString
301 def checkCounter(self, timestamp):
302 # 0-Count is considered "unset"
303 if self.matchCount == 0:
306 # Check if event is in current timespan (we can only manage one!)
307 limit = strftime(self.matchFormatString, timestamp)
308 if limit != self.matchLimit:
311 if self.matchLeft > 0:
316 def update(self, begin, timestamp):
317 # Only update limit when we have new begin
318 if begin > self.lastBegin:
319 self.lastBegin = begin
322 # %m is Month, %U is week (sunday), %W is week (monday)
323 newLimit = strftime(self.matchFormatString, timestamp)
325 if newLimit != self.matchLimit:
326 self.matchLeft = self.matchCount
327 self.matchLimit = newLimit
329 def getLastBegin(self):
330 return self.lastBegin
332 def getAvoidDuplicateDescription(self):
333 return self.avoidDuplicateDescription
345 str(self.afterevent),
347 str(self.maxduration),
349 str(self.destination),
350 str(self.matchCount),
352 str(self.matchLimit),
353 str(self.matchFormatString),