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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from util import Timer, threaded
  5. from util.primitives.funcs import do
  6. from Queue import Queue
  7. from threading import RLock
  8. from functools import wraps
  9. from util import callsback, callany
  10. from Queue import Empty
  11. import logging
  12. log = logging.getLogger('cmdq')
  13.  
  14. class SerialCommandQueue(object):
  15.     
  16.     def __init__(self, start_hooks = None, end_hooks = None, retry_time = 2):
  17.         self.start_hooks = start_hooks
  18.         self.end_hooks = end_hooks
  19.         self.retry_time = retry_time
  20.         self.do_queue = Queue()
  21.         self.state_lock = RLock()
  22.         self.loop_running = False
  23.  
  24.     
  25.     def add_new(self, .1):
  26.         (func, instance, a, k) = .1
  27.         self.state_lock.__enter__()
  28.         
  29.         try:
  30.             self.do_queue.put((func, instance, a, k))
  31.             if not self.loop_running:
  32.                 self.loop_running = True
  33.                 self.flush()
  34.         finally:
  35.             pass
  36.  
  37.  
  38.     
  39.     def flush(self):
  40.         
  41.         try:
  42.             do((lambda .0: for f in .0:
  43. f())(self.start_hooks))
  44.         except Exception:
  45.             e = None
  46.             import traceback
  47.             traceback.print_exc()
  48.             self.state_lock.__enter__()
  49.             
  50.             try:
  51.                 self.do_queue = Queue()
  52.                 self.loop_running = False
  53.                 return None
  54.             finally:
  55.                 pass
  56.  
  57.         except:
  58.             self.state_lock.__exit__
  59.  
  60.         self.state_lock.acquire()
  61.         while True:
  62.             
  63.             try:
  64.                 (func, instance, a, k) = self.do_queue.get(False)
  65.             except Empty:
  66.                 self.state_lock.__exit__
  67.                 self.state_lock.__exit__
  68.                 self.state_lock
  69.                 break
  70.                 continue
  71.  
  72.             self.state_lock.release()
  73.             func(instance, *a, **k)
  74.             self.state_lock.acquire()
  75.             continue
  76.             self.state_lock.__exit__
  77.         self.state_lock.release()
  78.         do((lambda .0: for f in .0:
  79. f())(self.end_hooks))
  80.         self.state_lock.__enter__()
  81.         
  82.         try:
  83.             if self.do_queue.empty():
  84.                 self.loop_running = False
  85.             else:
  86.                 self.flush()
  87.         finally:
  88.             pass
  89.  
  90.  
  91.     flush = threaded(flush)
  92.  
  93.  
  94. class CommandQueue(object):
  95.     
  96.     def __init__(self, start_hooks, end_hooks, shutdown_interval = 30, retry_time = 2):
  97.         self.start_hooks = start_hooks
  98.         self.end_hooks = end_hooks
  99.         self.shutdown_interval = shutdown_interval
  100.         self.retry_time = retry_time
  101.         self.do_queue = Queue()
  102.         self.state_lock = RLock()
  103.         self.timer = Timer(shutdown_interval, self.finish)
  104.         self.loop_running = False
  105.         self.initialized = False
  106.         self.initializing = False
  107.         self.shutting_down = False
  108.         self.timer_valid = False
  109.  
  110.     
  111.     def add_new(self, .1):
  112.         (func, instance, a, k) = .1
  113.         self.state_lock.__enter__()
  114.         
  115.         try:
  116.             self.timer_valid = False
  117.             self.timer.cancel()
  118.             self.do_queue.put((func, instance, a, k))
  119.             if not self.loop_running:
  120.                 self.loop_running = True
  121.                 if self.initialized:
  122.                     self.flush()
  123.                 elif not self.initializing:
  124.                     self.initializing = True
  125.                     threaded(self.initialize)()
  126.                 
  127.         finally:
  128.             pass
  129.  
  130.  
  131.     
  132.     def initialize(self):
  133.         self.state_lock.__enter__()
  134.         
  135.         try:
  136.             if self.shutting_down:
  137.                 t = Timer(self.retry_time, threaded(self.initialize))
  138.                 t.start()
  139.                 return None
  140.         finally:
  141.             pass
  142.  
  143.         
  144.         try:
  145.             do((lambda .0: for f in .0:
  146. f())(self.start_hooks))
  147.         except Exception:
  148.             self.state_lock.__exit__
  149.             e = self.state_lock.__exit__
  150.             self.state_lock
  151.             import traceback
  152.             traceback.print_exc()
  153.             self.state_lock.__enter__()
  154.             
  155.             try:
  156.                 self.do_queue = Queue()
  157.                 self.loop_running = False
  158.                 self.initialized = False
  159.                 self.initializing = False
  160.                 return None
  161.             finally:
  162.                 pass
  163.  
  164.         except:
  165.             self.state_lock.__exit__
  166.  
  167.         self.state_lock.__enter__()
  168.         
  169.         try:
  170.             self.initialized = True
  171.             self.initializing = False
  172.         finally:
  173.             pass
  174.  
  175.         self.flush()
  176.  
  177.     
  178.     def flush(self):
  179.         self.state_lock.acquire()
  180.         while True:
  181.             self.state_lock.release()
  182.             
  183.             try:
  184.                 (func, instance, a, k) = self.do_queue.get(False)
  185.             except Empty:
  186.                 break
  187.             else:
  188.                 func(instance, *a, **k)
  189.             finally:
  190.                 self.state_lock.acquire()
  191.  
  192.         self.loop_running = False
  193.         self.timer_valid = True
  194.         self.timer.start()
  195.         self.state_lock.release()
  196.  
  197.     flush = threaded(flush)
  198.     
  199.     def finish(self):
  200.         self.state_lock.__enter__()
  201.         
  202.         try:
  203.             if not self.timer_valid:
  204.                 return None
  205.             self.initialized = False
  206.             self.shutting_down = True
  207.         finally:
  208.             pass
  209.  
  210.         do((lambda .0: for f in .0:
  211. f())(self.end_hooks))
  212.         self.state_lock.__enter__()
  213.         
  214.         try:
  215.             self.shutting_down = False
  216.         finally:
  217.             pass
  218.  
  219.  
  220.     finish = threaded(finish)
  221.  
  222.  
  223. def cmdqueue(qname = 'cmdq'):
  224.     
  225.     def wrapper2(func):
  226.         
  227.         def wrapper1(instance, *args, **kws):
  228.             cmdq = getattr(instance, qname)
  229.             cmdq.add_new((func, instance, args, kws))
  230.  
  231.         return wrapper1
  232.  
  233.     return wrapper2
  234.  
  235.  
  236. def callback_cmdqueue(qname = 'cmdq'):
  237.     
  238.     def wrapper2(func):
  239.         
  240.         def wrapper(instance, callback = (callsback, None), *args, **kws):
  241.             requestID = kws.pop('requestID', None)
  242.             cmdq = getattr(instance, qname)
  243.             
  244.             def do_thing(*a, **k):
  245.                 
  246.                 try:
  247.                     result = func(instance, *args, **kws)
  248.                     exception = None
  249.                 except Exception:
  250.                     e = None
  251.                     exception = e
  252.                     result = None
  253.                     import traceback
  254.                     import sys
  255.                     sys.stderr.write('The following exception occurred in callback_cmdqueue:\n')
  256.                     traceback.print_exc()
  257.  
  258.                 if exception:
  259.                     callany(callback.error, e)
  260.                 else:
  261.                     callany(callback.success, result)
  262.  
  263.             cmdq.add_new((do_thing, None, (), { }))
  264.  
  265.         wrapper = None(wraps(func)(wrapper))
  266.         return wrapper
  267.  
  268.     return wrapper2
  269.  
  270. __all__ = [
  271.     'callback_cmdqueue',
  272.     'cmdqueue',
  273.     'CommandQueue']
  274.