U
    Zfh                     @   s@   d Z ddlmZ ddlmZ ddlmZ dZG dd deZdS )	a  
    flask_caching.jinja2ext
    ~~~~~~~~~~~~~~~~~~~~~~~

    Jinja2 extension that adds support for caching template fragments.

    Usage::

        {% cache timeout key1[, [key2, ...]] %}
        ...
        {% endcache %}

    By default, the value of "path to template file" + "block start line"
    is used as the cache key. Also, the key name can be set manually.
    Keys are concatenated together into a single string, that can be used
    to avoid the same block evaluating in different templates.

    Set the timeout to ``None`` for no timeout, but with custom keys::

        {% cache None "key" %}
        ...
        {% endcache %}

    Set timeout to ``del`` to delete cached value::

        {% cache 'del' key1 %}
        ...
        {% endcache %}

    Considering we have ``render_form_field`` and ``render_submit`` macros::

        {% cache 60*5 'myform' %}
        <div>
            <form>
            {% render_form_field(form.username) %}
            {% render_submit() %}
            </form>
        </div>
        {% endcache %}

    :copyright: (c) 2010 by Thadeus Burgess.
    :license: BSD, see LICENSE for more details.
    )nodes)	Extension)make_template_fragment_keyZ_template_fragment_cachec                   @   s"   e Zd ZdhZdd Zdd ZdS )CacheExtensioncachec                 C   s   t |jj}| g}|jdr2||  n|t|j |  g }|jdrl||  qP|r|t	| n|tg  |j
dgdd}t| d|g g ||S )Ncommazname:endcacheT)Zdrop_needle_cache)nextstreamlinenoZparse_expressionZskip_ifappendr   ZConstfilenameListZparse_statementsZ	CallBlockZcall_methodZ
set_lineno)selfparserr   argsvary_onbody r   ;/tmp/pip-unpacked-wheel-0x57cbey/flask_caching/jinja2ext.pyparse8   s(    

   zCacheExtension.parsec           	   
   C   s   zt | jt}W n& tk
r6 } z|W 5 d }~X Y nX t||d}|dkr\|| | S ||}|d kr| }|||| |S )N)r   del)getattrenvironmentJINJA_CACHE_ATTR_NAMEAttributeErrorr   deletegetset)	r   timeoutZfragment_namer   Zcallerr   ekeyrvr   r   r   r   V   s    

zCacheExtension._cacheN)__name__
__module____qualname__tagsr   r   r   r   r   r   r   5   s   r   N)	__doc__Zjinja2r   Z
jinja2.extr   Zflask_cachingr   r   r   r   r   r   r   <module>   s
   ,