16 def __init__(self, begin, end):
18 self.prepare_time = 10
23 def resetRepeated(self):
24 self.repeated = int(0)
26 def setRepeated(self, day):
27 self.repeated |= (2 ** day)
28 print "Repeated: " + str(self.repeated)
30 # update self.begin and self.end according to the self.repeated-flags
31 def processRepeated(self):
32 if (self.repeated != 0):
44 while ((day[time.localtime(self.begin).tm_wday] != 0) and (self.end > now)):
51 if self.state == self.StateWait:
52 return self.begin - self.prepare_time
53 elif self.state == self.StatePrepare:
59 return self.getTime() < o.getTime()
61 def activate(self, event):
62 print "[timer.py] timer %s got activated (%d)!" % (self.description, event)
66 # the time between "polls". We do this because
67 # we want to account for time jumps etc.
68 # of course if they occur <100s before starting,
69 # it's not good. thus, you have to repoll when
70 # you change the time.
72 # this is just in case. We don't want the timer
73 # hanging. we use this "edge-triggered-polling-scheme"
74 # anyway, so why don't make it a bit more fool-proof?
79 self.processed_timers = [ ]
82 self.timer.timeout.get().append(self.calcNextActivation)
83 self.lastActivation = time.time()
85 self.calcNextActivation()
87 def addTimerEntry(self, entry, noRecalc=0):
88 entry.processRepeated()
90 # we either go trough Prepare/Start/End-state if the timer is still running,
91 # or skip it when it's alrady past the end.
93 if entry.end > time.time():
94 bisect.insort(self.timer_list, entry)
96 self.calcNextActivation()
98 bisect.insort(self.processed_timers, entry)
100 def setNextActivation(self, when):
101 delay = int((when - time.time()) * 1000)
102 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
104 self.timer.start(delay, 1)
107 def calcNextActivation(self):
108 if self.lastActivation > time.time():
109 print "[timer.py] timewarp - re-evaluating all processed timers."
110 tl = self.processed_timers
111 self.processed_timers = [ ]
113 self.addTimerEntry(x, noRecalc=1)
115 self.processActivation()
116 self.lastActivation = time.time()
118 min = int(time.time()) + self.MaxWaitTime
120 # calculate next activation point
121 if len(self.timer_list):
122 w = self.timer_list[0].getTime()
126 self.setNextActivation(min)
128 def timeChanged(self, timer):
129 self.timer_list.remove(timer)
130 self.addTimerEntry(timer)
132 def doActivate(self, w):
134 self.timer_list.remove(w)
136 if w.state < TimerEntry.StateEnded:
137 bisect.insort(self.timer_list, w)
139 bisect.insort(self.processed_timers, w)
141 def processActivation(self):
142 t = int(time.time()) + 1
144 # we keep on processing the first entry until it goes into the future.
145 while len(self.timer_list) and self.timer_list[0].getTime() < t:
146 self.doActivate(self.timer_list[0])