o
    ÕèFh4  ã                   @   sÂ   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mZmZ ddlmZ G d	d
„ d
eƒZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zdd„ ZdS )é    )Úwraps)Úimport_module)ÚgetfullargspecÚunwrap)Úconditional_escape)Úis_iterableé   )ÚNodeÚTemplateÚtoken_kwargs)ÚTemplateSyntaxErrorc                   @   s   e Zd ZdS )ÚInvalidTemplateLibraryN)Ú__name__Ú
__module__Ú__qualname__© r   r   úP/var/www/html/loop/nvenv/lib/python3.10/site-packages/django/template/library.pyr      s    r   c                   @   sP   e Zd ZdZdd„ Zddd„Zdd„ Zdd	d
„Zdd„ Zddd„Z	ddd„Z
dS )ÚLibrarya  
    A class for registering template tags and filters. Compiled filter and
    template tag functions are stored in the filters and tags attributes.
    The filter, simple_tag, and inclusion_tag methods provide a convenient
    way to register callables as tags.
    c                 C   s   i | _ i | _d S ©N)ÚfiltersÚtags)Úselfr   r   r   Ú__init__   s   
zLibrary.__init__Nc                    sx   ˆ d u r|d u rˆj S ˆ d ur%|d u r%tˆ ƒrˆ  ˆ ¡S ‡ ‡fdd„}|S ˆ d ur4|d ur4|ˆjˆ < |S tdˆ |f ƒ‚)Nc                    s   ˆ  ˆ | ¡S r   )Útag©Úfunc©Únamer   r   r   Údec&   s   zLibrary.tag.<locals>.decz.Unsupported arguments to Library.tag: (%r, %r))Útag_functionÚcallabler   Ú
ValueError)r   r   Úcompile_functionr   r   r   r   r      s   

ÿÿzLibrary.tagc                 C   s   || j |j< |S r   )r   r   )r   r   r   r   r   r   4   s   zLibrary.tag_functionc                    sÊ   ˆdu r|du r‡ ‡fdd„}|S ˆdur0|du r0t ˆƒr&ˆjˆfi ˆ ¤ŽS ‡ ‡‡fdd„}|S ˆdur]|dur]|ˆjˆ< dD ]}|ˆ v rWˆ | }t|||ƒ tt|ƒ||ƒ q?ˆ|_|S tdˆ|f ƒ‚)z™
        Register a callable as a template filter. Example:

        @register.filter
        def lower(value):
            return value.lower()
        Nc                    s   ˆj | fi ˆ ¤ŽS r   )Úfilter_functionr   )Úflagsr   r   r   r   B   s   zLibrary.filter.<locals>.decc                    s   ˆj ˆ| fi ˆ ¤ŽS r   )Úfilterr   ©r$   r   r   r   r   r   L   s   )Úexpects_localtimeÚis_safeÚneeds_autoescapez1Unsupported arguments to Library.filter: (%r, %r))r    r#   r   Úsetattrr   Ú_filter_namer!   )r   r   Úfilter_funcr$   r   ÚattrÚvaluer   r&   r   r%   8   s.   
€ÿÿzLibrary.filterc                 K   s   | j |j|fi |¤ŽS r   )r%   r   )r   r   r$   r   r   r   r#   c   s   zLibrary.filter_functionc                    s4   ‡ ‡‡fdd„}|du r|S t |ƒr||ƒS tdƒ‚)z§
        Register a callable as a compiled template tag. Example:

        @register.simple_tag
        def hello(*args, **kwargs):
            return 'world'
        c              
      sX   t tˆƒƒ\‰‰‰‰ ‰‰}ˆpˆj‰tˆƒ‡ ‡‡‡‡‡‡
‡‡f	dd„ƒ}ˆ	 ˆ|¡ ˆS )Nc                    sn   |  ¡ dd … }d }t|ƒdkr |d dkr |d }|d d… }t| |ˆˆˆˆ ˆˆˆˆƒ
\}}tˆˆ|||ƒS )Nr   é   éþÿÿÿÚaséÿÿÿÿ)Úsplit_contentsÚlenÚ
parse_bitsÚ
SimpleNode)ÚparserÚtokenÚbitsÚ
target_varÚargsÚkwargs)	Údefaultsr   Úfunction_nameÚkwonlyÚkwonly_defaultsÚparamsÚtakes_contextÚvarargsÚvarkwr   r   Úcompile_func{   s$   öz5Library.simple_tag.<locals>.dec.<locals>.compile_func©r   r   r   r   r   ©r   Ú_rE   ©r   r   rB   ©r=   r   r>   r?   r@   rA   rC   rD   r   r   o   s   
	ø
zLibrary.simple_tag.<locals>.decNz(Invalid arguments provided to simple_tag)r    r!   )r   r   rB   r   r   r   rI   r   Ú
simple_tagf   s   	$zLibrary.simple_tagc                    s   ‡ ‡‡‡fdd„}|S )zß
        Register a callable as an inclusion tag:

        @register.inclusion_tag('results.html')
        def show_results(poll):
            choices = poll.choice_set.all()
            return {'choices': choices}
        c                    sZ   t tˆƒƒ\‰‰‰‰ ‰‰}ˆ	pˆj‰tˆƒ‡ ‡‡‡‡‡‡‡‡‡f
dd„ƒ}ˆ
 ˆ|¡ ˆS )Nc                    s>   |  ¡ dd … }t| |ˆˆˆ	ˆ ˆˆˆˆƒ
\}}tˆˆ||ˆƒS )Nr   )r3   r5   ÚInclusionNode)r7   r8   r9   r;   r<   )
r=   Úfilenamer   r>   r?   r@   rA   rB   rC   rD   r   r   rE   ²   s(   öûz8Library.inclusion_tag.<locals>.dec.<locals>.compile_funcrF   rG   ©rM   r   r   rB   rJ   r   r   ¦   s   
	ø
 z"Library.inclusion_tag.<locals>.decr   )r   rM   r   rB   r   r   r   rN   r   Úinclusion_tagœ   s   
&zLibrary.inclusion_tag)NN)NNN)r   r   r   Ú__doc__r   r   r   r%   r#   rK   rO   r   r   r   r   r      s    

+
6r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTagHelperNodez¬
    Base class for tag helper nodes such as SimpleNode and InclusionNode.
    Manages the positional and keyword arguments to be passed to the decorated
    function.
    c                 C   s   || _ || _|| _|| _d S r   )r   rB   r;   r<   )r   r   rB   r;   r<   r   r   r   r   Ö   s   
zTagHelperNode.__init__c                    sD   ‡ fdd„| j D ƒ}| jrˆ g| }‡ fdd„| j ¡ D ƒ}||fS )Nc                    s   g | ]}|  ˆ ¡‘qS r   ©Úresolve)Ú.0Úvar©Úcontextr   r   Ú
<listcomp>Ý   s    z8TagHelperNode.get_resolved_arguments.<locals>.<listcomp>c                    s   i | ]
\}}||  ˆ ¡“qS r   rR   )rT   ÚkÚvrV   r   r   Ú
<dictcomp>à   s    z8TagHelperNode.get_resolved_arguments.<locals>.<dictcomp>)r;   rB   r<   Úitems)r   rW   Úresolved_argsÚresolved_kwargsr   rV   r   Úget_resolved_argumentsÜ   s
   
z$TagHelperNode.get_resolved_argumentsN)r   r   r   rP   r   r_   r   r   r   r   rQ   Ï   s    rQ   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )r6   r   c                    ó   t ƒ  ||||¡ || _d S r   )Úsuperr   r:   )r   r   rB   r;   r<   r:   ©Ú	__class__r   r   r   ç   ó   
zSimpleNode.__init__c                 C   sH   |   |¡\}}| j|i |¤Ž}| jd ur||| j< dS |jr"t|ƒ}|S )NÚ )r_   r   r:   Ú
autoescaper   )r   rW   r]   r^   Úoutputr   r   r   Úrenderë   s   

zSimpleNode.render)r   r   r   Úchild_nodelistsr   rh   Ú__classcell__r   r   rb   r   r6   ä   s    r6   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )rL   c                    r`   r   )ra   r   rM   )r   r   rB   r;   r<   rM   rb   r   r   r   ÷   rd   zInclusionNode.__init__c                 C   sÔ   |   |¡\}}| j|i |¤Ž}|j | ¡}|du rSt| jtƒr#| j}n+tt| jddƒtƒr2| jj}nt| jt	ƒsFt
| jƒrF|jj | j¡}n|jj | j¡}||j| < | |¡}| d¡}|dure||d< | |¡S )z°
        Render the specified template and context. Cache the template object
        in render_context to avoid reparsing and loading when used in a for
        loop.
        NÚtemplateÚ
csrf_token)r_   r   Úrender_contextÚgetÚ
isinstancerM   r
   Úgetattrrk   Ústrr   ÚengineÚselect_templateÚget_templateÚnewrh   )r   rW   r]   r^   Ú_dictÚtÚnew_contextrl   r   r   r   rh   û   s"   




zInclusionNode.render)r   r   r   r   rh   rj   r   r   rb   r   rL   ö   s    rL   c
              	      sŒ  |r|r|d dkr|dd… }nt d|	 ƒ‚g }
i }t|ƒ}‡ fdd„|D ƒ}|D ]t}t|g| ƒ}|rt| ¡ \}}||vrN||vrN|du rNt d|	|f ƒ‚||v rZt d	|	|f ƒ‚||t|ƒ< ||v rj| |¡ q*||v rs| |¡ q*|r|t d
|	 ƒ‚|
 |  |¡¡ z| d¡ W q* t	yž   |du rœt d|	 ƒ‚Y q*w |dur¬|dt
|ƒ … }|s°|rÂt d|	d dd„ || D ƒ¡f ƒ‚|
|fS )z°
    Parse bits for template tag helpers simple_tag and inclusion_tag, in
    particular by detecting syntax errors and by extracting positional and
    keyword arguments.
    r   rW   r   NzW'%s' is decorated with takes_context=True so it must have a first argument of 'context'c                    s   g | ]
}ˆ r
|ˆ vr|‘qS r   r   )rT   Úkwarg©r@   r   r   rX   5  s    ÿzparse_bits.<locals>.<listcomp>z.'%s' received unexpected keyword argument '%s'z7'%s' received multiple values for keyword argument '%s'zH'%s' received some positional argument(s) after some keyword argument(s)z+'%s' received too many positional argumentsz5'%s' did not receive value(s) for the argument(s): %sz, c                 s   s   � | ]}d | V  qdS )z'%s'Nr   )rT   Úpr   r   r   Ú	<genexpr>l  s   € zparse_bits.<locals>.<genexpr>)r   Úlistr   Úpopitemrq   ÚremoveÚappendÚcompile_filterÚpopÚ
IndexErrorr4   Újoin)r7   r9   rA   rC   rD   r=   r?   r@   rB   r   r;   r<   Úunhandled_paramsÚunhandled_kwargsÚbitry   Úparamr.   r   rz   r   r5     sv   ÿÿ
ÿ
ÿÿÿ
€ÿÿÿÿÿÿÿr5   c              
   C   sZ   zt | ƒ}W n ty } ztd| |f ƒ‚d}~ww z|jW S  ty,   td|  ƒ‚w )z;
    Load a Library object from a template tag module.
    zSInvalid template library specified. ImportError raised when trying to load '%s': %sNz4Module  %s does not have a variable named 'register')r   ÚImportErrorr   ÚregisterÚAttributeError)r   ÚmoduleÚer   r   r   Úimport_libraryq  s"   ÿÿ€ÿÿÿrŽ   N)Ú	functoolsr   Ú	importlibr   Úinspectr   r   Údjango.utils.htmlr   Údjango.utils.itercompatr   Úbaser	   r
   r   Ú
exceptionsr   Ú	Exceptionr   r   rQ   r6   rL   r5   rŽ   r   r   r   r   Ú<module>   s     @#X