<s> from threading import local <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . urlresolvers import RegexURLResolver , RegexURLPattern , ResolverMatch <EOL> from django . utils . encoding import smart_str <EOL> from django . utils . functional import memoize <EOL> from urlmiddleware . base import MiddlewareResolver404 <EOL> from urlmiddleware . util . collections import OrderedSet <EOL> _resolver_cache = { } <EOL> _urlconfs = local ( ) <EOL> class MiddlewareRegexURLPattern ( RegexURLPattern ) : <EOL> pass <EOL> class MiddlewareRegexURLResolver ( RegexURLResolver ) : <EOL> """<STR_LIT>""" <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 ( self , path ) : <EOL> tried = [ ] <EOL> found = OrderedSet ( ) <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 MiddlewareResolver404 , 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 ( [ ( smart_str ( k ) , v ) for k , v in match . groupdict ( ) . items ( ) ] ) <EOL> sub_match_dict . update ( self . default_kwargs ) <EOL> for k , v in sub_match . kwargs . iteritems ( ) : <EOL> sub_match_dict [ smart_str ( k ) ] = v <EOL> middleware = 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> found . add ( middleware . func ) <EOL> tried . append ( [ pattern ] ) <EOL> if len ( found ) == <NUM_LIT:0> : <EOL> raise MiddlewareResolver404 ( { '<STR_LIT>' : tried , '<STR_LIT:path>' : new_path } ) <EOL> if len ( found ) == <NUM_LIT:0> : <EOL> raise MiddlewareResolver404 ( { '<STR_LIT:path>' : path } ) <EOL> return list ( found ) <EOL> def get_resolver ( urlconf ) : <EOL> if urlconf is None : <EOL> from django . conf import settings <EOL> urlconf = settings . ROOT_URLCONF <EOL> return MiddlewareRegexURLResolver ( r'<STR_LIT>' , urlconf ) <EOL> get_resolver = memoize ( get_resolver , _resolver_cache , <NUM_LIT:1> ) <EOL> def resolve ( path , urlconf = None ) : <EOL> if urlconf is None : <EOL> urlconf = get_urlconf ( ) <EOL> return get_resolver ( urlconf ) . resolve ( path ) <EOL> def get_urlconf ( default = None ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( _urlconfs , "<STR_LIT:value>" , default ) </s>
