Stickum

My Stickum is mine.
Python code pasted @ 15:11 on Fri, 05 Jan 07
Syntaxed up Plain Text
1
import cherrypy
2
from cherrypy import _cptree, _cpengine, engine
3
4
__all__ = ["Tree", "start_cp_engine"]
5
6
class Tree(_cptree.Tree):
7
    """Extends CherryPy's  _cptree.Tree to set the correct PATH_INFO
8
    and SCRIPT_NAME to support a SCRIPT_NAME != '' (when not mounted
9
    at the stack's root)."""
10
    def __call__(self, environ, start_response):
11
        # Try to look up the app using *only* path_info
12
        path = environ.get('PATH_INFO', '/')
13
        sn = self.script_name(path)
14
        if sn is None:
15
            start_response('404 Not Found', [])
16
            return ["Path %s was not found" % path]
17
        
18
        # Correct the SCRIPT_NAME and PATH_INFO environ entries.
19
        environ = environ.copy()
20
        # We concat script_name + path_info
21
        environ['SCRIPT_NAME'] = environ.get('SCRIPT_NAME', '') + sn
22
        environ['PATH_INFO'] = path[len(sn.rstrip("/")):]
23
24
        app = self.apps[sn]
25
        return app(environ, start_response)
26
27
    def mount(self, root, script_name="", config=None):
28
        if isinstance(root, _cptree.Application):
29
            app = root
30
        else:
31
            # Set script_name to None so apps always use environ['SCRIPT_NAME']
32
            app = _cptree.Application(root, None)
33
        super(Tree, self).mount(app, script_name, config)
34
        
35
36
def start_cp_engine():
37
    """Starts the CP engine (if not started already) letting paster 
38
    handle autoreloading and disabling config. checking."""
39
    engine.autoreload_on = False
40
    engine.checker = None
41
    #XXX Server won't exit on Interrupt if deadlock_poll_freq > 0
42
    engine.deadlock_poll_freq = -1
43
    if engine.state not in [_cpengine.STARTED, _cpengine.STARTING]:
44
        engine.start(blocking=False)