B
    CVvg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-install-o3oxmrkh/Django/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 )N)Ú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   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 nvˆdk	rÄ|dk	rÄ|ˆjˆ< x@dD ]8}|ˆ kr~ˆ | }t|||ƒ t|dƒr~t|j||ƒ q~W ˆ|_|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 )N)Úfilter_function)r   )Úflagsr   r   r   r   @   s    zLibrary.filter.<locals>.decc                s   ˆj ˆ| fˆ ŽS )N)Úfilter)r   )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   r   r"   6   s*    


zLibrary.filterc             K   s   t |d|ƒj}| j||f|ŽS )Nr   )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   Ú_r=   )r   r   r:   )r5   r   r6   r7   r8   r9   r;   r<   r   r   l   s
    &zLibrary.simple_tag.<locals>.decNz(Invalid arguments provided to simple_tag)r   r   )r   r   r:   r   r   r   )r   r   r:   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   )r+   r-   ÚInclusionNode)r/   r0   r1   r3   r4   )
r5   Úfilenamer   r6   r7   r8   r9   r:   r;   r<   r   r   r=   •   s    z8Library.inclusion_tag.<locals>.dec.<locals>.compile_func)r   r   r   r   r>   r?   r   )r   r@   r=   )rC   r   r   r:   )r5   r   r6   r7   r8   r9   r;   r<   r   r   ‘   s
    (
z"Library.inclusion_tag.<locals>.decr   )r   rC   r   r:   r   r   r   )rC   r   r   r:   r   Úinclusion_tagˆ   s    	zLibrary.inclusion_tag)NN)NN)NNN)NNN)r   r   r   Ú__doc__r   r   r   r"   r    rA   rD   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 )N)r   r:   r3   r4   )r   r   r:   r3   r4   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   )rG   )rH   ÚkÚv)rJ   r   r   ú
<dictcomp>´   s    z8TagHelperNode.get_resolved_arguments.<locals>.<dictcomp>)r3   r:   r4   Úitems)r   rJ   Úresolved_argsÚresolved_kwargsr   )rJ   r   Úget_resolved_arguments°   s
    
z$TagHelperNode.get_resolved_argumentsN)r   r   r   rE   r   rR   r   r   r   r   rF   ¤   s   rF   c                   s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )r.   c                s   t ƒ  ||||¡ || _d S )N)Úsuperr   r2   )r   r   r:   r3   r4   r2   )Ú	__class__r   r   r   º   s    zSimpleNode.__init__c             C   sD   |   |¡\}}| j||Ž}| jd k	r2||| j< dS |jr@t|ƒ}|S )NÚ )rR   r   r2   Z
autoescaper   )r   rJ   rP   rQ   Úoutputr   r   r   Úrender¾   s    

zSimpleNode.render)r   r   r   r   rW   Ú__classcell__r   r   )rT   r   r.   ¸   s   r.   c                   s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )rB   c                s   t ƒ  ||||¡ || _d S )N)rS   r   rC   )r   r   r:   r3   r4   rC   )rT   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)rR   r   Zrender_contextÚgetÚ
isinstancerC   r	   r   rY   Ústrr   ZengineZselect_templateZget_templateÚnewrW   )r   rJ   rP   rQ   Ú_dictÚtZnew_contextrZ   r   r   r   rW   Ï   s"    



zInclusionNode.render)r   r   r   r   rW   rX   r   r   )rT   r   rB   É   s   rB   c
          	      s   |r*|d dkr|dd… }nt d|	 ƒ‚g }
i }t|ƒ}‡ fdd„|D ƒ}xþ|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è| |¡ qR|rüt d
|	 ƒ‚qR|
 |  |¡¡ y| d¡ W qR t	k
rF   |dkrBt d|	 ƒ‚Y qRX qRW |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   rJ   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   )rH   Úkwarg)r8   r   r   rK   ÿ   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   )rH   Úpr   r   r   ú	<genexpr>4  s    zparse_bits.<locals>.<genexpr>)r   Úlistr
   Úpopitemr]   ÚremoveÚappendZcompile_filterÚpopÚ
IndexErrorr,   Újoin)r/   r1   r9   r;   r<   r5   r7   r8   r:   r   r3   r4   Zunhandled_paramsZunhandled_kwargsÚbitra   Úparamr&   r   )r8   r   r-   í   s\    



 r-   c          
   C   sl   yt | ƒ}W n2 tk
r> } ztd| |f ƒ‚W dd}~X Y nX y|jS  tk
rf   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    rr   )r>   Ú	importlibr   Úinspectr   r   Zdjango.utils.htmlr   Zdjango.utils.itercompatr   Úbaser   r	   r
   Ú
exceptionsr   Ú	Exceptionr   r   rF   r.   rB   r-   rr   r   r   r   r   Ú<module>   s    $K