o
    þÞh+A  ã                   @   sÒ   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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G dd„ deƒZdd„ Zdd„ ZdS )é    )ÚIterable)Úwraps)Úimport_module)ÚgetfullargspecÚunwrap)Úconditional_escapeé   )ÚNodeÚTemplateÚtoken_kwargs)ÚTemplateSyntaxErrorc                   @   s   e Zd ZdS )ÚInvalidTemplateLibraryN)Ú__name__Ú
__module__Ú__qualname__© r   r   úW/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/template/library.pyr      s    r   c                   @   sZ   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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                    s6   ‡ ‡‡‡fdd„}|du r|S t |ƒr||ƒS tdƒ‚)z«
        Register a callable as a compiled block template tag. Example:

        @register.simple_block_tag
        def hello(content):
            return 'world'
        c                    sl   t tˆƒƒ\‰‰‰‰ ‰‰}ˆ	pˆj‰ˆd u rdˆ› �‰tˆƒ‡ ‡‡‡‡‡‡‡‡‡f
dd„ƒ}ˆ
 ˆ|¡ ˆS )NÚendc                    sæ   ˆ  ¡ }ˆrt|ƒdkr|d dkr|d= ntˆ›d�ƒ‚|r)|d dkr)|d= ntdˆ› d�ƒ‚| ¡ dd … }d }t|ƒdkrQ|d d	krQ|d
 }|d d… }|  ˆf¡}|  ¡  t| ||ˆˆ	ˆ ˆˆˆˆƒ
\}}t|ˆˆ|||ƒS )Nr/   r   Úcontentzv is decorated with takes_context=True so it must have a first argument of 'context' and a second argument of 'content'r   ú'z)' must have a first argument of 'content'r0   r1   r2   )Úcopyr4   r   r3   ÚparseÚdelete_first_tokenr5   ÚSimpleBlockNode)r7   r8   Ú
tag_paramsr9   r:   Únodelistr;   r<   )
r=   Úend_namer   r>   r?   r@   rA   rB   rC   rD   r   r   rE   ¶   sD   ÿ
ÿöÿz;Library.simple_block_tag.<locals>.dec.<locals>.compile_funcrF   rG   ©rV   r   r   rB   rJ   r   r   ¥   s    
ø

 -z%Library.simple_block_tag.<locals>.decNz.Invalid arguments provided to simple_block_tagrK   )r   r   rB   r   rV   r   r   rW   r   Úsimple_block_tagœ   s   	BzLibrary.simple_block_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   ©rZ   r   r   rB   rJ   r   r   ú   s   
	ø
 z"Library.inclusion_tag.<locals>.decr   )r   rZ   r   rB   r   r   r   r[   r   Úinclusion_tagð   s   
&zLibrary.inclusion_tag)NN)NNN)NNNN)r   r   r   Ú__doc__r   r   r   r%   r#   rL   rX   r\   r   r   r   r   r      s    

+

6Tr   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>1  s    z8TagHelperNode.get_resolved_arguments.<locals>.<listcomp>c                    s   i | ]
\}}||  ˆ ¡“qS r   r_   )ra   ÚkÚvrc   r   r   Ú
<dictcomp>4  s    z8TagHelperNode.get_resolved_arguments.<locals>.<dictcomp>)r;   rB   r<   Úitems©r   rd   Úresolved_argsÚresolved_kwargsr   rc   r   Úget_resolved_arguments0  s
   
z$TagHelperNode.get_resolved_argumentsN)r   r   r   r]   r   rm   r   r   r   r   r^   #  s    r^   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Ú )rm   r   r:   Ú
autoescaper   )r   rd   rk   rl   Úoutputr   r   r   Úrender?  s   

zSimpleNode.render)r   r   r   Úchild_nodelistsr   rv   Ú__classcell__r   r   rp   r   r6   8  s    r6   c                       s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )rS   c                    s   t ƒ j|i |¤Ž || _d S r   )ro   r   rU   )r   rU   r;   r<   rp   r   r   r   K  rr   zSimpleBlockNode.__init__c                    s6   t ƒ  |¡\}}| | jrdnd| j |¡¡ ||fS )Nr   r   )ro   rm   ÚinsertrB   rU   rv   rj   rp   r   r   rm   O  s
   ÿz&SimpleBlockNode.get_resolved_arguments)r   r   r   r   rm   rx   r   r   rp   r   rS   J  s    rS   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )rY   c                    rn   r   )ro   r   rZ   )r   r   rB   r;   r<   rZ   rp   r   r   r   \  rr   zInclusionNode.__init__c                 C   sÖ   |   |¡\}}| j|i |¤Ž}|j | ¡}|du rTt| jtƒr#| j}n,tt| jddƒtƒr2| jj}nt| jt	ƒsGt| jt
ƒrG|jj | j¡}n|jj | j¡}||j| < | |¡}| d¡}|durf||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)rm   r   Úrender_contextÚgetÚ
isinstancerZ   r
   Úgetattrrz   Ústrr   ÚengineÚselect_templateÚget_templateÚnewrv   )r   rd   rk   rl   Ú_dictÚtÚnew_contextr{   r   r   r   rv   `  s&   
ÿ



zInclusionNode.render)r   r   r   r   rv   rx   r   r   rp   r   rY   [  s    rY   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   rd   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   )ra   Úkwarg©r@   r   r   re   œ  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   )ra   Úpr   r   r   Ú	<genexpr>Ó  s   € zparse_bits.<locals>.<genexpr>)r   Úlistr   Úpopitemr€   ÚremoveÚappendÚcompile_filterÚpopÚ
IndexErrorr4   Újoin)r7   r9   rA   rC   rD   r=   r?   r@   rB   r   r;   r<   Úunhandled_paramsÚunhandled_kwargsÚbitrˆ   Úparamr.   r   r‰   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_libraryØ  s"   ÿÿ€ÿÿÿr�   N)Úcollections.abcr   Ú	functoolsr   Ú	importlibr   Úinspectr   r   Údjango.utils.htmlr   Úbaser	   r
   r   Ú
exceptionsr   Ú	Exceptionr   r   r^   r6   rS   rY   r5   r�   r   r   r   r   Ú<module>   s"      %X