3
B^                 @   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mZm	Z	 ddl
mZ ddlmZmZ dd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zedd Zdd Zd.ddZdd Zdd Zd d! Zd"d# Zd$d% Zd&d' ZG d(d) d)eZG d*d+ d+e Z!d/d,d-Z"dS )0z
    jinja2.asyncsupport
    ~~~~~~~~~~~~~~~~~~~

    Has all the code for async support which is implemented as a patch
    for supported Python versions.

    :copyright: (c) 2017 by the Jinja Team.
    :license: BSD, see LICENSE for more details.
    N)update_wrapper)concatinternalcodeMarkup)TemplateModule)LoopContextBase_last_iterationc                s&   g  fdd}| I d H  t S )Nc                 s>   x8 2 d H y3 d H } W n
 t k
s.X j|  q
   Y  W d S )N)StopAsyncIterationappend)event)	async_genrv 5/tmp/pip-build-8app2_gc/Jinja2/jinja2/asyncsupport.pycollect   s    "zconcat_async.<locals>.collect)r   )r   r   r   )r   r   r   concat_async   s    r   c             O  s   t ||}yFx@| j| j|2 d H y3 d H }W n
 tk
sBX |V  q"   Y  W W n tk
rl   tj }Y nX d S | jj|dV  d S )NT)	dictroot_render_funcnew_contextr	   	Exceptionsysexc_infoenvironmenthandle_exception)selfargskwargsvarsr   r   r   r   r   generate_async   s    
.r   c                s    dd   fdd}t |S )Nc             s   s@   | j ||}yx|j|j V  qW W n tk
r:   Y nX d S )N)r   run_until_complete	__anext__r	   )r   loopr   r   r   r   r   r   _convert_generator,   s    z.wrap_generate_func.<locals>._convert_generatorc                s*   | j js| f||S  | tj ||S )N)r   is_asyncasyncioget_event_loop)r   r   r   )r"   original_generater   r   generate3   s    z$wrap_generate_func.<locals>.generate)r   )r&   r'   r   )r"   r&   r   wrap_generate_func+   s    r(   c                sd   | j jstdt||}| j|}yt| j|I d H S  tk
rT   tj	 }Y nX | j j
|dS )Nz8The environment was not created with async mode enabled.T)r   r#   RuntimeErrorr   r   r   r   r   r   r   r   )r   r   r   r   ctxr   r   r   r   render_async:   s    

r+   c                s    fdd}t | S )Nc                s2   | j js | f||S tj }|j| j||S )N)r   r#   r$   r%   r   r+   )r   r   r   r!   )original_renderr   r   renderJ   s    z wrap_render_func.<locals>.render)r   )r,   r-   r   )r,   r   wrap_render_funcI   s    r.   c                s(   t dd  t  fdd}t|S )Nc                s2   t | j| j | jI d H }| jjjr.t|}|S )N)r   _stack_depth_contextZeval_ctx
autoescaper   )r   r   r   r   r   
async_callS   s    
z-wrap_block_reference_call.<locals>.async_callc                s   | j jjs| S  | S )N)r1   r   r#   )r   )r3   original_callr   r   __call__Z   s    
z+wrap_block_reference_call.<locals>.__call__)r   r   )r4   r5   r   )r3   r4   r   wrap_block_reference_callR   s    r6   c                s(   t dd  t  fdd}t|S )Nc                s    | j | I d H }|rt|}|S )N)Z_funcr   )r   	argumentsr2   r   r   r   r   async_invoked   s    z'wrap_macro_invoke.<locals>.async_invokec                s    | j js| ||S  | ||S )N)Z_environmentr#   )r   r7   r2   )r8   original_invoker   r   _invokek   s    z"wrap_macro_invoke.<locals>._invoke)r   r   )r9   r:   r   )r8   r9   r   wrap_macro_invokec   s    r;   c                s(   | j d k	r| j S | j I d H  | _ }|S )N)_modulemake_module_async)r   r   r   r   r   get_default_module_asyncs   s    
r>   c                s   t  fdd}|S )Nc                s   | j jrtd | S )Nz6Template module attribute is unavailable in async mode)r   r#   r)   )r   )original_default_moduler   r   _get_default_module|   s    z0wrap_default_module.<locals>._get_default_module)r   )r?   r@   r   )r?   r   wrap_default_module{   s    rA   Fc                s^   | j |||}g }x>| j|2 d H y3 d H }W n
 tk
sFX |j| q"   Y  W t| ||S )N)r   r   r	   r
   r   )r   r   sharedlocalscontextZbody_streamitemr   r   r   r=      s
    (r=   c              C   sd   ddl m}  t| j| _tt| j| _tt| j| _t| j| _t	| j
| _
t| _tt| j| _d S )Nr   )Template)Zjinja2rF   r(   r'   r   r   r+   r.   r-   rA   r@   r>   Z_get_default_module_asyncr=   )rF   r   r   r   patch_template   s    
rG   c              C   s,   ddl m} m} t| j| _t|j|_d S )Nr   )BlockReferenceMacro)jinja2.runtimerH   rI   r6   r5   r;   r:   )rH   rI   r   r   r   patch_runtime   s    
rK   c              C   s&   ddl m}  ddlm} | j| d S )Nr   )FILTERS)ASYNC_FILTERS)Zjinja2.filtersrL   Zjinja2.asyncfiltersrM   update)rL   rM   r   r   r   patch_filters   s    rO   c               C   s   t   t  t  d S )N)rG   rK   rO   r   r   r   r   	patch_all   s    rP   c                s   t j| r| I d H S | S )N)inspectisawaitable)valuer   r   r   
auto_await   s    

rT   c             C  s\   t | drDx4| 2 d H y3 d H }W n
 tk
s4X |V  q   Y  W d S x| D ]
}|V  qJW d S )N	__aiter__)hasattrr	   )iterablerE   r   r   r   
auto_aiter   s    
"
rX   c               @   s*   e Zd Zd	ddZedd Zdd ZdS )
AsyncLoopContextNr   c             C   s&   t j| ||| || _|| _|| _d S )N)r   __init___async_iterator_after_length)r   async_iterator	undefinedafterlengthrecursedepth0r   r   r   rZ      s    zAsyncLoopContext.__init__c             C   s   | j d krtd| j S )NzHLoop length for some iterators cannot be lazily calculated in async mode)r]   	TypeError)r   r   r   r   ra      s    
zAsyncLoopContext.lengthc             C   s   t | S )N)AsyncLoopContextIterator)r   r   r   r   rU      s    zAsyncLoopContext.__aiter__)Nr   )__name__
__module____qualname__rZ   propertyra   rU   r   r   r   r   rY      s    
rY   c               @   s(   e Zd Zd	Zdd Zdd Zdd ZdS )
re   rD   c             C   s
   || _ d S )N)rD   )r   rD   r   r   r   rZ      s    z!AsyncLoopContextIterator.__init__c             C   s   | S )Nr   )r   r   r   r   rU      s    z"AsyncLoopContextIterator.__aiter__c                sp   | j }| jd7  _|jtkr$t |j|_|j|_y|jj I d H |_W n tk
rd   t|_Y nX |j|fS )N   )	rD   Zindex0r\   r   r	   _currentZ_beforer[   r    )r   r*   r   r   r   r       s    
z"AsyncLoopContextIterator.__anext__N)rD   )rf   rg   rh   	__slots__rZ   rU   r    r   r   r   r   re      s   re   c                s   yt | }W n8 ttfk
rD   t| ds<t| } t | }nd }Y nX t| }y|j I d H }W n tk
rx   t}Y nX t	||||||S )NrU   )
lenrd   AttributeErrorrV   tuplerX   r    r	   r   rY   )rW   r_   rb   rc   ra   r^   r`   r   r   r   make_async_loop_context   s    	



rp   )NFN)Nr   )#__doc__r   r$   rQ   	functoolsr   Zjinja2.utilsr   r   r   Zjinja2.environmentr   rJ   r   r   r   r   r(   r+   r.   r6   r;   r>   rA   r=   rG   rK   rO   rP   rT   rX   rY   objectre   rp   r   r   r   r   <module>   s4   		

	