16 def __init__(self, begin, end):
18 self.prepare_time = 20
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)
31 return self.state == self.StateRunning
33 # update self.begin and self.end according to the self.repeated-flags
34 def processRepeated(self):
35 print "ProcessRepeated"
36 print time.strftime("%c", time.localtime(self.begin))
37 print time.strftime("%c", time.localtime(self.end))
38 if (self.repeated != 0):
39 now = int(time.time()) + 1
46 print "Day: " + str(x)
51 print time.strftime("%c", time.localtime(now))
52 print time.strftime("%c", time.localtime(self.begin))
53 print time.strftime("%c", time.localtime(self.end))
54 print str(time.localtime(self.begin).tm_wday)
55 while ((day[time.localtime(self.begin).tm_wday] != 0) or ((day[time.localtime(self.begin).tm_wday] == 0) and self.end < now)):
56 print time.strftime("%c", time.localtime(self.begin))
57 print time.strftime("%c", time.localtime(self.end))
62 if self.state == self.StateWait:
63 return self.begin - self.prepare_time
64 elif self.state == self.StatePrepare:
70 return self.getTime() < o.getTime()
72 def activate(self, event):
73 print "[timer.py] timer %s got activated (%d)!" % (self.description, event)
77 # the time between "polls". We do this because
78 # we want to account for time jumps etc.
79 # of course if they occur <100s before starting,
80 # it's not good. thus, you have to repoll when
81 # you change the time.
83 # this is just in case. We don't want the timer
84 # hanging. we use this "edge-triggered-polling-scheme"
85 # anyway, so why don't make it a bit more fool-proof?
90 self.processed_timers = [ ]
93 self.timer.timeout.get().append(self.calcNextActivation)
94 self.lastActivation = time.time()
96 self.calcNextActivation()
98 def addTimerEntry(self, entry, noRecalc=0):
99 entry.processRepeated()
101 # when the timer has not yet started, and is already passed,
102 # don't go trough waiting/running/end-states, but sort it
103 # right into the processedTimers.
104 if entry.end <= time.time() and entry.state == TimerEntry.StateWait:
105 bisect.insort(self.processed_timers, entry)
106 entry.state = TimerEntry.StateEnded
108 bisect.insort(self.timer_list, entry)
110 self.calcNextActivation()
112 def setNextActivation(self, when):
113 delay = int((when - time.time()) * 1000)
114 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
116 self.timer.start(delay, 1)
119 def calcNextActivation(self):
120 if self.lastActivation > time.time():
121 print "[timer.py] timewarp - re-evaluating all processed timers."
122 tl = self.processed_timers
123 self.processed_timers = [ ]
125 # simulate a "waiting" state to give them a chance to re-occure
126 x.state = TimerEntry.StateWaiting
127 self.addTimerEntry(x, noRecalc=1)
129 self.processActivation()
130 self.lastActivation = time.time()
132 min = int(time.time()) + self.MaxWaitTime
134 # calculate next activation point
135 if len(self.timer_list):
136 w = self.timer_list[0].getTime()
140 self.setNextActivation(min)
142 def timeChanged(self, timer):
143 self.timer_list.remove(timer)
145 self.addTimerEntry(timer)
147 def doActivate(self, w):
149 self.timer_list.remove(w)
152 if w.state < TimerEntry.StateEnded:
153 bisect.insort(self.timer_list, w)
155 if (w.repeated != 0):
157 w.state = TimerEntry.StateWait
158 self.addTimerEntry(w)
160 bisect.insort(self.processed_timers, w)
162 def processActivation(self):
163 t = int(time.time()) + 1
165 # we keep on processing the first entry until it goes into the future.
166 while len(self.timer_list) and self.timer_list[0].getTime() < t:
167 self.doActivate(self.timer_list[0])