U
    Ha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   ;/tmp/pip-unpacked-wheel-3jxiddxt/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 t|d|j< |S N_decorated_function)r   getattrr   )r   r   r   r   r   r   2   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 nrdk	r|dk	r|j< dD ]8}| kr| | }t||| t|dr|t|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 S r   )filter_functionr   )flagsr   r   r   r   @   s    zLibrary.filter.<locals>.decc                    s   j | f S r   )filterr   r%   r   r   r   r   r   I   s    )Zexpects_localtimeZ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|S r!   )r#   r   r&   )r   r   r%   r   r   r   r   r$   `   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              
      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   _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                    sd   t 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)NN)NNN)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                       s$   e Zd Z fddZdd Z  ZS )r3   c                    s   t  |||| || _d S r   )superr   r7   )r   r   r?   r8   r9   r7   	__class__r   r   r      s    zSimpleNode.__init__c                 C   sD   |  |\}}| j||}| jd k	r2||| 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   rd   __classcell__r   r   r`   r   r3      s   r3   c                       s$   e Zd Z fddZdd Z  ZS )rK   c                    s   t  |||| || _d S r   )r_   r   rL   )r   r   r?   r8   r9   rL   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)r^   r   Zrender_contextget
isinstancerL   r	   r#   rg   strr   ZengineZselect_templateZget_templatenewrd   )r   rV   r\   r]   _dicttZnew_contextrh   r   r   r   rd      s"    



zInclusionNode.renderre   r   r   r`   r   rK      s   rK   c
              	      s  |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|| qP|rt d
|	 qP|
| | z|d W qP t	k
rD   |dkr@t d|	 Y qPX qP|dk	rd|dt
|  }|sp|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| k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
   popitemrk   removeappendZcompile_filterpop
IndexErrorr1   join)r4   r6   r>   r@   rA   r:   r<   r=   r?   r   r8   r9   Zunhandled_paramsZunhandled_kwargsbitro   paramr+   r   rp   r   r2      sx    


r2   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_library8  s    r   )rD   	importlibr   inspectr   r   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