o
    [h{                     @   s  d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZmZ ddlmZmZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z& ddl'm(Z( G dd dZ)d/ddZ*ej+d/ddZ,ej+dd Z-G dd dZ.G dd dZ/G d d! d!e/Z0ed"Z1e2ej3Z4ej5d#d$ Z6G d%d& d&Z7G d'd( d(e/Z8G d)d* d*Z9G d+d, d,Z:G d-d. d.Z;dS )0z
This module converts requested URLs to callback view functions.

URLResolver is the main class here. Its resolve() method takes a URL (as
a string) and returns a ResolverMatch object which provides access to all
attributes of the resolved URL match.
    N)import_module)PicklingError)quote)Local)settings)ErrorWarning)check_resolver)ImproperlyConfigured)MultiValueDict)cached_property)RFC3986_SUBDELIMSescape_leading_slashes)_lazy_re_compile	normalize)get_language   )get_converters)NoReverseMatchResolver404)get_callablec                   @   s<   e Zd Z							d
ddZdd Zdd Zdd	 ZdS )ResolverMatchNc                 C   s   || _ || _|| _|| _|| _|| _|	| _|
| _|r!dd |D ng | _d	| j| _
|r4dd |D ng | _d	| j| _t|drF|j}t|dsW|jjd |jj | _n	|jd |j | _|pd| j}d	| j|g | _d S )Nc                 S      g | ]}|r|qS  r   .0xr   r   |/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/django/urls/resolvers.py
<listcomp>;       z*ResolverMatch.__init__.<locals>.<listcomp>:c                 S   r   r   r   r   r   r   r   r   =   r   
view_class__name__.)funcargskwargsurl_nameroutetriedcaptured_kwargsextra_kwargs	app_namesjoinapp_name
namespaces	namespacehasattrr!   	__class__
__module__r"   
_func_pathZ	view_name)selfr$   r%   r&   r'   r,   r/   r(   r)   r*   r+   Z	view_pathr   r   r   __init__#   s&   


zResolverMatch.__init__c                 C   s   | j | j| jf| S N)r$   r%   r&   )r5   indexr   r   r   __getitem__L      zResolverMatch.__getitem__c                 C   sn   t | jtjrt| j}n| j}d|| j| j| j| j	| j
| j| jr'd| jnd| jr3d| jf	 S df	 S )NzbResolverMatch(func=%s, args=%r, kwargs=%r, url_name=%r, app_names=%r, namespaces=%r, route=%r%s%s)z, captured_kwargs= z, extra_kwargs=)
isinstancer$   	functoolspartialreprr4   r%   r&   r'   r,   r/   r(   r*   r+   )r5   r$   r   r   r   __repr__O   s,   zResolverMatch.__repr__c                 C   s   t d| jj d)NzCannot pickle r#   )r   r2   __qualname__)r5   protocolr   r   r   __reduce_ex__h   r:   zResolverMatch.__reduce_ex__)NNNNNNN)r"   r3   rA   r6   r9   r@   rC   r   r   r   r   r   "   s    
)r   c                 C   s   | d u rt j} t| S r7   )r   ZROOT_URLCONF_get_cached_resolverZurlconfr   r   r   get_resolverl   s   rF   c                 C   s   t td| S N^/)URLResolverRegexPatternrE   r   r   r   rD   r   s   rD   c                 C   s.   t | }t||_t||j}tt d|gS rG   )rJ   dict
convertersrI   url_patterns)Z
ns_patternresolverrL   patternZns_resolverr   r   r   get_ns_resolverw   s   
rP   c                   @   s   e Zd ZdddZdd ZdS )LocaleRegexDescriptorNc                 C   sd   |du r| S |j }t|tr| ||jd< |jd S t }||jvr-| t||j|< |j| S )T
        Return a compiled regular expression based on the active language.
        Nregex)_regexr<   str_compile__dict__r   _regex_dict)r5   instanceclsrO   language_coder   r   r   __get__   s   



zLocaleRegexDescriptor.__get__c              
   C   s>   zt |W S  t jy } ztd| d| |d }~ww )N"z%" is not a valid regular expression: )recompileerrorr
   )r5   rS   er   r   r   rV      s   zLocaleRegexDescriptor._compiler7   )r"   r3   rA   r\   rV   r   r   r   r   rQ      s    
rQ   c                   @   s   e Zd Zdd Zdd ZdS )CheckURLMixinc                 C   s$   d | }| jr|d | j7 }|S )zI
        Format the URL pattern for display in warning messages.
        '{}'z [name='{}'])formatname)r5   descriptionr   r   r   describe   s   
zCheckURLMixin.describec                 C   sB   t jsg S | jdr| jdstd|  dd}|gS g S )zM
        Check that the pattern does not begin with a forward slash.
        )/rH   z^\/rh   zYour URL pattern {} has a route beginning with a '/'. Remove this slash as it is unnecessary. If this pattern is targeted in an include(), ensure the include() pattern has a trailing '/'.z	urls.W002id)r   ZAPPEND_SLASHrT   
startswithendswithr   rd   rg   r5   warningr   r   r   _check_pattern_startswith_slash   s   z-CheckURLMixin._check_pattern_startswith_slashN)r"   r3   rA   rg   ro   r   r   r   r   rb      s    	rb   c                   @   <   e Zd Ze ZdddZdd Zdd Zd	d
 Zdd Z	dS )rJ   NFc                 C   s"   || _ i | _|| _|| _i | _d S r7   )rT   rX   _is_endpointre   rL   )r5   rS   re   is_endpointr   r   r   r6      s
   
zRegexPattern.__init__c                 C   st   | j r| jjdr| j|n| j|}|r8| }|r dn| }dd | D }||	 d  ||fS d S )N$r   c                 S   s   i | ]\}}|d ur||qS r7   r   )r   kvr   r   r   
<dictcomp>   s    z&RegexPattern.match.<locals>.<dictcomp>)
rq   rS   rO   rl   	fullmatchsearch	groupdictgroupsitemsend)r5   pathmatchr&   r%   r   r   r   r~      s   
zRegexPattern.matchc                 C   s*   g }| |   | js| |   |S r7   )extendro   rq   _check_include_trailing_dollarr5   warningsr   r   r   check   s
   zRegexPattern.checkc                 C   s4   | j dr| j dstd|  ddgS g S )Nrs   z\$zYour URL pattern {} uses include with a route ending with a '$'. Remove the dollar from the route to avoid problems including URLs.z	urls.W001ri   )rT   rl   r   rd   rg   r5   r   r   r   r      s   
	z+RegexPattern._check_include_trailing_dollarc                 C   
   t | jS r7   )rU   rT   r   r   r   r   __str__      
zRegexPattern.__str__NF)
r"   r3   rA   rQ   rS   r6   r~   r   r   r   r   r   r   r   rJ      s    
rJ   z1<(?:(?P<converter>[^>:]+):)?(?P<parameter>[^>]+)>c                 C   s,  dg}t  }i }d}t| D ]l}t|d s td| d|jdd\}}| s7td| d|dz|| }	W n tyU }
 ztd| d	|d
|
d}
~
ww |	||< |	 \}}|
t| ||  |}|
d| d|	j d q|
t| |d  |r|
d d||fS )a  
    Convert a path pattern into a regular expression. Return the regular
    expression and a dictionary mapping the capture names to the converters.
    For example, 'foo/<int:pk>' returns '^foo\/(?P<pk>[0-9]+)'
    and {'pk': <django.urls.converters.IntConverter>}.
    ^r   z
URL route u3    cannot contain whitespace in angle brackets <…>.rU   )defaultz uses parameter name z' which isn't a valid Python identifier.z uses invalid converter r#   N(?P<>)z\Zr;   )r   _PATH_PARAMETER_COMPONENT_REfinditerwhitespace_set
isdisjointr
   rz   isidentifierKeyErrorspanappendr^   escaperS   r-   )r(   rr   partsZall_convertersrL   Zprevious_endZmatch_Zraw_converterZ	parameter	converterra   startr|   r   r   r   _route_to_regex   sB   

r   c                   @   s   e Zd ZdddZdS )LocaleRegexRouteDescriptorNc                 C   sp   |du r| S t |jtrt|j|jd< |jd S t }||jvr3tt	t|j|j
d |j|< |j| S )rR   NrS   r   )r<   _routerU   r^   r_   rT   rW   r   rX   r   rq   )r5   rY   rZ   r[   r   r   r   r\   &  s   



z"LocaleRegexRouteDescriptor.__get__r7   )r"   r3   rA   r\   r   r   r   r   r   %  s    r   c                   @   rp   )RoutePatternNFc                 C   s2   || _ tt||\| _| _i | _|| _|| _d S r7   )r   r   rU   rT   rL   rX   rq   re   )r5   r(   re   rr   r   r   r   r6   =  s
   
zRoutePattern.__init__c              	   C   sv   | j |}|r9| }| D ]\}}| j| }z	||||< W q ty-   Y  d S w || d  d|fS d S Nr   )rS   rx   ry   r{   rL   Z	to_python
ValueErrorr|   )r5   r}   r~   r&   keyvaluer   r   r   r   r~   D  s   
zRoutePattern.matchc                 C   sV   g |   |  }| j}d|v s|ds|dr)|td|  dd |S )Nr   r   rs   zYour URL pattern {} has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().z2_0.W001ri   )	ro   '_check_pattern_unmatched_angle_bracketsr   rk   rl   r   r   rd   rg   )r5   r   r(   r   r   r   r   R  s   
zRoutePattern.checkc                 C   s   g }d}t dt| j}d}|D ](}|dkr|d7 }q|dkr9|d8 }|dk r9|t||  df dd d}q|dkrM|t||  df dd |S )	Nz2Your URL pattern %s has an unmatched '%s' bracket.z[<>]r   <r   r   z	urls.W010ri   )r^   findallrU   r   r   r   rg   )r5   r   msgZbracketsZopen_bracket_counterZbracketr   r   r   r   c  s$   
z4RoutePattern._check_pattern_unmatched_angle_bracketsc                 C   r   r7   )rU   r   r   r   r   r   r   v  r   zRoutePattern.__str__r   )
r"   r3   rA   r   rS   r6   r~   r   r   r   r   r   r   r   r   :  s    
r   c                   @   sN   e Zd ZdddZedd Zedd Zdd	 Zd
d Zdd Z	dd Z
dS )LocalePrefixPatternTc                 C   s   || _ i | _d S r7   )prefix_default_languagerL   )r5   r   r   r   r   r6   {  s   
zLocalePrefixPattern.__init__c                 C   s   t t | jS r7   )r^   r_   r   language_prefixr   r   r   r   rS     s   zLocalePrefixPattern.regexc                 C   s(   t  ptj}|tjkr| jsdS d| S )Nr;   z%s/)r   r   ZLANGUAGE_CODEr   r5   r[   r   r   r   r     s   z#LocalePrefixPattern.language_prefixc                 C   s$   | j }||r||di fS d S r   )r   rk   removeprefix)r5   r}   r   r   r   r   r~     s   
zLocalePrefixPattern.matchc                 C   s   g S r7   r   r   r   r   r   r     s   zLocalePrefixPattern.checkc                 C   s
   d | S )Nrc   )rd   r   r   r   r   rg     r   zLocalePrefixPattern.describec                 C   s   | j S r7   )r   r   r   r   r   r     s   zLocalePrefixPattern.__str__N)T)r"   r3   rA   r6   propertyrS   r   r~   r   rg   r   r   r   r   r   r   z  s    


r   c                   @   sJ   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Ze	dd Z
dS )
URLPatternNc                 C   s    || _ || _|p	i | _|| _d S r7   )rO   callbackdefault_argsre   )r5   rO   r   r   re   r   r   r   r6     s   

zURLPattern.__init__c                 C   s   d| j j| j f S )Nz<%s %s>)r2   r"   rO   rg   r   r   r   r   r@     s   zURLPattern.__repr__c                 C   s*   |   }|| j  ||   |S r7   )_check_pattern_namer   rO   r   _check_callbackr   r   r   r   r     s   zURLPattern.checkc                 C   s:   | j jdurd| j jv rtd| j  dd}|gS g S )zG
        Check that the pattern name does not contain a colon.
        Nr    zjYour URL pattern {} has a name including a ':'. Remove the colon, to avoid ambiguous namespace references.z	urls.W003ri   )rO   re   r   rd   rg   rm   r   r   r   r     s   zURLPattern._check_pattern_namec                 C   sL   ddl m} | j}t|r$t||r$td| j |j	|j	f ddgS g S )Nr   )ViewzIYour URL pattern %s has an invalid view, pass %s.as_view() instead of %s.z	urls.E009ri   )
Zdjango.viewsr   r   inspectisclass
issubclassr   rO   rg   r"   )r5   r   viewr   r   r   r     s   zURLPattern._check_callbackc              	   C   sP   | j |}|r&|\}}}i || j}t| j||| j jt| j || jdS d S )N)r(   r*   r+   )rO   r~   r   r   r   re   rU   )r5   r}   r~   new_pathr%   r*   r&   r   r   r   resolve  s   
zURLPattern.resolvec                 C   sV   | j }t|tjr|j}t|dr|j}nt|ds#|jd |jj	 S |jd |j
 S )zw
        A string that identifies the view (e.g. 'path.to.view_function' or
        'path.to.ClassBasedView').
        r!   r"   r#   )r   r<   r=   r>   r$   r1   r!   r3   r2   r"   rA   )r5   r   r   r   r   
lookup_str  s   

zURLPattern.lookup_str)NN)r"   r3   rA   r6   r@   r   r   r   r   r   r   r   r   r   r   r     s    
r   c                   @   s   e Zd Z	d"ddZdd Zdd Zdd	 Zed
d Zedd Z	edd Z
ed#ddZedd Zdd Zdd Zedd Zedd Zdd Zdd Zd d! ZdS )$rI   Nc                 C   sT   || _ || _d | _|pi | _|| _|| _i | _i | _i | _t	 | _
d| _t | _d S r   )rO   urlconf_namer   default_kwargsr0   r.   _reverse_dict_namespace_dict	_app_dictset_callback_strs
_populatedr   _local)r5   rO   r   r   r.   r0   r   r   r   r6     s   
zURLResolver.__init__c                 C   sP   t | jtr| jrd| jd jj }nt| j}d| jj|| j| j| j	 f S )Nz	<%s list>r   z<%s %s (%s:%s) %s>)
r<   r   listr2   r"   r?   r.   r0   rO   rg   )r5   Zurlconf_reprr   r   r   r@     s   
zURLResolver.__repr__c                 C   s,   g }| j D ]	}|t| q|p| j S r7   )rM   r   r	   rO   r   )r5   messagesrO   r   r   r   r     s   
zURLResolver.checkc                 C   s  t | jddr	d S zd| j_t }i }i }t }t| jD ]}|jjj}|	d}t
|tr^| j|j t|jjj}||j|||j|jjf |jd ur]||j|||j|jjf q|  |jrx||jg |j ||f||j< nf|jD ]0}|j|D ]'\}	}
}}t||
 }|||||
 i ||ji | jj|jj|f qq{|j D ]\}\}}|jj}|jj| || |f||< q|j  D ]\}}||g !| q| j|j q|| j"|< || j#|< || j$|< d| _%W d| j_d S d| j_w )N
populatingFTr   )&getattrr   r   r   r   reversedrM   rO   rS   r   r<   r   r   addr   r   Z
appendlistr   r   rL   re   	_populater.   
setdefaultr   r0   reverse_dictgetlistr   namespace_dictr{   updateapp_dictr   r   r   r   r   )r5   Zlookupsr/   Zappsr[   Zurl_patternZ	p_patternbitsre   matchespatdefaultsrL   Znew_matchesr0   prefixZsub_patternZcurrent_convertersr.   Znamespace_listr   r   r   r     s   



	






zURLResolver._populatec                 C   "   t  }|| jvr|   | j| S r7   )r   r   r   r   r   r   r   r   k     

zURLResolver.reverse_dictc                 C   r   r7   )r   r   r   r   r   r   r   r   r  r   zURLResolver.namespace_dictc                 C   r   r7   )r   r   r   r   r   r   r   r   y  r   zURLResolver.app_dictc                    s4   |d u r|   g d S |  fdd|D  d S )Nc                 3   s    | ]} g|V  qd S r7   r   )r   trO   r   r   	<genexpr>  s    z,URLResolver._extend_tried.<locals>.<genexpr>)r   r   )r)   rO   Z	sub_triedr   r   r   _extend_tried  s   zURLResolver._extend_triedc                 C   s   | s|S | d}| | S )z<Join two routes, without the starting ^ in the second route.r   )r   )Zroute1Zroute2r   r   r   _join_route  s   
zURLResolver._join_routec                 C   s   | j s|   || jv S r7   )r   r   r   )r5   re   r   r   r   _is_callback  s   
zURLResolver._is_callbackc                 C   sP  t |}g }| j|}|r|\}}}| jD ]}z||}W n ty> }	 z| |||	jd d W Y d }	~	qd }	~	ww |ri || j	}
|

|j |j}|
sX||j }t|tr_dnt |j}| |||j t|j||
|j| jg|j | jg|j | ||j||ji | j	|jd
  S ||g qt||dtd|i)Nr   r)   r;   )r*   r+   )r)   r}   r}   )rU   rO   r~   rM   r   r   r   r%   getr   r   r&   r<   r   r)   r   r$   r'   r.   r,   r0   r/   r   r(   r*   r+   r   )r5   r}   r)   r~   r   r%   r&   rO   Z	sub_matchra   Zsub_match_dictZsub_match_argsZcurrent_router   r   r   r     sT   

&

zURLResolver.resolvec                 C   s   t | jtrt| jS | jS r7   )r<   r   rU   r   r   r   r   r   urlconf_module  s   
zURLResolver.urlconf_modulec              
   C   sP   t | jd| j}zt| W |S  ty' } zd}t|j| jd|d }~ww )NZurlpatternszThe included URLconf '{name}' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.)re   )r   r   iter	TypeErrorr
   rd   r   )r5   patternsra   r   r   r   r   rM     s   
	zURLResolver.url_patternsc                 C   s8   t | jd| d }|sddlm} t |d| }t|S )Nz	handler%sr   )urls)r   r   django.confr   r   )r5   Z	view_typer   r   r   r   r   resolve_error_handler  s
   z!URLResolver.resolve_error_handlerc                 O   s   | j |dg|R i |S )Nr;   )_reverse_with_prefix)r5   lookup_viewr%   r&   r   r   r   reverse  s   zURLResolver.reversec                 O   s  |r|rt d| js|   | j|}|D ]\}}}}	|D ]\}
}|r6t|t|kr.qtt||}n,t|	|
|rAqd}| D ]\}}||v rPqG||||kr\d} nqG|s`q|}i }d}| D ]&\}}||	v rz|	| |||< W qj t y   d}Y  nw t|||< qj|sq|dd|
 }tdt||f || rt|| td d}t|    S qqt|d	d }t|d
d }|d ur|d urd||f }n|}dd |D }|r|rd|f }n	|rd| }nd}d||t||f }t|dd|i }t|)Nz2Don't mix *args and **kwargs in call to reverse()!TF%z%%z^%s%sz/~:@)safer3   r"   z%s.%sc                 S   s   g | ]\}}}}|qS r   r   )r   _rO   r   r   r   r   ,  s    z4URLResolver._reverse_with_prefix.<locals>.<listcomp>zarguments '%s'zkeyword arguments '%s'zno argumentsz;Reverse for '%s' with %s not found. %d pattern(s) tried: %szZReverse for '%(view)s' not found. '%(view)s' is not a valid view function or pattern name.r   )r   r   r   r   r   lenrK   zipr   symmetric_difference
differencer{   r   Zto_urlrU   replacer^   rx   r   r   r   r   r   r   )r5   r   _prefixr%   r&   possibilitiesZpossibilityrO   r   rL   resultparamsZcandidate_subsr   rt   ru   Ztext_candidate_subsr~   Zcandidate_paturlmnZlookup_view_sr   Zarg_msgr   r   r   r   r     s   3
z URLResolver._reverse_with_prefix)NNNr7   )r"   r3   rA   r6   r@   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   rM   r   r   r   r   r   r   r   rI     s2    
T



0


rI   r7   )<__doc__r=   r   r^   string	importlibr   pickler   urllib.parser   Zasgiref.localr   r   r   Zdjango.core.checksr   r   Zdjango.core.checks.urlsr	   Zdjango.core.exceptionsr
   Zdjango.utils.datastructuresr   Zdjango.utils.functionalr   Zdjango.utils.httpr   r   Zdjango.utils.regex_helperr   r   Zdjango.utils.translationr   rL   r   
exceptionsr   r   utilsr   r   rF   cacherD   rP   rQ   rb   rJ   r   	frozenset
whitespacer   	lru_cacher   r   r   r   r   rI   r   r   r   r   <module>   sR    
J

"2

+@"Q