U
    7h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   Q/var/www/formularioweb/env/lib/python3.8/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 kr|d krj S  d k	rL|d krLt r8  S  fdd}|S n. d k	rj|d k	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 |j< |S r   )r   r   )r   r   r   r   r   r   4   s    zLibrary.tag_functionc                    s   dkr"|dkr" fdd}|S dk	r^|dkr^t rHjf S  fdd}|S njdk	r|dk	r|j< dD ]0}| kr| | }t||| tt||| 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 S r   )filter_functionr   )flagsr   r   r   r   B   s    zLibrary.filter.<locals>.decc                    s   j | f S r   )filterr   r#   r   r   r   r   r   L   s    )Zexpects_localtimeis_safeZneeds_autoescapez1Unsupported arguments to Library.filter: (%r, %r))r    r"   r   setattrr   Z_filter_namer!   )r   r   Zfilter_funcr#   r   attrvaluer   r%   r   r$   8   s,    
zLibrary.filterc                 K   s   | j |j|f|S r   )r$   r   )r   r   r#   r   r   r   r"   c   s    zLibrary.filter_functionc                    s8    fdd}|dk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              
      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   _r@   r   r   r=   r8   r   r9   r:   r;   r<   r>   r?   r   r   o   s    	

zLibrary.simple_tag.<locals>.decNz(Invalid arguments provided to simple_tag)r    r!   )r   r   r=   r   r   r   rD   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   )r.   r0   InclusionNode)r2   r3   r4   r6   r7   )
r8   filenamer   r9   r:   r;   r<   r=   r>   r?   r   r   r@      s(    z8Library.inclusion_tag.<locals>.dec.<locals>.compile_funcrA   rB   rH   r   r   r=   rE   r   r      s    	

 z"Library.inclusion_tag.<locals>.decr   )r   rH   r   r=   r   r   r   rI   r   inclusion_tag   s    
&zLibrary.inclusion_tag)NN)NN)NNN)NNN)r   r   r   __doc__r   r   r   r$   r"   rF   rJ   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   r=   r6   r7   )r   r   r=   r6   r7   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   rM   )rO   kvrQ   r   r   
<dictcomp>   s      z8TagHelperNode.get_resolved_arguments.<locals>.<dictcomp>)r6   r=   r7   items)r   rR   resolved_argsresolved_kwargsr   rQ   r   get_resolved_arguments   s
    
z$TagHelperNode.get_resolved_argumentsN)r   r   r   rK   r   rZ   r   r   r   r   rL      s   rL   c                       s(   e Zd ZdZ fddZdd Z  ZS )r1   r   c                    s   t  |||| || _d S r   )superr   r5   )r   r   r=   r6   r7   r5   	__class__r   r   r      s    zSimpleNode.__init__c                 C   sD   |  |\}}| j||}| jd k	r2||| j< dS |jr@t|}|S )N )rZ   r   r5   Z
autoescaper   )r   rR   rX   rY   outputr   r   r   render   s    

zSimpleNode.render)r   r   r   Zchild_nodelistsr   r`   __classcell__r   r   r\   r   r1      s   r1   c                       s$   e Zd Z fddZdd Z  ZS )rG   c                    s   t  |||| || _d S r   )r[   r   rH   )r   r   r=   r6   r7   rH   r\   r   r   r      s    zInclusionNode.__init__c                 C   s   |  |\}}| j||}|j| }|dkrt| jtrB| j}nVtt| jddtr`| jj}n8t| jt	st
| jr|jj| j}n|jj| j}||j| < ||}|d}|dk	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)rZ   r   Zrender_contextget
isinstancerH   r
   getattrrb   strr   ZengineZselect_templateZget_templatenewr`   )r   rR   rX   rY   _dicttZnew_contextrc   r   r   r   r`      s"    



zInclusionNode.render)r   r   r   r   r`   ra   r   r   r\   r   rG      s   rG   c
              	      s  |r.|r"|d dkr"|dd }nt d|	 g }
i }t|} fdd|D }|D ]}t|g| }|r| \}}||kr||kr|dkrt d|	|f nL||krt d	|	|f n2||t|< ||kr|| n||kr|| qT|rt d
|	 qT|
| | z|d W qT t	k
rH   |dkrDt d|	 Y qTX qT|dk	rh|dt
|  }|st|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   rR   r   NzW'%s' is decorated with takes_context=True so it must have a first argument of 'context'c                    s   g | ]} r| kr|qS r   r   )rO   kwargr;   r   r   rS   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   )rO   pr   r   r   	<genexpr>l  s     zparse_bits.<locals>.<genexpr>)r   listr   popitemrg   removeappendZcompile_filterpop
IndexErrorr/   join)r2   r4   r<   r>   r?   r8   r:   r;   r=   r   r6   r7   Zunhandled_paramsZunhandled_kwargsbitrk   paramr)   r   rl   r   r0     sp    



r0   c              
   C   sn   zt | }W n2 tk
r> } ztd| |f W 5 d}~X Y nX z|jW S  tk
rh   td|  Y nX 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_libraryq  s    r}   N)	functoolsr   	importlibr   inspectr   r   Zdjango.utils.htmlr   Zdjango.utils.itercompatr   baser	   r
   r   
exceptionsr   	Exceptionr   r   rL   r1   rG   r0   r}   r   r   r   r   <module>   s    @#X