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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import cherrypy
  6. from cherrypy import _cpconfig, _cplogging, _cprequest, _cpwsgi, tools
  7. from cherrypy.lib import http as _http
  8.  
  9. class Application(object):
  10.     __metaclass__ = cherrypy._AttributeDocstrings
  11.     root = None
  12.     root__doc = '\n    The top-most container of page handlers for this app. Handlers should\n    be arranged in a hierarchy of attributes, matching the expected URI\n    hierarchy; the default dispatcher then searches this hierarchy for a\n    matching handler. When using a dispatcher other than the default,\n    this value may be None.'
  13.     config = { }
  14.     config__doc = "\n    A dict of {path: pathconf} pairs, where 'pathconf' is itself a dict\n    of {key: value} pairs."
  15.     namespaces = _cpconfig.NamespaceSet()
  16.     toolboxes = {
  17.         'tools': cherrypy.tools }
  18.     log = None
  19.     log__doc = 'A LogManager instance. See _cplogging.'
  20.     wsgiapp = None
  21.     wsgiapp__doc = 'A CPWSGIApp instance. See _cpwsgi.'
  22.     request_class = _cprequest.Request
  23.     response_class = _cprequest.Response
  24.     relative_urls = False
  25.     
  26.     def __init__(self, root, script_name = '', config = None):
  27.         self.log = _cplogging.LogManager(id(self), cherrypy.log.logger_root)
  28.         self.root = root
  29.         self.script_name = script_name
  30.         self.wsgiapp = _cpwsgi.CPWSGIApp(self)
  31.         self.namespaces = self.namespaces.copy()
  32.         
  33.         self.namespaces['log'] = lambda k, v: setattr(self.log, k, v)
  34.         self.namespaces['wsgi'] = self.wsgiapp.namespace_handler
  35.         self.config = self.__class__.config.copy()
  36.         if config:
  37.             self.merge(config)
  38.         
  39.  
  40.     
  41.     def __repr__(self):
  42.         return '%s.%s(%r, %r)' % (self.__module__, self.__class__.__name__, self.root, self.script_name)
  43.  
  44.     script_name__doc = '\n    The URI "mount point" for this app. A mount point is that portion of\n    the URI which is constant for all URIs that are serviced by this\n    application; it does not include scheme, host, or proxy ("virtual host")\n    portions of the URI.\n    \n    For example, if script_name is "/my/cool/app", then the URL\n    "http://www.example.com/my/cool/app/page1" might be handled by a\n    "page1" method on the root object.\n    \n    The value of script_name MUST NOT end in a slash. If the script_name\n    refers to the root of the URI, it MUST be an empty string (not "/").\n    \n    If script_name is explicitly set to None, then the script_name will be\n    provided for each call from request.wsgi_environ[\'SCRIPT_NAME\'].\n    '
  45.     
  46.     def _get_script_name(self):
  47.         if self._script_name is None:
  48.             return cherrypy.request.wsgi_environ['SCRIPT_NAME'].rstrip('/')
  49.         return self._script_name
  50.  
  51.     
  52.     def _set_script_name(self, value):
  53.         if value:
  54.             value = value.rstrip('/')
  55.         
  56.         self._script_name = value
  57.  
  58.     script_name = property(fget = _get_script_name, fset = _set_script_name, doc = script_name__doc)
  59.     
  60.     def merge(self, config):
  61.         _cpconfig.merge(self.config, config)
  62.         self.namespaces(self.config.get('/', { }))
  63.  
  64.     
  65.     def get_serving(self, local, remote, scheme, sproto):
  66.         req = self.request_class(local, remote, scheme, sproto)
  67.         req.app = self
  68.         for name, toolbox in self.toolboxes.iteritems():
  69.             req.namespaces[name] = toolbox
  70.         
  71.         resp = self.response_class()
  72.         cherrypy.serving.load(req, resp)
  73.         cherrypy.engine.timeout_monitor.acquire()
  74.         cherrypy.engine.publish('acquire_thread')
  75.         return (req, resp)
  76.  
  77.     
  78.     def release_serving(self):
  79.         req = cherrypy.serving.request
  80.         cherrypy.engine.timeout_monitor.release()
  81.         
  82.         try:
  83.             req.close()
  84.         except:
  85.             cherrypy.log(traceback = True, severity = 40)
  86.  
  87.         cherrypy.serving.clear()
  88.  
  89.     
  90.     def __call__(self, environ, start_response):
  91.         return self.wsgiapp(environ, start_response)
  92.  
  93.  
  94.  
  95. class Tree(object):
  96.     apps = { }
  97.     apps__doc = '\n    A dict of the form {script name: application}, where "script name"\n    is a string declaring the URI mount point (no trailing slash), and\n    "application" is an instance of cherrypy.Application (or an arbitrary\n    WSGI callable if you happen to be using a WSGI server).'
  98.     
  99.     def __init__(self):
  100.         self.apps = { }
  101.  
  102.     
  103.     def mount(self, root, script_name = '', config = None):
  104.         script_name = script_name.rstrip('/')
  105.         if isinstance(root, Application):
  106.             app = root
  107.             if script_name != '' and script_name != app.script_name:
  108.                 raise ValueError, 'Cannot specify a different script name and pass an Application instance to cherrypy.mount'
  109.             script_name != app.script_name
  110.             script_name = app.script_name
  111.         else:
  112.             app = Application(root, script_name)
  113.             if script_name == '' and root is not None and not hasattr(root, 'favicon_ico'):
  114.                 favicon = os.path.join(os.getcwd(), os.path.dirname(__file__), 'favicon.ico')
  115.                 root.favicon_ico = tools.staticfile.handler(favicon)
  116.             
  117.         if config:
  118.             app.merge(config)
  119.         
  120.         self.apps[script_name] = app
  121.         return app
  122.  
  123.     
  124.     def graft(self, wsgi_callable, script_name = ''):
  125.         script_name = script_name.rstrip('/')
  126.         self.apps[script_name] = wsgi_callable
  127.  
  128.     
  129.     def script_name(self, path = None):
  130.         if path is None:
  131.             
  132.             try:
  133.                 path = _http.urljoin(cherrypy.request.script_name, cherrypy.request.path_info)
  134.             except AttributeError:
  135.                 return None
  136.             
  137.  
  138.         None<EXCEPTION MATCH>AttributeError
  139.         while True:
  140.             if path in self.apps:
  141.                 return path
  142.             if path == '':
  143.                 return None
  144.             path = path[:path.rfind('/')]
  145.             continue
  146.             path == ''
  147.  
  148.     
  149.     def __call__(self, environ, start_response):
  150.         path = _http.urljoin(environ.get('SCRIPT_NAME', ''), environ.get('PATH_INFO', ''))
  151.         if not path:
  152.             pass
  153.         sn = self.script_name('/')
  154.         if sn is None:
  155.             start_response('404 Not Found', [])
  156.             return []
  157.         app = self.apps[sn]
  158.         environ = environ.copy()
  159.         environ['SCRIPT_NAME'] = sn
  160.         environ['PATH_INFO'] = path[len(sn.rstrip('/')):]
  161.         return app(environ, start_response)
  162.  
  163.  
  164.