<s> """<STR_LIT>""" <EOL> import string <EOL> import sys <EOL> import types <EOL> try : <EOL> classtype = ( type , types . ClassType ) <EOL> except AttributeError : <EOL> classtype = type <EOL> import cherrypy <EOL> from cherrypy . _cpcompat import set <EOL> class PageHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , callable , * args , ** kwargs ) : <EOL> self . callable = callable <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> def __call__ ( self ) : <EOL> try : <EOL> return self . callable ( * self . args , ** self . kwargs ) <EOL> except TypeError : <EOL> x = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> try : <EOL> test_callable_spec ( self . callable , self . args , self . kwargs ) <EOL> except cherrypy . HTTPError : <EOL> raise sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> except : <EOL> raise x <EOL> raise <EOL> def test_callable_spec ( callable , callable_args , callable_kwargs ) : <EOL> """<STR_LIT>""" <EOL> show_mismatched_params = getattr ( <EOL> cherrypy . serving . request , '<STR_LIT>' , False ) <EOL> try : <EOL> ( args , varargs , varkw , defaults ) = inspect . getargspec ( callable ) <EOL> except TypeError : <EOL> if isinstance ( callable , object ) and hasattr ( callable , '<STR_LIT>' ) : <EOL> ( args , varargs , varkw , defaults ) = inspect . getargspec ( callable . __call__ ) <EOL> else : <EOL> raise <EOL> if args and args [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> args = args [ <NUM_LIT:1> : ] <EOL> arg_usage = dict ( [ ( arg , <NUM_LIT:0> , ) for arg in args ] ) <EOL> vararg_usage = <NUM_LIT:0> <EOL> varkw_usage = <NUM_LIT:0> <EOL> extra_kwargs = set ( ) <EOL> for i , value in enumerate ( callable_args ) : <EOL> try : <EOL> arg_usage [ args [ i ] ] += <NUM_LIT:1> <EOL> except IndexError : <EOL> vararg_usage += <NUM_LIT:1> <EOL> for key in callable_kwargs . keys ( ) : <EOL> try : <EOL> arg_usage [ key ] += <NUM_LIT:1> <EOL> except KeyError : <EOL> varkw_usage += <NUM_LIT:1> <EOL> extra_kwargs . add ( key ) <EOL> args_with_defaults = args [ - len ( defaults or [ ] ) : ] <EOL> for i , val in enumerate ( defaults or [ ] ) : <EOL> if arg_usage [ args_with_defaults [ i ] ] == <NUM_LIT:0> : <EOL> arg_usage [ args_with_defaults [ i ] ] += <NUM_LIT:1> <EOL> missing_args = [ ] <EOL> multiple_args = [ ] <EOL> for key , usage in arg_usage . items ( ) : <EOL> if usage == <NUM_LIT:0> : <EOL> missing_args . append ( key ) <EOL> elif usage > <NUM_LIT:1> : <EOL> multiple_args . append ( key ) <EOL> if missing_args : <EOL> message = None <EOL> if show_mismatched_params : <EOL> message = "<STR_LIT>" % "<STR_LIT:U+002C>" . join ( missing_args ) <EOL> raise cherrypy . HTTPError ( <NUM_LIT> , message = message ) <EOL> if not varargs and vararg_usage > <NUM_LIT:0> : <EOL> raise cherrypy . HTTPError ( <NUM_LIT> ) <EOL> body_params = cherrypy . serving . request . body . params or { } <EOL> body_params = set ( body_params . keys ( ) ) <EOL> qs_params = set ( callable_kwargs . keys ( ) ) - body_params <EOL> if multiple_args : <EOL> if qs_params . intersection ( set ( multiple_args ) ) : <EOL> error = <NUM_LIT> <EOL> else : <EOL> error = <NUM_LIT> <EOL> message = None <EOL> if show_mismatched_params : <EOL> message = "<STR_LIT>" "<STR_LIT:%s>" % "<STR_LIT:U+002C>" . join ( multiple_args ) <EOL> raise cherrypy . HTTPError ( error , message = message ) <EOL> if not varkw and varkw_usage > <NUM_LIT:0> : <EOL> extra_qs_params = set ( qs_params ) . intersection ( extra_kwargs ) <EOL> if extra_qs_params : <EOL> message = None <EOL> if show_mismatched_params : <EOL> message = "<STR_LIT>" "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( extra_qs_params ) <EOL> raise cherrypy . HTTPError ( <NUM_LIT> , message = message ) <EOL> extra_body_params = set ( body_params ) . intersection ( extra_kwargs ) <EOL> if extra_body_params : <EOL> message = None <EOL> if show_mismatched_params : <EOL> message = "<STR_LIT>" "<STR_LIT:%s>" % "<STR_LIT:U+002CU+0020>" . join ( extra_body_params ) <EOL> raise cherrypy . HTTPError ( <NUM_LIT> , message = message ) <EOL> try : <EOL> import inspect <EOL> except ImportError : <EOL> test_callable_spec = lambda callable , args , kwargs : None <EOL> class LateParamPageHandler ( PageHandler ) : <EOL> """<STR_LIT>""" <EOL> def _get_kwargs ( self ) : <EOL> kwargs = cherrypy . serving . request . params . copy ( ) <EOL> if self . _kwargs : <EOL> kwargs . update ( self . _kwargs ) <EOL> return kwargs <EOL> def _set_kwargs ( self , kwargs ) : <EOL> self . _kwargs = kwargs <EOL> kwargs = property ( _get_kwargs , _set_kwargs , <EOL> doc = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> punctuation_to_underscores = string . maketrans ( <EOL> string . punctuation , '<STR_LIT:_>' * len ( string . punctuation ) ) <EOL> def validate_translator ( t ) : <EOL> if not isinstance ( t , str ) or len ( t ) != <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> punctuation_to_underscores = str . maketrans ( <EOL> string . punctuation , '<STR_LIT:_>' * len ( string . punctuation ) ) <EOL> def validate_translator ( t ) : <EOL> if not isinstance ( t , dict ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> class Dispatcher ( object ) : <EOL> """<STR_LIT>""" <EOL> dispatch_method_name = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dispatch_method_name = None , <EOL> translate = punctuation_to_underscores ) : <EOL> validate_translator ( translate ) <EOL> self . translate = translate <EOL> if dispatch_method_name : <EOL> self . dispatch_method_name = dispatch_method_name <EOL> def __call__ ( self , path_info ) : <EOL> """<STR_LIT>""" <EOL> request = cherrypy . serving . request <EOL> func , vpath = self . find_handler ( path_info ) <EOL> if func : <EOL> vpath = [ x . replace ( "<STR_LIT>" , "<STR_LIT:/>" ) for x in vpath ] <EOL> request . handler = LateParamPageHandler ( func , * vpath ) <EOL> else : <EOL> request . handler = cherrypy . NotFound ( ) <EOL> def find_handler ( self , path ) : <EOL> """<STR_LIT>""" <EOL> request = cherrypy . serving . request <EOL> app = request . app <EOL> root = app . root <EOL> dispatch_name = self . dispatch_method_name <EOL> fullpath = [ x for x in path . strip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' ) if x ] + [ '<STR_LIT:index>' ] <EOL> fullpath_len = len ( fullpath ) <EOL> segleft = fullpath_len <EOL> nodeconf = { } <EOL> if hasattr ( root , "<STR_LIT>" ) : <EOL> nodeconf . update ( root . _cp_config ) <EOL> if "<STR_LIT:/>" in app . config : <EOL> nodeconf . update ( app . config [ "<STR_LIT:/>" ] ) <EOL> object_trail = [ [ '<STR_LIT:root>' , root , nodeconf , segleft ] ] <EOL> node = root <EOL> iternames = fullpath [ : ] <EOL> while iternames : <EOL> name = iternames [ <NUM_LIT:0> ] <EOL> objname = name . translate ( self . translate ) <EOL> nodeconf = { } <EOL> subnode = getattr ( node , objname , None ) <EOL> pre_len = len ( iternames ) <EOL> if subnode is None : <EOL> dispatch = getattr ( node , dispatch_name , None ) <EOL> if dispatch and hasattr ( dispatch , '<STR_LIT>' ) and not getattr ( dispatch , '<STR_LIT>' , False ) and pre_len > <NUM_LIT:1> : <EOL> index_name = iternames . pop ( ) <EOL> subnode = dispatch ( vpath = iternames ) <EOL> iternames . append ( index_name ) <EOL> else : <EOL> iternames . pop ( <NUM_LIT:0> ) <EOL> else : <EOL> iternames . pop ( <NUM_LIT:0> ) <EOL> segleft = len ( iternames ) <EOL> if segleft > pre_len : <EOL> raise cherrypy . CherryPyException ( <EOL> "<STR_LIT>" <EOL> + "<STR_LIT>" <EOL> + "<STR_LIT>" . format ( name , fullpath ) <EOL> ) <EOL> elif segleft == pre_len : <EOL> iternames . pop ( <NUM_LIT:0> ) <EOL> segleft -= <NUM_LIT:1> <EOL> node = subnode <EOL> if node is not None : <EOL> if hasattr ( node , "<STR_LIT>" ) : <EOL> nodeconf . update ( node . _cp_config ) <EOL> existing_len = fullpath_len - pre_len <EOL> if existing_len != <NUM_LIT:0> : <EOL> curpath = '<STR_LIT:/>' + '<STR_LIT:/>' . join ( fullpath [ <NUM_LIT:0> : existing_len ] ) <EOL> else : <EOL> curpath = '<STR_LIT>' <EOL> new_segs = fullpath [ fullpath_len - pre_len : fullpath_len - segleft ] <EOL> for seg in new_segs : <EOL> curpath += '<STR_LIT:/>' + seg <EOL> if curpath in app . config : <EOL> nodeconf . update ( app . config [ curpath ] ) <EOL> object_trail . append ( [ name , node , nodeconf , segleft ] ) <EOL> def set_conf ( ) : <EOL> """<STR_LIT>""" <EOL> base = cherrypy . config . copy ( ) <EOL> for name , obj , conf , segleft in object_trail : <EOL> base . update ( conf ) <EOL> if '<STR_LIT>' in conf : <EOL> base [ '<STR_LIT>' ] = '<STR_LIT:/>' + '<STR_LIT:/>' . join ( fullpath [ <NUM_LIT:0> : fullpath_len - segleft ] ) <EOL> return base <EOL> num_candidates = len ( object_trail ) - <NUM_LIT:1> <EOL> for i in range ( num_candidates , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> name , candidate , nodeconf , segleft = object_trail [ i ] <EOL> if candidate is None : <EOL> continue <EOL> if hasattr ( candidate , "<STR_LIT:default>" ) : <EOL> defhandler = candidate . default <EOL> if getattr ( defhandler , '<STR_LIT>' , False ) : <EOL> conf = getattr ( defhandler , "<STR_LIT>" , { } ) <EOL> object_trail . insert ( i + <NUM_LIT:1> , [ "<STR_LIT:default>" , defhandler , conf , segleft ] ) <EOL> request . config = set_conf ( ) <EOL> request . is_index = path . endswith ( "<STR_LIT:/>" ) <EOL> return defhandler , fullpath [ fullpath_len - segleft : - <NUM_LIT:1> ] <EOL> if getattr ( candidate , '<STR_LIT>' , False ) : <EOL> request . config = set_conf ( ) <EOL> if i == num_candidates : <EOL> request . is_index = True <EOL> else : <EOL> request . is_index = False <EOL> return candidate , fullpath [ fullpath_len - segleft : - <NUM_LIT:1> ] <EOL> request . config = set_conf ( ) <EOL> return None , [ ] <EOL> class MethodDispatcher ( Dispatcher ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , path_info ) : <EOL> """<STR_LIT>""" <EOL> request = cherrypy . serving . request <EOL> resource , vpath = self . find_handler ( path_info ) <EOL> if resource : <EOL> avail = [ m for m in dir ( resource ) if m . isupper ( ) ] <EOL> if "<STR_LIT:GET>" in avail and "<STR_LIT>" not in avail : <EOL> avail . append ( "<STR_LIT>" ) <EOL> avail . sort ( ) <EOL> cherrypy . serving . response . headers [ '<STR_LIT>' ] = "<STR_LIT:U+002CU+0020>" . join ( avail ) <EOL> meth = request . method . upper ( ) <EOL> func = getattr ( resource , meth , None ) <EOL> if func is None and meth == "<STR_LIT>" : <EOL> func = getattr ( resource , "<STR_LIT:GET>" , None ) <EOL> if func : <EOL> if hasattr ( func , "<STR_LIT>" ) : <EOL> request . config . update ( func . _cp_config ) <EOL> vpath = [ x . replace ( "<STR_LIT>" , "<STR_LIT:/>" ) for x in vpath ] <EOL> request . handler = LateParamPageHandler ( func , * vpath ) <EOL> else : <EOL> request . handler = cherrypy . HTTPError ( <NUM_LIT> ) <EOL> else : <EOL> request . handler = cherrypy . NotFound ( ) <EOL> class RoutesDispatcher ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , full_result = False ) : <EOL> """<STR_LIT>""" <EOL> import routes <EOL> self . full_result = full_result <EOL> self . controllers = { } <EOL> self . mapper = routes . Mapper ( ) <EOL> self . mapper . controller_scan = self . controllers . keys <EOL> def connect ( self , name , route , controller , ** kwargs ) : <EOL> self . controllers [ name ] = controller <EOL> self . mapper . connect ( name , route , controller = name , ** kwargs ) <EOL> def redirect ( self , url ) : <EOL> raise cherrypy . HTTPRedirect ( url ) <EOL> def __call__ ( self , path_info ) : <EOL> """<STR_LIT>""" <EOL> func = self . find_handler ( path_info ) <EOL> if func : <EOL> cherrypy . serving . request . handler = LateParamPageHandler ( func ) <EOL> else : <EOL> cherrypy . serving . request . handler = cherrypy . NotFound ( ) <EOL> def find_handler ( self , path_info ) : <EOL> """<STR_LIT>""" <EOL> import routes <EOL> request = cherrypy . serving . request <EOL> config = routes . request_config ( ) <EOL> config . mapper = self . mapper <EOL> if hasattr ( request , '<STR_LIT>' ) : <EOL> config . environ = request . wsgi_environ <EOL> config . host = request . headers . get ( '<STR_LIT>' , None ) <EOL> config . protocol = request . scheme <EOL> config . redirect = self . redirect <EOL> result = self . mapper . match ( path_info ) <EOL> config . mapper_dict = result <EOL> params = { } <EOL> if result : <EOL> params = result . copy ( ) <EOL> if not self . full_result : <EOL> params . pop ( '<STR_LIT>' , None ) <EOL> params . pop ( '<STR_LIT:action>' , None ) <EOL> request . params . update ( params ) <EOL> request . config = base = cherrypy . config . copy ( ) <EOL> curpath = "<STR_LIT>" <EOL> def merge ( nodeconf ) : <EOL> if '<STR_LIT>' in nodeconf : <EOL> nodeconf [ '<STR_LIT>' ] = curpath or "<STR_LIT:/>" <EOL> base . update ( nodeconf ) <EOL> app = request . app <EOL> root = app . root <EOL> if hasattr ( root , "<STR_LIT>" ) : <EOL> merge ( root . _cp_config ) <EOL> if "<STR_LIT:/>" in app . config : <EOL> merge ( app . config [ "<STR_LIT:/>" ] ) <EOL> atoms = [ x for x in path_info . split ( "<STR_LIT:/>" ) if x ] <EOL> if atoms : <EOL> last = atoms . pop ( ) <EOL> else : <EOL> last = None <EOL> for atom in atoms : <EOL> curpath = "<STR_LIT:/>" . join ( ( curpath , atom ) ) <EOL> if curpath in app . config : <EOL> merge ( app . config [ curpath ] ) <EOL> handler = None <EOL> if result : <EOL> controller = result . get ( '<STR_LIT>' ) <EOL> controller = self . controllers . get ( controller , controller ) <EOL> if controller : <EOL> if isinstance ( controller , classtype ) : <EOL> controller = controller ( ) <EOL> if hasattr ( controller , "<STR_LIT>" ) : <EOL> merge ( controller . _cp_config ) <EOL> action = result . get ( '<STR_LIT:action>' ) <EOL> if action is not None : <EOL> handler = getattr ( controller , action , None ) <EOL> if hasattr ( handler , "<STR_LIT>" ) : <EOL> merge ( handler . _cp_config ) <EOL> else : <EOL> handler = controller <EOL> if last : <EOL> curpath = "<STR_LIT:/>" . join ( ( curpath , last ) ) <EOL> if curpath in app . config : <EOL> merge ( app . config [ curpath ] ) <EOL> return handler <EOL> def XMLRPCDispatcher ( next_dispatcher = Dispatcher ( ) ) : <EOL> from cherrypy . lib import xmlrpcutil <EOL> def xmlrpc_dispatch ( path_info ) : <EOL> path_info = xmlrpcutil . patched_path ( path_info ) <EOL> return next_dispatcher ( path_info ) <EOL> return xmlrpc_dispatch <EOL> def VirtualHost ( next_dispatcher = Dispatcher ( ) , use_x_forwarded_host = True , ** domains ) : <EOL> """<STR_LIT>""" <EOL> from cherrypy . lib import httputil <EOL> def vhost_dispatch ( path_info ) : <EOL> request = cherrypy . serving . request <EOL> header = request . headers . get <EOL> domain = header ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if use_x_forwarded_host : <EOL> domain = header ( "<STR_LIT>" , domain ) <EOL> prefix = domains . get ( domain , "<STR_LIT>" ) <EOL> if prefix : <EOL> path_info = httputil . urljoin ( prefix , path_info ) <EOL> result = next_dispatcher ( path_info ) <EOL> section = request . config . get ( '<STR_LIT>' ) <EOL> if section : <EOL> section = section [ len ( prefix ) : ] <EOL> request . config [ '<STR_LIT>' ] = section <EOL> return result <EOL> return vhost_dispatch </s>
