B
    DaY                 @   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:   x4 2 y3 d H } W n
 t k
s*X |  q   Y  W d S )N)StopAsyncIterationappend)event)	async_genrv 2lib/python3.7/site-packages/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 ||}yBx<| | |2 y3 d H }W n
 tk
s>X |V  q   Y  W W n tk
rh   t }Y nX d S | 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|| 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 ||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||}| |}yt| |I d H S  tk
rT   t	 }Y nX | 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||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   Z_stackZ_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)r/   r   r#   )r   )r1   original_callr   r   __call__Z   s    
z+wrap_block_reference_call.<locals>.__call__)r   r   )r2   r3   r   )r1   r2   r   wrap_block_reference_callR   s    r4   c                s(   t dd  t  fdd}t|S )Nc                s    | j | I d H }|rt|}|S )N)Z_funcr   )r   	argumentsr0   r   r   r   r   async_invoked   s    z'wrap_macro_invoke.<locals>.async_invokec                s    | j js| ||S  | ||S )N)Z_environmentr#   )r   r5   r0   )r6   original_invoker   r   _invokek   s    z"wrap_macro_invoke.<locals>._invoke)r   r   )r7   r8   r   )r6   r7   r   wrap_macro_invokec   s    r9   c                s(   | j d k	r| j S |  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    r?   Fc          	      sZ   |  |||}g }x:| |2 y3 d H }W n
 tk
sBX || q   Y  W t| ||S )N)r   r   r	   r
   r   )r   r   Z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jinja2rC   r(   r'   r   r   r+   r.   r-   r?   r>   r<   Z_get_default_module_asyncr;   )rC   r   r   r   patch_template   s    
rD   c              C   s,   ddl m} m} t| j| _t|j|_d S )Nr   )BlockReferenceMacro)jinja2.runtimerE   rF   r4   r3   r9   r8   )rE   rF   r   r   r   patch_runtime   s    
rH   c              C   s&   ddl m}  ddlm} | | d S )Nr   )FILTERS)ASYNC_FILTERS)Zjinja2.filtersrI   Zjinja2.asyncfiltersrJ   update)rI   rJ   r   r   r   patch_filters   s    rL   c               C   s   t   t  t  d S )N)rD   rH   rL   r   r   r   r   	patch_all   s    rM   c                s   t | r| I d H S | S )N)inspectZisawaitable)valuer   r   r   
auto_await   s    

rP   c          	   C  sX   t | dr@x0| 2 y3 d H }W n
 tk
s0X |V  q   Y  W d S x| D ]
}|V  qFW d S )N	__aiter__)hasattrr	   )iterablerB   r   r   r   
auto_aiter   s    

rT   c               @   s*   e Zd Zd	ddZedd Zdd ZdS )
AsyncLoopContextNr   c             C   s&   t | ||| || _|| _|| _d S )N)r   __init___async_iterator_after_length)r   async_iterator	undefinedafterlengthrecursedepth0r   r   r   rV      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)rY   	TypeError)r   r   r   r   r]      s    
zAsyncLoopContext.lengthc             C   s   t | S )N)AsyncLoopContextIterator)r   r   r   r   rQ      s    zAsyncLoopContext.__aiter__)Nr   )__name__
__module____qualname__rV   propertyr]   rQ   r   r   r   r   rU      s    
rU   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ra   )rA   c             C   s
   || _ d S )N)rA   )r   rA   r   r   r   rV      s    z!AsyncLoopContextIterator.__init__c             C   s   | S )Nr   )r   r   r   r   rQ      s    z"AsyncLoopContextIterator.__aiter__c                sp   | j }| jd7  _|jtkr$t |j|_|j|_y|j I d H |_W n tk
rd   t|_Y nX |j|fS )N   )	rA   Zindex0rX   r   r	   Z_currentZ_beforerW   r    )r   r*   r   r   r   r       s    
z"AsyncLoopContextIterator.__anext__N)rb   rc   rd   	__slots__rV   rQ   r    r   r   r   r   ra      s   ra   c          	      s   yt | }W n8 ttfk
rD   t| ds<t| } t | }nd }Y nX t| }y| I d H }W n tk
rx   t}Y nX t	||||||S )NrQ   )
lenr`   AttributeErrorrR   tuplerT   r    r	   r   rU   )rS   r[   r^   r_   r]   rZ   r\   r   r   r   make_async_loop_context   s    	



rk   )NFN)Nr   )#__doc__r   r$   rN   	functoolsr   Zjinja2.utilsr   r   r   Zjinja2.environmentr   rG   r   r   r   r   r(   r+   r.   r4   r9   r<   r?   r;   rD   rH   rL   rM   rP   rT   rU   objectra   rk   r   r   r   r   <module>   s4   		

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