1 """GNUmed wx.Timer proxy object.
2
3 @copyright: author(s)
4 """
5
6 __author__ = "K. Hilbert <Karsten.Hilbert@gmx.net>"
7 __licence__ = "GPL v2 or later (details at http://www.gnu.org)"
8
9
10 import logging
11
12
13 import wx
14
15
16 _log = logging.getLogger('gm.timers')
17 _timers = []
18
19
27
28
30 """wx.Timer proxy.
31
32 It would be quite useful to tune the delay
33 according to current network speed either at
34 application startup or even during runtime.
35 """
36 - def __init__(self, callback = None, delay = 300, cookie = None):
37 """Set up our timer with reasonable defaults.
38
39 - delay default is 300ms as per Richard Terry's experience
40 - delay should be tailored to network speed/user speed
41 - <cookie> is passed to <callback> when <delay> is up
42 """
43
44 if not callable(callback):
45 raise ValueError("[%s]: <callback> %s is not a callable()" % (self.__class__.__name__, callback))
46
47 if cookie is None:
48 self.cookie = id(self)
49 else:
50 self.cookie = cookie
51 self.__callback = callback
52 self.__delay = delay
53
54 wx.Timer.__init__(self)
55
56 _log.debug('setting up timer: cookie [%s], delay %sms', self.cookie, self.__delay)
57
58 global _timers
59 _timers.append(self)
60
61 - def Start(self, milliseconds=-1, oneShot=False):
62 if milliseconds == -1:
63 milliseconds = self.__delay
64 wx.Timer.Start(self, milliseconds=milliseconds, oneShot=oneShot)
65
66
68 self.__callback(self.cookie)
69
70
72 if cookie is None:
73 self.cookie = id(self)
74 else:
75 self.cookie = cookie
76
77
78 if __name__ == '__main__':
79 import time
80
81
83 print("timer <%s> fired" % cookie)
84
87 print("setting up timer")
88 timer = cTimer(callback = cb_timer)
89 print("starting timer")
90 timer.Start()
91 return True
92
93 app = cApp(0)
94
95 app.MainLoop()
96 print("waiting 10 seconds for timer to trigger")
97 time.sleep(10)
98
99