<s> """<STR_LIT>""" <EOL> import re <EOL> import uuid <EOL> import posixpath <EOL> from pprint import pformat <EOL> from threading import Lock <EOL> from werkzeug . urls import url_encode , url_quote , url_join <EOL> from werkzeug . utils import redirect , format_string <EOL> from werkzeug . exceptions import HTTPException , NotFound , MethodNotAllowed <EOL> from werkzeug . _internal import _get_environ , _encode_idna <EOL> from werkzeug . _compat import itervalues , iteritems , to_unicode , to_bytes , text_type , string_types , native_string_result , implements_to_string , wsgi_decoding_dance <EOL> from werkzeug . datastructures import ImmutableDict , MultiDict <EOL> _rule_re = re . compile ( r'''<STR_LIT>''' , re . VERBOSE ) <EOL> _simple_rule_re = re . compile ( r'<STR_LIT>' ) <EOL> _converter_args_re = re . compile ( r'''<STR_LIT>''' , re . VERBOSE | re . UNICODE ) <EOL> _PYTHON_CONSTANTS = { <EOL> '<STR_LIT:None>' : None , <EOL> '<STR_LIT:True>' : True , <EOL> '<STR_LIT:False>' : False <EOL> } <EOL> def _pythonize ( value ) : <EOL> if value in _PYTHON_CONSTANTS : <EOL> return _PYTHON_CONSTANTS [ value ] <EOL> for convert in int , float : <EOL> try : <EOL> return convert ( value ) <EOL> except ValueError : <EOL> pass <EOL> if value [ : <NUM_LIT:1> ] == value [ - <NUM_LIT:1> : ] and value [ <NUM_LIT:0> ] in '<STR_LIT>' : <EOL> value = value [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> return text_type ( value ) <EOL> def parse_converter_args ( argstr ) : <EOL> argstr += '<STR_LIT:U+002C>' <EOL> args = [ ] <EOL> kwargs = { } <EOL> for item in _converter_args_re . finditer ( argstr ) : <EOL> value = item . group ( '<STR_LIT>' ) <EOL> if value is None : <EOL> value = item . group ( '<STR_LIT:value>' ) <EOL> value = _pythonize ( value ) <EOL> if not item . group ( '<STR_LIT:name>' ) : <EOL> args . append ( value ) <EOL> else : <EOL> name = item . group ( '<STR_LIT:name>' ) <EOL> kwargs [ name ] = value <EOL> return tuple ( args ) , kwargs <EOL> def parse_rule ( rule ) : <EOL> """<STR_LIT>""" <EOL> pos = <NUM_LIT:0> <EOL> end = len ( rule ) <EOL> do_match = _rule_re . match <EOL> used_names = set ( ) <EOL> while pos < end : <EOL> m = do_match ( rule , pos ) <EOL> if m is None : <EOL> break <EOL> data = m . groupdict ( ) <EOL> if data [ '<STR_LIT>' ] : <EOL> yield None , None , data [ '<STR_LIT>' ] <EOL> variable = data [ '<STR_LIT>' ] <EOL> converter = data [ '<STR_LIT>' ] or '<STR_LIT:default>' <EOL> if variable in used_names : <EOL> raise ValueError ( '<STR_LIT>' % variable ) <EOL> used_names . add ( variable ) <EOL> yield converter , data [ '<STR_LIT:args>' ] or None , variable <EOL> pos = m . end ( ) <EOL> if pos < end : <EOL> remaining = rule [ pos : ] <EOL> if '<STR_LIT:>>' in remaining or '<STR_LIT:<>' in remaining : <EOL> raise ValueError ( '<STR_LIT>' % rule ) <EOL> yield None , None , remaining <EOL> class RoutingException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class RequestRedirect ( HTTPException , RoutingException ) : <EOL> """<STR_LIT>""" <EOL> code = <NUM_LIT> <EOL> def __init__ ( self , new_url ) : <EOL> RoutingException . __init__ ( self , new_url ) <EOL> self . new_url = new_url <EOL> def get_response ( self , environ ) : <EOL> return redirect ( self . new_url , self . code ) <EOL> class RequestSlash ( RoutingException ) : <EOL> """<STR_LIT>""" <EOL> class RequestAliasRedirect ( RoutingException ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , matched_values ) : <EOL> self . matched_values = matched_values <EOL> class BuildError ( RoutingException , LookupError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , endpoint , values , method ) : <EOL> LookupError . __init__ ( self , endpoint , values , method ) <EOL> self . endpoint = endpoint <EOL> self . values = values <EOL> self . method = method <EOL> class ValidationError ( ValueError ) : <EOL> """<STR_LIT>""" <EOL> class RuleFactory ( object ) : <EOL> """<STR_LIT>""" <EOL> def get_rules ( self , map ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class Subdomain ( RuleFactory ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , subdomain , rules ) : <EOL> self . subdomain = subdomain <EOL> self . rules = rules <EOL> def get_rules ( self , map ) : <EOL> for rulefactory in self . rules : <EOL> for rule in rulefactory . get_rules ( map ) : <EOL> rule = rule . empty ( ) <EOL> rule . subdomain = self . subdomain <EOL> yield rule <EOL> class Submount ( RuleFactory ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path , rules ) : <EOL> self . path = path . rstrip ( '<STR_LIT:/>' ) <EOL> self . rules = rules <EOL> def get_rules ( self , map ) : <EOL> for rulefactory in self . rules : <EOL> for rule in rulefactory . get_rules ( map ) : <EOL> rule = rule . empty ( ) <EOL> rule . rule = self . path + rule . rule <EOL> yield rule <EOL> class EndpointPrefix ( RuleFactory ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , prefix , rules ) : <EOL> self . prefix = prefix <EOL> self . rules = rules <EOL> def get_rules ( self , map ) : <EOL> for rulefactory in self . rules : <EOL> for rule in rulefactory . get_rules ( map ) : <EOL> rule = rule . empty ( ) <EOL> rule . endpoint = self . prefix + rule . endpoint <EOL> yield rule <EOL> class RuleTemplate ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rules ) : <EOL> self . rules = list ( rules ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> return RuleTemplateFactory ( self . rules , dict ( * args , ** kwargs ) ) <EOL> class RuleTemplateFactory ( RuleFactory ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rules , context ) : <EOL> self . rules = rules <EOL> self . context = context <EOL> def get_rules ( self , map ) : <EOL> for rulefactory in self . rules : <EOL> for rule in rulefactory . get_rules ( map ) : <EOL> new_defaults = subdomain = None <EOL> if rule . defaults : <EOL> new_defaults = { } <EOL> for key , value in iteritems ( rule . defaults ) : <EOL> if isinstance ( value , string_types ) : <EOL> value = format_string ( value , self . context ) <EOL> new_defaults [ key ] = value <EOL> if rule . subdomain is not None : <EOL> subdomain = format_string ( rule . subdomain , self . context ) <EOL> new_endpoint = rule . endpoint <EOL> if isinstance ( new_endpoint , string_types ) : <EOL> new_endpoint = format_string ( new_endpoint , self . context ) <EOL> yield Rule ( <EOL> format_string ( rule . rule , self . context ) , <EOL> new_defaults , <EOL> subdomain , <EOL> rule . methods , <EOL> rule . build_only , <EOL> new_endpoint , <EOL> rule . strict_slashes <EOL> ) <EOL> @ implements_to_string <EOL> class Rule ( RuleFactory ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , string , defaults = None , subdomain = None , methods = None , <EOL> build_only = False , endpoint = None , strict_slashes = None , <EOL> redirect_to = None , alias = False , host = None ) : <EOL> if not string . startswith ( '<STR_LIT:/>' ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . rule = string <EOL> self . is_leaf = not string . endswith ( '<STR_LIT:/>' ) <EOL> self . map = None <EOL> self . strict_slashes = strict_slashes <EOL> self . subdomain = subdomain <EOL> self . host = host <EOL> self . defaults = defaults <EOL> self . build_only = build_only <EOL> self . alias = alias <EOL> if methods is None : <EOL> self . methods = None <EOL> else : <EOL> self . methods = set ( [ x . upper ( ) for x in methods ] ) <EOL> if '<STR_LIT>' not in self . methods and '<STR_LIT:GET>' in self . methods : <EOL> self . methods . add ( '<STR_LIT>' ) <EOL> self . endpoint = endpoint <EOL> self . redirect_to = redirect_to <EOL> if defaults : <EOL> self . arguments = set ( map ( str , defaults ) ) <EOL> else : <EOL> self . arguments = set ( ) <EOL> self . _trace = self . _converters = self . _regex = self . _weights = None <EOL> def empty ( self ) : <EOL> """<STR_LIT>""" <EOL> return type ( self ) ( self . rule , ** self . get_empty_kwargs ( ) ) <EOL> def get_empty_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> defaults = None <EOL> if self . defaults : <EOL> defaults = dict ( self . defaults ) <EOL> return dict ( defaults = defaults , subdomain = self . subdomain , <EOL> methods = self . methods , build_only = self . build_only , <EOL> endpoint = self . endpoint , strict_slashes = self . strict_slashes , <EOL> redirect_to = self . redirect_to , alias = self . alias , <EOL> host = self . host ) <EOL> def get_rules ( self , map ) : <EOL> yield self <EOL> def refresh ( self ) : <EOL> """<STR_LIT>""" <EOL> self . bind ( self . map , rebind = True ) <EOL> def bind ( self , map , rebind = False ) : <EOL> """<STR_LIT>""" <EOL> if self . map is not None and not rebind : <EOL> raise RuntimeError ( '<STR_LIT>' % <EOL> ( self , self . map ) ) <EOL> self . map = map <EOL> if self . strict_slashes is None : <EOL> self . strict_slashes = map . strict_slashes <EOL> if self . subdomain is None : <EOL> self . subdomain = map . default_subdomain <EOL> self . compile ( ) <EOL> def get_converter ( self , variable_name , converter_name , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> if converter_name not in self . map . converters : <EOL> raise LookupError ( '<STR_LIT>' % converter_name ) <EOL> return self . map . converters [ converter_name ] ( self . map , * args , ** kwargs ) <EOL> def compile ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . map is not None , '<STR_LIT>' <EOL> if self . map . host_matching : <EOL> domain_rule = self . host or '<STR_LIT>' <EOL> else : <EOL> domain_rule = self . subdomain or '<STR_LIT>' <EOL> self . _trace = [ ] <EOL> self . _converters = { } <EOL> self . _weights = [ ] <EOL> regex_parts = [ ] <EOL> def _build_regex ( rule ) : <EOL> for converter , arguments , variable in parse_rule ( rule ) : <EOL> if converter is None : <EOL> regex_parts . append ( re . escape ( variable ) ) <EOL> self . _trace . append ( ( False , variable ) ) <EOL> for part in variable . split ( '<STR_LIT:/>' ) : <EOL> if part : <EOL> self . _weights . append ( ( <NUM_LIT:0> , - len ( part ) ) ) <EOL> else : <EOL> if arguments : <EOL> c_args , c_kwargs = parse_converter_args ( arguments ) <EOL> else : <EOL> c_args = ( ) <EOL> c_kwargs = { } <EOL> convobj = self . get_converter ( <EOL> variable , converter , c_args , c_kwargs ) <EOL> regex_parts . append ( '<STR_LIT>' % ( variable , convobj . regex ) ) <EOL> self . _converters [ variable ] = convobj <EOL> self . _trace . append ( ( True , variable ) ) <EOL> self . _weights . append ( ( <NUM_LIT:1> , convobj . weight ) ) <EOL> self . arguments . add ( str ( variable ) ) <EOL> _build_regex ( domain_rule ) <EOL> regex_parts . append ( '<STR_LIT>' ) <EOL> self . _trace . append ( ( False , '<STR_LIT:|>' ) ) <EOL> _build_regex ( self . is_leaf and self . rule or self . rule . rstrip ( '<STR_LIT:/>' ) ) <EOL> if not self . is_leaf : <EOL> self . _trace . append ( ( False , '<STR_LIT:/>' ) ) <EOL> if self . build_only : <EOL> return <EOL> regex = r'<STR_LIT>' % ( <EOL> u'<STR_LIT>' . join ( regex_parts ) , <EOL> ( not self . is_leaf or not self . strict_slashes ) and <EOL> '<STR_LIT>' or '<STR_LIT>' <EOL> ) <EOL> self . _regex = re . compile ( regex , re . UNICODE ) <EOL> def match ( self , path ) : <EOL> """<STR_LIT>""" <EOL> if not self . build_only : <EOL> m = self . _regex . search ( path ) <EOL> if m is not None : <EOL> groups = m . groupdict ( ) <EOL> if self . strict_slashes and not self . is_leaf and not groups . pop ( '<STR_LIT>' ) : <EOL> raise RequestSlash ( ) <EOL> elif not self . strict_slashes : <EOL> del groups [ '<STR_LIT>' ] <EOL> result = { } <EOL> for name , value in iteritems ( groups ) : <EOL> try : <EOL> value = self . _converters [ name ] . to_python ( value ) <EOL> except ValidationError : <EOL> return <EOL> result [ str ( name ) ] = value <EOL> if self . defaults : <EOL> result . update ( self . defaults ) <EOL> if self . alias and self . map . redirect_defaults : <EOL> raise RequestAliasRedirect ( result ) <EOL> return result <EOL> def build ( self , values , append_unknown = True ) : <EOL> """<STR_LIT>""" <EOL> tmp = [ ] <EOL> add = tmp . append <EOL> processed = set ( self . arguments ) <EOL> for is_dynamic , data in self . _trace : <EOL> if is_dynamic : <EOL> try : <EOL> add ( self . _converters [ data ] . to_url ( values [ data ] ) ) <EOL> except ValidationError : <EOL> return <EOL> processed . add ( data ) <EOL> else : <EOL> add ( url_quote ( to_bytes ( data , self . map . charset ) , safe = '<STR_LIT>' ) ) <EOL> domain_part , url = ( u'<STR_LIT>' . join ( tmp ) ) . split ( u'<STR_LIT:|>' , <NUM_LIT:1> ) <EOL> if append_unknown : <EOL> query_vars = MultiDict ( values ) <EOL> for key in processed : <EOL> if key in query_vars : <EOL> del query_vars [ key ] <EOL> if query_vars : <EOL> url += u'<STR_LIT:?>' + url_encode ( query_vars , charset = self . map . charset , <EOL> sort = self . map . sort_parameters , <EOL> key = self . map . sort_key ) <EOL> return domain_part , url <EOL> def provides_defaults_for ( self , rule ) : <EOL> """<STR_LIT>""" <EOL> return not self . build_only and self . defaults and self . endpoint == rule . endpoint and self != rule and self . arguments == rule . arguments <EOL> def suitable_for ( self , values , method = None ) : <EOL> """<STR_LIT>""" <EOL> if method is not None and self . methods is not None and method not in self . methods : <EOL> return False <EOL> defaults = self . defaults or ( ) <EOL> for key in self . arguments : <EOL> if key not in defaults and key not in values : <EOL> return False <EOL> if defaults : <EOL> for key , value in iteritems ( defaults ) : <EOL> if key in values and value != values [ key ] : <EOL> return False <EOL> return True <EOL> def match_compare_key ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( self . arguments ) , - len ( self . _weights ) , self . _weights <EOL> def build_compare_key ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . alias and <NUM_LIT:1> or <NUM_LIT:0> , - len ( self . arguments ) , - len ( self . defaults or ( ) ) <EOL> def __eq__ ( self , other ) : <EOL> return self . __class__ is other . __class__ and self . _trace == other . _trace <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __str__ ( self ) : <EOL> return self . rule <EOL> @ native_string_result <EOL> def __repr__ ( self ) : <EOL> if self . map is None : <EOL> return u'<STR_LIT>' % self . __class__ . __name__ <EOL> tmp = [ ] <EOL> for is_dynamic , data in self . _trace : <EOL> if is_dynamic : <EOL> tmp . append ( u'<STR_LIT>' % data ) <EOL> else : <EOL> tmp . append ( data ) <EOL> return u'<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> repr ( ( u'<STR_LIT>' . join ( tmp ) ) . lstrip ( u'<STR_LIT:|>' ) ) . lstrip ( u'<STR_LIT:u>' ) , <EOL> self . methods is not None and u'<STR_LIT>' % <EOL> u'<STR_LIT:U+002CU+0020>' . join ( self . methods ) or u'<STR_LIT>' , <EOL> self . endpoint <EOL> ) <EOL> class BaseConverter ( object ) : <EOL> """<STR_LIT>""" <EOL> regex = '<STR_LIT>' <EOL> weight = <NUM_LIT:100> <EOL> def __init__ ( self , map ) : <EOL> self . map = map <EOL> def to_python ( self , value ) : <EOL> return value <EOL> def to_url ( self , value ) : <EOL> return url_quote ( value , charset = self . map . charset ) <EOL> class UnicodeConverter ( BaseConverter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , map , minlength = <NUM_LIT:1> , maxlength = None , length = None ) : <EOL> BaseConverter . __init__ ( self , map ) <EOL> if length is not None : <EOL> length = '<STR_LIT>' % int ( length ) <EOL> else : <EOL> if maxlength is None : <EOL> maxlength = '<STR_LIT>' <EOL> else : <EOL> maxlength = int ( maxlength ) <EOL> length = '<STR_LIT>' % ( <EOL> int ( minlength ) , <EOL> maxlength <EOL> ) <EOL> self . regex = '<STR_LIT>' + length <EOL> class AnyConverter ( BaseConverter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , map , * items ) : <EOL> BaseConverter . __init__ ( self , map ) <EOL> self . regex = '<STR_LIT>' % '<STR_LIT:|>' . join ( [ re . escape ( x ) for x in items ] ) <EOL> class PathConverter ( BaseConverter ) : <EOL> """<STR_LIT>""" <EOL> regex = '<STR_LIT>' <EOL> weight = <NUM_LIT:200> <EOL> class NumberConverter ( BaseConverter ) : <EOL> """<STR_LIT>""" <EOL> weight = <NUM_LIT:50> <EOL> def __init__ ( self , map , fixed_digits = <NUM_LIT:0> , min = None , max = None ) : <EOL> BaseConverter . __init__ ( self , map ) <EOL> self . fixed_digits = fixed_digits <EOL> self . min = min <EOL> self . max = max <EOL> def to_python ( self , value ) : <EOL> if ( self . fixed_digits and len ( value ) != self . fixed_digits ) : <EOL> raise ValidationError ( ) <EOL> value = self . num_convert ( value ) <EOL> if ( self . min is not None and value < self . min ) or ( self . max is not None and value > self . max ) : <EOL> raise ValidationError ( ) <EOL> return value <EOL> def to_url ( self , value ) : <EOL> value = self . num_convert ( value ) <EOL> if self . fixed_digits : <EOL> value = ( '<STR_LIT>' % self . fixed_digits ) % value <EOL> return str ( value ) <EOL> class IntegerConverter ( NumberConverter ) : <EOL> """<STR_LIT>""" <EOL> regex = r'<STR_LIT>' <EOL> num_convert = int <EOL> class FloatConverter ( NumberConverter ) : <EOL> """<STR_LIT>""" <EOL> regex = r'<STR_LIT>' <EOL> num_convert = float <EOL> def __init__ ( self , map , min = None , max = None ) : <EOL> NumberConverter . __init__ ( self , map , <NUM_LIT:0> , min , max ) <EOL> class UUIDConverter ( BaseConverter ) : <EOL> """<STR_LIT>""" <EOL> regex = r'<STR_LIT>' r'<STR_LIT>' <EOL> def to_python ( self , value ) : <EOL> return uuid . UUID ( value ) <EOL> def to_url ( self , value ) : <EOL> return str ( value ) <EOL> DEFAULT_CONVERTERS = { <EOL> '<STR_LIT:default>' : UnicodeConverter , <EOL> '<STR_LIT:string>' : UnicodeConverter , <EOL> '<STR_LIT>' : AnyConverter , <EOL> '<STR_LIT:path>' : PathConverter , <EOL> '<STR_LIT:int>' : IntegerConverter , <EOL> '<STR_LIT:float>' : FloatConverter , <EOL> '<STR_LIT>' : UUIDConverter , <EOL> } <EOL> class Map ( object ) : <EOL> """<STR_LIT>""" <EOL> default_converters = ImmutableDict ( DEFAULT_CONVERTERS ) <EOL> def __init__ ( self , rules = None , default_subdomain = '<STR_LIT>' , charset = '<STR_LIT:utf-8>' , <EOL> strict_slashes = True , redirect_defaults = True , <EOL> converters = None , sort_parameters = False , sort_key = None , <EOL> encoding_errors = '<STR_LIT:replace>' , host_matching = False ) : <EOL> self . _rules = [ ] <EOL> self . _rules_by_endpoint = { } <EOL> self . _remap = True <EOL> self . _remap_lock = Lock ( ) <EOL> self . default_subdomain = default_subdomain <EOL> self . charset = charset <EOL> self . encoding_errors = encoding_errors <EOL> self . strict_slashes = strict_slashes <EOL> self . redirect_defaults = redirect_defaults <EOL> self . host_matching = host_matching <EOL> self . converters = self . default_converters . copy ( ) <EOL> if converters : <EOL> self . converters . update ( converters ) <EOL> self . sort_parameters = sort_parameters <EOL> self . sort_key = sort_key <EOL> for rulefactory in rules or ( ) : <EOL> self . add ( rulefactory ) <EOL> def is_endpoint_expecting ( self , endpoint , * arguments ) : <EOL> """<STR_LIT>""" <EOL> self . update ( ) <EOL> arguments = set ( arguments ) <EOL> for rule in self . _rules_by_endpoint [ endpoint ] : <EOL> if arguments . issubset ( rule . arguments ) : <EOL> return True <EOL> return False <EOL> def iter_rules ( self , endpoint = None ) : <EOL> """<STR_LIT>""" <EOL> self . update ( ) <EOL> if endpoint is not None : <EOL> return iter ( self . _rules_by_endpoint [ endpoint ] ) <EOL> return iter ( self . _rules ) <EOL> def add ( self , rulefactory ) : <EOL> """<STR_LIT>""" <EOL> for rule in rulefactory . get_rules ( self ) : <EOL> rule . bind ( self ) <EOL> self . _rules . append ( rule ) <EOL> self . _rules_by_endpoint . setdefault ( rule . endpoint , [ ] ) . append ( rule ) <EOL> self . _remap = True <EOL> def bind ( self , server_name , script_name = None , subdomain = None , <EOL> url_scheme = '<STR_LIT:http>' , default_method = '<STR_LIT:GET>' , path_info = None , <EOL> query_args = None ) : <EOL> """<STR_LIT>""" <EOL> server_name = server_name . lower ( ) <EOL> if self . host_matching : <EOL> if subdomain is not None : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> elif subdomain is None : <EOL> subdomain = self . default_subdomain <EOL> if script_name is None : <EOL> script_name = '<STR_LIT:/>' <EOL> server_name = _encode_idna ( server_name ) <EOL> return MapAdapter ( self , server_name , script_name , subdomain , <EOL> url_scheme , path_info , default_method , query_args ) <EOL> def bind_to_environ ( self , environ , server_name = None , subdomain = None ) : <EOL> """<STR_LIT>""" <EOL> environ = _get_environ ( environ ) <EOL> if server_name is None : <EOL> if '<STR_LIT>' in environ : <EOL> server_name = environ [ '<STR_LIT>' ] <EOL> else : <EOL> server_name = environ [ '<STR_LIT>' ] <EOL> if ( environ [ '<STR_LIT>' ] , environ [ '<STR_LIT>' ] ) not in ( ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT:http>' , '<STR_LIT>' ) ) : <EOL> server_name += '<STR_LIT::>' + environ [ '<STR_LIT>' ] <EOL> elif subdomain is None and not self . host_matching : <EOL> server_name = server_name . lower ( ) <EOL> if '<STR_LIT>' in environ : <EOL> wsgi_server_name = environ . get ( '<STR_LIT>' ) <EOL> else : <EOL> wsgi_server_name = environ . get ( '<STR_LIT>' ) <EOL> if ( environ [ '<STR_LIT>' ] , environ [ '<STR_LIT>' ] ) not in ( ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT:http>' , '<STR_LIT>' ) ) : <EOL> wsgi_server_name += '<STR_LIT::>' + environ [ '<STR_LIT>' ] <EOL> wsgi_server_name = wsgi_server_name . lower ( ) <EOL> cur_server_name = wsgi_server_name . split ( '<STR_LIT:.>' ) <EOL> real_server_name = server_name . split ( '<STR_LIT:.>' ) <EOL> offset = - len ( real_server_name ) <EOL> if cur_server_name [ offset : ] != real_server_name : <EOL> subdomain = '<STR_LIT>' <EOL> else : <EOL> subdomain = '<STR_LIT:.>' . join ( filter ( None , cur_server_name [ : offset ] ) ) <EOL> def _get_wsgi_string ( name ) : <EOL> val = environ . get ( name ) <EOL> if val is not None : <EOL> return wsgi_decoding_dance ( val , self . charset ) <EOL> script_name = _get_wsgi_string ( '<STR_LIT>' ) <EOL> path_info = _get_wsgi_string ( '<STR_LIT>' ) <EOL> query_args = _get_wsgi_string ( '<STR_LIT>' ) <EOL> return Map . bind ( self , server_name , script_name , <EOL> subdomain , environ [ '<STR_LIT>' ] , <EOL> environ [ '<STR_LIT>' ] , path_info , <EOL> query_args = query_args ) <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _remap : <EOL> return <EOL> with self . _remap_lock : <EOL> if not self . _remap : <EOL> return <EOL> self . _rules . sort ( key = lambda x : x . match_compare_key ( ) ) <EOL> for rules in itervalues ( self . _rules_by_endpoint ) : <EOL> rules . sort ( key = lambda x : x . build_compare_key ( ) ) <EOL> self . _remap = False <EOL> def __repr__ ( self ) : <EOL> rules = self . iter_rules ( ) <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , pformat ( list ( rules ) ) ) <EOL> class MapAdapter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , map , server_name , script_name , subdomain , <EOL> url_scheme , path_info , default_method , query_args = None ) : <EOL> self . map = map <EOL> self . server_name = to_unicode ( server_name ) <EOL> script_name = to_unicode ( script_name ) <EOL> if not script_name . endswith ( u'<STR_LIT:/>' ) : <EOL> script_name += u'<STR_LIT:/>' <EOL> self . script_name = script_name <EOL> self . subdomain = to_unicode ( subdomain ) <EOL> self . url_scheme = to_unicode ( url_scheme ) <EOL> self . path_info = to_unicode ( path_info ) <EOL> self . default_method = to_unicode ( default_method ) <EOL> self . query_args = query_args <EOL> def dispatch ( self , view_func , path_info = None , method = None , <EOL> catch_http_exceptions = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> try : <EOL> endpoint , args = self . match ( path_info , method ) <EOL> except RequestRedirect as e : <EOL> return e <EOL> return view_func ( endpoint , args ) <EOL> except HTTPException as e : <EOL> if catch_http_exceptions : <EOL> return e <EOL> raise <EOL> def match ( self , path_info = None , method = None , return_rule = False , <EOL> query_args = None ) : <EOL> """<STR_LIT>""" <EOL> self . map . update ( ) <EOL> if path_info is None : <EOL> path_info = self . path_info <EOL> else : <EOL> path_info = to_unicode ( path_info , self . map . charset ) <EOL> if query_args is None : <EOL> query_args = self . query_args <EOL> method = ( method or self . default_method ) . upper ( ) <EOL> path = u'<STR_LIT>' % ( <EOL> self . map . host_matching and self . server_name or self . subdomain , <EOL> path_info and '<STR_LIT>' % path_info . lstrip ( '<STR_LIT:/>' ) <EOL> ) <EOL> have_match_for = set ( ) <EOL> for rule in self . map . _rules : <EOL> try : <EOL> rv = rule . match ( path ) <EOL> except RequestSlash : <EOL> raise RequestRedirect ( self . make_redirect_url ( <EOL> url_quote ( path_info , self . map . charset , <EOL> safe = '<STR_LIT>' ) + '<STR_LIT:/>' , query_args ) ) <EOL> except RequestAliasRedirect as e : <EOL> raise RequestRedirect ( self . make_alias_redirect_url ( <EOL> path , rule . endpoint , e . matched_values , method , query_args ) ) <EOL> if rv is None : <EOL> continue <EOL> if rule . methods is not None and method not in rule . methods : <EOL> have_match_for . update ( rule . methods ) <EOL> continue <EOL> if self . map . redirect_defaults : <EOL> redirect_url = self . get_default_redirect ( rule , method , rv , <EOL> query_args ) <EOL> if redirect_url is not None : <EOL> raise RequestRedirect ( redirect_url ) <EOL> if rule . redirect_to is not None : <EOL> if isinstance ( rule . redirect_to , string_types ) : <EOL> def _handle_match ( match ) : <EOL> value = rv [ match . group ( <NUM_LIT:1> ) ] <EOL> return rule . _converters [ match . group ( <NUM_LIT:1> ) ] . to_url ( value ) <EOL> redirect_url = _simple_rule_re . sub ( _handle_match , <EOL> rule . redirect_to ) <EOL> else : <EOL> redirect_url = rule . redirect_to ( self , ** rv ) <EOL> raise RequestRedirect ( str ( url_join ( '<STR_LIT>' % ( <EOL> self . url_scheme or '<STR_LIT:http>' , <EOL> self . subdomain and self . subdomain + '<STR_LIT:.>' or '<STR_LIT>' , <EOL> self . server_name , <EOL> self . script_name <EOL> ) , redirect_url ) ) ) <EOL> if return_rule : <EOL> return rule , rv <EOL> else : <EOL> return rule . endpoint , rv <EOL> if have_match_for : <EOL> raise MethodNotAllowed ( valid_methods = list ( have_match_for ) ) <EOL> raise NotFound ( ) <EOL> def test ( self , path_info = None , method = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . match ( path_info , method ) <EOL> except RequestRedirect : <EOL> pass <EOL> except HTTPException : <EOL> return False <EOL> return True <EOL> def allowed_methods ( self , path_info = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . match ( path_info , method = '<STR_LIT>' ) <EOL> except MethodNotAllowed as e : <EOL> return e . valid_methods <EOL> except HTTPException as e : <EOL> pass <EOL> return [ ] <EOL> def get_host ( self , domain_part ) : <EOL> """<STR_LIT>""" <EOL> if self . map . host_matching : <EOL> if domain_part is None : <EOL> return self . server_name <EOL> return to_unicode ( domain_part , '<STR_LIT:ascii>' ) <EOL> subdomain = domain_part <EOL> if subdomain is None : <EOL> subdomain = self . subdomain <EOL> else : <EOL> subdomain = to_unicode ( subdomain , '<STR_LIT:ascii>' ) <EOL> return ( subdomain and subdomain + u'<STR_LIT:.>' or u'<STR_LIT>' ) + self . server_name <EOL> def get_default_redirect ( self , rule , method , values , query_args ) : <EOL> """<STR_LIT>""" <EOL> assert self . map . redirect_defaults <EOL> for r in self . map . _rules_by_endpoint [ rule . endpoint ] : <EOL> if r is rule : <EOL> break <EOL> if r . provides_defaults_for ( rule ) and r . suitable_for ( values , method ) : <EOL> values . update ( r . defaults ) <EOL> domain_part , path = r . build ( values ) <EOL> return self . make_redirect_url ( <EOL> path , query_args , domain_part = domain_part ) <EOL> def encode_query_args ( self , query_args ) : <EOL> if not isinstance ( query_args , string_types ) : <EOL> query_args = url_encode ( query_args , self . map . charset ) <EOL> return query_args <EOL> def make_redirect_url ( self , path_info , query_args = None , domain_part = None ) : <EOL> """<STR_LIT>""" <EOL> suffix = '<STR_LIT>' <EOL> if query_args : <EOL> suffix = '<STR_LIT:?>' + self . encode_query_args ( query_args ) <EOL> return str ( '<STR_LIT>' % ( <EOL> self . url_scheme or '<STR_LIT:http>' , <EOL> self . get_host ( domain_part ) , <EOL> posixpath . join ( self . script_name [ : - <NUM_LIT:1> ] . lstrip ( '<STR_LIT:/>' ) , <EOL> path_info . lstrip ( '<STR_LIT:/>' ) ) , <EOL> suffix <EOL> ) ) <EOL> def make_alias_redirect_url ( self , path , endpoint , values , method , query_args ) : <EOL> """<STR_LIT>""" <EOL> url = self . build ( endpoint , values , method , append_unknown = False , <EOL> force_external = True ) <EOL> if query_args : <EOL> url += '<STR_LIT:?>' + self . encode_query_args ( query_args ) <EOL> assert url != path , '<STR_LIT>' '<STR_LIT>' <EOL> return url <EOL> def _partial_build ( self , endpoint , values , method , append_unknown ) : <EOL> """<STR_LIT>""" <EOL> if method is None : <EOL> rv = self . _partial_build ( endpoint , values , self . default_method , <EOL> append_unknown ) <EOL> if rv is not None : <EOL> return rv <EOL> for rule in self . map . _rules_by_endpoint . get ( endpoint , ( ) ) : <EOL> if rule . suitable_for ( values , method ) : <EOL> rv = rule . build ( values , append_unknown ) <EOL> if rv is not None : <EOL> return rv <EOL> def build ( self , endpoint , values = None , method = None , force_external = False , <EOL> append_unknown = True ) : <EOL> """<STR_LIT>""" <EOL> self . map . update ( ) <EOL> if values : <EOL> if isinstance ( values , MultiDict ) : <EOL> valueiter = iteritems ( values , multi = True ) <EOL> else : <EOL> valueiter = iteritems ( values ) <EOL> values = dict ( ( k , v ) for k , v in valueiter if v is not None ) <EOL> else : <EOL> values = { } <EOL> rv = self . _partial_build ( endpoint , values , method , append_unknown ) <EOL> if rv is None : <EOL> raise BuildError ( endpoint , values , method ) <EOL> domain_part , path = rv <EOL> host = self . get_host ( domain_part ) <EOL> if not force_external and ( <EOL> ( self . map . host_matching and host == self . server_name ) or <EOL> ( not self . map . host_matching and domain_part == self . subdomain ) ) : <EOL> return str ( url_join ( self . script_name , '<STR_LIT>' + path . lstrip ( '<STR_LIT:/>' ) ) ) <EOL> return str ( '<STR_LIT>' % ( <EOL> self . url_scheme + '<STR_LIT::>' if self . url_scheme else '<STR_LIT>' , <EOL> host , <EOL> self . script_name [ : - <NUM_LIT:1> ] , <EOL> path . lstrip ( '<STR_LIT:/>' ) <EOL> ) ) </s>
