
bUX                 @   ss  d  Z  d d l Z d d l m Z d d l m Z m Z d d l m Z m	 Z	 m
 Z
 m Z m Z m Z m Z d d l m Z m Z m Z d d l m Z m Z m Z m Z m Z m Z m Z d d	 d
 d d d d d d d d d d d g Z e Z d d   Z e   Z d d   Z  d d   Z! d d d d d d  Z" Gd d	   d	 e  Z# Gd d   d e  Z$ y! d d l% m& Z& e& j' e$  Wn e( k
 rYn XGd  d!   d! e  Z) Gd" d   d e  Z* e Gd# d$   d$ e   Z+ Gd% d
   d
 e  Z, e Gd& d'   d' e   Z- d d d( d  Z. e Gd) d*   d* e-   Z/ e Gd+ d,   d, e-   Z0 e- `1 e/ `1 e0 `1 d S)-z}
    jinja2.runtime
    ~~~~~~~~~~~~~~

    Runtime helpers.

    :copyright: (c) 2010 by the Jinja Team.
    :license: BSD.
    N)chain)EvalContext_context_function_types)Markupsoft_unicodeescapemissingconcatinternalcodeobject_type_repr)UndefinedErrorTemplateRuntimeErrorTemplateNotFound)imap	text_type	iteritemsimplements_iteratorimplements_to_stringstring_typesPY2LoopContextTemplateReferenceMacror   r   r   r	   r   markup_joinunicode_join	to_stringidentityr   make_logging_undefinedc             C   s   |  S)N )xr   r   0/usr/lib/python3/dist-packages/jinja2/runtime.py<lambda>"   s    r!   c             C   sh   g  } t  t |   } xF | D]> } | j |  t | d  r t d  j t | |   Sq Wt |  S)z@Concatenation that escapes if necessary and converts to unicode.Z__html__ )r   r   appendhasattrr   joinr   r	   )seqZbufiteratorargr   r   r    r   '   s     c             C   s   t  t t |    S)z4Simple args to unicode conversion and concatenation.)r	   r   r   )r&   r   r   r    r   2   s    c       
      C   s   | d k r i  } | r! | } n t  | p- f  |  } | r | rN t  |  } xP t |  D]B \ } }	 | d d  d k r[ |	 t k	 r[ |	 | | d d  <q[ W|  j |  | | |  S)z(Internal helper to for context creation.N   Zl_)dictr   r   Zcontext_class)
environmentZtemplate_nameblocksvarsZsharedglobalslocalsparentkeyvaluer   r   r    new_context7   s    	"r3   c               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	r   zThe `self` in templates.c             C   s   | |  _  d  S)N)_TemplateReference__context)selfcontextr   r   r    __init__O   s    zTemplateReference.__init__c             C   s&   |  j  j | } t | |  j  | d  S)Nr   )r4   r,   BlockReference)r5   namer,   r   r   r    __getitem__R   s    zTemplateReference.__getitem__c             C   s   d |  j  j |  j j f S)Nz<%s %r>)	__class____name__r4   r9   )r5   r   r   r    __repr__V   s    	zTemplateReference.__repr__N)r<   
__module____qualname____doc__r7   r:   r=   r   r   r   r    r   L   s   c               @   s	  e  Z d  Z d Z d) Z d
 d   Z d d   Z d d d  Z d d   Z d d   Z	 d d   Z
 e d d    Z d d d  Z d d   Z e d  Z e d  Z e d  Z e r e d   Z e d!  Z e d"  Z [ d# d$   Z d% d&   Z d' d(   Z d S)*Contexta  The template context holds the variables of a template.  It stores the
    values passed to the template and also the names the template exports.
    Creating instances is neither supported nor useful as it's created
    automatically at various stages of the template evaluation and should not
    be created by hand.

    The context is immutable.  Modifications on :attr:`parent` **must not**
    happen and modifications on :attr:`vars` are allowed from generated
    template code only.  Template filters and global functions marked as
    :func:`contextfunction`\s get the active context passed as first argument
    and are allowed to access the context read-only.

    The template context supports read only dict operations (`get`,
    `keys`, `values`, `items`, `iterkeys`, `itervalues`, `iteritems`,
    `__getitem__`, `__contains__`).  Additionally there is a :meth:`resolve`
    method that doesn't fail with a `KeyError` but returns an
    :class:`Undefined` object for missing variables.
    r0   r-   r+   eval_ctxexported_varsr9   r,   __weakref__c             C   sk   | |  _  i  |  _ | |  _ t |  j |  |  _ t   |  _ | |  _ t d d   t	 |  D  |  _
 d  S)Nc             s   s$   |  ] \ } } | | g f Vq d  S)Nr   ).0kvr   r   r    	<genexpr>~   s    z#Context.__init__.<locals>.<genexpr>)r0   r-   r+   r   rB   setrC   r9   r*   r   r,   )r5   r+   r0   r9   r,   r   r   r    r7   s   s    				zContext.__init__c             C   sn   y, |  j  | } | j |  d } | | Wn, t k
 rZ |  j j d | d d SYn Xt | |  | |  S)zRender a parent block.   z#there is no parent block called %r.r9   super)r,   indexLookupErrorr+   	undefinedr8   )r5   r9   Zcurrentr,   rL   r   r   r    rK      s    zContext.superNc             C   s)   y |  | SWn t  k
 r$ | SYn Xd S)zfReturns an item from the template context, if it doesn't exist
        `default` is returned.
        N)KeyError)r5   r1   defaultr   r   r    get   s    zContext.getc             C   sG   | |  j  k r |  j  | S| |  j k r4 |  j | S|  j j d |  S)zLooks up a variable like `__getitem__` or `get` but returns an
        :class:`Undefined` object with the name of the name looked up.
        r9   )r-   r0   r+   rN   )r5   r1   r   r   r    resolve   s
    zContext.resolvec                s    t    f d d     j D  S)z+Get a new dict with the exported variables.c             3   s"   |  ] } |   j  | f Vq d  S)N)r-   )rE   rF   )r5   r   r    rH      s    z'Context.get_exported.<locals>.<genexpr>)r*   rC   )r5   r   )r5   r    get_exported   s    zContext.get_exportedc             C   s   t  |  j |  j  S)z`Return a copy of the complete context as dict including the
        exported variables.
        )r*   r0   r-   )r5   r   r   r    get_all   s    zContext.get_allc             O   s   d } | j  } x$ d D] } t | |  r | } Pq Wt | t  r t | d d  rg |  f | } nG t | d d  r |  j f | } n" t | d d  r |  j f | } y | | |   SWn" t k
 r |  j j d  SYn Xd S)	zCall the callable with the arguments and keyword arguments
        provided but inject the active context or environment as first
        argument if the callable is a :func:`contextfunction` or
        :func:`environmentfunction`.
        Tcontextfunctionevalcontextfunctionenvironmentfunctionr   zGvalue was undefined because a callable raised a StopIteration exceptionN)rU   rV   rW   )	__call__r$   
isinstancer   getattrrB   r+   StopIterationrN   )Z_Context__selfZ_Context__objargskwargsZ__traceback_hide__fnZfn_typer   r   r    call   s&    	 
zContext.callc             C   sp   t  |  j |  j i  |  j d d |  } | j j |  j  |  j | _ | j j d d   t |  j  D  | S)z5Internal helper function to create a derived context.TNc             s   s'   |  ] \ } } | t  |  f Vq d  S)N)list)rE   rF   rG   r   r   r    rH      s    z"Context.derived.<locals>.<genexpr>)	r3   r+   r9   r0   r-   updaterB   r,   r   )r5   r/   r6   r   r   r    derived   s    &zContext.derivedc                s4     f d d   } t  t    j | _   | _ | S)Nc                s   t  |  j        S)N)rZ   rT   )r5   )methr   r    r!      s    zContext._all.<locals>.<lambda>)rZ   r*   r@   r<   )rc   proxyr   )rc   r    _all   s    	zContext._allkeysvaluesitemsiterkeys
itervaluesr   c             C   s   | |  j  k p | |  j k S)N)r-   r0   )r5   r9   r   r   r    __contains__   s    zContext.__contains__c             C   s.   |  j  |  } t | t  r* t |   | S)zTLookup a variable or raise `KeyError` if the variable is
        undefined.
        )rR   rY   	UndefinedrO   )r5   r1   itemr   r   r    r:      s    zContext.__getitem__c             C   s&   d |  j  j t |  j    |  j f S)Nz<%s %s of %r>)r;   r<   reprrT   r9   )r5   r   r   r    r=      s    	zContext.__repr__)zparentzvarszenvironmentzeval_ctxzexported_varsznamezblocksz__weakref__)r<   r>   r?   r@   	__slots__r7   rK   rQ   rR   rS   rT   r
   r_   rb   re   rf   rg   rh   r   ri   rj   r   rk   r:   r=   r   r   r   r    rA   ]   s.    	
!		rA   )Mappingc               @   sF   e  Z d  Z d Z d d   Z e d d    Z e d d    Z d S)	r8   z"One block on a template reference.c             C   s(   | |  _  | |  _ | |  _ | |  _ d  S)N)r9   _context_stack_depth)r5   r9   r6   stackdepthr   r   r    r7     s    			zBlockReference.__init__c             C   s_   |  j  d t |  j  k r< |  j j j d |  j d d St |  j |  j |  j |  j  d  S)zSuper the block.rJ   z#there is no parent block called %r.r9   rK   )rs   lenrr   rq   r+   rN   r9   r8   )r5   r   r   r    rK   	  s    zBlockReference.superc             C   s>   t  |  j |  j |  j   } |  j j j r: t |  } | S)N)r	   rr   rs   rq   rB   Z
autoescaper   )r5   rvr   r   r    rX     s    zBlockReference.__call__N)	r<   r>   r?   r@   r7   propertyrK   r
   rX   r   r   r   r    r8      s   
r8   c               @   s   e  Z d  Z d Z d d d d  Z d d   Z e d d	    Z e d
 d	    Z e d d	    Z	 e d d	    Z
 e d d	    Z e d d	    Z d d   Z d d   Z d d   Z e d d    Z e Z [ e d d    Z d d   Z d S)r   z%A loop context for dynamic iteration.Nr   c             C   st   t  |  |  _ | |  _ |  j   |  _ d |  _ | |  _ y t |  |  _ Wn! t	 t
 f k
 ro d  |  _ Yn Xd  S)NrJ   )iter	_iterator_recurse
_safe_next_afterindex0depth0rv   _length	TypeErrorAttributeError)r5   iterableZrecurser   r   r   r    r7     s    			zLoopContext.__init__c             G   s'   | s t  d   | |  j t |  S)z7Cycles among the arguments with the current loop index.zno items for cycling given)r   r   rv   )r5   r\   r   r   r    cycle.  s    zLoopContext.cyclec             C   s   |  j  d k S)Nr   )r   )r   r   r   r    r!   4  s    zLoopContext.<lambda>c             C   s   |  j  t k S)N)r~   _last_iteration)r   r   r   r    r!   5  s    c             C   s   |  j  d S)NrJ   )r   )r   r   r   r    r!   6  s    c             C   s   |  j  |  j S)N)lengthr   )r   r   r   r    r!   7  s    c             C   s   |  j  |  j S)N)r   rL   )r   r   r   r    r!   8  s    c             C   s   |  j  d S)NrJ   )r   )r   r   r   r    r!   9  s    c             C   s   |  j  S)N)r   )r5   r   r   r    __len__;  s    zLoopContext.__len__c             C   s
   t  |   S)N)LoopContextIterator)r5   r   r   r    __iter__>  s    zLoopContext.__iter__c             C   s.   y t  |  j  SWn t k
 r) t SYn Xd  S)N)nextr{   r[   r   )r5   r   r   r    r}   A  s    zLoopContext._safe_nextc             C   s8   |  j  d  k r t d   |  j  | |  j  |  j d  S)NzMTried to call non recursive loop.  Maybe you forgot the 'recursive' modifier.rJ   )r|   r   r   )r5   r   r   r   r    loopG  s    zLoopContext.loopc             C   sT   |  j  d  k rM t |  j  } t |  |  _ |  j d } t |  | |  _  |  j  S)Nr)   )r   tupler{   rz   r   rv   )r5   r   Ziterations_doner   r   r    r   S  s    zLoopContext.lengthc             C   s   d |  j  j |  j |  j f S)Nz
<%s %r/%r>)r;   r<   rL   r   )r5   r   r   r    r=   `  s    	zLoopContext.__repr__)r<   r>   r?   r@   r7   r   rx   firstZlastrL   ZrevindexZ	revindex0ru   r   r   r}   r
   r   rX   r   r=   r   r   r   r    r     s"   	c               @   s@   e  Z d  Z d Z d
 Z d d   Z d d   Z d d   Z d	 S)r   z The iterator for a loop context.r6   c             C   s   | |  _  d  S)N)r6   )r5   r6   r   r   r    r7   m  s    zLoopContextIterator.__init__c             C   s   |  S)Nr   )r5   r   r   r    r   p  s    zLoopContextIterator.__iter__c             C   sR   |  j  } | j d 7_ | j t k r0 t    | j } | j   | _ | | f S)NrJ   )r6   r   r~   r   r[   r}   )r5   ZctxZ	next_elemr   r   r    __next__s  s    			zLoopContextIterator.__next__N)zcontext)r<   r>   r?   r@   ro   r7   r   r   r   r   r   r    r   h  s
   r   c               @   s@   e  Z d  Z d Z d d   Z e d d    Z d d   Z d S)	r   zWraps a macro function.c	       	      C   s[   | |  _  | |  _ t |  |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _	 d  S)N)
_environment_funcrv   _argument_countr9   	argumentsdefaultscatch_kwargscatch_varargscaller)	r5   r+   funcr9   r   r   r   r   r   r   r   r    r7     s    							zMacro.__init__c       	      O   s  t  | d  |  j   } t |  } | |  j k r x t |  j t |  d    D] \ } } y | j |  } Wn_ t k
 r y |  j | |  j | } Wn. t k
 r |  j	 j
 d | d | } Yn XYn X| j |  qT W|  j r2| j d d   } | d  k r%|  j	 j
 d d d } | j |  |  j rK| j |  n+ | rvt d |  j t t |   f   |  j r| j | |  j d    n7 t |  |  j k rt d |  j t |  j  f   |  j |   S)Nzparameter %r was not providedr9   r   zNo caller definedz%macro %r takes no keyword argument %rz+macro %r takes not more than %d argument(s))r`   r   rv   	enumerater   poprO   r   
IndexErrorr   rN   r#   r   r   r   r9   r   rz   r   r   )	r5   r\   r]   r   Zoffidxr9   r2   r   r   r   r    rX     s<    ,					zMacro.__call__c             C   s2   d |  j  j |  j d  k r! d p- t |  j  f S)Nz<%s %s>Z	anonymous)r;   r<   r9   rn   )r5   r   r   r    r=     s    	zMacro.__repr__N)r<   r>   r?   r@   r7   r
   rX   r=   r   r   r   r    r   }  s   *c               @   s$  e  Z d  Z d Z d Z d e d e d d  Z e d	 d
    Z	 e d d    Z
 e	 Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z  Z! Z" Z# d d   Z$ d d   Z% d d   Z& d d   Z' d d   Z( d d   Z) d d   Z* e* Z+ d d   Z, d S)rl   az  The default undefined type.  This undefined type can be printed and
    iterated over, but every other access will raise an :exc:`jinja2.exceptions.UndefinedError`:

    >>> foo = Undefined(name='foo')
    >>> str(foo)
    ''
    >>> not foo
    True
    >>> foo + 42
    Traceback (most recent call last):
      ...
    jinja2.exceptions.UndefinedError: 'foo' is undefined
    _undefined_hint_undefined_obj_undefined_name_undefined_exceptionNc             C   s(   | |  _  | |  _ | |  _ | |  _ d  S)N)r   r   r   r   )r5   hintobjr9   excr   r   r    r7     s    			zUndefined.__init__c             O   s   |  j  d k r~ |  j t k r. d |  j } q t |  j t  s_ d t |  j  |  j f } q d t |  j  |  j f } n	 |  j  } |  j |   d S)z{Regular callback function for undefined objects that raises an
        `jinja2.exceptions.UndefinedError` on call.
        Nz%r is undefinedz%s has no element %rz%r has no attribute %r)r   r   r   r   rY   r   r   r   )r5   r\   r]   r   r   r   r    _fail_with_undefined_error  s    	z$Undefined._fail_with_undefined_errorc             C   s,   | d  d  d k r" t  |   |  j   S)Nr)   __)r   r   )r5   r9   r   r   r    __getattr__  s    zUndefined.__getattr__c             C   s   t  |   t  |  k S)N)type)r5   otherr   r   r    __eq__  s    zUndefined.__eq__c             C   s   |  j  |  S)N)r   )r5   r   r   r   r    __ne__  s    zUndefined.__ne__c             C   s   t  t |    S)N)idr   )r5   r   r   r    __hash__  s    zUndefined.__hash__c             C   s   d S)Nr"   r   )r5   r   r   r    __str__  s    zUndefined.__str__c             C   s   d S)Nr   r   )r5   r   r   r    r     s    zUndefined.__len__c             c   s   d  S)Nr   )r5   r   r   r    r     s    zUndefined.__iter__c             C   s   d S)NFr   )r5   r   r   r    __nonzero__  s    zUndefined.__nonzero__c             C   s   d S)Nrl   r   )r5   r   r   r    r=     s    zUndefined.__repr__)z_undefined_hintz_undefined_objz_undefined_namez_undefined_exception)-r<   r>   r?   r@   ro   r   r   r7   r
   r   r   __add____radd____mul____rmul__Z__div__Z__rdiv____truediv____rtruediv____floordiv____rfloordiv____mod____rmod____pos____neg__rX   r:   __lt____le____gt____ge____int__	__float____complex____pow____rpow__r   r   r   r   r   r   r   __bool__r=   r   r   r   r    rl     s     frl   c                s    d k r@ d d l  } | j t    j | j t j    d k rR t   f d d     G    f d d   d   } | S)ak  Given a logger object this returns a new undefined class that will
    log certain failures.  It will log iterations and printing.  If no
    logger is given a default logger is created.

    Example::

        logger = logging.getLogger(__name__)
        LoggingUndefined = make_logging_undefined(
            logger=logger,
            base=Undefined
        )

    .. versionadded:: 2.8

    :param logger: the logger to use.  If not provided, a default logger
                   is created.
    :param base: the base class to add logging functionality to.  This
                 defaults to :class:`Undefined`.
    Nr   c                s   |  j  d  k r~ |  j t k r. d |  j } q t |  j t  s_ d t |  j  |  j f } q d t |  j  |  j f } n	 |  j  }   j d |  d  S)Nz%s is undefinedz%s has no element %sz%s has no attribute %szTemplate variable warning: %s)r   r   r   r   rY   r   r   Zwarning)Zundefr   )loggerr   r    _log_message.  s    	z,make_logging_undefined.<locals>._log_messagec                   s   e  Z d  Z   f d d   Z    f d d   Z    f d d   Z e r~    f d d   Z    f d	 d
   Z n    f d d   Z	 d S)z0make_logging_undefined.<locals>.LoggingUndefinedc                sb   y   j  |  | |  SWnD |  j k
 r] } z!  j d t |   |  WYd  d  } ~ Xn Xd  S)NzTemplate variable error: %s)r   r   errorstr)r5   r\   r]   e)baser   r   r    r   @  s
    zKmake_logging_undefined.<locals>.LoggingUndefined._fail_with_undefined_errorc                s    j  |   }   |   | S)N)r   )r5   rw   )r   r   r   r    r   G  s    
z8make_logging_undefined.<locals>.LoggingUndefined.__str__c                s    j  |   }   |   | S)N)r   )r5   rw   )r   r   r   r    r   L  s    
z9make_logging_undefined.<locals>.LoggingUndefined.__iter__c                s    j  |   }   |   | S)N)r   )r5   rw   )r   r   r   r    r   R  s    
z<make_logging_undefined.<locals>.LoggingUndefined.__nonzero__c                s    j  |   }   |   | S)N)__unicode__)r5   rw   )r   r   r   r    r   W  s    
z<make_logging_undefined.<locals>.LoggingUndefined.__unicode__c                s    j  |   }   |   | S)N)r   )r5   rw   )r   r   r   r    r   \  s    
z9make_logging_undefined.<locals>.LoggingUndefined.__bool__N)
r<   r>   r?   r   r   r   r   r   r   r   r   )r   r   r   r   r    LoggingUndefined>  s   r   )loggingZ	getLoggerr<   Z
addHandlerZStreamHandlersysstderrrl   )r   r   r   r   r   )r   r   r   r    r     s    "#c               @   s(   e  Z d  Z d Z f  Z d d   Z d S)DebugUndefineda  An undefined that returns the debug info when printed.

    >>> foo = DebugUndefined(name='foo')
    >>> str(foo)
    '{{ foo }}'
    >>> not foo
    True
    >>> foo + 42
    Traceback (most recent call last):
      ...
    jinja2.exceptions.UndefinedError: 'foo' is undefined
    c             C   sN   |  j  d  k rC |  j t k r) d |  j Sd t |  j  |  j f Sd |  j  S)Nz{{ %s }}z{{ no such element: %s[%r] }}z!{{ undefined value printed: %s }})r   r   r   r   r   )r5   r   r   r    r   t  s    zDebugUndefined.__str__N)r<   r>   r?   r@   ro   r   r   r   r   r    r   d  s   r   c               @   sA   e  Z d  Z d Z f  Z e j Z Z Z	 Z
 Z Z Z Z d S)StrictUndefinedas  An undefined that barks on print and iteration as well as boolean
    tests and all kinds of comparisons.  In other words: you can do nothing
    with it except checking if it's defined using the `defined` test.

    >>> foo = StrictUndefined(name='foo')
    >>> str(foo)
    Traceback (most recent call last):
      ...
    jinja2.exceptions.UndefinedError: 'foo' is undefined
    >>> not foo
    Traceback (most recent call last):
      ...
    jinja2.exceptions.UndefinedError: 'foo' is undefined
    >>> foo + 42
    Traceback (most recent call last):
      ...
    jinja2.exceptions.UndefinedError: 'foo' is undefined
    N)r<   r>   r?   r@   ro   rl   r   r   r   r   r   r   r   r   r   r   r   r   r    r     s   r   )2r@   r   	itertoolsr   Zjinja2.nodesr   r   Zjinja2.utilsr   r   r   r   r	   r
   r   Zjinja2.exceptionsr   r   r   Zjinja2._compatr   r   r   r   r   r   r   __all__r   r   objectr   r   r   r3   r   rA   collectionsrp   registerImportErrorr8   r   r   r   rl   r   r   r   ro   r   r   r   r    <module>
   sJ   44	M@UQ