3
\f[                   @   s,  d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZ ejdZejddjeejdSdjeejdTf ZejdZejdZejdZdZdZedf ddd i Ze ZdjZdejdkZdd Zd d! Z d"d# Z!d$d% Z"d&d' Z#d(d) Z$d*d+ Z%dUd-d.Z&dVd0d1Z'd2d3 Z(dWd4d5Z)dXd6d7Z*dYd<d=Z+dZd?d@Z,G dAdB dBe-Z.yddClm/Z/ e/j0e. W n e1k
r   Y nX d[f d9d,fdGdHZ2d\dIdJZ3eG dKdL dLe-Z4G dMdN dNe-Z5G dOdP dPe-Z6ye7dQ d9Z8W n e9k
r   d,Z8Y nX ddRl:m;Z;mZm<Z< dS )]z
    jinja2.utils
    ~~~~~~~~~~~~

    Utility functions.

    :copyright: (c) 2017 by the Jinja Team.
    :license: BSD, see LICENSE for more details.
    N)deque)Lock)	text_typestring_typesimplements_iterator	url_quotez(\s+)z4^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$|(<&lt;.,)>
&gt;z&^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$z(<!--.*?-->|<[^>]*>)z	&([^;]+);4abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789ZMissingType__repr__c             C   s   dS )Nmissing )xr   r   >C:\prj\env\wsgidav_build_3.6\lib\site-packages\jinja2\utils.py<lambda>"   s    r    z\//c             C   s
   d| _ | S )a+  This decorator can be used to mark a function or method context callable.
    A context callable is passed the active :class:`Context` as first argument when
    called from the template.  This is useful if a function wants to get access
    to the context or functions provided on the context object.  For example
    a function that returns a sorted list of template variables the current
    template exports could look like this::

        @contextfunction
        def get_exported_names(context):
            return sorted(context.exported_vars)
    T)contextfunction)fr   r   r   r   ,   s    r   c             C   s
   d| _ | S )aG  This decorator can be used to mark a function or method as an eval
    context callable.  This is similar to the :func:`contextfunction`
    but instead of passing the context, an evaluation context object is
    passed.  For more information about the eval context, see
    :ref:`eval-context`.

    .. versionadded:: 2.4
    T)evalcontextfunction)r   r   r   r   r   <   s    	r   c             C   s
   d| _ | S )zThis decorator can be used to mark a function or method as environment
    callable.  This decorator works exactly like the :func:`contextfunction`
    decorator just that the first argument is the active :class:`Environment`
    and not context.
    T)environmentfunction)r   r   r   r   r   I   s    r   c             C   s   t j| j | S )z%Marks the function as internally used)internal_codeadd__code__)r   r   r   r   internalcodeS   s    r#   c             C   s   ddl m} t| |S )a  Check if the object passed is undefined.  This does nothing more than
    performing an instance check against :class:`Undefined` but looks nicer.
    This can be used for custom filters or tests that want to react to
    undefined variables.  For example a custom default filter can look like
    this::

        def default(var, default=''):
            if is_undefined(var):
                return default
            return var
    r   )	Undefined)jinja2.runtimer$   
isinstance)objr$   r   r   r   is_undefinedY   s    r(   c             C   s   x| D ]}qW dS )z4Consumes an iterable without doing anything with it.Nr   )iterableeventr   r   r   consumei   s    
r+   c              C   s,   ddl m}  ddlm} | j  |j  dS )a"  Jinja2 keeps internal caches for environments and lexers.  These are
    used so that Jinja2 doesn't have to recreate environments and lexers all
    the time.  Normally you don't have to care about that but if you are
    measuring memory consumption you may want to clean the caches.
    r   )_spontaneous_environments)_lexer_cacheN)jinja2.environmentr,   jinja2.lexerr-   clear)r,   r-   r   r   r   clear_cacheso   s    r1   Fc             C   s   yfd| kr| j dd\}}n6d| krJ| j d}dj|dd }|d }nt| S tt|dd|g|S  ttfk
r   |s Y nX dS )a  Imports an object based on a string.  This is useful if you want to
    use import paths as endpoints or something similar.  An import path can
    be specified either in dotted notation (``xml.sax.saxutils.escape``)
    or with a colon as object delimiter (``xml.sax.saxutils:escape``).

    If the `silent` is True the return value will be `None` if the import
    fails.

    :return: imported object
    :   r   Nr4   )splitjoin
__import__getattrImportErrorAttributeError)import_namesilentmoduler'   itemsr   r   r   import_string{   s    

r?   rbc             C   sJ   y
t | |S  tk
rD } z|jtjtjtjfkr4 W Y dd}~X nX dS )zZReturns a file descriptor for the filename if that file exists,
    otherwise `None`.
    N)openIOErrorerrnoENOENTEISDIREINVAL)filenamemodeer   r   r   open_if_exists   s
    
rJ   c             C   sJ   | dkrdS | t krdS | jjdkr.| jj}n| jjd | jj }d| S )	zReturns the name of the object's type.  For some recognized
    singletons the name of the object is returned instead. (For
    example for `None` and `Ellipsis`).
    NNoneEllipsis__builtin__builtinsr   z	%s object)rM   rN   )rL   	__class__
__module____name__)r'   namer   r   r   object_type_repr   s    
rS   c             C   sB   yddl m } || |dS  tk
r<   ddlm} || S X dS )zYPrettyprint an object.  Either use the `pretty` library or the
    builtin `pprint`.
    r   )pretty)verbose)pformatN)rT   r9   pprintrV   )r'   rU   rT   rV   r   r   r   rV      s    rV   c             C   s  |fdd}t jtt| }|r2dtt| p4d}|rFdt| pHd}x0t|D ]"\}}	tj|	}
|
rV|
j \}}}|jdsd|kr|jd r|jd	 rt	|d
kr|d
 t
t kr|jds|jds|jdrd|||||f }|jds|jd	r d|||||f }d|krZ|jd rZd|krZtj|rZd||f }|| | |	krV|| | ||< qVW dj|S )a  Converts any URLs in text into clickable links. Works on http://,
    https:// and www. links. Links can have trailing punctuation (periods,
    commas, close-parens) and leading punctuation (opening parens) and
    it'll still do the right thing.

    If trim_url_limit is not None, the URLs in link text will be limited
    to trim_url_limit characters.

    If nofollow is True, the URLs in link text will get a rel="nofollow"
    attribute.

    If target is not None, a target attribute will be added to the link.
    c             S   s,   |d k	r(| d | t | |kr"dp$d p*| S )Nz...r   )len)r   limitr   r   r   r      s   zurlize.<locals>.<lambda>z	 rel="%s"r   z target="%s"zwww.@zhttp://zhttps://r   z.orgz.netz.comz<a href="http://%s"%s%s>%s</a>z<a href="%s"%s%s>%s</a>r2   z<a href="mailto:%s">%s</a>)_word_split_rer5   r   escape	enumerate_punctuation_rematchgroups
startswithrX   _letters_digitsendswith_simple_email_rer6   )texttrim_url_limitreltargetZtrim_urlwordsZrel_attrZtarget_attriwordr_   ZleadZmiddletrailr   r   r   urlize   s:    




rn      T   d   c             C   sf  ddl m} ddlm}m} |j }g }xt| D ]}	d}
d }}d}d}g }xtt|||D ]\}}	x||}||krp|}P qpW |
r|j }d}
||dd |kr|}|d	7 }|d
7 }||dd |kr| }}|d7 }d}
|j	| qfW dj
|}|jd
r|dd d }n|jds0|d7 }|j	| q4W |sNdj
|S tdj
dd |D S )z+Generate some lorem ipsum for the template.r   )LOREM_IPSUM_WORDS)choice	randrangeTNF         r   
   rp   r    r3   z

r   c             s   s   | ]}d t | V  qdS )z	<p>%s</p>N)r\   ).0r   r   r   r   	<genexpr>  s    z'generate_lorem_ipsum.<locals>.<genexpr>r4   )jinja2.constantsrr   randomrs   rt   r5   ranger]   
capitalizeappendr6   rd   Markup)nhtmlminmaxrr   rs   rt   rj   result_Znext_capitalizedZ
last_commaZlast_fullstoprl   lastpidxr   r   r   generate_lorem_ipsum   sJ    

r   utf-8c             C   sV   t | tst| } t | tr&| j|} | r0dp2d}tt| |}|rR|jdd}|S )a&  URL escapes a single bytestring or unicode string with the
    given charset if applicable to URL safe quoting under all rules
    that need to be considered under all supported Python versions.

    If non strings are provided they are converted to their unicode
    representation first.
       /    z%20+)r&   r   r   encoder   replace)r'   charsetfor_qssafervr   r   r   unicode_urlencode  s    


r   c               @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	d/ddZ
d0ddZdd Zdd Zdd Zdd Zdd Zdd 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e	ZdS )1LRUCachez"A simple LRU Cache implementation.c             C   s    || _ i | _t | _| j  d S )N)capacity_mappingr   _queue	_postinit)selfr   r   r   r   __init__9  s    zLRUCache.__init__c             C   s4   | j j| _| j j| _| j j| _t | _| j j	| _
d S )N)r   popleft_popleftpopZ_popremove_remover   _wlockr   _append)r   r   r   r   r   ?  s
    


zLRUCache._postinitc             C   s   | j | j| jdS )N)r   r   r   )r   r   r   )r   r   r   r   __getstate__G  s    zLRUCache.__getstate__c             C   s   | j j| | j  d S )N)__dict__updater   )r   dr   r   r   __setstate__N  s    zLRUCache.__setstate__c             C   s   | j fS )N)r   )r   r   r   r   __getnewargs__R  s    zLRUCache.__getnewargs__c             C   s*   | j | j}|jj| j t| j|_|S )z&Return a shallow copy of the instance.)rO   r   r   r   r   r   )r   r   r   r   r   copyU  s    zLRUCache.copyNc             C   s"   y| | S  t k
r   |S X dS )z/Return an item from the cache dict or `default`N)KeyError)r   keydefaultr   r   r   get\  s    zLRUCache.getc             C   sF   | j j  z*y| | S  tk
r0   || |< |S X W d| j j  X dS )zvSet `default` if the key is not in the cache otherwise
        leave unchanged. Return the value of this key.
        N)r   acquirer   release)r   r   r   r   r   r   
setdefaultc  s    

zLRUCache.setdefaultc          
   C   s4   | j j  z| jj  | jj  W d| j j  X dS )zClear the cache.N)r   r   r   r0   r   r   )r   r   r   r   r0   q  s
    

zLRUCache.clearc             C   s
   || j kS )z$Check if a key exists in this cache.)r   )r   r   r   r   r   __contains__z  s    zLRUCache.__contains__c             C   s
   t | jS )z%Return the current size of the cache.)rX   r   )r   r   r   r   __len__~  s    zLRUCache.__len__c             C   s   d| j j| jf S )Nz<%s %r>)rO   rQ   r   )r   r   r   r   r     s    zLRUCache.__repr__c             C   sf   | j j  zJ| j| }| jd |krRy| j| W n tk
rF   Y nX | j| |S | j j  X dS )zGet an item from the cache. Moves the item up so that it has the
        highest priority then.

        Raise a `KeyError` if it does not exist.
        r3   Nr4   )r   r   r   r   r   
ValueErrorr   r   )r   r   r   r   r   r   __getitem__  s    


zLRUCache.__getitem__c          
   C   sf   | j j  zJ|| jkr"| j| nt| j| jkr>| j| j = | j| || j|< W d| j j  X dS )zhSets the value for an item. Moves the item up so that it
        has the highest priority then.
        N)	r   r   r   r   rX   r   r   r   r   )r   r   valuer   r   r   __setitem__  s    


zLRUCache.__setitem__c             C   sL   | j j  z0| j|= y| j| W n tk
r6   Y nX W d| j j  X dS )z]Remove an item from the cache dict.
        Raise a `KeyError` if it does not exist.
        N)r   r   r   r   r   r   )r   r   r   r   r   __delitem__  s    

zLRUCache.__delitem__c                s$    fddt  jD }|j  |S )zReturn a list of items.c                s   g | ]}| j | fqS r   )r   )rz   r   )r   r   r   
<listcomp>  s    z"LRUCache.items.<locals>.<listcomp>)listr   reverse)r   r   r   )r   r   r>     s    zLRUCache.itemsc             C   s   t | j S )zIterate over all items.)iterr>   )r   r   r   r   	iteritems  s    zLRUCache.iteritemsc             C   s   dd | j  D S )zReturn a list of all values.c             S   s   g | ]}|d  qS )r3   r   )rz   r   r   r   r   r     s    z#LRUCache.values.<locals>.<listcomp>)r>   )r   r   r   r   values  s    zLRUCache.valuesc             C   s   t | j S )zIterate over all values.)r   r   )r   r   r   r   	itervalue  s    zLRUCache.itervaluec             C   s   t | S )z7Return a list of all keys ordered by most recent usage.)r   )r   r   r   r   keys  s    zLRUCache.keysc             C   s   t t| jS )z[Iterate over all keys in the cache dict, ordered by
        the most recent usage.
        )reversedtupler   )r   r   r   r   iterkeys  s    zLRUCache.iterkeysc             C   s   t t| jS )zVIterate over the values in the cache dict, oldest items
        coming first.
        )r   r   r   )r   r   r   r   __reversed__  s    zLRUCache.__reversed__)N)N)rQ   rP   __qualname____doc__r   r   r   r   r   r   r   r   r0   r   r   r   r   r   r   r>   r   r   r   r   r   __iter__r   __copy__r   r   r   r   r   2  s2   

	r   )MutableMappingr   htmxmlc                s:   t dd | D t dd |D  fdd}|S )ac  Intelligently sets the initial value of autoescaping based on the
    filename of the template.  This is the recommended way to configure
    autoescaping if you do not want to write a custom function yourself.

    If you want to enable it for all templates created from strings or
    for all templates with `.html` and `.xml` extensions::

        from jinja2 import Environment, select_autoescape
        env = Environment(autoescape=select_autoescape(
            enabled_extensions=('html', 'xml'),
            default_for_string=True,
        ))

    Example configuration to turn it on at all times except if the template
    ends with `.txt`::

        from jinja2 import Environment, select_autoescape
        env = Environment(autoescape=select_autoescape(
            disabled_extensions=('txt',),
            default_for_string=True,
            default=True,
        ))

    The `enabled_extensions` is an iterable of all the extensions that
    autoescaping should be enabled for.  Likewise `disabled_extensions` is
    a list of all templates it should be disabled for.  If a template is
    loaded from a string then the default from `default_for_string` is used.
    If nothing matches then the initial value of autoescaping is set to the
    value of `default`.

    For security reasons this function operates case insensitive.

    .. versionadded:: 2.9
    c             s   s    | ]}d |j d j  V  qdS )r   N)lstriplower)rz   r   r   r   r   r{     s   z$select_autoescape.<locals>.<genexpr>c             s   s    | ]}d |j d j  V  qdS )r   N)r   r   )rz   r   r   r   r   r{     s   c                s4   | d krS | j  } | jr"dS | jr0dS  S )NTF)r   rd   )template_name)r   default_for_stringdisabled_patternsenabled_patternsr   r   
autoescape  s    

z%select_autoescape.<locals>.autoescape)r   )Zenabled_extensionsZdisabled_extensionsr   r   r   r   )r   r   r   r   r   select_autoescape  s    &

	r   c             K   sB   |dkrt j}|| f|jddjddjddjdd	}t|S )
a  Works exactly like :func:`dumps` but is safe for use in ``<script>``
    tags.  It accepts the same arguments and returns a JSON string.  Note that
    this is available in templates through the ``|tojson`` filter which will
    also mark the result as safe.  Due to how this function escapes certain
    characters this is safe even if used outside of ``<script>`` tags.

    The following characters are escaped in strings:

    -   ``<``
    -   ``>``
    -   ``&``
    -   ``'``

    This makes it safe to embed such strings in any place in HTML with the
    notable exception of double quoted attributes.  In that case single
    quote your attributes or HTML escape it in addition.
    Nr
   z\u003cr   z\u003e&z\u0026'z\u0027)jsondumpsr   r   )r'   dumperkwargsr   r   r   r   htmlsafe_json_dumps!  s    r   c               @   s8   e Zd ZdZdd Zdd Zedd Zdd	 ZeZ	d
S )CyclerzA cycle helper for templates.c             G   s   |st d|| _| j  d S )Nz$at least one item has to be provided)RuntimeErrorr>   reset)r   r>   r   r   r   r   A  s    zCycler.__init__c             C   s
   d| _ dS )zResets the cycle.r   N)pos)r   r   r   r   r   G  s    zCycler.resetc             C   s   | j | j S )zReturns the current item.)r>   r   )r   r   r   r   currentK  s    zCycler.currentc             C   s    | j }| jd t| j | _|S )z#Goes one item ahead and returns it.r3   )r   r   rX   r>   )r   r   r   r   r   nextP  s    zCycler.nextN)
rQ   rP   r   r   r   r   propertyr   r   __next__r   r   r   r   r   =  s   r   c               @   s"   e Zd ZdZdddZdd ZdS )	JoinerzA joining helper for templates., c             C   s   || _ d| _d S )NF)sepused)r   r   r   r   r   r   \  s    zJoiner.__init__c             C   s   | j sd| _ dS | jS )NTr   )r   r   )r   r   r   r   __call__`  s    zJoiner.__call__N)r   )rQ   rP   r   r   r   r   r   r   r   r   r   Y  s   
r   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )	Namespacez}A namespace object that can hold arbitrary attributes.  It may be
    initialized from a dictionary or with keyword argments.c              O   s&   | d | dd   }} t | ||_d S )Nr   r3   )dict_Namespace__attrs)argsr   r   r   r   r   r   k  s    zNamespace.__init__c             C   s@   |dkrt j| |S y
| j| S  tk
r:   t|Y nX d S )Nr   )object__getattribute__r   r   r:   )r   rR   r   r   r   r   o  s    
zNamespace.__getattribute__c             C   s   || j |< d S )N)r   )r   rR   r   r   r   r   r   w  s    zNamespace.__setitem__c             C   s
   d| j  S )Nz<Namespace %r>)r   )r   r   r   r   r   z  s    zNamespace.__repr__N)rQ   rP   r   r   r   r   r   r   r   r   r   r   r   g  s
   r   z,async def _():
 async for _ in ():
  yield _)r   r\   soft_unicode)r	   r
   r   )r   r   r   r   r   r   )F)r@   )F)NNN)ro   Trp   rq   )r   F)r   r   r   )N)=r   rer   rC   collectionsr   	threadingr   jinja2._compatr   r   r   r   compiler[   r6   mapr\   r^   re   _striptags_re
_entity_rerb   rc   typer   setr    concatr   _slash_escaper   r   r   r#   r(   r+   r1   r?   rJ   rS   rV   rn   r   r   r   r   r   registerr9   r   r   r   r   r   exechave_async_genSyntaxError
markupsafer   r   r   r   r   r   <module>
   sp   








1
1
 33

