3
\f[                   @   s  d Z ddlZddlm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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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$e# Z%dd Z&dd Z'd7ddZ(G dd
 d
e#Z)dd Z*G d d! d!e+Z,efd"d#Z-G d$d% d%ee,Z.ydd&l/m0Z0 e0j1e. W n e2k
rf   Y nX G d'd( d(e#Z3G d)d* d*e#Z4G d+d	 d	e4Z5eG d,d- d-e#Z6G d.d de#Z7eG d/d0 d0e#Z8d8d1d2Z9eG d3d4 d4e8Z:eG d5d6 d6e8Z;e8`<e:`<e;`<dS )9z}
    jinja2.runtime
    ~~~~~~~~~~~~~~

    Runtime helpers.

    :copyright: (c) 2017 by the Jinja Team.
    :license: BSD.
    N)chain)
MethodType)EvalContext_context_function_types)	Markupsoft_unicodeescapemissingconcatinternalcodeobject_type_reprevalcontextfunction	Namespace)UndefinedErrorTemplateRuntimeErrorTemplateNotFound)imap	text_type	iteritemsimplements_iteratorimplements_to_stringstring_typesPY2with_metaclassLoopContextTemplateReferenceMacror   r   r	   r
   r   markup_joinunicode_join	to_stringidentityr   r   c             C   s   | S )N )xr!   r!   @C:\prj\env\wsgidav_build_3.6\lib\site-packages\jinja2\runtime.py<lambda>%   s    r$   c             C   sL   g }t t| }x4|D ],}|j| t|drtdjt||S qW t|S )z@Concatenation that escapes if necessary and converts to unicode.__html__ )r   r   appendhasattrr   joinr   r
   )seq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   6   s    c       
      C   sl   |dkri }|r|}nt |pf f|}|r\|r6t |}x$t|D ]\}}	|	tk	r@|	||< q@W | j| |||S )z(Internal helper to for context creation.N)dictr   r	   context_class)
environmenttemplate_nameblocksvarssharedglobalslocalsparentkeyvaluer!   r!   r#   new_context;   s    
r:   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__S   s    zTemplateReference.__init__c             C   s   | j j| }t|| j |dS )Nr   )r;   r2   BlockReference)r<   namer2   r!   r!   r#   __getitem__V   s    zTemplateReference.__getitem__c             C   s   d| j j| jjf S )Nz<%s %r>)	__class____name__r;   r@   )r<   r!   r!   r#   __repr__Z   s    zTemplateReference.__repr__N)rC   
__module____qualname____doc__r>   rA   rD   r!   r!   r!   r#   r   P   s   c             C   s   t | d| S )N__func__)getattr)r"   r!   r!   r#   	_get_funca   s    rJ   c               @   s   e Zd Zdd ZdS )ContextMetac       	      C   sv   t j| |||}|f kr|S t|j}ttj}t|j}ttj}||k	r\||kr\d|_n||krr||krrd|_|S )NT)type__new__rJ   resolveContextresolve_or_missing_legacy_resolve_mode_fast_resolve_mode)	clsr@   basesdrvrN   Zdefault_resolverP   Zdefault_resolve_or_missingr!   r!   r#   rM   g   s    



zContextMeta.__new__N)rC   rE   rF   rM   r!   r!   r!   r#   rK   e   s   rK   c             C   s,   || j kr| j | S || jkr(| j| S |S )N)r3   r7   )r=   r8   r	   r!   r!   r#   rP   }   s
    



rP   c               @   s   e Zd 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dd Zedd Zd%ddZdd ZedZedZedZeredZedZedZ[dd Zd d! Zd"d# ZdS )&rO   a  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.
    Fc             C   s\   || _ i | _|| _t| j|| _t | _|| _tdd t	|D | _
| jrXtt| | _d S )Nc             s   s   | ]\}}||gfV  qd S )Nr!   ).0kvr!   r!   r#   	<genexpr>   s    z#Context.__init__.<locals>.<genexpr>)r7   r3   r0   r   eval_ctxsetexported_varsr@   r.   r   r2   rR   r   rP   )r<   r0   r7   r@   r2   r!   r!   r#   r>      s    zContext.__init__c             C   sX   y$| j | }|j|d }||  W n$ tk
rH   | jjd| ddS X t|| ||S )zRender a parent block.   z#there is no parent block called %r.super)r@   )r2   indexLookupErrorr0   	undefinedr?   )r<   r@   currentr2   r`   r!   r!   r#   r_      s    

zContext.superNc             C   s"   y| | S  t k
r   |S X dS )zfReturns an item from the template context, if it doesn't exist
        `default` is returned.
        N)KeyError)r<   r8   defaultr!   r!   r#   get   s    zContext.getc             C   s6   | j rt| |}n
| j|}|tkr2| jj|dS |S )zLooks up a variable like `__getitem__` or `get` but returns an
        :class:`Undefined` object with the name of the name looked up.
        )r@   )rQ   rP   r	   r0   rb   )r<   r8   rV   r!   r!   r#   rN      s    
zContext.resolvec             C   s,   | j r"| j|}t|trt}|S t| |S )zxResolves a variable like :meth:`resolve` but returns the
        special `missing` value if it cannot be found.
        )rQ   rN   
isinstance	Undefinedr	   rP   )r<   r8   rV   r!   r!   r#   rP      s    

zContext.resolve_or_missingc                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)r3   )rW   rX   )r<   r!   r#   rZ      s    z'Context.get_exported.<locals>.<genexpr>)r.   r]   )r<   r!   )r<   r#   get_exported   s    zContext.get_exportedc             C   s(   | j s| jS | js| j S t| jf| j S )zReturn the complete context as dict including the exported
        variables.  For optimizations reasons this might not return an
        actual copy so be careful with using it.
        )r3   r7   r.   )r<   r!   r!   r#   get_all   s
    zContext.get_allc             O   s   d}t |dr2|j}xd	D ]}t ||r|}P qW t|trt|ddrT| f| }n2t|ddrn| jf| }nt|ddr| jf| }y
|||S  tk
r   | jjdS X d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__call__contextfunctionr   environmentfunctionr   zGvalue was undefined because a callable raised a StopIteration exceptionN)rl   r   rm   )	r(   rk   rg   r   rI   r[   r0   StopIterationrb   )Z_Context__selfZ_Context__objargskwargsZ__traceback_hide__fnZfn_typer!   r!   r#   call   s(    
 


zContext.callc             C   sD   t | j| ji | j dd|}| j|_|jjdd t| jD  |S )zInternal helper function to create a derived context.  This is
        used in situations where the system needs a new context in the same
        template that is independent.
        TNc             s   s   | ]\}}|t |fV  qd S )N)list)rW   rX   rY   r!   r!   r#   rZ     s    z"Context.derived.<locals>.<genexpr>)r:   r0   r@   rj   r[   r2   updater   )r<   r6   r=   r!   r!   r#   derived  s
    zContext.derivedc                s$    fdd}t t j|_ |_|S )Nc                s   t | j   S )N)rI   rj   )r<   )methr!   r#   r$     s    zContext._all.<locals>.<lambda>)rI   r.   rG   rC   )rv   proxyr!   )rv   r#   _all  s    zContext._allkeysvaluesitemsiterkeys
itervaluesr   c             C   s   || j kp|| jkS )N)r3   r7   )r<   r@   r!   r!   r#   __contains__(  s    zContext.__contains__c             C   s   | j |}|tkrt||S )zTLookup a variable or raise `KeyError` if the variable is
        undefined.
        )rP   r	   rd   )r<   r8   itemr!   r!   r#   rA   +  s    
zContext.__getitem__c             C   s   d| j jt| j | jf S )Nz<%s %s of %r>)rB   rC   reprrj   r@   )r<   r!   r!   r#   rD   4  s    
zContext.__repr__)N)N)rC   rE   rF   rG   rQ   rR   r>   r_   rf   rN   rP   ri   rj   r   rr   ru   rx   ry   rz   r{   r   r|   r}   r   r~   rA   rD   r!   r!   r!   r#   rO      s0   
	"
	rO   )Mappingc               @   s0   e Zd ZdZdd Zedd Zedd ZdS )	r?   z"One block on a template reference.c             C   s   || _ || _|| _|| _d S )N)r@   _context_stack_depth)r<   r@   r=   stackdepthr!   r!   r#   r>   G  s    zBlockReference.__init__c             C   sF   | j d t| jkr,| jjjd| j ddS t| j| j| j| j d S )zSuper the block.r^   z#there is no parent block called %r.r_   )r@   )r   lenr   r   r0   rb   r@   r?   )r<   r!   r!   r#   r_   M  s    zBlockReference.superc             C   s,   t | j| j | j}| jjjr(t|}|S )N)r
   r   r   r   r[   
autoescaper   )r<   rV   r!   r!   r#   rk   W  s    
zBlockReference.__call__N)	rC   rE   rF   rG   r>   propertyr_   r   rk   r!   r!   r!   r#   r?   D  s   
r?   c               @   s   e Zd ZdZeZeZeZdZ	dddZ
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edd Zedd Zdd Zedd ZeZ[dd ZdS )LoopContextBasez%A loop context for dynamic iteration.Nr   c             C   s"   || _ || _d| _|| _t| _d S )Nr^   )
_undefined_recurseindex0depth0r	   _last_checked_value)r<   rb   recurser   r!   r!   r#   r>   g  s
    zLoopContextBase.__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)	TypeErrorr   r   )r<   ro   r!   r!   r#   cyclen  s    zLoopContextBase.cyclec             G   s   | j |kr|| _ dS dS )z9Checks whether the value has changed since the last call.TF)r   )r<   r9   r!   r!   r#   changedt  s    
zLoopContextBase.changedc             C   s
   | j dkS )Nr   )r   )r"   r!   r!   r#   r$   {  s    zLoopContextBase.<lambda>c             C   s
   | j tkS )N)_after_last_iteration)r"   r!   r!   r#   r$   |  s    c             C   s
   | j d S )Nr^   )r   )r"   r!   r!   r#   r$   }  s    c             C   s   | j | j S )N)lengthr   )r"   r!   r!   r#   r$   ~  s    c             C   s   | j | j S )N)r   r`   )r"   r!   r!   r#   r$     s    c             C   s
   | j d S )Nr^   )r   )r"   r!   r!   r#   r$     s    c             C   s   | j tkr| jdS | j S )Nzthere is no previous item)_before_first_iterationr   )r<   r!   r!   r#   previtem  s    

zLoopContextBase.previtemc             C   s   | j tkr| jdS | j S )Nzthere is no next item)r   r   r   )r<   r!   r!   r#   nextitem  s    

zLoopContextBase.nextitemc             C   s   | j S )N)r   )r<   r!   r!   r#   __len__  s    zLoopContextBase.__len__c             C   s(   | j d krtd| j || j | jd S )NzMTried to call non recursive loop.  Maybe you forgot the 'recursive' modifier.r^   )r   r   r   )r<   iterabler!   r!   r#   loop  s    
zLoopContextBase.loopc             C   s   d| j j| j| jf S )Nz
<%s %r/%r>)rB   rC   r`   r   )r<   r!   r!   r#   rD     s    zLoopContextBase.__repr__)Nr   )rC   rE   rF   rG   r   r   _currentr   r   _lengthr>   r   r   r   firstlastr`   ZrevindexZ	revindex0r   r   r   r   r   r   rk   rD   r!   r!   r!   r#   r   _  s*   
	r   c               @   s2   e Zd ZdddZedd Zdd Zd	d
 ZdS )r   Nr   c             C   sV   t j| ||| t|| _yt|| _W n ttfk
rF   d | _Y nX | j | _	d S )N)
r   r>   iter	_iteratorr   r   r   AttributeError
_safe_nextr   )r<   r   rb   r   r   r!   r!   r#   r>     s    
zLoopContext.__init__c             C   s<   | j d kr6t| j}t|| _| jd }t|| | _ | j S )N   )r   tupler   r   r   r   )r<   r   Ziterations_doner!   r!   r#   r     s    



zLoopContext.lengthc             C   s   t | S )N)LoopContextIterator)r<   r!   r!   r#   __iter__  s    zLoopContext.__iter__c             C   s$   y
t | jS  tk
r   tS X d S )N)nextr   rn   r   )r<   r!   r!   r#   r     s    
zLoopContext._safe_next)Nr   )rC   rE   rF   r>   r   r   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.r=   c             C   s
   || _ d S )N)r=   )r<   r=   r!   r!   r#   r>     s    zLoopContextIterator.__init__c             C   s   | S )Nr!   )r<   r!   r!   r#   r     s    zLoopContextIterator.__iter__c             C   sH   | j }| jd7  _|jtkr$t |j|_|j|_|j |_|j|fS )Nr^   )r=   r   r   r   rn   r   r   r   )r<   ctxr!   r!   r#   __next__  s    

zLoopContextIterator.__next__N)r=   )rC   rE   rF   rG   	__slots__r>   r   r   r!   r!   r!   r#   r     s
   r   c               @   s:   e Zd ZdZdddZeedd Zdd Zd	d
 Z	dS )r   zWraps a macro function.Nc	       	      C   sV   || _ || _t|| _|| _|| _|| _|| _|| _d|k| _	|d krL|j
}|| _d S )Ncaller)_environment_funcr   _argument_countr@   	argumentscatch_kwargscatch_varargsr   explicit_callerr   _default_autoescape)	r<   r0   funcr@   r   r   r   r   Zdefault_autoescaper!   r!   r#   r>     s    

zMacro.__init__c             O   s  |r*t |d tr*|d j}|dd  }n| j}t|d | j }t|}d}|| jkrxht| jt|d  D ]F\}}y|j	|}	W n t
k
r   t}	Y nX |dkrd}|j|	 qpW n| j}| jr| r|j	dd }
|
d kr| jjddd}
|j|
 | jr|j| n8|rHd|kr.td| j td	| jtt|f | jrf|j|| jd   n(t|| jkrtd
| jt| jf | j||S )Nr   r^   Fr   TzNo caller defined)r@   zamacro %r was invoked with two values for the special caller argument.  This is most likely a bug.z%macro %r takes no keyword argument %rz+macro %r takes not more than %d argument(s))rg   r   r   r   rs   r   r   	enumerater   poprd   r	   r'   r   r   r   rb   r   r   r@   r   r   r   _invoke)r<   ro   rp   r   r   offZfound_calleridxr@   r9   r   r!   r!   r#   rk     sJ    

 



zMacro.__call__c             C   s   | j | }|rt|}|S )z=This method is being swapped out by the async implementation.)r   r   )r<   r   r   rV   r!   r!   r#   r   A  s    
zMacro._invokec             C   s$   d| j j| jd krdpt| jf S )Nz<%s %s>	anonymous)rB   rC   r@   r   )r<   r!   r!   r#   rD   H  s    zMacro.__repr__)N)
rC   rE   rF   rG   r>   r   r   rk   r   rD   r!   r!   r!   r#   r     s   
Kc               @   s   e Zd ZdZdZdedef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# 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 )rh   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   )r<   hintobjr@   excr!   r!   r#   r>   a  s    zUndefined.__init__c             O   sl   | j dkrX| jtkr d| j }q^t| jtsBd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   rg   r   r   r   )r<   ro   rp   r   r!   r!   r#   _fail_with_undefined_errorg  s    

z$Undefined._fail_with_undefined_errorc             C   s    |d d dkrt || j S )Nr   __)r   r   )r<   r@   r!   r!   r#   __getattr__}  s    zUndefined.__getattr__c             C   s   t | t |kS )N)rL   )r<   otherr!   r!   r#   __eq__  s    zUndefined.__eq__c             C   s   | j | S )N)r   )r<   r   r!   r!   r#   __ne__  s    zUndefined.__ne__c             C   s   t t| S )N)idrL   )r<   r!   r!   r#   __hash__  s    zUndefined.__hash__c             C   s   dS )Nr&   r!   )r<   r!   r!   r#   __str__  s    zUndefined.__str__c             C   s   dS )Nr   r!   )r<   r!   r!   r#   r     s    zUndefined.__len__c             c   s   d S )Nr!   )r<   r!   r!   r#   r     s    zUndefined.__iter__c             C   s   dS )NFr!   )r<   r!   r!   r#   __nonzero__  s    zUndefined.__nonzero__c             C   s   dS )Nrh   r!   )r<   r!   r!   r#   rD     s    zUndefined.__repr__)r   r   r   r   )/rC   rE   rF   rG   r   r	   r   r>   r   r   r   __add____radd____mul____rmul__Z__div__Z__rdiv____truediv____rtruediv____floordiv____rfloordiv____mod____rmod____pos____neg__rk   rA   __lt____le____gt____ge____int__	__float____complex____pow____rpow____sub____rsub__r   r   r   r   r   r   r   __bool__rD   r!   r!   r!   r#   rh   O  s     lrh   c                s`   dkr,ddl }|jtj|jtj dkr8t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                sn   | j d krX| jtkr d| j }q^t| jtsBd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   rg   r   r   warning)Zundefr   )loggerr!   r#   _log_message  s    

z,make_logging_undefined.<locals>._log_messagec                   sf   e Zd ZfddZ fddZ fddZerT fddZ fd	d
Zn fddZ	dS )z0make_logging_undefined.<locals>.LoggingUndefinedc                sP   y j | f||S  | jk
rJ } zjdt| |W Y d d }~X nX d S )NzTemplate variable error: %s)r   r   errorstr)r<   ro   rp   e)baser   r!   r#   r     s
    zKmake_logging_undefined.<locals>.LoggingUndefined._fail_with_undefined_errorc                s   j | } |  |S )N)r   )r<   rV   )r   r   r!   r#   r     s    
z8make_logging_undefined.<locals>.LoggingUndefined.__str__c                s   j | } |  |S )N)r   )r<   rV   )r   r   r!   r#   r     s    
z9make_logging_undefined.<locals>.LoggingUndefined.__iter__c                s   j | } |  |S )N)r   )r<   rV   )r   r   r!   r#   r     s    
z<make_logging_undefined.<locals>.LoggingUndefined.__nonzero__c                s   j | } |  |S )N)__unicode__)r<   rV   )r   r   r!   r#   r     s    
z<make_logging_undefined.<locals>.LoggingUndefined.__unicode__c                s   j | } |  |S )N)r   )r<   rV   )r   r   r!   r#   r     s    
z9make_logging_undefined.<locals>.LoggingUndefined.__bool__N)
rC   rE   rF   r   r   r   r   r   r   r   r!   )r   r   r   r!   r#   LoggingUndefined  s   r   )logging	getLoggerrC   
addHandlerStreamHandlersysstderrrh   )r   r   r   r   r!   )r   r   r   r#   make_logging_undefined  s    
#r   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   s<   | j d kr2| jtkrd| j S dt| j| jf S d| j  S )Nz{{ %s }}z{{ no such element: %s[%r] }}z!{{ undefined value printed: %s }})r   r   r	   r   r   )r<   r!   r!   r#   r     s    



zDebugUndefined.__str__N)rC   rE   rF   rG   r   r   r!   r!   r!   r#   r     s   r   c               @   s6   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)rC   rE   rF   rG   r   rh   r   r   r   r   r   r   r   r   r   r!   r!   r!   r#   r     s   r   )NNNN)NN)=rG   r   	itertoolsr   typesr   jinja2.nodesr   r   jinja2.utilsr   r   r   r	   r
   r   r   r   r   jinja2.exceptionsr   r   r   jinja2._compatr   r   r   r   r   r   r   r   __all__r   r    objectr   r   r   r   r:   r   rJ   rL   rK   rP   rO   collectionsr   registerImportErrorr?   r   r   r   r   rh   r   r   r   r   r!   r!   r!   r#   <module>
   sX   ,( 
 9F'mU
Q