U
    @ޫ^)                     @   s  d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ dd
l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d0d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d1d.d/Z e  dS )2zWThe code for async support. Importing this patches Jinja on supported
Python versions.
    Nupdate_wrapper)Markup   )TemplateModule)LoopContextconcatinternalcode)missingc                    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 7/tmp/pip-unpacked-wheel-o736f1kv/jinja2/asyncsupport.pycollect   s    zconcat_async.<locals>.collectr   )r   r   r   r   r   concat_async   s    r   c                 O  sZ   t ||}z*| | |2 z3 d H W }|V  q6 W n  tk
rT   | j V  Y nX d S r   )dictroot_render_funcnew_context	Exceptionenvironmenthandle_exception)selfargskwargsvarsr   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   generate/   s    z$wrap_generate_func.<locals>.generater   )r+   r,   r   r*   r   wrap_generate_func&   s    r-   c                    s^   | j jstdt||}| |}zt| |I d H W S  tk
rX   | j   Y S X d S )Nz8The environment was not created with async mode enabled.)	r   r'   RuntimeErrorr   r   r   r   r   r   )r   r   r   r    ctxr   r   r   render_async7   s    

r0   c                    s    fdd}t | S )Nc                    s2   | j js | f||S t }|| j||S r   )r   r'   r(   r)   r"   r0   )r   r   r   r%   original_renderr   r   renderE   s    z wrap_render_func.<locals>.renderr   )r2   r3   r   r1   r   wrap_render_funcD   s    r4   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   _stack_depth_contextZeval_ctx
autoescaper   r   r   r   r   r   
async_callO   s    
z-wrap_block_reference_call.<locals>.async_callc                    s   | j jjs| S  | S r   )r7   r   r'   r   r:   original_callr   r   __call__V   s    
z+wrap_block_reference_call.<locals>.__call__r   r   )r=   r>   r   r<   r   wrap_block_reference_callN   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   	argumentsr8   r   r   r   r   async_invoke`   s    z'wrap_macro_invoke.<locals>.async_invokec                    s    | j js| ||S  | ||S r   )Z_environmentr'   )r   rA   r8   rB   original_invoker   r   _invokeg   s    z"wrap_macro_invoke.<locals>._invoker?   )rD   rE   r   rC   r   wrap_macro_invoke_   s
    
rF   c                    s(   | j d k	r| j S |  I d H  | _ }|S r   )_modulemake_module_asyncr9   r   r   r   get_default_module_asyncp   s    
rI   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_moduley   s    z0wrap_default_module.<locals>._get_default_moduler
   )rK   rL   r   rJ   r   wrap_default_modulex   s    rM   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   rH      s
    rH   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) rR   r-   r,   r   r!   r0   r4   r3   rM   rL   rI   Z_get_default_module_asyncrH   rQ   r   r   r   patch_template   s     rT   c                  C   s,   ddl m} m} t| j| _t|j|_d S )Nr   BlockReferenceMacro)runtimerV   rW   r@   r>   rF   rE   rU   r   r   r   patch_runtime   s    rY   c                  C   s&   ddl m}  ddlm} | | d S )Nr   )FILTERS)ASYNC_FILTERS)filtersrZ   Zasyncfiltersr[   update)rZ   r[   r   r   r   patch_filters   s    r^   c                   C   s   t   t  t  d S r   )rT   rY   r^   r   r   r   r   	patch_all   s    r_   c                    s   t | r| I d H S | S r   )inspectisawaitable)valuer   r   r   
auto_await   s    

rc   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)iterablerP   r   r   r   
auto_aiter   s    

rg   c                   @   sh   e Zd ZeeZedd Zedd Zedd Z	dd Z
ed	d
 Zedd Zdd Zdd ZdS )AsyncLoopContextc                    sx   | j d k	r| j S zt| j| _ W nP tk
rp   dd | j2 I d H }| || _t|| j | jtk	 | _ Y nX | j S )Nc                    s   g | z3 d H W }|q6 S r   r   ).0xr   r   r   
<listcomp>   s     z+AsyncLoopContext.length.<locals>.<listcomp>)	_lengthlenZ	_iterable	TypeError	_iterator_to_iteratorindex_afterr   )r   rf   r   r   r   length   s    
 zAsyncLoopContext.lengthc                    s   | j I d H | j S r   )rs   rq   r;   r   r   r   	revindex0   s    zAsyncLoopContext.revindex0c                    s   | j I d H | j S r   )rs   index0r;   r   r   r   revindex   s    zAsyncLoopContext.revindexc                    sH   | j tk	r| j S z| j I d H | _ W n tk
r@   t| _ Y nX | j S r   )rr   r   ro   r#   r$   r;   r   r   r   
_peek_next   s    
zAsyncLoopContext._peek_nextc                    s   |   I d H tkS r   )rw   r   r;   r   r   r   last   s    zAsyncLoopContext.lastc                    s$   |   I d H }|tkr | dS |S )Nzthere is no next item)rw   r   
_undefinedr9   r   r   r   nextitem   s    
zAsyncLoopContext.nextitemc                 C   s   | S r   r   r;   r   r   r   rd      s    zAsyncLoopContext.__aiter__c                    sL   | j tk	r| j }t| _ n| j I d H }|  jd7  _| j| _|| _|| fS )Nr   )rr   r   ro   r#   ru   _currentZ_beforer9   r   r   r   r#      s    
zAsyncLoopContext.__anext__N)__name__
__module____qualname__staticmethodrg   rp   propertyrs   rt   rv   rw   rx   rz   rd   r#   r   r   r   r   rh      s   




rh   c                    s&   dd l }|jdtdd t| |||S )Nr   zRThis template must be recompiled with at least Jinja 2.11, or it will fail in 3.0.   )
stacklevel)warningswarnDeprecationWarningrh   )rf   Z	undefinedZrecurseZdepth0r   r   r   r   make_async_loop_context   s    r   )NFN)Nr   )!__doc__r(   r`   	functoolsr   Z
markupsafer   r   r   rX   r   utilsr	   r   r   r   r!   r-   r0   r4   r@   rF   rI   rM   rH   rT   rY   r^   r_   rc   rg   rh   r   r   r   r   r   <module>   s:   	



	A
