U
    aj                     @   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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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& G dd dZ'd.ddZ(ej)ddd/ddZ*ej)dddd Z+G dd dZ,G dd dZ-G d d! d!e-Z.e/d"Z0d0d$d%Z1G d&d' d'e-Z2G d(d) d)Z3G d*d+ d+Z4G d,d- d-Z5dS )1z
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)quote)Local)settings)ErrorWarning)check_resolver)ImproperlyConfiguredViewDoesNotExist)MultiValueDict)cached_property)RFC3986_SUBDELIMSescape_leading_slashes)	normalize)get_language   )get_converter)NoReverseMatchResolver404)get_callablec                   @   s&   e Zd ZdddZdd Zdd ZdS )	ResolverMatchNc           	      C   s   || _ || _|| _|| _|| _|r0dd |D ng | _d| j| _|rVdd |D ng | _d| j| _	t
|ds|jjd |jj | _n|jd |j | _|p| j}d| j|g | _d S )Nc                 S   s   g | ]}|r|qS  r   .0xr   r   /home/adriano.carvalho/ftp/files/BrinquedotecaVirtual/brinquedotecavirtual/venv/lib/python3.8/site-packages/django/urls/resolvers.py
<listcomp>*   s      z*ResolverMatch.__init__.<locals>.<listcomp>:c                 S   s   g | ]}|r|qS r   r   r   r   r   r   r   ,   s      __name__.)funcargskwargsurl_nameroute	app_namesjoinapp_name
namespaces	namespacehasattr	__class__
__module__r   
_func_pathZ	view_name)	selfr    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"   )r.   indexr   r   r   __getitem__9   s    zResolverMatch.__getitem__c                 C   s$   d| j | j| j| j| j| j| jf S )Nz^ResolverMatch(func=%s, args=%s, kwargs=%s, url_name=%s, app_names=%s, namespaces=%s, route=%s))r-   r!   r"   r#   r%   r(   r$   r.   r   r   r   __repr__<   s         zResolverMatch.__repr__)NNNN)r   r,   __qualname__r/   r2   r4   r   r   r   r   r       s   
r   c                 C   s   | d krt j} t| S r0   )r   ZROOT_URLCONF_get_cached_resolverZurlconfr   r   r   get_resolverC   s    r8   )maxsizec                 C   s   t td| S N^/)URLResolverRegexPatternr7   r   r   r   r6   I   s    r6   c                 C   s.   t | }t||_t||j}tt d|gS r:   )r=   dict
convertersr<   url_patterns)Z
ns_patternresolverr?   patternZns_resolverr   r   r   get_ns_resolverN   s    
rC   c                   @   s   e Zd Zdd ZdddZdS )LocaleRegexDescriptorc                 C   s
   || _ d S r0   )attr)r.   rE   r   r   r   r/   Z   s    zLocaleRegexDescriptor.__init__Nc                 C   sj   |dkr| S t || j}t|tr<|||jd< |jd S t }||jkr`|t||j|< |j| S )zT
        Return a compiled regular expression based on the active language.
        Nregex)getattrrE   
isinstancestr_compile__dict__r   _regex_dict)r.   instanceclsrB   language_coder   r   r   __get__]   s    


zLocaleRegexDescriptor.__get__)N)r   r,   r5   r/   rP   r   r   r   r   rD   Y   s   rD   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)r.   descriptionr   r   r   describeq   s    
zCheckURLMixin.describec                 C   sJ   | j j}tjsg S |drB|dsBtd|  dd}|gS g S dS )zM
        Check that the pattern does not begin with a forward slash.
        )/r;   z^\/rW   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idN)	rF   rB   r   ZAPPEND_SLASH
startswithendswithr   rS   rV   )r.   regex_patternwarningr   r   r   _check_pattern_startswith_slashz   s    z-CheckURLMixin._check_pattern_startswith_slashN)r   r,   r5   rV   r^   r   r   r   r   rQ   p   s   	rQ   c                   @   sF   e Zd ZedZdddZdd Zdd	 Zd
d Zdd Z	dd Z
dS )r=   _regexNFc                 C   s"   || _ i | _|| _|| _i | _d S r0   )r_   rL   _is_endpointrT   r?   )r.   rF   rT   is_endpointr   r   r   r/      s
    zRegexPattern.__init__c                 C   sT   | j |}|rP| }|r dn| }dd | D }|| d  ||fS d S )Nr   c                 S   s   i | ]\}}|d k	r||qS r0   r   r   kvr   r   r   
<dictcomp>   s       z&RegexPattern.match.<locals>.<dictcomp>)rF   search	groupdictgroupsitemsend)r.   pathmatchr"   r!   r   r   r   rl      s    zRegexPattern.matchc                 C   s*   g }| |   | js&| |   |S r0   )extendr^   r`   _check_include_trailing_dollarr.   warningsr   r   r   check   s
    zRegexPattern.checkc                 C   s<   | j j}|dr4|ds4td|  ddgS g S d S )N$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.W001rX   )rF   rB   r[   r   rS   rV   )r.   r\   r   r   r   rn      s    z+RegexPattern._check_include_trailing_dollarc              
   C   sF   zt |W S  t jk
r@ } ztd||f W 5 d}~X Y nX dS )z0Compile and return the given regular expression.z*"%s" is not a valid regular expression: %sN)recompileerrorr	   )r.   rF   er   r   r   rJ      s    
zRegexPattern._compilec                 C   s
   t | jS r0   )rI   r_   r3   r   r   r   __str__   s    zRegexPattern.__str__)NF)r   r,   r5   rD   rF   r/   rl   rq   rn   rJ   rw   r   r   r   r   r=      s   
	r=   z/<(?:(?P<converter>[^>:]+):)?(?P<parameter>\w+)>Fc           
   
   C   s4  t | tjstd|  | }dg}i }t| }|sL|t	|  q|t	| d|
   | | d } |d}| std||f |d}|dkrd}zt|}W n2 tk
r }	 ztd||	f W 5 d}	~	X Y nX |||< |d	| d
 |j d  q*|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>}.
    z)URL route '%s' cannot contain whitespace.^N	parameterzLURL route '%s' uses parameter name %r which isn't a valid Python identifier.	converterrI   z)URL route '%s' uses invalid converter %s.(?P<>)rr    )set
isdisjointstring
whitespacer	   _PATH_PARAMETER_COMPONENT_RErf   appendrs   escapestartrj   groupisidentifierr   KeyErrorrF   r&   )
r$   ra   Zoriginal_routepartsr?   rl   ry   Zraw_converterrz   rv   r   r   r   _route_to_regex   s@    




r   c                   @   s>   e Zd ZedZdddZdd Zdd	 Zd
d Zdd Z	dS )RoutePattern_routeNFc                 C   s0   || _ i | _|| _|| _tt||d | _d S )Nr   )r   rL   r`   rT   r   rI   r?   )r.   r$   rT   ra   r   r   r   r/      s
    zRoutePattern.__init__c              	   C   sz   | j |}|rv| }| D ]>\}}| j| }z||||< W q  tk
r\   Y  d S X q || d  d|fS d S Nr   )rF   rf   rg   ri   r?   Z	to_python
ValueErrorrj   )r.   rk   rl   r"   keyvaluerz   r   r   r   rl      s    
zRoutePattern.matchc                 C   sJ   |   }| j}d|ks*|ds*|drF|td|  dd |S )Nr{   rx   rr   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.W001rX   )r^   r   rZ   r[   r   r   rS   rV   )r.   rp   r$   r   r   r   rq     s    zRoutePattern.checkc                 C   s   t t|| jd S )Nr   )rs   rt   r   r`   )r.   r$   r   r   r   rJ     s    zRoutePattern._compilec                 C   s
   t | jS r0   )rI   r   r3   r   r   r   rw     s    zRoutePattern.__str__)NF)
r   r,   r5   rD   rF   r/   rl   rq   rJ   rw   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 r0   )prefix_default_languager?   )r.   r   r   r   r   r/   "  s    zLocalePrefixPattern.__init__c                 C   s   t | jS r0   )rs   rt   language_prefixr3   r   r   r   rF   &  s    zLocalePrefixPattern.regexc                 C   s,   t  p
tj}|tjkr | js dS d| S d S )Nr~   z%s/)r   r   ZLANGUAGE_CODEr   r.   rO   r   r   r   r   +  s    z#LocalePrefixPattern.language_prefixc                 C   s*   | j }||r&|t|d  di fS d S r   )r   rZ   len)r.   rk   r   r   r   r   rl   3  s    
zLocalePrefixPattern.matchc                 C   s   g S r0   r   r3   r   r   r   rq   9  s    zLocalePrefixPattern.checkc                 C   s
   d | S )NrR   )rS   r3   r   r   r   rV   <  s    zLocalePrefixPattern.describec                 C   s   | j S r0   )r   r3   r   r   r   rw   ?  s    zLocalePrefixPattern.__str__N)T)r   r,   r5   r/   propertyrF   r   rl   rq   rV   rw   r   r   r   r   r   !  s   


r   c                   @   sB   e Zd Zd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 r0   )rB   callbackdefault_argsrT   )r.   rB   r   r   rT   r   r   r   r/   D  s    
zURLPattern.__init__c                 C   s   d| j j| j f S )Nz<%s %s>)r+   r   rB   rV   r3   r   r   r   r4   J  s    zURLPattern.__repr__c                 C   s   |   }|| j  |S r0   )_check_pattern_namerm   rB   rq   ro   r   r   r   rq   M  s    zURLPattern.checkc                 C   s>   | j jdk	r6d| j jkr6td| j  dd}|gS g S d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.W003rX   )rB   rT   r   rS   rV   )r.   r]   r   r   r   r   R  s    zURLPattern._check_pattern_namec                 C   sH   | j |}|rD|\}}}|| j t| j||| j jt| j dS d S )N)r$   )rB   rl   updater   r   r   rT   rI   )r.   rk   rl   new_pathr!   r"   r   r   r   resolve`  s
    
zURLPattern.resolvec                 C   sD   | j }t|tjr|j}t|ds4|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   rH   	functoolspartialr    r*   r,   r+   r   r5   )r.   r   r   r   r   
lookup_strh  s    
zURLPattern.lookup_str)NN)
r   r,   r5   r/   r4   rq   r   r   r   r   r   r   r   r   r   C  s   
r   c                   @   s   e Zd Zd"ddZ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 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 )#r<   Nc                 C   sT   || _ || _d | _|pi | _|| _|| _i | _i | _i | _t	 | _
d| _t | _d S )NF)rB   urlconf_namer   default_kwargsr)   r'   _reverse_dict_namespace_dict	_app_dictr   _callback_strs
_populatedr   _local)r.   rB   r   r   r'   r)   r   r   r   r/   w  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>)
rH   r   listr+   r   reprr'   r)   rB   rV   )r.   Zurlconf_reprr   r   r   r4     s    
   zURLResolver.__repr__c                 C   s:   g }| j D ]}|t| q
||   |p8| j S r0   )r@   rm   r   _check_custom_error_handlersrB   rq   )r.   messagesrB   r   r   r   rq     s
    
zURLResolver.checkc                 C   s   g }dD ]\}}z|  |\}}W nd ttfk
r } zBt| jd| }dj||d}|t|t|dd W Y qW 5 d }~X Y nX t	
|}	d g| }
z|	j|
  W q tk
r   dj||jd |j |d	krd
ndd}|t|dd Y qX q|S )N))i     )i  r   )i  r   )i  r   	handler%szDThe custom handler{status_code} view '{path}' could not be imported.)status_coderk   z	urls.E008)hintrY   zeThe custom handler{status_code} view '{path}' does not take the correct number of arguments ({args}).r   r   zrequest, exceptionrequest)r   rk   r!   z	urls.E007rX   )resolve_error_handlerImportErrorr
   rG   urlconf_modulerS   r   r   rI   inspect	signaturebind	TypeErrorr,   r5   )r.   r   r   Znum_parametershandlerZ
param_dictrv   rk   msgr   r!   r   r   r   r     s4     

z(URLResolver._check_custom_error_handlersc                 C   s  t | jddrd S zd| j_t }i }i }t }t| jD ]}|jjj}|	drb|dd  }t
|tr| j|j t|jjj}||j|||j|jjf |jd k	r||j|||j|jjf q<|  |jr||jg |j ||f||j< n|jD ]Z}|j|D ]F\}	}
}}t||
 }|||||
 ||j| jj|jj|f qq|j D ]4\}\}}|jj}|jj| || |f||< qn|j  D ]\}}||g !| q| j|j q<|| j"|< || j#|< || j$|< d| _%W 5 d| j_X d S )N
populatingFTrx   r   )&rG   r   r   r   r   reversedr@   rB   rF   rZ   rH   r   r   addr   r   Z
appendlistr   r   r?   rT   	_populater'   
setdefaultr   r)   reverse_dictgetlistr   namespace_dictri   r   app_dictrm   r   r   r   r   )r.   Zlookupsr(   ZappsrO   Zurl_patternZ	p_patternbitsrT   matchespatdefaultsr?   Znew_matchesr)   prefixZsub_patternZcurrent_convertersr'   Znamespace_listr   r   r   r     sf    




	



zURLResolver._populatec                 C   s"   t  }|| jkr|   | j| S r0   )r   r   r   r   r   r   r   r     s    
zURLResolver.reverse_dictc                 C   s"   t  }|| jkr|   | j| S r0   )r   r   r   r   r   r   r   r     s    
zURLResolver.namespace_dictc                 C   s"   t  }|| jkr|   | j| S r0   )r   r   r   r   r   r   r   r     s    
zURLResolver.app_dictc                 C   s&   | s|S | dr|dd }| | S )z<Join two routes, without the starting ^ in the second route.rx   r   N)rZ   )Zroute1Zroute2r   r   r   _join_route  s
    
zURLResolver._join_routec                 C   s   | j s|   || jkS r0   )r   r   r   )r.   rT   r   r   r   _is_callback  s    zURLResolver._is_callbackc                    sR  t |}g }| j|}|rB|\}}}| jD ] z |}W n` tk
r } zB|jd d}	|	d k	r| fdd|	D  n|	 g W 5 d }~X Y q.X |r&|| j
}
|
|j |j}|
s||j }t trdnt  j}t|j||
|j| jg|j | jg|j | ||j  S |	 g q.t||dtd|id S )Nr   triedc                 3   s   | ]} g| V  qd S r0   r   )r   trB   r   r   	<genexpr>&  s     z&URLResolver.resolve.<locals>.<genexpr>r~   )r   rk   rk   )rI   rB   rl   r@   r   r   r!   getrm   r   r   r   r"   rH   r   r   r    r#   r'   r%   r)   r(   r   r$   )r.   rk   r   rl   r   r!   r"   Z	sub_matchrv   Z	sub_triedZsub_match_dictZsub_match_argsZcurrent_router   r   r   r     s@    


	zURLResolver.resolvec                 C   s    t | jtrt| jS | jS d S r0   )rH   r   rI   r   r3   r   r   r   r   B  s    
zURLResolver.urlconf_modulec                 C   sL   t | jd| j}zt| W n* tk
rF   d}t|j| jdY nX |S )NZurlpatternszThe included URLconf '{name}' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.)rT   )rG   r   iterr   r	   rS   r   )r.   patternsr   r   r   r   r@   I  s    zURLResolver.url_patternsc                 C   s<   t | jd| d }|s0ddlm} t |d| }t|i fS )Nr   r   )urls)rG   r   django.confr   r   )r.   Z	view_typer   r   r   r   r   r   X  s
    z!URLResolver.resolve_error_handlerc                 O   s   | j |df||S )Nr~   )_reverse_with_prefix)r.   lookup_viewr!   r"   r   r   r   reversea  s    zURLResolver.reversec                    s  |r rt d| js|   | j|}|D ]\}}}}	|D ]\}
}|rnt|t|kr^q@tt||}n6t 	|
|rq@t fdd| D rq@ }i }| D ]0\}}||	kr|	| |||< qt|||< q|dd|
 }tdt||f || r@t|| td d}t|    S q@q.t|d	d }t|d
d }|d k	rr|d k	rrd||f }n|}dd |D }|r|rd|f }n rd f }nd}d||t||f }ndd|i }t|d S )Nz2Don't mix *args and **kwargs in call to reverse()!c                 3   s"   | ]\}}  |||kV  qd S r0   )r   rb   r"   r   r   r   v  s     z3URLResolver._reverse_with_prefix.<locals>.<genexpr>%z%%z^%s%sz/~:@)safer,   r   z%s.%sc                 S   s   g | ]\}}}}|qS r   r   )r   _rB   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.view)r   r   r   r   r   r   r>   zipr   symmetric_difference
differenceanyri   Zto_urlrI   replacers   rf   r   r   r   r   rG   r   )r.   r   _prefixr!   r"   possibilitiesZpossibilityrB   r   r?   resultparamsZcandidate_subsZtext_candidate_subsrc   rd   Zcandidate_paturlmnZlookup_view_sr   Zarg_msgr   r   r   r   r   d  s\    z URLResolver._reverse_with_prefix)NNN)r   r,   r5   r/   r4   rq   r   r   r   r   r   r   staticmethodr   r   r   r   r   r@   r   r   r   r   r   r   r   r<   v  s,   
=



(

	r<   )N)N)F)6__doc__r   r   rs   r   	importlibr   urllib.parser   Zasgiref.localr   r   r   Zdjango.core.checksr   r   Zdjango.core.checks.urlsr   Zdjango.core.exceptionsr	   r
   Zdjango.utils.datastructuresr   Zdjango.utils.functionalr   Zdjango.utils.httpr   r   Zdjango.utils.regex_helperr   Zdjango.utils.translationr   r?   r   
exceptionsr   r   utilsr   r   r8   	lru_cacher6   rC   rD   rQ   r=   rt   r   r   r   r   r   r<   r   r   r   r   <module>   sH   #




!6
*+"3