U
    ‰d4  ã                   @   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-7vj2equ7/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_localtimeZ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              
      sZ   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   Ú	functoolsÚwrapsr   ©r   Ú_r>   ©r   r   r;   ©r6   r   r7   r8   r9   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                    s\   t tˆƒƒ\‰‰‰‰ ‰‰}ˆ	p"ˆj‰t ˆ¡‡ ‡‡‡‡‡‡‡‡‡f
dd„ƒ}ˆ
 ˆ|¡ ˆS )Nc                    s>   |  ¡ dd … }t| |ˆˆˆ	ˆ ˆˆˆˆƒ
\}}tˆˆ||ˆƒS )Nr   )r,   r.   ÚInclusionNode)r0   r1   r2   r4   r5   )
r6   Úfilenamer   r7   r8   r9   r:   r;   r<   r=   r   r   r>   ²   s(    öûz8Library.inclusion_tag.<locals>.dec.<locals>.compile_funcr?   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;   r4   r5   )r   r   r;   r4   r5   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>)r4   r;   r5   Ú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 )r/   r   c                    s   t ƒ  ||||¡ || _d S r   )Úsuperr   r3   )r   r   r;   r4   r5   r3   ©Ú	__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   r3   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   r/   ä   s   r/   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;   r4   r5   rH   r\   r   r   r   ÷   s    zInclusionNode.__init__c                 C   sÐ   |   |¡\}}| j||Ž}|j | ¡}|dkr¢t| jtƒrB| j}nVtt| jddƒtƒr`| jj}n8t| jt	ƒsˆt
| 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ržt d|	|f ƒ‚nL||kr¸t d	|	|f ƒ‚n2||t|ƒ< ||krØ| |¡ n||krê| |¡ qT|rþt d
|	 ƒ‚qT|
 |  |¡¡ z| d¡ W qT t	k
rH   |dkrDt d|	 ƒ‚Y qTX qT|dk	rh|dt
|ƒ … }|st|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   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©r9   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)r0   r2   r:   r<   r=   r6   r8   r9   r;   r   r4   r5   Zunhandled_paramsZunhandled_kwargsÚbitrk   Úparamr'   r   rl   r   r.     sp    ÿÿ
ÿ
ÿÿÿÿÿ
ÿ
ÿÿr.   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}   )r@   Ú	importlibr   Úinspectr   r   Zdjango.utils.htmlr   Zdjango.utils.itercompatr   Úbaser   r	   r
   Ú
exceptionsr   Ú	Exceptionr   r   rL   r/   rG   r.   r}   r   r   r   r   Ú<module>   s    @#X