Package turbogears :: Module dispatchers

Source Code for Module turbogears.dispatchers

 1  """Standard TurboGears request dispatchers for CherryPy.""" 
 2  
 
 3  __all__ = ['VirtualPathDispatcher'] 
 4  
 
 5  from cherrypy import NotFound, dispatch, request 
 6  
 
 7  
 
8 -class VirtualPathDispatcher(object):
9 """Dispatcher that makes CherryPy ignorant of a URL root path. 10 11 That is, you can mount your app so the URI "/users/~rdel/myapp/" maps to 12 the root object "/". 13 14 Note that this can not be done by a hook since they are run too late. 15 16 """ 17
18 - def __init__(self, next_dispatcher=None, webpath=''):
19 self.next_dispatcher = next_dispatcher or dispatch.Dispatcher() 20 webpath = webpath.strip('/') 21 if webpath: 22 webpath = '/' + webpath 23 self.webpath = webpath
24
25 - def __call__(self, path_info):
26 """Determine the relevant path info by stripping off prefixes. 27 28 Strips webpath and request.script_name from request.path_info. 29 30 """ 31 webpath = self.webpath 32 try: 33 webpath += request.script_name.rstrip('/') 34 except AttributeError: 35 pass 36 if webpath: 37 try: 38 if path_info.startswith(webpath + '/'): 39 request.path_info = path_info = path_info[len(webpath):] 40 else: 41 if not request.prev and not request.wsgi_environ[ 42 'HTTP_X_FORWARDED_SERVER']: 43 # check for webpath only if not forwarded 44 raise KeyError 45 except (AttributeError, KeyError): 46 request.handler = NotFound() 47 else: 48 request.script_name = webpath 49 return self.next_dispatcher(path_info)
50