<s> """<STR_LIT>""" <EOL> import re <EOL> import urllib <EOL> from webob import exc <EOL> import webapp2 <EOL> class MultiRoute ( object ) : <EOL> """<STR_LIT>""" <EOL> routes = None <EOL> children = None <EOL> match_children = None <EOL> build_children = None <EOL> def __init__ ( self , routes ) : <EOL> self . routes = routes <EOL> def get_children ( self ) : <EOL> if self . children is None : <EOL> self . children = [ ] <EOL> for route in self . routes : <EOL> for r in route . get_routes ( ) : <EOL> self . children . append ( r ) <EOL> for rv in self . children : <EOL> yield rv <EOL> def get_match_children ( self ) : <EOL> if self . match_children is None : <EOL> self . match_children = [ ] <EOL> for route in self . get_children ( ) : <EOL> for r in route . get_match_routes ( ) : <EOL> self . match_children . append ( r ) <EOL> for rv in self . match_children : <EOL> yield rv <EOL> def get_build_children ( self ) : <EOL> if self . build_children is None : <EOL> self . build_children = { } <EOL> for route in self . get_children ( ) : <EOL> for n , r in route . get_build_routes ( ) : <EOL> self . build_children [ n ] = r <EOL> for rv in self . build_children . iteritems ( ) : <EOL> yield rv <EOL> get_routes = get_children <EOL> get_match_routes = get_match_children <EOL> get_build_routes = get_build_children <EOL> class DomainRoute ( MultiRoute ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , template , routes ) : <EOL> """<STR_LIT>""" <EOL> super ( DomainRoute , self ) . __init__ ( routes ) <EOL> self . template = template <EOL> def get_match_routes ( self ) : <EOL> yield self <EOL> def match ( self , request ) : <EOL> host_match = self . regex . match ( request . environ [ '<STR_LIT>' ] ) <EOL> if host_match : <EOL> args , kwargs = webapp2 . _get_route_variables ( host_match ) <EOL> return _match_routes ( self . get_match_children , request , None , <EOL> kwargs ) <EOL> @ webapp2 . cached_property <EOL> def regex ( self ) : <EOL> regex , reverse_template , args_count , kwargs_count , variables = webapp2 . _parse_route_template ( self . template , <EOL> default_sufix = '<STR_LIT>' ) <EOL> return regex <EOL> class NamePrefixRoute ( MultiRoute ) : <EOL> """<STR_LIT>""" <EOL> _attr = '<STR_LIT:name>' <EOL> def __init__ ( self , prefix , routes ) : <EOL> """<STR_LIT>""" <EOL> super ( NamePrefixRoute , self ) . __init__ ( routes ) <EOL> self . prefix = prefix <EOL> for route in self . get_routes ( ) : <EOL> setattr ( route , self . _attr , prefix + getattr ( route , self . _attr ) ) <EOL> class HandlerPrefixRoute ( NamePrefixRoute ) : <EOL> """<STR_LIT>""" <EOL> _attr = '<STR_LIT>' <EOL> class PathPrefixRoute ( NamePrefixRoute ) : <EOL> """<STR_LIT>""" <EOL> _attr = '<STR_LIT>' <EOL> def __init__ ( self , prefix , routes ) : <EOL> """<STR_LIT>""" <EOL> assert prefix . startswith ( '<STR_LIT:/>' ) and not prefix . endswith ( '<STR_LIT:/>' ) , '<STR_LIT>' <EOL> super ( PathPrefixRoute , self ) . __init__ ( prefix , routes ) <EOL> def get_match_routes ( self ) : <EOL> yield self <EOL> def match ( self , request ) : <EOL> if not self . regex . match ( urllib . unquote ( request . path ) ) : <EOL> return None <EOL> return _match_routes ( self . get_match_children , request ) <EOL> @ webapp2 . cached_property <EOL> def regex ( self ) : <EOL> regex , reverse_template , args_count , kwargs_count , variables = webapp2 . _parse_route_template ( self . prefix + '<STR_LIT>' ) <EOL> return regex <EOL> class RedirectRoute ( webapp2 . Route ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , template , handler = None , name = None , defaults = None , <EOL> build_only = False , handler_method = None , methods = None , <EOL> schemes = None , redirect_to = None , redirect_to_name = None , <EOL> strict_slash = False ) : <EOL> """<STR_LIT>""" <EOL> super ( RedirectRoute , self ) . __init__ ( <EOL> template , handler = handler , name = name , defaults = defaults , <EOL> build_only = build_only , handler_method = handler_method , <EOL> methods = methods , schemes = schemes ) <EOL> if strict_slash and not name : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . strict_slash = strict_slash <EOL> self . redirect_to_name = redirect_to_name <EOL> if redirect_to is not None : <EOL> assert redirect_to_name is None <EOL> self . handler = webapp2 . RedirectHandler <EOL> self . defaults [ '<STR_LIT>' ] = redirect_to <EOL> def get_match_routes ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . redirect_to_name : <EOL> main_route = self . _get_redirect_route ( name = self . redirect_to_name ) <EOL> else : <EOL> main_route = self <EOL> if not self . build_only : <EOL> if self . strict_slash is True : <EOL> if self . template . endswith ( '<STR_LIT:/>' ) : <EOL> template = self . template [ : - <NUM_LIT:1> ] <EOL> else : <EOL> template = self . template + '<STR_LIT:/>' <EOL> yield main_route <EOL> yield self . _get_redirect_route ( template = template ) <EOL> else : <EOL> yield main_route <EOL> def _get_redirect_route ( self , template = None , name = None ) : <EOL> template = template or self . template <EOL> name = name or self . name <EOL> defaults = self . defaults . copy ( ) <EOL> defaults . update ( { <EOL> '<STR_LIT>' : self . _redirect , <EOL> '<STR_LIT>' : name , <EOL> } ) <EOL> new_route = webapp2 . Route ( template , webapp2 . RedirectHandler , <EOL> defaults = defaults ) <EOL> return new_route <EOL> def _redirect ( self , handler , * args , ** kwargs ) : <EOL> kwargs . pop ( '<STR_LIT>' , None ) <EOL> kwargs . pop ( '<STR_LIT>' , None ) <EOL> return handler . uri_for ( kwargs . pop ( '<STR_LIT>' ) , * args , ** kwargs ) <EOL> def _match_routes ( iter_func , request , extra_args = None , extra_kwargs = None ) : <EOL> """<STR_LIT>""" <EOL> method_not_allowed = False <EOL> for route in iter_func ( ) : <EOL> try : <EOL> match = route . match ( request ) <EOL> if match : <EOL> route , args , kwargs = match <EOL> if extra_args : <EOL> args += extra_args <EOL> if extra_kwargs : <EOL> kwargs . update ( extra_kwargs ) <EOL> return route , args , kwargs <EOL> except exc . HTTPMethodNotAllowed : <EOL> method_not_allowed = True <EOL> if method_not_allowed : <EOL> raise exc . HTTPMethodNotAllowed ( ) </s>
