-
Notifications
You must be signed in to change notification settings - Fork 4
/
delayed_call.py
186 lines (150 loc) · 5.13 KB
/
delayed_call.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import pyglet
import weakref
from talkshowLogger import logger
debug = logger.debug
info = logger.info
warn = logger.warn
class NoCookie: pass
class DelayedCall:
# In order to be able to cancel a scheduled function call
# we need to make sure to pass a unique callable to pyglet's
# scheduleXX() function. (in contrast to a single static function)
# However, since we want to have automatic unscheduling when
# a DelayedCall object gets out of scope, we cannot pass a strong
# reference to our DelayedCall object to scheduleXXX() (pyglet would
# keep its reference count from becoming zero)
# That's why we construct a unique Identifier object that
# simply forwards the scheduled call to a static method and passes
# the *weak* reference to our instance we passed to scheduleXXX()
# The static method _callme() acts like an instance method after having
# resolved the weak reference.
# THe Identifier instance can be used to cancel a specific instance
# of DelayedCall
class Identifier:
def __call__(self,delta,ref):
DelayedCall._callme(ref)
def __init__(self, func, delay, cookie = NoCookie, periodic = False):
assert delay != None
assert delay == int(delay)
self.periodic = periodic
self.scheduled = False
self.cookie = cookie
self.identifier = DelayedCall.Identifier()
if func:
if periodic:
if delay > 0:
pyglet.clock.schedule_interval(self.identifier, delay / 1000.0, weakref.ref(self))
else:
pyglet.clock.schedule(self.identifier, weakref.ref(self))
else:
pyglet.clock.schedule_once(self.identifier, delay / 1000.0, weakref.ref(self))
self.func = func
self.scheduled = True
@staticmethod
def _callme(ref):
self = ref()
if self == None:
return
if self.cookie == NoCookie:
result = self.func()
else:
result = self.func(self.cookie)
if not self.periodic:
self.scheduled = False
else:
if result != True:
self.cancel()
def cancel(self):
if self.scheduled:
debug("cancel!")
pyglet.clock.unschedule(self.identifier)
self.scheduled = False
self.func = None
def __del__(self):
self.cancel()
class PeriodicCall(DelayedCall):
def __init__(self, func, delay, cookie = NoCookie):
DelayedCall.__init__(self, func, delay, cookie, periodic=True)
##
# Regression Tests
##
import unittest
from test import test_support
class TestDelayedCall(unittest.TestCase):
def setUp(self):
self.window = pyglet.window.Window()
def tearDown(self):
pass
def callme(self, cookie):
self.cookie = cookie
self.window.close()
def test_dc(self):
self.cookie = None
dc = DelayedCall(self.callme, 1000, "hello")
pyglet.app.run()
assert self.cookie == "hello"
class TestEveryFrame(TestDelayedCall):
def countdown(self):
self.counter -=1
if self.counter == 0:
self.window.close()
return self.counter > 0
def test_dc(self):
self.counter=100
dc = PeriodicCall(self.countdown, 0)
pyglet.app.run()
assert self.counter == 0
class TestPeriodic(TestDelayedCall):
def countdown(self):
self.counter -=1
if self.counter == 0:
self.window.close()
return self.counter > 0
def test_dc(self):
self.counter=3
dc = PeriodicCall(self.countdown, 1000)
pyglet.app.run()
assert self.counter == 0
class TestTwoCalls(TestDelayedCall):
def callme(self, cookie):
self.cookie = cookie
def test_dc(self):
self.cookie = None
dc1 = DelayedCall(self.callme, 100, "huhu")
dc2 = DelayedCall(self.window.close, 200)
pyglet.app.run()
assert self.cookie == "huhu"
class TestCancel1(TestDelayedCall):
def dontcallme(self, cookie):
print "DONT CALL ME I SAY!"
self.cookie = cookie
def test_dc(self):
self.cookie = None
dc1 = DelayedCall(self.dontcallme, 100, "huhu")
dc2 = DelayedCall(self.window.close, 200)
dc3 = DelayedCall(dc1.cancel, 50)
pyglet.app.run()
assert self.cookie == None
class TestCancel2(TestDelayedCall):
def dontcallme(self, cookie):
print "DONT CALL ME I SAY!"
self.cookie = cookie
def test_dc(self):
self.cookie = None
dc1 = DelayedCall(self.dontcallme, 100, "huhu")
dc2 = DelayedCall(self.window.close, 200)
dc1 = None
pyglet.app.run()
assert self.cookie == None
def test_main():
test_support.run_unittest(
TestDelayedCall,
TestEveryFrame,
TestPeriodic,
TestTwoCalls,
TestCancel1,
TestCancel2
#... list other tests ...
)
if __name__ == "__main__":
test_main()