Package Gnumed :: Package wxpython :: Module gmTimer
[frames] | no frames]

Source Code for Module Gnumed.wxpython.gmTimer

 1  """GNUmed wx.Timer proxy object. 
 2   
 3  @copyright: author(s) 
 4  """ 
 5  ############################################################################ 
 6  # $Source: /home/ncq/Projekte/cvs2git/vcs-mirror/gnumed/gnumed/client/wxpython/gmTimer.py,v $ 
 7  # $Id: gmTimer.py,v 1.13 2008-12-26 16:04:12 ncq Exp $ 
 8  __version__ = "$Revision: 1.13 $" 
 9  __author__  = "K. Hilbert <Karsten.Hilbert@gmx.net>" 
10  __licence__ = "GPL v2 or later (details at http://www.gnu.org)" 
11   
12  # stdlib 
13  import logging 
14   
15  # 3rd party 
16  import wx 
17   
18   
19  _log = logging.getLogger('gm.timers') 
20  _timers = [] 
21  #=========================================================================== 
22 -def shutdown():
23 global _timers 24 _log.info('shutting down %s pending timers', len(_timers)) 25 for timer in _timers: 26 _log.debug('timer [%s]', timer.cookie) 27 timer.Stop() 28 _timers = []
29 #===========================================================================
30 -class cTimer(wx.Timer):
31 """wx.Timer proxy. 32 33 It would be quite useful to tune the delay 34 according to current network speed either at 35 application startup or even during runtime. 36 """
37 - def __init__(self, callback = None, delay = 300, cookie = None):
38 """Set up our timer with reasonable defaults. 39 40 - delay default is 300ms as per Richard Terry's experience 41 - delay should be tailored to network speed/user speed 42 - <cookie> is passed to <callback> when <delay> is up 43 """ 44 # sanity check 45 if not callable(callback): 46 raise ValueError("[%s]: <callback> %s is not a callable()" % (self.__class__.__name__, callback)) 47 48 if cookie is None: 49 self.cookie = id(self) 50 else: 51 self.cookie = cookie 52 self.__callback = callback 53 self.__delay = delay 54 55 wx.Timer.__init__(self) 56 57 _log.debug('setting up timer: cookie [%s], delay %sms', self.cookie, self.__delay) 58 59 global _timers 60 _timers.append(self)
61 #-----------------------------------------------------------------------
62 - def Start(self, milliseconds=-1, oneShot=False):
63 if milliseconds == -1: 64 milliseconds = self.__delay 65 wx.Timer.Start(self, milliseconds=milliseconds, oneShot=oneShot)
66 #-----------------------------------------------------------------------
67 - def Notify(self):
68 self.__callback(self.cookie)
69 #-----------------------------------------------------------------------
75 #=========================================================================== 76 if __name__ == '__main__': 77 import time 78 79 #-----------------------------------------------------------------------
80 - def cb_timer(cookie):
81 print "timer <%s> fired" % cookie
82 #-----------------------------------------------------------------------
83 - class cApp(wx.App):
84 - def OnInit(self):
85 print "setting up timer" 86 timer = cTimer(callback = cb_timer) 87 print "starting timer" 88 timer.Start() 89 return True
90 #----------------------------------------------------------------------- 91 app = cApp(0) 92 # and enter the main event loop 93 app.MainLoop() 94 print "waiting 10 seconds for timer to trigger" 95 time.sleep(10) 96 #=========================================================================== 97