a
    •˜ëj“'  ã                   @  s®  d dl mZ d dl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
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lm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 eded�Zededd�Zeded�Zdddœd d!„Z G d"d#„ d#ee ƒZ!G d$d%„ d%e	e ej"ƒZ#G d&d'„ d'e#e ƒZ$d(d)d*œd+d,„Z%G d-d.„ d.e!e ƒZ&dS )/é    )ÚannotationsN)ÚAny)ÚAsyncGenerator)ÚAsyncIterator)Ú	Awaitable)ÚCallable)ÚClassVar)Ú	Coroutine)ÚDict)Ú	Generator)ÚGeneric)ÚNoReturn)ÚOptional)Úoverload)ÚTuple)ÚTypeVaré   )Úexcé   )Úutil)ÚLiteral)ÚSelfÚ_T)ÚboundÚ_T_coT)r   Ú	covariantÚ_PTzCoroutine[Any, Any, _T])ÚcoroÚreturnc              
   Ã  sl   t  | ¡}d}zt  |¡I dH }W q\ t jyV } z| ¡ r>‚ |}W Y d}~qd}~0 0 q\q|durh|‚|S )ab  Run ``coro`` in a task of its own and wait for it to complete, even
    if the calling task is cancelled meanwhile.

    Used for cleanup, such as the ``close()`` performed by an async context
    manager on exit, that must not be abandoned partway through.  A
    cancellation received while waiting is re-raised once ``coro`` has
    completed, so that the caller does not return before its cleanup has
    finished; were it to return early, the cleanup would carry on in the
    background, where for example it could return a connection into a pool
    that the application has meanwhile disposed of.

    N)ÚasyncioZcreate_taskZshieldZCancelledErrorÚdone)r   ZtaskZ	cancelledÚresultÚerr© r#   úZ/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/sqlalchemy/ext/asyncio/base.pyÚ_run_to_completion*   s    
r%   c                   @  sÚ   e Zd ZU i Zded< dZedddœdd„ƒZedddœd	d„ƒZd
d
dœdd„Zed!ddddœdd„ƒZ	eddddœdd„ƒZ
eed"dddddœdd„ƒƒZeed#dddddœdd„ƒƒZed$dddddœd d„ƒZdS )%ÚReversibleProxyzCClassVar[Dict[weakref.ref[Any], weakref.ref[ReversibleProxy[Any]]]]Ú_proxy_objects)Ú__weakref__r   )Útargetr   c                 C  s   d S ©Nr#   ©Úselfr)   r#   r#   r$   Ú_assign_proxiedM   s    zReversibleProxy._assign_proxiedÚNonec                 C  s   d S r*   r#   r+   r#   r#   r$   r-   P   s    zOptional[_PT]c                 C  s:   |d ur6t  |tj¡}t  | t tj|¡¡}|tj|< |S r*   )ÚweakrefÚrefr&   Ú_target_gcedÚ	functoolsÚpartialr'   )r,   r)   Z
target_refÚ	proxy_refr#   r#   r$   r-   S   s    ÿþ
Nzweakref.ref[_PT]zOptional[weakref.ref[Self]])r0   r4   r   c                 C  s   | j  |d ¡ d S r*   )r'   Úpop)Úclsr0   r4   r#   r#   r$   r1   `   s    zReversibleProxy._target_gcedr   r   )r)   Úadditional_kwr   c                 K  s
   t ƒ ‚d S r*   ©ÚNotImplementedError)r6   r)   r7   r#   r#   r$   Ú_regenerate_proxy_for_targeth   s    z,ReversibleProxy._regenerate_proxy_for_target.zLiteral[True])r)   Ú
regenerater7   r   c                 K  s   d S r*   r#   ©r6   r)   r;   r7   r#   r#   r$   Ú_retrieve_proxy_for_targetn   s    z*ReversibleProxy._retrieve_proxy_for_targetTÚboolzOptional[Self]c                 K  s   d S r*   r#   r<   r#   r#   r$   r=   t   s    c                 K  sX   z| j t |¡ }W n ty&   Y n0 |ƒ }|d ur:|S |rP| j|fi |¤ŽS d S d S r*   )r'   r/   r0   ÚKeyErrorr:   )r6   r)   r;   r7   r4   Úproxyr#   r#   r$   r=   z   s    )N).)T)T)Ú__name__Ú
__module__Ú__qualname__r'   Ú__annotations__Ú	__slots__r   r-   Úclassmethodr1   r:   r=   r#   r#   r#   r$   r&   G   s0   
þ
 ý ÿ ÿ ÿr&   c                   @  sl   e Zd ZdZejddddœdd„ƒZdd	œd
d„Zdd	œdd„Zejdddddœdd„ƒZ	dd	œdd„Z
dS )ÚStartableContextr#   Fr>   r   ©Úis_ctxmanagerr   c                 Ã  s
   t ƒ ‚d S r*   r8   )r,   rI   r#   r#   r$   Ústart�   s    zStartableContext.startzGenerator[Any, Any, _T_co]©r   c                 C  s   |   ¡  ¡ S r*   )rJ   Ú	__await__©r,   r#   r#   r$   rL   ”   s    zStartableContext.__await__c                 Ã  s   | j dd�I d H S )NT)rI   )rJ   rM   r#   r#   r$   Ú
__aenter__—   s    zStartableContext.__aenter__r   úOptional[bool])Útype_ÚvalueÚ	tracebackr   c                 Ã  s   d S r*   r#   )r,   rP   rQ   rR   r#   r#   r$   Ú	__aexit__š   s    zStartableContext.__aexit__r   c                 C  s   t  d| jj ¡‚d S )Nz@%s context has not been started and object has not been awaited.)Ú	async_excZAsyncContextNotStartedÚ	__class__rA   rM   r#   r#   r$   Ú_raise_for_not_started    s
    ÿÿz'StartableContext._raise_for_not_startedN)F)rA   rB   rC   rE   ÚabcÚabstractmethodrJ   rL   rN   rS   rV   r#   r#   r#   r$   rG   �   s   rG   c                   @  sR   e Zd ZU dZded< ddddœdd	„Zddddœdd„Zdddddœdd„ZdS )ÚGeneratorStartableContext©ÚgenzAsyncGenerator[_T_co, Any]r[   ú#Callable[..., AsyncIterator[_T_co]]zTuple[Any, ...]zDict[str, Any])ÚfuncÚargsÚkwdsc                 C  s   ||i |¤Ž| _ d S r*   rZ   )r,   r]   r^   r_   r#   r#   r$   Ú__init__¬   s    z"GeneratorStartableContext.__init__Fr>   r   rH   c                 Ã  sL   zt  | j¡I d H }W n ty2   tdƒd ‚Y n0 |sH| j ¡ I d H  |S )Nzgenerator didn't yield)r   Úanext_r[   ÚStopAsyncIterationÚRuntimeErrorÚaclose)r,   rI   Zstart_valuer#   r#   r$   rJ   ´   s    zGeneratorStartableContext.startr   rO   )ÚtyprQ   rR   r   c              
   Ã  s2  |d u r@zt  | j¡I d H  W n ty2   Y dS 0 tdƒ‚� nî|d u rN|ƒ }z| j |¡I d H  W nÀ ty� } z||uW  Y d }~S d }~0  t� yò } zJ||u r¶W Y d }~dS t|ttfƒrÜ|j|u rÜW Y d }~dS ‚ W Y d }~n<d }~0  t	�y$ } z||u�r‚ W Y d }~dS d }~0 0 tdƒ‚d S )NFzgenerator didn't stopz$generator didn't stop after athrow())
r   ra   r[   rb   rc   ÚathrowÚ
isinstanceÚStopIterationÚ	__cause__ÚBaseException)r,   re   rQ   rR   r   r#   r#   r$   rS   Ã   s4    	ÿþ
z#GeneratorStartableContext.__aexit__N)F)rA   rB   rC   rE   rD   r`   rJ   rS   r#   r#   r#   r$   rY   §   s
   
rY   r\   z/Callable[..., GeneratorStartableContext[_T_co]])r]   r   c                   s$   t  ˆ ¡ddddœ‡ fdd„ƒ}|S )a=  @asyncstartablecontext decorator.

    the decorated function can be called either as ``async with fn()``, **or**
    ``await fn()``.   This is decidedly different from what
    ``@contextlib.asynccontextmanager`` supports, and the usage pattern
    is different as well.

    Typical usage:

    .. sourcecode:: text

        @asyncstartablecontext
        async def some_async_generator(<arguments>):
            <setup>
            try:
                yield <value>
            except GeneratorExit:
                # return value was awaited, no context manager is present
                # and caller will .close() the resource explicitly
                pass
            else:
                <context manager cleanup>


    Above, ``GeneratorExit`` is caught if the function were used as an
    ``await``.  In this case, it's essential that the cleanup does **not**
    occur, so there should not be a ``finally`` block.

    If ``GeneratorExit`` is not invoked, this means we're in ``__aexit__``
    and we were invoked as a context manager, and cleanup should proceed.


    r   z GeneratorStartableContext[_T_co])r^   r_   r   c                    s   t ˆ | |ƒS r*   )rY   )r^   r_   ©r]   r#   r$   Úhelper  s    z%asyncstartablecontext.<locals>.helper)r2   Úwraps)r]   rl   r#   rk   r$   Úasyncstartablecontextø   s    %rn   c                   @  sR   e Zd ZdZejddœdd„ƒZddœdd„Zd	d
dœdd„Zd	d
dœdd„Z	dS )ÚProxyComparabler#   r   rK   c                 C  s
   t ƒ ‚d S r*   r8   rM   r#   r#   r$   Ú_proxied'  s    zProxyComparable._proxiedÚintc                 C  s   t | ƒS r*   )ÚidrM   r#   r#   r$   Ú__hash__+  s    zProxyComparable.__hash__r   r>   )Úotherr   c                 C  s   t || jƒo| j|jkS r*   ©rg   rU   rp   ©r,   rt   r#   r#   r$   Ú__eq__.  s    
þzProxyComparable.__eq__c                 C  s   t || jƒ p| j|jkS r*   ru   rv   r#   r#   r$   Ú__ne__4  s    
þzProxyComparable.__ne__N)
rA   rB   rC   rE   r   Zro_non_memoized_propertyrp   rs   rw   rx   r#   r#   r#   r$   ro   $  s   ro   )'Ú
__future__r   rW   r   r2   Útypingr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r/   Ú r   rT   r   Zutil.typingr   r   r   r   r   r%   r&   ÚABCrG   rY   rn   ro   r#   r#   r#   r$   Ú<module>   s@   FQ,