home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import cherrypy
-
- def _getargs(func):
- import types
- if isinstance(func, types.MethodType):
- func = func.im_func
-
- co = func.func_code
- return co.co_varnames[:co.co_argcount]
-
-
- class Tool(object):
- namespace = 'tools'
-
- def __init__(self, point, callable, name = None, priority = 50):
- self._point = point
- self.callable = callable
- self._name = name
- self._priority = priority
- self.__doc__ = self.callable.__doc__
- self._setargs()
-
-
- def _setargs(self):
-
- try:
- for arg in _getargs(self.callable):
- setattr(self, arg, None)
- except (TypeError, AttributeError):
- if hasattr(self.callable, '__call__'):
- for arg in _getargs(self.callable.__call__):
- setattr(self, arg, None)
-
-
- except NotImplementedError:
- pass
- except IndexError:
- pass
-
-
-
- def _merged_args(self, d = None):
- if d:
- conf = d.copy()
- else:
- conf = { }
- tm = cherrypy.request.toolmaps[self.namespace]
- if self._name in tm:
- conf.update(tm[self._name])
-
- if 'on' in conf:
- del conf['on']
-
- return conf
-
-
- def __call__(self, *args, **kwargs):
- if args:
- raise TypeError('The %r Tool does not accept positional arguments; you must use keyword arguments.' % self._name)
- args
-
- def tool_decorator(f):
- if not hasattr(f, '_cp_config'):
- f._cp_config = { }
-
- subspace = self.namespace + '.' + self._name + '.'
- f._cp_config[subspace + 'on'] = True
- for k, v in kwargs.iteritems():
- f._cp_config[subspace + k] = v
-
- return f
-
- return tool_decorator
-
-
- def _setup(self):
- conf = self._merged_args()
- p = conf.pop('priority', None)
- if p is None:
- p = getattr(self.callable, 'priority', self._priority)
-
- cherrypy.request.hooks.attach(self._point, self.callable, priority = p, **conf)
-
-
-
- class HandlerTool(Tool):
-
- def __init__(self, callable, name = None):
- Tool.__init__(self, 'before_handler', callable, name)
-
-
- def handler(self, *args, **kwargs):
-
- def handle_func(*a, **kw):
- handled = self.callable(*args, **self._merged_args(kwargs))
- if not handled:
- raise cherrypy.NotFound()
- handled
- return cherrypy.response.body
-
- handle_func.exposed = True
- return handle_func
-
-
- def _wrapper(self, **kwargs):
- if self.callable(**kwargs):
- cherrypy.request.handler = None
-
-
-
- def _setup(self):
- conf = self._merged_args()
- p = conf.pop('priority', None)
- if p is None:
- p = getattr(self.callable, 'priority', self._priority)
-
- cherrypy.request.hooks.attach(self._point, self._wrapper, priority = p, **conf)
-
-
-
- class HandlerWrapperTool(Tool):
-
- def __init__(self, newhandler, point = 'before_handler', name = None, priority = 50):
- self.newhandler = newhandler
- self._point = point
- self._name = name
- self._priority = priority
-
-
- def callable(self):
- innerfunc = cherrypy.request.handler
-
- def wrap(*args, **kwargs):
- return self.newhandler(innerfunc, *args, **kwargs)
-
- cherrypy.request.handler = wrap
-
-
-
- class ErrorTool(Tool):
-
- def __init__(self, callable, name = None):
- Tool.__init__(self, None, callable, name)
-
-
- def _wrapper(self):
- self.callable(**self._merged_args())
-
-
- def _setup(self):
- cherrypy.request.error_response = self._wrapper
-
-
- from cherrypy.lib import cptools, encoding, auth, static, tidy
- from cherrypy.lib import sessions as _sessions, xmlrpc as _xmlrpc
- from cherrypy.lib import caching as _caching, wsgiapp as _wsgiapp
-
- class SessionTool(Tool):
-
- def __init__(self):
- Tool.__init__(self, 'before_request_body', _sessions.init)
-
-
- def _lock_session(self):
- cherrypy.serving.session.acquire_lock()
-
-
- def _setup(self):
- hooks = cherrypy.request.hooks
- conf = self._merged_args()
- p = conf.pop('priority', None)
- if p is None:
- p = getattr(self.callable, 'priority', self._priority)
-
- hooks.attach(self._point, self.callable, priority = p, **conf)
- locking = conf.pop('locking', 'implicit')
- if locking == 'implicit':
- hooks.attach('before_handler', self._lock_session)
- elif locking == 'early':
- hooks.attach('before_request_body', self._lock_session, priority = 60)
-
- hooks.attach('before_finalize', _sessions.save)
- hooks.attach('on_end_request', _sessions.close)
-
-
- def regenerate(self):
- sess = cherrypy.serving.session
- sess.regenerate()
- conf = [](_[1])
- _sessions.set_response_cookie(**conf)
-
-
-
- class XMLRPCController(object):
- _cp_config = {
- 'tools.xmlrpc.on': True }
-
- def default(self, *vpath, **params):
- (rpcparams, rpcmethod) = _xmlrpc.process_body()
- subhandler = self
- for attr in str(rpcmethod).split('.'):
- subhandler = getattr(subhandler, attr, None)
-
- if subhandler and getattr(subhandler, 'exposed', False):
- body = subhandler(*vpath + rpcparams, **params)
- else:
- raise Exception, 'method "%s" is not supported' % attr
- conf = getattr(subhandler, 'exposed', False).request.toolmaps['tools'].get('xmlrpc', { })
- _xmlrpc.respond(body, conf.get('encoding', 'utf-8'), conf.get('allow_none', 0))
- return cherrypy.response.body
-
- default.exposed = True
-
-
- class WSGIAppTool(HandlerTool):
-
- def _setup(self):
- cherrypy.request.process_request_body = False
- HandlerTool._setup(self)
-
-
-
- class SessionAuthTool(HandlerTool):
-
- def _setargs(self):
- for name in dir(cptools.SessionAuth):
- if not name.startswith('__'):
- setattr(self, name, None)
- continue
-
-
-
-
- class CachingTool(Tool):
-
- def _wrapper(self, invalid_methods = ('POST', 'PUT', 'DELETE'), **kwargs):
- request = cherrypy.request
- if not hasattr(cherrypy, '_cache'):
- cherrypy._cache = kwargs.pop('cache_class', _caching.MemoryCache)()
- for k, v in kwargs.iteritems():
- setattr(cherrypy._cache, k, v)
-
-
- if _caching.get(invalid_methods = invalid_methods):
- request.handler = None
- elif request.cacheable:
- request.hooks.attach('before_finalize', _caching.tee_output, priority = 90)
-
-
- _wrapper.priority = 20
-
- def _setup(self):
- conf = self._merged_args()
- p = conf.pop('priority', None)
- cherrypy.request.hooks.attach('before_handler', self._wrapper, priority = p, **conf)
-
-
-
- class Toolbox(object):
-
- def __init__(self, namespace):
- self.namespace = namespace
-
-
- def __setattr__(self, name, value):
- if isinstance(value, Tool):
- if value._name is None:
- value._name = name
-
- value.namespace = self.namespace
-
- object.__setattr__(self, name, value)
-
-
- def __enter__(self):
- cherrypy.request.toolmaps[self.namespace] = map = { }
-
- def populate(k, v):
- (toolname, arg) = k.split('.', 1)
- bucket = map.setdefault(toolname, { })
- bucket[arg] = v
-
- return populate
-
-
- def __exit__(self, exc_type, exc_val, exc_tb):
- map = cherrypy.request.toolmaps.get(self.namespace)
- if map:
- for name, settings in map.items():
- if settings.get('on', False):
- tool = getattr(self, name)
- tool._setup()
- continue
-
-
-
-
- default_toolbox = _d = Toolbox('tools')
- _d.session_auth = SessionAuthTool(cptools.session_auth)
- _d.proxy = Tool('before_request_body', cptools.proxy, priority = 30)
- _d.response_headers = Tool('on_start_resource', cptools.response_headers)
- _d.log_tracebacks = Tool('before_error_response', cptools.log_traceback)
- _d.log_headers = Tool('before_error_response', cptools.log_request_headers)
- _d.log_hooks = Tool('on_end_request', cptools.log_hooks, priority = 100)
- _d.err_redirect = ErrorTool(cptools.redirect)
- _d.etags = Tool('before_finalize', cptools.validate_etags, priority = 75)
- _d.decode = Tool('before_handler', encoding.decode)
- _d.encode = Tool('before_finalize', encoding.encode, priority = 70)
- _d.gzip = Tool('before_finalize', encoding.gzip, priority = 80)
- _d.staticdir = HandlerTool(static.staticdir)
- _d.staticfile = HandlerTool(static.staticfile)
- _d.sessions = SessionTool()
- _d.xmlrpc = ErrorTool(_xmlrpc.on_error)
- _d.wsgiapp = WSGIAppTool(_wsgiapp.run)
- _d.caching = CachingTool('before_handler', _caching.get, 'caching')
- _d.expires = Tool('before_finalize', _caching.expires)
- _d.tidy = Tool('before_finalize', tidy.tidy)
- _d.nsgmls = Tool('before_finalize', tidy.nsgmls)
- _d.ignore_headers = Tool('before_request_body', cptools.ignore_headers)
- _d.referer = Tool('before_request_body', cptools.referer)
- _d.basic_auth = Tool('on_start_resource', auth.basic_auth)
- _d.digest_auth = Tool('on_start_resource', auth.digest_auth)
- _d.trailing_slash = Tool('before_handler', cptools.trailing_slash, priority = 60)
- _d.flatten = Tool('before_finalize', cptools.flatten)
- _d.accept = Tool('on_start_resource', cptools.accept)
- _d.redirect = Tool('on_start_resource', cptools.redirect)
- del _d
- del cptools
- del encoding
- del auth
- del static
- del tidy
-