
v^$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 Gd	 d
   d
 e  Z Gd d   d  Z Gd d   d e  Z Gd d   d e  Z Gd 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   9/tmp/pip-build-8lau8j11/django/django/template/library.pyr      s   r   c               @   s   e  Z d  Z d Z d d   Z d d d d  Z d d   Z d d d	 d
  Z d d   Z d d d d d  Z	 d d 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 rl t    rP  j     S   f d d   } | Sn?   d  k	 r | d  k	 r |  j   <| St d   | f   d  S)Nc                s    j    |   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 r1 | d k r1    f d d   } | S d k	 r | d k r t    re  j     S    f d d   } | Sn  d k	 r| d k	 r|  j  <xV d
 D]N } |   k r   | } t | | |  t | d  r t | j | |  q W | _ | St d	  | f   d S)z
        Register a callable as a template filter. Example:

        @register.filter
        def lower(value):
            return value.lower()
        Nc                s    j  |     S)N)filter_function)r   )flagsr   r   r   r   @   s    zLibrary.filter.<locals>.decc                s    j   |     S)N)filter)r   )r!   r   r   r   r   r   I   s    expects_localtimeis_safeneeds_autoescaper   z1Unsupported arguments to Library.filter: (%r, %r))r#   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 | | |  S)Nr   )r   r   r"   )r   r   r!   r   r   r   r   r    `   s    zLibrary.filter_functionc                sN       f d d   } | d k r( | St  |  r> | |  St d   d S)z
        Register a callable as a compiled template tag. Example:

        @register.simple_tag
        def hello(*args, **kwargs):
            return 'world'
        c          
      s   t  t    \        }  p? t  d   j  t j          
   f	 d d    } 	 j  |   S)Nr   c                s   | j    d d   } d  } t |  d k rX | d d k rX | d } | d  d  } t |  |          
 \ } } t   | | |  S)Nr      asr,   )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=   )r8   r   r9   r:   r;   r<   r>   r?   r   r   l   s
    '9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                s   t  t    \        } 	 p? t  d   j  t j              f
 d d    } 
 j  |   S)Nr   c                sY   | j    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_func)r   r   r   r   rA   rB   r   )r   rC   r@   )rF   r   r   r=   )r8   r   r9   r:   r;   r<   r>   r?   r   r      s
    '<
z"Library.inclusion_tag.<locals>.decr   )r   rF   r   r=   r   r   r   )rF   r   r   r=   r   inclusion_tag   s    	zLibrary.inclusion_tag)r   r   r   __doc__r   r   r   r"   r    rD   rG   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=   r6   r7   )r   r   r=   r6   r7   r   r   r   r      s    			zTagHelperNode.__init__c                s^     f d d   |  j  D } |  j r2   g | }   f d d   |  j j   D } | | f S)Nc                s   g  |  ] } | j      q Sr   )resolve).0var)contextr   r   
<listcomp>   s   	 z8TagHelperNode.get_resolved_arguments.<locals>.<listcomp>c                s(   i  |  ] \ } } | j     |  q Sr   )rJ   )rK   kv)rM   r   r   
<dictcomp>   s   	 z8TagHelperNode.get_resolved_arguments.<locals>.<dictcomp>)r6   r=   r7   items)r   rM   resolved_argsresolved_kwargsr   )rM   r   get_resolved_arguments   s
    	"z$TagHelperNode.get_resolved_argumentsN)r   r   r   rH   r   rU   r   r   r   r   rI      s   rI   c                   s.   e  Z d  Z   f d d   Z d d   Z   S)r1   c                s&   t    j | | | |  | |  _ d  S)N)superr   r5   )r   r   r=   r6   r7   r5   )	__class__r   r   r      s    zSimpleNode.__init__c             C   s`   |  j  |  \ } } |  j | |   } |  j d  k	 rG | | |  j <d S| j r\ t |  } | S)N )rU   r   r5   Z
autoescaper   )r   rM   rS   rT   outputr   r   r   render   s    	zSimpleNode.render)r   r   r   r   rZ   r   r   )rW   r   r1      s   r1   c                   s.   e  Z d  Z   f d d   Z d d   Z   S)rE   c                s&   t    j | | | |  | |  _ d  S)N)rV   r   rF   )r   r   r=   r6   r7   rF   )rW   r   r   r      s    zInclusionNode.__init__c             C   s3  |  j  |  \ } } |  j | |   } | j j |   } | d k r t |  j t  rc |  j } n t t |  j d d  t  r |  j j } nU t |  j t	  r t
 |  j  r | j j j |  j  } n | j j j |  j  } | | j |  <| j |  } | j d  } | d k	 r&| | d <| j |  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)rU   r   Zrender_contextget
isinstancerF   r	   r   r[   strr   ZengineZselect_templateZget_templatenewrZ   )r   rM   rS   rT   _dicttZnew_contextr\   r   r   r   rZ      s"    "
zInclusionNode.render)r   r   r   r   rZ   r   r   )rW   r   rE      s   rE   c
                s)  | r9 | d d k r) | d d  } n t  d |	   g  }
 i  } t |  }   f d d   | D } xS| D]K} t | g |   } | rK| j   \ } } | | k r | | k r | d k r t  d |	 | f   q| | k rt  d	 |	 | f   q| | t |  <| | k r/| j |  q| | k r| j |  qq | rdt  d
 |	   qq |
 j |  j |   y | j d  Wqq t	 k
 r| d k rt  d |	   Yqq Xqq W| d k	 r| d t
 |   } | s| rt  d |	 d j 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   rM   r   NzW'%s' is decorated with takes_context=True so it must have a first argument of 'context'c                s)   g  |  ] }   s |   k r |  q Sr   r   )rK   kwarg)r;   r   r   rN      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   )rK   pr   r   r   	<genexpr>4  s    zparse_bits.<locals>.<genexpr>)r   listr
   popitemr_   removeappendZcompile_filterpop
IndexErrorr/   join)r2   r4   r<   r>   r?   r8   r:   r;   r=   r   r6   r7   Zunhandled_paramsZunhandled_kwargsbitrc   paramr)   r   )r;   r   r0      s\    
$*r0   c             C   s   y t  |   } Wn; t k
 rM } z t d |  | f   WYd d } ~ Xn Xy | j SWn" t k
 r} t d |    Yn Xd 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    #rt   )rA   	importlibr   inspectr   r   Zdjango.utils.htmlr   Zdjango.utils.itercompatr   baser   r	   r
   
exceptionsr   	Exceptionr   r   rI   r1   rE   r0   rt   r   r   r   r   <module>   s   $K