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  __author__  = "K. Hilbert <Karsten.Hilbert@gmx.net>" 
  7  __licence__ = "GPL v2 or later (details at http://www.gnu.org)" 
  8   
  9  # stdlib 
 10  import logging 
 11   
 12  # 3rd party 
 13  import wx 
 14   
 15   
 16  _log = logging.getLogger('gm.timers') 
 17  _timers = [] 
 18   
 19  #=========================================================================== 
20 -def shutdown():
21 global _timers 22 _log.info('shutting down %s pending timers', len(_timers)) 23 for timer in _timers: 24 _log.debug('timer [%s]', timer.cookie) 25 timer.Stop() 26 _timers = []
27 28 #===========================================================================
29 -class cTimer(wx.Timer):
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 # sanity check 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 #-----------------------------------------------------------------------
67 - def Notify(self):
68 self.__callback(self.cookie)
69 70 #-----------------------------------------------------------------------
76 77 #=========================================================================== 78 if __name__ == '__main__': 79 import time 80 81 #-----------------------------------------------------------------------
82 - def cb_timer(cookie):
83 print("timer <%s> fired" % cookie)
84 #-----------------------------------------------------------------------
85 - class cApp(wx.App):
86 - def OnInit(self):
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 # and enter the main event loop 95 app.MainLoop() 96 print("waiting 10 seconds for timer to trigger") 97 time.sleep(10) 98 #=========================================================================== 99