<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import functools <EOL> import re <EOL> from threading import local <EOL> from django . http import Http404 <EOL> from django . core . exceptions import ImproperlyConfigured , ViewDoesNotExist <EOL> from django . utils . datastructures import MultiValueDict <EOL> from django . utils . encoding import force_str , force_text , iri_to_uri <EOL> from django . utils . functional import memoize , lazy <EOL> from django . utils . http import urlquote <EOL> from django . utils . importlib import import_module <EOL> from django . utils . module_loading import module_has_submodule <EOL> from django . utils . regex_helper import normalize <EOL> from django . utils import six <EOL> from django . utils . translation import get_language <EOL> _resolver_cache = { } <EOL> _ns_resolver_cache = { } <EOL> _callable_cache = { } <EOL> _prefixes = local ( ) <EOL> _urlconfs = local ( ) <EOL> class ResolverMatch ( object ) : <EOL> def __init__ ( self , func , args , kwargs , url_name = None , app_name = None , namespaces = None ) : <EOL> self . func = func <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> self . app_name = app_name <EOL> if namespaces : <EOL> self . namespaces = [ x for x in namespaces if x ] <EOL> else : <EOL> self . namespaces = [ ] <EOL> if not url_name : <EOL> if not hasattr ( func , '<STR_LIT>' ) : <EOL> url_name = '<STR_LIT:.>' . join ( [ func . __class__ . __module__ , func . __class__ . __name__ ] ) <EOL> else : <EOL> url_name = '<STR_LIT:.>' . join ( [ func . __module__ , func . __name__ ] ) <EOL> self . url_name = url_name <EOL> @ property <EOL> def namespace ( self ) : <EOL> return '<STR_LIT::>' . join ( self . namespaces ) <EOL> @ property <EOL> def view_name ( self ) : <EOL> return '<STR_LIT::>' . join ( [ x for x in [ self . namespace , self . url_name ] if x ] ) <EOL> def __getitem__ ( self , index ) : <EOL> return ( self . func , self . args , self . kwargs ) [ index ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . func , self . args , self . kwargs , self . url_name , self . app_name , self . namespace ) <EOL> class Resolver404 ( Http404 ) : <EOL> pass <EOL> class NoReverseMatch ( Exception ) : <EOL> pass <EOL> def get_callable ( lookup_view , can_fail = False ) : <EOL> """<STR_LIT>""" <EOL> if not callable ( lookup_view ) : <EOL> mod_name , func_name = get_mod_func ( lookup_view ) <EOL> if func_name == '<STR_LIT>' : <EOL> return lookup_view <EOL> try : <EOL> mod = import_module ( mod_name ) <EOL> except ImportError : <EOL> parentmod , submod = get_mod_func ( mod_name ) <EOL> if ( not can_fail and submod != '<STR_LIT>' and <EOL> not module_has_submodule ( import_module ( parentmod ) , submod ) ) : <EOL> raise ViewDoesNotExist ( <EOL> "<STR_LIT>" % <EOL> ( lookup_view , mod_name ) ) <EOL> if not can_fail : <EOL> raise <EOL> else : <EOL> try : <EOL> lookup_view = getattr ( mod , func_name ) <EOL> if not callable ( lookup_view ) : <EOL> raise ViewDoesNotExist ( <EOL> "<STR_LIT>" % <EOL> ( mod_name , func_name ) ) <EOL> except AttributeError : <EOL> if not can_fail : <EOL> raise ViewDoesNotExist ( <EOL> "<STR_LIT>" % <EOL> ( lookup_view , mod_name ) ) <EOL> return lookup_view <EOL> get_callable = memoize ( get_callable , _callable_cache , <NUM_LIT:1> ) <EOL> def get_resolver ( urlconf ) : <EOL> if urlconf is None : <EOL> from django . conf import settings <EOL> urlconf = settings . ROOT_URLCONF <EOL> return RegexURLResolver ( r'<STR_LIT>' , urlconf ) <EOL> get_resolver = memoize ( get_resolver , _resolver_cache , <NUM_LIT:1> ) <EOL> def get_ns_resolver ( ns_pattern , resolver ) : <EOL> ns_resolver = RegexURLResolver ( ns_pattern , <EOL> resolver . url_patterns ) <EOL> return RegexURLResolver ( r'<STR_LIT>' , [ ns_resolver ] ) <EOL> get_ns_resolver = memoize ( get_ns_resolver , _ns_resolver_cache , <NUM_LIT:2> ) <EOL> def get_mod_func ( callback ) : <EOL> try : <EOL> dot = callback . rindex ( '<STR_LIT:.>' ) <EOL> except ValueError : <EOL> return callback , '<STR_LIT>' <EOL> return callback [ : dot ] , callback [ dot + <NUM_LIT:1> : ] <EOL> class LocaleRegexProvider ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , regex ) : <EOL> self . _regex = regex <EOL> self . _regex_dict = { } <EOL> @ property <EOL> def regex ( self ) : <EOL> """<STR_LIT>""" <EOL> language_code = get_language ( ) <EOL> if language_code not in self . _regex_dict : <EOL> if isinstance ( self . _regex , six . string_types ) : <EOL> regex = self . _regex <EOL> else : <EOL> regex = force_text ( self . _regex ) <EOL> try : <EOL> compiled_regex = re . compile ( regex , re . UNICODE ) <EOL> except re . error as e : <EOL> raise ImproperlyConfigured ( <EOL> '<STR_LIT>' % <EOL> ( regex , six . text_type ( e ) ) ) <EOL> self . _regex_dict [ language_code ] = compiled_regex <EOL> return self . _regex_dict [ language_code ] <EOL> class RegexURLPattern ( LocaleRegexProvider ) : <EOL> def __init__ ( self , regex , callback , default_args = None , name = None ) : <EOL> LocaleRegexProvider . __init__ ( self , regex ) <EOL> if callable ( callback ) : <EOL> self . _callback = callback <EOL> else : <EOL> self . _callback = None <EOL> self . _callback_str = callback <EOL> self . default_args = default_args or { } <EOL> self . name = name <EOL> def __repr__ ( self ) : <EOL> return force_str ( '<STR_LIT>' % ( self . __class__ . __name__ , self . name , self . regex . pattern ) ) <EOL> def add_prefix ( self , prefix ) : <EOL> """<STR_LIT>""" <EOL> if not prefix or not hasattr ( self , '<STR_LIT>' ) : <EOL> return <EOL> self . _callback_str = prefix + '<STR_LIT:.>' + self . _callback_str <EOL> def resolve ( self , path ) : <EOL> match = self . regex . search ( path ) <EOL> if match : <EOL> kwargs = match . groupdict ( ) <EOL> if kwargs : <EOL> args = ( ) <EOL> else : <EOL> args = match . groups ( ) <EOL> kwargs . update ( self . default_args ) <EOL> return ResolverMatch ( self . callback , args , kwargs , self . name ) <EOL> @ property <EOL> def callback ( self ) : <EOL> if self . _callback is not None : <EOL> return self . _callback <EOL> self . _callback = get_callable ( self . _callback_str ) <EOL> return self . _callback <EOL> class RegexURLResolver ( LocaleRegexProvider ) : <EOL> def __init__ ( self , regex , urlconf_name , default_kwargs = None , app_name = None , namespace = None ) : <EOL> LocaleRegexProvider . __init__ ( self , regex ) <EOL> self . urlconf_name = urlconf_name <EOL> if not isinstance ( urlconf_name , six . string_types ) : <EOL> self . _urlconf_module = self . urlconf_name <EOL> self . callback = None <EOL> self . default_kwargs = default_kwargs or { } <EOL> self . namespace = namespace <EOL> self . app_name = app_name <EOL> self . _reverse_dict = { } <EOL> self . _namespace_dict = { } <EOL> self . _app_dict = { } <EOL> self . _callback_strs = set ( ) <EOL> self . _populated = False <EOL> def __repr__ ( self ) : <EOL> if isinstance ( self . urlconf_name , list ) and len ( self . urlconf_name ) : <EOL> urlconf_repr = '<STR_LIT>' % self . urlconf_name [ <NUM_LIT:0> ] . __class__ . __name__ <EOL> else : <EOL> urlconf_repr = repr ( self . urlconf_name ) <EOL> return str ( '<STR_LIT>' ) % ( <EOL> self . __class__ . __name__ , urlconf_repr , self . app_name , <EOL> self . namespace , self . regex . pattern ) <EOL> def _populate ( self ) : <EOL> lookups = MultiValueDict ( ) <EOL> namespaces = { } <EOL> apps = { } <EOL> language_code = get_language ( ) <EOL> for pattern in reversed ( self . url_patterns ) : <EOL> if hasattr ( pattern , '<STR_LIT>' ) : <EOL> self . _callback_strs . add ( pattern . _callback_str ) <EOL> elif hasattr ( pattern , '<STR_LIT>' ) : <EOL> callback = pattern . _callback <EOL> if isinstance ( callback , functools . partial ) : <EOL> callback = callback . func <EOL> if not hasattr ( callback , '<STR_LIT>' ) : <EOL> lookup_str = callback . __module__ + "<STR_LIT:.>" + callback . __class__ . __name__ <EOL> else : <EOL> lookup_str = callback . __module__ + "<STR_LIT:.>" + callback . __name__ <EOL> self . _callback_strs . add ( lookup_str ) <EOL> p_pattern = pattern . regex . pattern <EOL> if p_pattern . startswith ( '<STR_LIT>' ) : <EOL> p_pattern = p_pattern [ <NUM_LIT:1> : ] <EOL> if isinstance ( pattern , RegexURLResolver ) : <EOL> if pattern . namespace : <EOL> namespaces [ pattern . namespace ] = ( p_pattern , pattern ) <EOL> if pattern . app_name : <EOL> apps . setdefault ( pattern . app_name , [ ] ) . append ( pattern . namespace ) <EOL> else : <EOL> parent = normalize ( pattern . regex . pattern ) <EOL> for name in pattern . reverse_dict : <EOL> for matches , pat , defaults in pattern . reverse_dict . getlist ( name ) : <EOL> new_matches = [ ] <EOL> for piece , p_args in parent : <EOL> new_matches . extend ( [ ( piece + suffix , p_args + args ) for ( suffix , args ) in matches ] ) <EOL> lookups . appendlist ( name , ( new_matches , p_pattern + pat , dict ( defaults , ** pattern . default_kwargs ) ) ) <EOL> for namespace , ( prefix , sub_pattern ) in pattern . namespace_dict . items ( ) : <EOL> namespaces [ namespace ] = ( p_pattern + prefix , sub_pattern ) <EOL> for app_name , namespace_list in pattern . app_dict . items ( ) : <EOL> apps . setdefault ( app_name , [ ] ) . extend ( namespace_list ) <EOL> self . _callback_strs . update ( pattern . _callback_strs ) <EOL> else : <EOL> bits = normalize ( p_pattern ) <EOL> lookups . appendlist ( pattern . callback , ( bits , p_pattern , pattern . default_args ) ) <EOL> if pattern . name is not None : <EOL> lookups . appendlist ( pattern . name , ( bits , p_pattern , pattern . default_args ) ) <EOL> self . _reverse_dict [ language_code ] = lookups <EOL> self . _namespace_dict [ language_code ] = namespaces <EOL> self . _app_dict [ language_code ] = apps <EOL> self . _populated = True <EOL> @ property <EOL> def reverse_dict ( self ) : <EOL> language_code = get_language ( ) <EOL> if language_code not in self . _reverse_dict : <EOL> self . _populate ( ) <EOL> return self . _reverse_dict [ language_code ] <EOL> @ property <EOL> def namespace_dict ( self ) : <EOL> language_code = get_language ( ) <EOL> if language_code not in self . _namespace_dict : <EOL> self . _populate ( ) <EOL> return self . _namespace_dict [ language_code ] <EOL> @ property <EOL> def app_dict ( self ) : <EOL> language_code = get_language ( ) <EOL> if language_code not in self . _app_dict : <EOL> self . _populate ( ) <EOL> return self . _app_dict [ language_code ] <EOL> def resolve ( self , path ) : <EOL> tried = [ ] <EOL> match = self . regex . search ( path ) <EOL> if match : <EOL> new_path = path [ match . end ( ) : ] <EOL> for pattern in self . url_patterns : <EOL> try : <EOL> sub_match = pattern . resolve ( new_path ) <EOL> except Resolver404 as e : <EOL> sub_tried = e . args [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) <EOL> if sub_tried is not None : <EOL> tried . extend ( [ [ pattern ] + t for t in sub_tried ] ) <EOL> else : <EOL> tried . append ( [ pattern ] ) <EOL> else : <EOL> if sub_match : <EOL> sub_match_dict = dict ( match . groupdict ( ) , ** self . default_kwargs ) <EOL> sub_match_dict . update ( sub_match . kwargs ) <EOL> return ResolverMatch ( sub_match . func , sub_match . args , sub_match_dict , sub_match . url_name , self . app_name or sub_match . app_name , [ self . namespace ] + sub_match . namespaces ) <EOL> tried . append ( [ pattern ] ) <EOL> raise Resolver404 ( { '<STR_LIT>' : tried , '<STR_LIT:path>' : new_path } ) <EOL> raise Resolver404 ( { '<STR_LIT:path>' : path } ) <EOL> @ property <EOL> def urlconf_module ( self ) : <EOL> try : <EOL> return self . _urlconf_module <EOL> except AttributeError : <EOL> self . _urlconf_module = import_module ( self . urlconf_name ) <EOL> return self . _urlconf_module <EOL> @ property <EOL> def url_patterns ( self ) : <EOL> patterns = getattr ( self . urlconf_module , "<STR_LIT>" , self . urlconf_module ) <EOL> try : <EOL> iter ( patterns ) <EOL> except TypeError : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" % self . urlconf_name ) <EOL> return patterns <EOL> def _resolve_special ( self , view_type ) : <EOL> callback = getattr ( self . urlconf_module , '<STR_LIT>' % view_type , None ) <EOL> if not callback : <EOL> from django . conf import urls <EOL> callback = getattr ( urls , '<STR_LIT>' % view_type ) <EOL> return get_callable ( callback ) , { } <EOL> def resolve400 ( self ) : <EOL> return self . _resolve_special ( '<STR_LIT>' ) <EOL> def resolve403 ( self ) : <EOL> return self . _resolve_special ( '<STR_LIT>' ) <EOL> def resolve404 ( self ) : <EOL> return self . _resolve_special ( '<STR_LIT>' ) <EOL> def resolve500 ( self ) : <EOL> return self . _resolve_special ( '<STR_LIT>' ) <EOL> def reverse ( self , lookup_view , * args , ** kwargs ) : <EOL> return self . _reverse_with_prefix ( lookup_view , '<STR_LIT>' , * args , ** kwargs ) <EOL> def _reverse_with_prefix ( self , lookup_view , _prefix , * args , ** kwargs ) : <EOL> if args and kwargs : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> text_args = [ force_text ( v ) for v in args ] <EOL> text_kwargs = dict ( ( k , force_text ( v ) ) for ( k , v ) in kwargs . items ( ) ) <EOL> if not self . _populated : <EOL> self . _populate ( ) <EOL> try : <EOL> if lookup_view in self . _callback_strs : <EOL> lookup_view = get_callable ( lookup_view , True ) <EOL> except ( ImportError , AttributeError ) as e : <EOL> raise NoReverseMatch ( "<STR_LIT>" % ( lookup_view , e ) ) <EOL> possibilities = self . reverse_dict . getlist ( lookup_view ) <EOL> prefix_norm , prefix_args = normalize ( urlquote ( _prefix ) ) [ <NUM_LIT:0> ] <EOL> for possibility , pattern , defaults in possibilities : <EOL> for result , params in possibility : <EOL> if args : <EOL> if len ( args ) != len ( params ) + len ( prefix_args ) : <EOL> continue <EOL> candidate_subs = dict ( zip ( prefix_args + params , text_args ) ) <EOL> else : <EOL> if set ( kwargs . keys ( ) ) | set ( defaults . keys ( ) ) != set ( params ) | set ( defaults . keys ( ) ) | set ( prefix_args ) : <EOL> continue <EOL> matches = True <EOL> for k , v in defaults . items ( ) : <EOL> if kwargs . get ( k , v ) != v : <EOL> matches = False <EOL> break <EOL> if not matches : <EOL> continue <EOL> candidate_subs = text_kwargs <EOL> candidate_pat = prefix_norm . replace ( '<STR_LIT:%>' , '<STR_LIT>' ) + result <EOL> if re . search ( '<STR_LIT>' % ( prefix_norm , pattern ) , candidate_pat % candidate_subs , re . UNICODE ) : <EOL> candidate_subs = dict ( ( k , urlquote ( v ) ) for ( k , v ) in candidate_subs . items ( ) ) <EOL> url = candidate_pat % candidate_subs <EOL> if url . startswith ( '<STR_LIT>' ) : <EOL> url = '<STR_LIT>' % url [ <NUM_LIT:2> : ] <EOL> return url <EOL> m = getattr ( lookup_view , '<STR_LIT>' , None ) <EOL> n = getattr ( lookup_view , '<STR_LIT>' , None ) <EOL> if m is not None and n is not None : <EOL> lookup_view_s = "<STR_LIT>" % ( m , n ) <EOL> else : <EOL> lookup_view_s = lookup_view <EOL> patterns = [ pattern for ( possibility , pattern , defaults ) in possibilities ] <EOL> raise NoReverseMatch ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( lookup_view_s , args , kwargs , len ( patterns ) , patterns ) ) <EOL> class LocaleRegexURLResolver ( RegexURLResolver ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , urlconf_name , default_kwargs = None , app_name = None , namespace = None ) : <EOL> super ( LocaleRegexURLResolver , self ) . __init__ ( <EOL> None , urlconf_name , default_kwargs , app_name , namespace ) <EOL> @ property <EOL> def regex ( self ) : <EOL> language_code = get_language ( ) <EOL> if language_code not in self . _regex_dict : <EOL> regex_compiled = re . compile ( '<STR_LIT>' % language_code , re . UNICODE ) <EOL> self . _regex_dict [ language_code ] = regex_compiled <EOL> return self . _regex_dict [ language_code ] <EOL> def resolve ( path , urlconf = None ) : <EOL> if urlconf is None : <EOL> urlconf = get_urlconf ( ) <EOL> return get_resolver ( urlconf ) . resolve ( path ) <EOL> def reverse ( viewname , urlconf = None , args = None , kwargs = None , prefix = None , current_app = None ) : <EOL> if urlconf is None : <EOL> urlconf = get_urlconf ( ) <EOL> resolver = get_resolver ( urlconf ) <EOL> args = args or [ ] <EOL> kwargs = kwargs or { } <EOL> if prefix is None : <EOL> prefix = get_script_prefix ( ) <EOL> if not isinstance ( viewname , six . string_types ) : <EOL> view = viewname <EOL> else : <EOL> parts = viewname . split ( '<STR_LIT::>' ) <EOL> parts . reverse ( ) <EOL> view = parts [ <NUM_LIT:0> ] <EOL> path = parts [ <NUM_LIT:1> : ] <EOL> resolved_path = [ ] <EOL> ns_pattern = '<STR_LIT>' <EOL> while path : <EOL> ns = path . pop ( ) <EOL> try : <EOL> app_list = resolver . app_dict [ ns ] <EOL> if current_app and current_app in app_list : <EOL> ns = current_app <EOL> elif ns not in app_list : <EOL> ns = app_list [ <NUM_LIT:0> ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> extra , resolver = resolver . namespace_dict [ ns ] <EOL> resolved_path . append ( ns ) <EOL> ns_pattern = ns_pattern + extra <EOL> except KeyError as key : <EOL> if resolved_path : <EOL> raise NoReverseMatch ( <EOL> "<STR_LIT>" % <EOL> ( key , '<STR_LIT::>' . join ( resolved_path ) ) ) <EOL> else : <EOL> raise NoReverseMatch ( "<STR_LIT>" % <EOL> key ) <EOL> if ns_pattern : <EOL> resolver = get_ns_resolver ( ns_pattern , resolver ) <EOL> return iri_to_uri ( resolver . _reverse_with_prefix ( view , prefix , * args , ** kwargs ) ) <EOL> reverse_lazy = lazy ( reverse , str ) <EOL> def clear_url_caches ( ) : <EOL> global _resolver_cache <EOL> global _ns_resolver_cache <EOL> global _callable_cache <EOL> _resolver_cache . clear ( ) <EOL> _ns_resolver_cache . clear ( ) <EOL> _callable_cache . clear ( ) <EOL> def set_script_prefix ( prefix ) : <EOL> """<STR_LIT>""" <EOL> if not prefix . endswith ( '<STR_LIT:/>' ) : <EOL> prefix += '<STR_LIT:/>' <EOL> _prefixes . value = prefix <EOL> def get_script_prefix ( ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( _prefixes , "<STR_LIT:value>" , '<STR_LIT:/>' ) <EOL> def clear_script_prefix ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del _prefixes . value <EOL> except AttributeError : <EOL> pass <EOL> def set_urlconf ( urlconf_name ) : <EOL> """<STR_LIT>""" <EOL> if urlconf_name : <EOL> _urlconfs . value = urlconf_name <EOL> else : <EOL> if hasattr ( _urlconfs , "<STR_LIT:value>" ) : <EOL> del _urlconfs . value <EOL> def get_urlconf ( default = None ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( _urlconfs , "<STR_LIT:value>" , default ) <EOL> def is_valid_path ( path , urlconf = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> resolve ( path , urlconf ) <EOL> return True <EOL> except Resolver404 : <EOL> return False </s>
