U
    sݨ\                     @   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"    2 z3 d H W }  |  q6 d S N)append)event	async_genrv 5/usr/lib/python3/dist-packages/jinja2/asyncsupport.pycollect   s    zconcat_async.<locals>.collect)r   )r   r   r   r   r   concat_async   s    r   c                 O  sj   t ||}z*| | |2 z3 d H W }|V  q6 W n tk
rP   t }Y nX d S | j|dV  d S )NT)dictroot_render_funcnew_context	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 ||}z|| V  qW n tk
r6   Y nX d S r
   )r    run_until_complete	__anext__StopAsyncIteration)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 r
   )r   is_asyncasyncioget_event_loop)r   r   r   r%   original_generater   r   generate3   s    z$wrap_generate_func.<locals>.generater   )r*   r+   r   r)   r   wrap_generate_func+   s    r,   c                    sf   | j jstdt||}| |}zt| |I d H W S  tk
rV   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 r
   )r   r&   r'   r(   r!   r/   )r   r   r   r$   original_renderr   r   renderJ   s    z wrap_render_func.<locals>.renderr   )r1   r2   r   r0   r   wrap_render_funcI   s    r3   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 r
   )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 r
   )r4   r   r&   r   r7   original_callr   r   __call__Z   s    
z+wrap_block_reference_call.<locals>.__call__r   r   )r:   r;   r   r9   r   wrap_block_reference_callR   s
    
r=   c                    s(   t dd  t  fdd}t|S )Nc                    s    | j | I d H }|rt|}|S r
   )Z_funcr   )r   	argumentsr5   r   r   r   r   async_invoked   s    z'wrap_macro_invoke.<locals>.async_invokec                    s    | j js| ||S  | ||S r
   )Z_environmentr&   )r   r>   r5   r?   original_invoker   r   _invokek   s    z"wrap_macro_invoke.<locals>._invoker<   )rA   rB   r   r@   r   wrap_macro_invokec   s
    
rC   c                    s(   | j d k	r| j S |  I d H  | _ }|S r
   )_modulemake_module_asyncr6   r   r   r   get_default_module_asyncs   s    
rF   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-   r8   original_default_moduler   r   _get_default_module|   s    z0wrap_default_module.<locals>._get_default_module)r   )rH   rI   r   rG   r   wrap_default_module{   s    rJ   Fc                    sB   |  |||}g }| |2 z3 d H W }|| q6 t| ||S r
   )r   r   r   r   )r   r   ZsharedlocalscontextZbody_streamitemr   r   r   rE      s
    rE   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jinja2rO   r,   r+   r   r    r/   r3   r2   rJ   rI   rF   Z_get_default_module_asyncrE   rN   r   r   r   patch_template   s&       rP   c                  C   s,   ddl m} m} t| j| _t|j|_d S )Nr   BlockReferenceMacro)jinja2.runtimerR   rS   r=   r;   rC   rB   rQ   r   r   r   patch_runtime   s
    rU   c                  C   s&   ddl m}  ddlm} | | d S )Nr   )FILTERS)ASYNC_FILTERS)Zjinja2.filtersrV   Zjinja2.asyncfiltersrW   update)rV   rW   r   r   r   patch_filters   s    rY   c                   C   s   t   t  t  d S r
   )rP   rU   rY   r   r   r   r   	patch_all   s    rZ   c                    s   t | r| I d H S | S r
   )inspectZisawaitable)valuer   r   r   
auto_await   s    

r]   c                 C  s<   t | dr(| 2 z3 d H W }|V  q6 d S | D ]
}|V  q,d S N	__aiter__)hasattr)iterablerM   r   r   r   
auto_aiter   s    

rb   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 r
   )r   __init___async_iterator_after_length)r   async_iterator	undefinedafterlengthrecursedepth0r   r   r   rd      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)rg   	TypeErrorr8   r   r   r   rk      s    
zAsyncLoopContext.lengthc                 C   s   t | S r
   )AsyncLoopContextIteratorr8   r   r   r   r_      s    zAsyncLoopContext.__aiter__)Nr   )__name__
__module____qualname__rd   propertyrk   r_   r   r   r   r   rc      s     

rc   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ro   rL   c                 C   s
   || _ d S r
   rt   )r   rL   r   r   r   rd      s    z!AsyncLoopContextIterator.__init__c                 C   s   | S r
   r   r8   r   r   r   r_      s    z"AsyncLoopContextIterator.__aiter__c                    sp   | j }| jd7  _|jtkr$t |j|_|j|_z|j I d H |_W n tk
rd   t|_Y nX |j|fS )N   )	rL   Zindex0rf   r	   r#   Z_currentZ_beforere   r"   )r   r.   r   r   r   r"      s    
z"AsyncLoopContextIterator.__anext__N)rp   rq   rr   	__slots__rd   r_   r"   r   r   r   r   ro      s   ro   c              	      s   zt | }W n8 ttfk
rD   t| ds<t| } t | }nd }Y nX t| }z| I d H }W n tk
rx   t}Y nX t	||||||S r^   )
lenrn   AttributeErrorr`   tuplerb   r"   r#   r	   rc   )ra   ri   rl   rm   rk   rh   rj   r   r   r   make_async_loop_context   s    	



rz   )NFN)Nr   )#__doc__r   r'   r[   	functoolsr   Zjinja2.utilsr   r   r   Zjinja2.environmentr   rT   r   r	   r   r    r,   r/   r3   r=   rC   rF   rJ   rE   rP   rU   rY   rZ   r]   rb   rc   objectro   rz   r   r   r   r   <module>   s6   
		


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