CherryPy / cherrypy / _cptree.py

Robert Brewer 72c947a 

Robert Brewer 7b8ea0f 
Robert Brewer 72c947a 
Robert Brewer 15f965a 
Robert Brewer f2faf78 
Robert Brewer 1dee819 
Robert Brewer 8058909 
Robert Brewer 7fd8e8c 



Robert Brewer d26d4c9 
Robert Brewer 7fd8e8c 
Robert Brewer d26d4c9 














Robert Brewer 41585da 
Robert Brewer 1a1da0f 
Robert Brewer d26d4c9 





Robert Brewer 8d8f1c5 
Robert Brewer 3e9feb7 
Robert Brewer 1a1da0f 
Robert Brewer d539ac9 
Robert Brewer 15f965a 
Robert Brewer d26d4c9 




Robert Brewer a802f42 
Robert Brewer f2faf78 
Robert Brewer d26d4c9 





Robert Brewer 7fd8e8c 






Robert Brewer d26d4c9 

Robert Brewer 7fd8e8c 
Robert Brewer a802f42 
Robert Brewer 77f9517 
Robert Brewer a802f42 
Robert Brewer 77f9517 
Robert Brewer 8d8f1c5 
Robert Brewer 41585da 
Robert Brewer aebf1d7 
Robert Brewer 7fd8e8c 
Robert Brewer 72c947a 


Robert Brewer f501624 
Robert Brewer 7fd8e8c 




Robert Brewer d26d4c9 
Robert Brewer 8336de3 
Robert Brewer d26d4c9 





Robert Brewer 1dee819 

Robert Brewer 1a1da0f 
Robert Brewer 273d7b7 
Robert Brewer a802f42 

Robert Brewer 5c6cb96 

Robert Brewer 8d8f1c5 











Robert Brewer a802f42 

Robert Brewer 8d8f1c5 
Robert Brewer f2faf78 
Robert Brewer c60cbbc 
Robert Brewer f2faf78 
Robert Brewer 1a1da0f 
Robert Brewer 7fd8e8c 





Robert Brewer 1a1da0f 

Robert Brewer 1dee819 
Robert Brewer ff8c3d5 
Robert Brewer d985b13 



Robert Brewer ff8c3d5 
Robert Brewer d985b13 


Robert Brewer d539ac9 
Robert Brewer 1a1da0f 
Robert Brewer d985b13 

Robert Brewer d539ac9 


Robert Brewer d985b13 
Robert Brewer d539ac9 
Robert Brewer d985b13 
Robert Brewer 7fd8e8c 
Robert Brewer 8058909 















Robert Brewer 1dee819 
"""CherryPy Application and Tree objects."""

import os
import cherrypy
from cherrypy import _cpconfig, _cplogging, _cpwsgi, tools


class Application(object):
    """A CherryPy Application.
    
    An instance of this class may also be used as a WSGI callable
    (WSGI application object) for itself.
    """
    
    __metaclass__ = cherrypy._AttributeDocstrings
    
    root = None
    root__doc = """
    The top-most container of page handlers for this app. Handlers should
    be arranged in a hierarchy of attributes, matching the expected URI
    hierarchy; the default dispatcher then searches this hierarchy for a
    matching handler. When using a dispatcher other than the default,
    this value may be None."""
    
    config = {}
    config__doc = """
    A dict of {path: pathconf} pairs, where 'pathconf' is itself a dict
    of {key: value} pairs."""
    
    namespaces = _cpconfig.NamespaceSet()
    
    log = None
    log__doc = """A LogManager instance. See _cplogging."""
    
    wsgiapp = None
    wsgiapp__doc = """A CPWSGIApp instance. See _cpwsgi."""
    
    def __init__(self, root, script_name=""):
        self.log = _cplogging.LogManager(id(self), cherrypy.log.logger_root)
        self.root = root
        self.script_name = script_name
        self.wsgiapp = _cpwsgi.CPWSGIApp(self)
        
        self.namespaces = self.namespaces.copy()
        self.namespaces["log"] = lambda k, v: setattr(self.log, k, v)
        self.namespaces["wsgi"] = self.wsgiapp.namespace_handler
        
        self.config = {}
    
    script_name__doc = """
    The URI "mount point" for this app; for example, if script_name is
    "/my/cool/app", then the URL "http://my.domain.tld/my/cool/app/page1"
    might be handled by a "page1" method on the root object. If script_name
    is explicitly set to None, then the script_name will be provided
    for each call from request.wsgi_environ['SCRIPT_NAME']."""
    def _get_script_name(self):
        if self._script_name is None:
            # None signals that the script name should be pulled from WSGI environ.
            return cherrypy.request.wsgi_environ['SCRIPT_NAME']
        return self._script_name
    def _set_script_name(self, value):
        self._script_name = value
    script_name = property(fget=_get_script_name, fset=_set_script_name,
                           doc=script_name__doc)
    
    def merge(self, config):
        """Merge the given config into self.config."""
        _cpconfig.merge(self.config, config)
        
        # Handle namespaces specified in config.
        self.namespaces(self.config.get("/", {}))
    
    def __call__(self, environ, start_response):
        return self.wsgiapp(environ, start_response)


class Tree(object):
    """A registry of CherryPy applications, mounted at diverse points.
    
    An instance of this class may also be used as a WSGI callable
    (WSGI application object), in which case it dispatches to all
    mounted apps.
    """
    
    apps = {}
    apps__doc = """
    A dict of the form {script name: application}, where "script name"
    is a string declaring the URI mount point (no trailing slash), and
    "application" is an instance of cherrypy.Application (or an arbitrary
    WSGI callable if you happen to be using a WSGI server)."""
    
    def __init__(self):
        self.apps = {}
    
    def mount(self, root, script_name="", config=None):
        """Mount a new app from a root object, script_name, and config."""
        # Next line both 1) strips trailing slash and 2) maps "/" -> "".
        script_name = script_name.rstrip("/")
        
        if isinstance(root, Application):
            app = root
        else:
            app = Application(root, script_name)
            
            # If mounted at "", add favicon.ico
            if script_name == "" and root and not hasattr(root, "favicon_ico"):
                favicon = os.path.join(os.getcwd(), os.path.dirname(__file__),
                                       "favicon.ico")
                root.favicon_ico = tools.staticfile.handler(favicon)
        
        if config:
            app.merge(config)
        
        self.apps[script_name] = app
        
        return app
    
    def graft(self, wsgi_callable, script_name=""):
        """Mount a wsgi callable at the given script_name."""
        # Next line both 1) strips trailing slash and 2) maps "/" -> "".
        script_name = script_name.rstrip("/")
        self.apps[script_name] = wsgi_callable
    
    def script_name(self, path=None):
        """The script_name of the app at the given path, or None.
        
        If path is None, cherrypy.request is used.
        """
        
        if path is None:
            try:
                path = cherrypy.request.script_name + cherrypy.request.path_info
            except AttributeError:
                return None
        
        while True:
            if path in self.apps:
                return path
            
            if path == "":
                return None
            
            # Move one node up the tree and try again.
            path = path[:path.rfind("/")]
    
    def __call__(self, environ, start_response):
        # If you're calling this, then you're probably setting SCRIPT_NAME
        # to '' (some WSGI servers always set SCRIPT_NAME to '').
        # Try to look up the app using the full path.
        path = environ.get('SCRIPT_NAME', '') + environ.get('PATH_INFO', '')
        sn = self.script_name(path or "/")
        if sn is None:
            start_response('404 Not Found', [])
            return []
        
        app = self.apps[sn]
        
        # Correct the SCRIPT_NAME and PATH_INFO environ entries.
        environ = environ.copy()
        environ['SCRIPT_NAME'] = sn
        environ['PATH_INFO'] = path[len(sn.rstrip("/")):]
        return app(environ, start_response)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.