a
    xd2                     @   s   d dl 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 )    N)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   O/var/www/html/Ranjet/env/lib/python3.9/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                    s~    d u r|d u rj S  d urL|d u rLt r8  S  fdd}|S n. d urj|d urj|j < |S td |f d S )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rb|du rbt rLjfi  S  fdd}|S nrdur|dur|j< dD ]8}| v r | }t||| t|drt|j|| q|_|S td|f dS )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   s.    

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                    s8    fdd}|du r|S t |r,||S tddS )z
        Register a callable as a compiled template tag. Example:

        @register.simple_tag
        def hello(*args, **kwargs):
            return 'world'
        c              
      sb   t 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#   r   	functoolswrapsr   r   _rC   r   r   r@   r;   r   r<   r=   r>   r?   rA   rB   r   r   l   s    zLibrary.simple_tag.<locals>.decNz(Invalid arguments provided to simple_tag)r   r    )r   r   r@   r   r   r   rI   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                    sd   t t\ }	p*tdjt f
dd}
| S )Nr"   c                    s>   |  dd  }t| |	 
\}}t||S )Nr   )r1   r3   InclusionNode)r5   r6   r7   r9   r:   )
r;   filenamer   r<   r=   r>   r?   r@   rA   rB   r   r   rC      s    
z8Library.inclusion_tag.<locals>.dec.<locals>.compile_funcrD   rG   rM   r   r   r@   rJ   r   r      s     	z"Library.inclusion_tag.<locals>.decr   )r   rM   r   r@   r   r   r   rN   r   inclusion_tag   s    	zLibrary.inclusion_tag)NN)NN)NNN)NNN)r   r   r   __doc__r   r   r   r&   r$   rK   rO   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@   r9   r:   )r   r   r@   r9   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>       z8TagHelperNode.get_resolved_arguments.<locals>.<listcomp>c                    s   i | ]\}}||  qS r   rR   )rT   kvrV   r   r   
<dictcomp>   rY   z8TagHelperNode.get_resolved_arguments.<locals>.<dictcomp>)r9   r@   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 fddZdd Z  ZS )r4   c                    s   t  |||| || _d S r   )superr   r8   )r   r   r@   r9   r:   r8   	__class__r   r   r      s    zSimpleNode.__init__c                 C   sH   |  |\}}| j|i |}| jd ur6||| j< dS |jrDt|}|S )N )r`   r   r8   Z
autoescaper   )r   rW   r^   r_   outputr   r   r   render   s    

zSimpleNode.renderr   r   r   r   rf   __classcell__r   r   rb   r   r4      s   r4   c                       s$   e Zd Z fddZdd Z  ZS )rL   c                    s   t  |||| || _d S r   )ra   r   rM   )r   r   r@   r9   r:   rM   rb   r   r   r      s    zInclusionNode.__init__c                 C   s   |  |\}}| j|i |}|j| }|du rt| jtrF| j}nVtt| jddtrd| jj}n8t| jt	st
| jr|jj| j}n|jj| j}||j| < ||}|d}|dur||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
isinstancerM   r	   r#   ri   strr   ZengineZselect_templateZget_templatenewrf   )r   rW   r^   r_   _dicttZnew_contextrj   r   r   r   rf      s"    



zInclusionNode.renderrg   r   r   rb   r   rL      s   rL   c
              	      s  |r*|d dkr|dd }nt d|	 g }
i }t|} fdd|D }|D ]}t|g| }|r| \}}||vr||vr|du rt d|	|f nL||v rt d	|	|f n2||t|< ||v r|| n||v r|| qP|rt d
|	 qP|
| | z|d W qP t	yB   |du r>t d|	 Y qP0 qP|durb|dt
|  }|sn|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   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      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>4  rY   zparse_bits.<locals>.<genexpr>)r   listr
   popitemrm   removeappendZcompile_filterpop
IndexErrorr2   join)r5   r7   r?   rA   rB   r;   r=   r>   r@   r   r9   r:   Zunhandled_paramsZunhandled_kwargsbitrq   paramr,   r   rr   r   r3      sx    


r3   c              
   C   sn   zt | }W n4 ty@ } ztd| |f W Y d}~n
d}~0 0 z|jW S  tyh   td|  Y n0 dS )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   )rE   	importlibr   inspectr   r   Zdjango.utils.htmlr   Zdjango.utils.itercompatr   baser   r	   r
   
exceptionsr   	Exceptionr   r   rQ   r4   rL   r3   r   r   r   r   r   <module>   s    $K