home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / common / UpdateMixin.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  3.6 KB  |  118 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from logging import getLogger
  5. log = getLogger('updatemixin')
  6. info = log.info
  7. from Protocol import StateMixin
  8. from util import RepeatTimer
  9. from util.primitives.funcs import get, Delegate
  10.  
  11. nothing = lambda *a, **k: pass
  12.  
  13. class UpdateMixin(StateMixin):
  14.     updatefreq = 300
  15.     update_mixin_timer = True
  16.     
  17.     def __init__(self, updatefreq = None, *a, **k):
  18.         StateMixin.__init__(self, *a, **k)
  19.         if updatefreq is not None:
  20.             
  21.             try:
  22.                 updatefreq = int(updatefreq)
  23.             except ValueError:
  24.                 pass
  25.  
  26.             if updatefreq < 1:
  27.                 updatefreq = 60
  28.             
  29.             self.updatefreq = max(updatefreq, 15)
  30.         
  31.         self.on_connect = Delegate()
  32.         self.on_disable = Delegate()
  33.  
  34.     
  35.     def disconnect(self):
  36.         if self.update_mixin_timer:
  37.             self.timer.stop()
  38.         
  39.  
  40.     
  41.     def update_now(self):
  42.         raise NotImplementedError
  43.  
  44.     
  45.     def get_options(self):
  46.         options = (dict,)((lambda .0: for a in .0:
  47. (a, getattr(self, a)))(('enabled', 'updatefreq')))
  48.         if self.alias is not None:
  49.             options['alias'] = self.alias
  50.         
  51.         return options
  52.  
  53.     
  54.     def get_enabled(self):
  55.         return get(self, '_enabled', False)
  56.  
  57.     
  58.     def set_enabled(self, value):
  59.         has_been_set = hasattr(self, '_enabled')
  60.         if not has_been_set:
  61.             self.change_reason(self.Reasons.NONE)
  62.             if self.update_mixin_timer:
  63.                 info('%s creating a timer with update frequency %s', self, self.updatefreq)
  64.                 self.timer = RepeatTimer(int(self.updatefreq), self.update_now)
  65.                 self.timer.start()
  66.                 self.timer.stop()
  67.                 if get(self, 'on_connect', None) is not None and self.update_now not in self.on_connect:
  68.                     self.on_connect += self.update_now
  69.                 
  70.             
  71.         
  72.         self._enabled = value
  73.         if value:
  74.             info('enabling timer for %s', self)
  75.             if self.OFFLINE and getattr(self, '_needs_connect', True):
  76.                 self._needs_connect = False
  77.                 get(self, 'Connect', nothing)()
  78.             
  79.             if self.update_mixin_timer:
  80.                 self.timer.reset(int(self.updatefreq))
  81.             
  82.         elif has_been_set:
  83.             if not self.OFFLINE:
  84.                 get(self, 'Disconnect', nothing)()
  85.             
  86.             self._needs_connect = True
  87.             if self.update_mixin_timer:
  88.                 info('stopping timer for %s', self)
  89.                 self.timer.stop()
  90.             
  91.             get(self, 'on_disable', nothing)()
  92.         
  93.  
  94.     enabled = property(get_enabled, set_enabled)
  95.     
  96.     def pause_timer(self):
  97.         self.timer._cv.__enter__()
  98.         
  99.         try:
  100.             if not self.timer.paused:
  101.                 self.timer.pause()
  102.         finally:
  103.             pass
  104.  
  105.  
  106.     
  107.     def start_timer(self):
  108.         self.timer._cv.__enter__()
  109.         
  110.         try:
  111.             if self.timer.paused:
  112.                 self.timer.start()
  113.         finally:
  114.             pass
  115.  
  116.  
  117.  
  118.