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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import cherrypy
  5.  
  6. def _getargs(func):
  7.     import types
  8.     if isinstance(func, types.MethodType):
  9.         func = func.im_func
  10.     
  11.     co = func.func_code
  12.     return co.co_varnames[:co.co_argcount]
  13.  
  14.  
  15. class Tool(object):
  16.     namespace = 'tools'
  17.     
  18.     def __init__(self, point, callable, name = None, priority = 50):
  19.         self._point = point
  20.         self.callable = callable
  21.         self._name = name
  22.         self._priority = priority
  23.         self.__doc__ = self.callable.__doc__
  24.         self._setargs()
  25.  
  26.     
  27.     def _setargs(self):
  28.         
  29.         try:
  30.             for arg in _getargs(self.callable):
  31.                 setattr(self, arg, None)
  32.         except (TypeError, AttributeError):
  33.             if hasattr(self.callable, '__call__'):
  34.                 for arg in _getargs(self.callable.__call__):
  35.                     setattr(self, arg, None)
  36.                 
  37.             
  38.         except NotImplementedError:
  39.             pass
  40.         except IndexError:
  41.             pass
  42.  
  43.  
  44.     
  45.     def _merged_args(self, d = None):
  46.         if d:
  47.             conf = d.copy()
  48.         else:
  49.             conf = { }
  50.         tm = cherrypy.request.toolmaps[self.namespace]
  51.         if self._name in tm:
  52.             conf.update(tm[self._name])
  53.         
  54.         if 'on' in conf:
  55.             del conf['on']
  56.         
  57.         return conf
  58.  
  59.     
  60.     def __call__(self, *args, **kwargs):
  61.         if args:
  62.             raise TypeError('The %r Tool does not accept positional arguments; you must use keyword arguments.' % self._name)
  63.         args
  64.         
  65.         def tool_decorator(f):
  66.             if not hasattr(f, '_cp_config'):
  67.                 f._cp_config = { }
  68.             
  69.             subspace = self.namespace + '.' + self._name + '.'
  70.             f._cp_config[subspace + 'on'] = True
  71.             for k, v in kwargs.iteritems():
  72.                 f._cp_config[subspace + k] = v
  73.             
  74.             return f
  75.  
  76.         return tool_decorator
  77.  
  78.     
  79.     def _setup(self):
  80.         conf = self._merged_args()
  81.         p = conf.pop('priority', None)
  82.         if p is None:
  83.             p = getattr(self.callable, 'priority', self._priority)
  84.         
  85.         cherrypy.request.hooks.attach(self._point, self.callable, priority = p, **conf)
  86.  
  87.  
  88.  
  89. class HandlerTool(Tool):
  90.     
  91.     def __init__(self, callable, name = None):
  92.         Tool.__init__(self, 'before_handler', callable, name)
  93.  
  94.     
  95.     def handler(self, *args, **kwargs):
  96.         
  97.         def handle_func(*a, **kw):
  98.             handled = self.callable(*args, **self._merged_args(kwargs))
  99.             if not handled:
  100.                 raise cherrypy.NotFound()
  101.             handled
  102.             return cherrypy.response.body
  103.  
  104.         handle_func.exposed = True
  105.         return handle_func
  106.  
  107.     
  108.     def _wrapper(self, **kwargs):
  109.         if self.callable(**kwargs):
  110.             cherrypy.request.handler = None
  111.         
  112.  
  113.     
  114.     def _setup(self):
  115.         conf = self._merged_args()
  116.         p = conf.pop('priority', None)
  117.         if p is None:
  118.             p = getattr(self.callable, 'priority', self._priority)
  119.         
  120.         cherrypy.request.hooks.attach(self._point, self._wrapper, priority = p, **conf)
  121.  
  122.  
  123.  
  124. class HandlerWrapperTool(Tool):
  125.     
  126.     def __init__(self, newhandler, point = 'before_handler', name = None, priority = 50):
  127.         self.newhandler = newhandler
  128.         self._point = point
  129.         self._name = name
  130.         self._priority = priority
  131.  
  132.     
  133.     def callable(self):
  134.         innerfunc = cherrypy.request.handler
  135.         
  136.         def wrap(*args, **kwargs):
  137.             return self.newhandler(innerfunc, *args, **kwargs)
  138.  
  139.         cherrypy.request.handler = wrap
  140.  
  141.  
  142.  
  143. class ErrorTool(Tool):
  144.     
  145.     def __init__(self, callable, name = None):
  146.         Tool.__init__(self, None, callable, name)
  147.  
  148.     
  149.     def _wrapper(self):
  150.         self.callable(**self._merged_args())
  151.  
  152.     
  153.     def _setup(self):
  154.         cherrypy.request.error_response = self._wrapper
  155.  
  156.  
  157. from cherrypy.lib import cptools, encoding, auth, static, tidy
  158. from cherrypy.lib import sessions as _sessions, xmlrpc as _xmlrpc
  159. from cherrypy.lib import caching as _caching, wsgiapp as _wsgiapp
  160.  
  161. class SessionTool(Tool):
  162.     
  163.     def __init__(self):
  164.         Tool.__init__(self, 'before_request_body', _sessions.init)
  165.  
  166.     
  167.     def _lock_session(self):
  168.         cherrypy.serving.session.acquire_lock()
  169.  
  170.     
  171.     def _setup(self):
  172.         hooks = cherrypy.request.hooks
  173.         conf = self._merged_args()
  174.         p = conf.pop('priority', None)
  175.         if p is None:
  176.             p = getattr(self.callable, 'priority', self._priority)
  177.         
  178.         hooks.attach(self._point, self.callable, priority = p, **conf)
  179.         locking = conf.pop('locking', 'implicit')
  180.         if locking == 'implicit':
  181.             hooks.attach('before_handler', self._lock_session)
  182.         elif locking == 'early':
  183.             hooks.attach('before_request_body', self._lock_session, priority = 60)
  184.         
  185.         hooks.attach('before_finalize', _sessions.save)
  186.         hooks.attach('on_end_request', _sessions.close)
  187.  
  188.     
  189.     def regenerate(self):
  190.         sess = cherrypy.serving.session
  191.         sess.regenerate()
  192.         conf = [](_[1])
  193.         _sessions.set_response_cookie(**conf)
  194.  
  195.  
  196.  
  197. class XMLRPCController(object):
  198.     _cp_config = {
  199.         'tools.xmlrpc.on': True }
  200.     
  201.     def default(self, *vpath, **params):
  202.         (rpcparams, rpcmethod) = _xmlrpc.process_body()
  203.         subhandler = self
  204.         for attr in str(rpcmethod).split('.'):
  205.             subhandler = getattr(subhandler, attr, None)
  206.         
  207.         if subhandler and getattr(subhandler, 'exposed', False):
  208.             body = subhandler(*vpath + rpcparams, **params)
  209.         else:
  210.             raise Exception, 'method "%s" is not supported' % attr
  211.         conf = getattr(subhandler, 'exposed', False).request.toolmaps['tools'].get('xmlrpc', { })
  212.         _xmlrpc.respond(body, conf.get('encoding', 'utf-8'), conf.get('allow_none', 0))
  213.         return cherrypy.response.body
  214.  
  215.     default.exposed = True
  216.  
  217.  
  218. class WSGIAppTool(HandlerTool):
  219.     
  220.     def _setup(self):
  221.         cherrypy.request.process_request_body = False
  222.         HandlerTool._setup(self)
  223.  
  224.  
  225.  
  226. class SessionAuthTool(HandlerTool):
  227.     
  228.     def _setargs(self):
  229.         for name in dir(cptools.SessionAuth):
  230.             if not name.startswith('__'):
  231.                 setattr(self, name, None)
  232.                 continue
  233.         
  234.  
  235.  
  236.  
  237. class CachingTool(Tool):
  238.     
  239.     def _wrapper(self, invalid_methods = ('POST', 'PUT', 'DELETE'), **kwargs):
  240.         request = cherrypy.request
  241.         if not hasattr(cherrypy, '_cache'):
  242.             cherrypy._cache = kwargs.pop('cache_class', _caching.MemoryCache)()
  243.             for k, v in kwargs.iteritems():
  244.                 setattr(cherrypy._cache, k, v)
  245.             
  246.         
  247.         if _caching.get(invalid_methods = invalid_methods):
  248.             request.handler = None
  249.         elif request.cacheable:
  250.             request.hooks.attach('before_finalize', _caching.tee_output, priority = 90)
  251.         
  252.  
  253.     _wrapper.priority = 20
  254.     
  255.     def _setup(self):
  256.         conf = self._merged_args()
  257.         p = conf.pop('priority', None)
  258.         cherrypy.request.hooks.attach('before_handler', self._wrapper, priority = p, **conf)
  259.  
  260.  
  261.  
  262. class Toolbox(object):
  263.     
  264.     def __init__(self, namespace):
  265.         self.namespace = namespace
  266.  
  267.     
  268.     def __setattr__(self, name, value):
  269.         if isinstance(value, Tool):
  270.             if value._name is None:
  271.                 value._name = name
  272.             
  273.             value.namespace = self.namespace
  274.         
  275.         object.__setattr__(self, name, value)
  276.  
  277.     
  278.     def __enter__(self):
  279.         cherrypy.request.toolmaps[self.namespace] = map = { }
  280.         
  281.         def populate(k, v):
  282.             (toolname, arg) = k.split('.', 1)
  283.             bucket = map.setdefault(toolname, { })
  284.             bucket[arg] = v
  285.  
  286.         return populate
  287.  
  288.     
  289.     def __exit__(self, exc_type, exc_val, exc_tb):
  290.         map = cherrypy.request.toolmaps.get(self.namespace)
  291.         if map:
  292.             for name, settings in map.items():
  293.                 if settings.get('on', False):
  294.                     tool = getattr(self, name)
  295.                     tool._setup()
  296.                     continue
  297.             
  298.         
  299.  
  300.  
  301. default_toolbox = _d = Toolbox('tools')
  302. _d.session_auth = SessionAuthTool(cptools.session_auth)
  303. _d.proxy = Tool('before_request_body', cptools.proxy, priority = 30)
  304. _d.response_headers = Tool('on_start_resource', cptools.response_headers)
  305. _d.log_tracebacks = Tool('before_error_response', cptools.log_traceback)
  306. _d.log_headers = Tool('before_error_response', cptools.log_request_headers)
  307. _d.log_hooks = Tool('on_end_request', cptools.log_hooks, priority = 100)
  308. _d.err_redirect = ErrorTool(cptools.redirect)
  309. _d.etags = Tool('before_finalize', cptools.validate_etags, priority = 75)
  310. _d.decode = Tool('before_handler', encoding.decode)
  311. _d.encode = Tool('before_finalize', encoding.encode, priority = 70)
  312. _d.gzip = Tool('before_finalize', encoding.gzip, priority = 80)
  313. _d.staticdir = HandlerTool(static.staticdir)
  314. _d.staticfile = HandlerTool(static.staticfile)
  315. _d.sessions = SessionTool()
  316. _d.xmlrpc = ErrorTool(_xmlrpc.on_error)
  317. _d.wsgiapp = WSGIAppTool(_wsgiapp.run)
  318. _d.caching = CachingTool('before_handler', _caching.get, 'caching')
  319. _d.expires = Tool('before_finalize', _caching.expires)
  320. _d.tidy = Tool('before_finalize', tidy.tidy)
  321. _d.nsgmls = Tool('before_finalize', tidy.nsgmls)
  322. _d.ignore_headers = Tool('before_request_body', cptools.ignore_headers)
  323. _d.referer = Tool('before_request_body', cptools.referer)
  324. _d.basic_auth = Tool('on_start_resource', auth.basic_auth)
  325. _d.digest_auth = Tool('on_start_resource', auth.digest_auth)
  326. _d.trailing_slash = Tool('before_handler', cptools.trailing_slash, priority = 60)
  327. _d.flatten = Tool('before_finalize', cptools.flatten)
  328. _d.accept = Tool('on_start_resource', cptools.accept)
  329. _d.redirect = Tool('on_start_resource', cptools.redirect)
  330. del _d
  331. del cptools
  332. del encoding
  333. del auth
  334. del static
  335. del tidy
  336.