home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_651 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  8.1 KB  |  277 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import itertools
  5. import weakref
  6. import atexit
  7. import threading
  8. from multiprocessing.process import current_process, active_children
  9. __all__ = [
  10.     'sub_debug',
  11.     'debug',
  12.     'info',
  13.     'sub_warning',
  14.     'get_logger',
  15.     'log_to_stderr',
  16.     'get_temp_dir',
  17.     'register_after_fork',
  18.     'is_exiting',
  19.     'Finalize',
  20.     'ForkAwareThreadLock',
  21.     'ForkAwareLocal',
  22.     'SUBDEBUG',
  23.     'SUBWARNING']
  24. NOTSET = 0
  25. SUBDEBUG = 5
  26. DEBUG = 10
  27. INFO = 20
  28. SUBWARNING = 25
  29. LOGGER_NAME = 'multiprocessing'
  30. DEFAULT_LOGGING_FORMAT = '[%(levelname)s/%(processName)s] %(message)s'
  31. _logger = None
  32. _log_to_stderr = False
  33.  
  34. def sub_debug(msg, *args):
  35.     if _logger:
  36.         _logger.log(SUBDEBUG, msg, *args)
  37.     
  38.  
  39.  
  40. def debug(msg, *args):
  41.     if _logger:
  42.         _logger.log(DEBUG, msg, *args)
  43.     
  44.  
  45.  
  46. def info(msg, *args):
  47.     if _logger:
  48.         _logger.log(INFO, msg, *args)
  49.     
  50.  
  51.  
  52. def sub_warning(msg, *args):
  53.     if _logger:
  54.         _logger.log(SUBWARNING, msg, *args)
  55.     
  56.  
  57.  
  58. def get_logger():
  59.     global _logger
  60.     import logging
  61.     import atexit
  62.     logging._acquireLock()
  63.     
  64.     try:
  65.         if not _logger:
  66.             _logger = logging.getLogger(LOGGER_NAME)
  67.             _logger.propagate = 0
  68.             logging.addLevelName(SUBDEBUG, 'SUBDEBUG')
  69.             logging.addLevelName(SUBWARNING, 'SUBWARNING')
  70.             if hasattr(atexit, 'unregister'):
  71.                 atexit.unregister(_exit_function)
  72.                 atexit.register(_exit_function)
  73.             else:
  74.                 atexit._exithandlers.remove((_exit_function, (), { }))
  75.                 atexit._exithandlers.append((_exit_function, (), { }))
  76.     finally:
  77.         logging._releaseLock()
  78.  
  79.     return _logger
  80.  
  81.  
  82. def log_to_stderr(level = None):
  83.     global _log_to_stderr
  84.     import logging
  85.     logger = get_logger()
  86.     formatter = logging.Formatter(DEFAULT_LOGGING_FORMAT)
  87.     handler = logging.StreamHandler()
  88.     handler.setFormatter(formatter)
  89.     logger.addHandler(handler)
  90.     if level:
  91.         logger.setLevel(level)
  92.     
  93.     _log_to_stderr = True
  94.     return _logger
  95.  
  96.  
  97. def get_temp_dir():
  98.     if current_process()._tempdir is None:
  99.         import shutil
  100.         import tempfile
  101.         tempdir = tempfile.mkdtemp(prefix = 'pymp-')
  102.         info('created temp directory %s', tempdir)
  103.         Finalize(None, shutil.rmtree, args = [
  104.             tempdir], exitpriority = -100)
  105.         current_process()._tempdir = tempdir
  106.     
  107.     return current_process()._tempdir
  108.  
  109. _afterfork_registry = weakref.WeakValueDictionary()
  110. _afterfork_counter = itertools.count()
  111.  
  112. def _run_after_forkers():
  113.     items = list(_afterfork_registry.items())
  114.     items.sort()
  115.     for index, ident, func in items:
  116.         obj = None
  117.         
  118.         try:
  119.             func(obj)
  120.         continue
  121.         except Exception:
  122.             e = None
  123.             info('after forker raised exception %s', e)
  124.             continue
  125.         
  126.  
  127.     
  128.  
  129.  
  130. def register_after_fork(obj, func):
  131.     _afterfork_registry[(_afterfork_counter.next(), id(obj), func)] = obj
  132.  
  133. _finalizer_registry = { }
  134. _finalizer_counter = itertools.count()
  135.  
  136. class Finalize(object):
  137.     
  138.     def __init__(self, obj, callback, args = (), kwargs = None, exitpriority = None):
  139.         if obj is not None:
  140.             self._weakref = weakref.ref(obj, self)
  141.         
  142.         self._callback = callback
  143.         self._args = args
  144.         if not kwargs:
  145.             pass
  146.         self._kwargs = { }
  147.         self._key = (exitpriority, _finalizer_counter.next())
  148.         _finalizer_registry[self._key] = self
  149.  
  150.     
  151.     def __call__(self, wr = None):
  152.         
  153.         try:
  154.             del _finalizer_registry[self._key]
  155.         except KeyError:
  156.             sub_debug('finalizer no longer registered')
  157.  
  158.         sub_debug('finalizer calling %s with args %s and kwargs %s', self._callback, self._args, self._kwargs)
  159.         res = self._callback(*self._args, **self._kwargs)
  160.         self._weakref = None
  161.         self._callback = None
  162.         self._args = None
  163.         self._kwargs = None
  164.         self._key = None
  165.         return res
  166.  
  167.     
  168.     def cancel(self):
  169.         
  170.         try:
  171.             del _finalizer_registry[self._key]
  172.         except KeyError:
  173.             pass
  174.  
  175.         self._weakref = None
  176.         self._callback = None
  177.         self._args = None
  178.         self._kwargs = None
  179.         self._key = None
  180.  
  181.     
  182.     def still_active(self):
  183.         return self._key in _finalizer_registry
  184.  
  185.     
  186.     def __repr__(self):
  187.         
  188.         try:
  189.             obj = self._weakref()
  190.         except (AttributeError, TypeError):
  191.             obj = None
  192.  
  193.         if obj is None:
  194.             return '<Finalize object, dead>'
  195.         x = '<Finalize object, callback=%s' % getattr(self._callback, '__name__', self._callback)
  196.         if self._args:
  197.             x += ', args=' + str(self._args)
  198.         
  199.         if self._kwargs:
  200.             x += ', kwargs=' + str(self._kwargs)
  201.         
  202.         if self._key[0] is not None:
  203.             x += ', exitprority=' + str(self._key[0])
  204.         
  205.         return x + '>'
  206.  
  207.  
  208.  
  209. def _run_finalizers(minpriority = None):
  210.     items = _[1]
  211.     items.sort(reverse = True)
  212.     for key, finalizer in items:
  213.         sub_debug('calling %s', finalizer)
  214.         
  215.         try:
  216.             finalizer()
  217.         continue
  218.         except Exception:
  219.             []
  220.             []
  221.             []
  222.             import traceback
  223.             traceback.print_exc()
  224.             continue
  225.             None if minpriority is None else (None,)
  226.         
  227.  
  228.     
  229.  
  230.  
  231. def is_exiting():
  232.     if not _exiting:
  233.         pass
  234.     return _exiting is None
  235.  
  236. _exiting = False
  237.  
  238. def _exit_function():
  239.     info('process shutting down')
  240.     debug('running all "atexit" finalizers with priority >= 0')
  241.     _run_finalizers(0)
  242.     for p in active_children():
  243.         if p._daemonic:
  244.             info('calling terminate() for daemon %s', p.name)
  245.             p._popen.terminate()
  246.             continue
  247.     
  248.     for p in active_children():
  249.         info('calling join() for process %s', p.name)
  250.         p.join()
  251.     
  252.     debug('running the remaining "atexit" finalizers')
  253.     _run_finalizers()
  254.  
  255. atexit.register(_exit_function)
  256.  
  257. class ForkAwareThreadLock(object):
  258.     
  259.     def __init__(self):
  260.         self._lock = threading.Lock()
  261.         self.acquire = self._lock.acquire
  262.         self.release = self._lock.release
  263.         register_after_fork(self, ForkAwareThreadLock.__init__)
  264.  
  265.  
  266.  
  267. class ForkAwareLocal(threading.local):
  268.     
  269.     def __init__(self):
  270.         register_after_fork(self, (lambda obj: obj.__dict__.clear()))
  271.  
  272.     
  273.     def __reduce__(self):
  274.         return (type(self), ())
  275.  
  276.  
  277.