o
    بf2                     @   s   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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 )    N)import_module)getfullargspec)conditional_escape)is_iterable   )NodeTemplatetoken_kwargs)TemplateSyntaxErrorc                   @   s   e Zd ZdS )InvalidTemplateLibraryN)__name__
__module____qualname__ r   r   O/var/www/dscantool/venv/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   Zcompile_functionr   r   r   r   r      s   

zLibrary.tagc                 C   s   || j t|d|j< |S N_decorated_function)r   getattrr   )r   r   r   r   r   r   2   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ra|dura|j< dD ]}| v r[ | }t||| t|dr[t|j|| 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   @   s   zLibrary.filter.<locals>.decc                    s   j | fi  S r   )filterr   r$   r   r   r   r   r   I   s   )Zexpects_localtimeis_safeZneeds_autoescaper!   z1Unsupported arguments to Library.filter: (%r, %r))r   r#   r   setattrhasattrr!   Z_filter_namer   )r   r   Zfilter_funcr$   r   attrvaluer   r&   r   r%   6   s0   

zLibrary.filterc                 K   s"   t |d|j}| j||fi |S r    )r"   r   r%   )r   r   r$   r   r   r   r   r#   `   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              
      s^   t \ }ptdjt 
f	dd}	| S )Nr!   c                    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_funcp   s   z5Library.simple_tag.<locals>.dec.<locals>.compile_funcr   r"   r   	functoolswrapsr   r   _rB   r   r   r?   r:   r   r;   r<   r=   r>   r@   rA   r   r   l   s   zLibrary.simple_tag.<locals>.decNz(Invalid arguments provided to simple_tag)r   r   )r   r   r?   r   r   r   rH   r   
simple_tagd   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                    s`   t \ }	ptdjt f
dd}
| S )Nr!   c                    s>   |  dd  }t| |	 
\}}t||S )Nr   )r0   r2   InclusionNode)r4   r5   r6   r8   r9   )
r:   filenamer   r;   r<   r=   r>   r?   r@   rA   r   r   rB      s   
z8Library.inclusion_tag.<locals>.dec.<locals>.compile_funcrC   rF   rL   r   r   r?   rI   r   r      s    	z"Library.inclusion_tag.<locals>.decr   )r   rL   r   r?   r   r   r   rM   r   inclusion_tag   s   	zLibrary.inclusion_tag)NN)NNN)r   r   r   __doc__r   r   r   r%   r#   rJ   rN   r   r   r   r   r      s    

*
$r   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   r?   r8   r9   )r   r   r?   r8   r9   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   rQ   )rS   kvrU   r   r   
<dictcomp>   s    z8TagHelperNode.get_resolved_arguments.<locals>.<dictcomp>)r8   r?   r9   items)r   rV   resolved_argsresolved_kwargsr   rU   r   get_resolved_arguments   s
   
z$TagHelperNode.get_resolved_argumentsN)r   r   r   rO   r   r^   r   r   r   r   rP      s    rP   c                       $   e Zd Z fddZdd Z  ZS )r3   c                       t  |||| || _d S r   )superr   r7   )r   r   r?   r8   r9   r7   	__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   r7   Z
autoescaper   )r   rV   r\   r]   outputr   r   r   render   s   

zSimpleNode.renderr   r   r   r   rg   __classcell__r   r   rb   r   r3          r3   c                       r_   )rK   c                    r`   r   )ra   r   rL   )r   r   r?   r8   r9   rL   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   Zrender_contextget
isinstancerL   r   r"   rk   strr   ZengineZselect_templateZget_templatenewrg   )r   rV   r\   r]   _dicttZnew_contextrl   r   r   r   rg      s"   




zInclusionNode.renderrh   r   r   rb   r   rK      rj   rK   c
              	      s  |r|d dkr|dd }nt d|	 g }
i }t|} fdd|D }|D ]t}t|g| }|rr| \}}||vrL||vrL|du rLt d|	|f ||v rXt d	|	|f ||t|< ||v rh|| q(||v rq|| q(|rzt d
|	 |
| | z|d W q( t	y   |du rt d|	 Y q(w |dur|dt
|  }|s|rt 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   rV   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   )rS   kwargr=   r   r   rW      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   )rS   pr   r   r   	<genexpr>4  s    zparse_bits.<locals>.<genexpr>)r
   listr	   popitemro   removeappendZcompile_filterpop
IndexErrorr1   join)r4   r6   r>   r@   rA   r:   r<   r=   r?   r   r8   r9   Zunhandled_paramsZunhandled_kwargsbitrs   paramr+   r   rt   r   r2      s~   

r2   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_library8  s"   r   )rD   	importlibr   inspectr   Zdjango.utils.htmlr   Zdjango.utils.itercompatr   baser   r   r	   
exceptionsr
   	Exceptionr   r   rP   r3   rK   r2   r   r   r   r   r   <module>   s     $K