o
    j(                     @   s   d Z ddlZddlZeh dZedhZeh dZeh dZdddZG d	d
 d
Z	G dd dZ
G dd dZG dd dZdd Zdd Zdd ZdS )a/  
Async wrappers for py-lmdb via :func:`asyncio.loop.run_in_executor`.

Usage::

    import lmdb
    import lmdb.aio

    env = lmdb.open('/tmp/mydb')
    aenv = lmdb.aio.wrap(env)

    async with aenv.begin(write=True) as txn:
        await txn.put(b'key', b'value')
        val = await txn.get(b'key')
    N>   Zmax_readerspathZmax_key_sizeflagsid>   valuekeyitem>   iterprev_dupiterprev_nodupiternextiternext_nodupiternext_dupiterprevc                 C   s
   t | |S )zWrap an :class:`lmdb.Environment` for async use.

    *executor* is passed to :meth:`loop.run_in_executor`.  ``None`` (the
    default) uses the loop's default executor.
    )AsyncEnvironment)envexecutor r   I/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/lmdb/aio.pywrap6   s   
r   c                   @   s4   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZdS )_AsyncContextWrapperzWraps a coroutine so it can be used as both ``await`` and ``async with``.

    Supports::

        txn = await aenv.begin(write=True)      # just await
        async with aenv.begin(write=True) as txn:  # context manager
    _coro_resultc                 C   s   || _ d | _d S Nr   )selfcoror   r   r   __init__J      
z_AsyncContextWrapper.__init__c                 C   s
   | j  S r   )r   	__await__r   r   r   r   r   N   s   
z_AsyncContextWrapper.__await__c                    s   | j I d H | _| jS r   r   r   r   r   r   
__aenter__Q   s   z_AsyncContextWrapper.__aenter__c                    s   | j |||I d H S r   )r   	__aexit__)r   exc_typeexc_valexc_tbr   r   r   r    U   s   z_AsyncContextWrapper.__aexit__N)	__name__
__module____qualname____doc__	__slots__r   r   r   r    r   r   r   r   r   ?   s    r   c                   @   >   e Zd ZdZdZdddZdd Zdd	 Zd
d Zdd Z	dS )r   u  Async wrapper for :py:class:`lmdb.Environment`.

    Created by :py:func:`wrap`.  All methods of the underlying
    :py:class:`~lmdb.Environment` are available and are dispatched to an
    executor, except for the low-overhead accessors ``path()``,
    ``max_key_size()``, ``max_readers()``, and ``flags()``, which are called
    directly.

    Supports ``async with`` for lifetime management — the environment is
    closed on exit.
    _env	_executorNc                 C   s   || _ || _d S r   r*   )r   r   r   r   r   r   r   h   r   zAsyncEnvironment.__init__c                       | S r   r   r   r   r   r   r   n      zAsyncEnvironment.__aenter__c                       | j   d S r   )r+   closer   Z_excr   r   r   r    q      zAsyncEnvironment.__aexit__c                        fdd}t | S )a4  Start a new transaction, returning an :py:class:`AsyncTransaction`.

        Accepts the same arguments as :py:meth:`lmdb.Environment.begin`.
        Can be used with ``await`` or ``async with``::

            async with aenv.begin(write=True) as txn:
                await txn.put(b'key', b'value')
        c                     sB   t  } | jtjjjg R i I d H }t|jS r   )	asyncioget_running_looprun_in_executorr,   	functoolspartialr+   beginAsyncTransaction)looptxnargskwargsr   r   r   _begin   s   
z&AsyncEnvironment.begin.<locals>._beginr   )r   r>   r?   r@   r   r=   r   r9   v   s   	
zAsyncEnvironment.beginc                 C   s0   t | j|}t|s|S |tv r|S t|| jS r   )getattrr+   callable	_ENV_SYNC_async_methodr,   r   nameattrr   r   r   __getattr__   s   zAsyncEnvironment.__getattr__r   )
r$   r%   r&   r'   r(   r   r   r    r9   rI   r   r   r   r   r   Y   s    
r   c                   @   r)   )r:   u  Async wrapper for :py:class:`lmdb.Transaction`.

    All methods of the underlying :py:class:`~lmdb.Transaction` are available.
    Most are dispatched to an executor; ``id()`` is called directly.

    An :py:class:`asyncio.Lock` serializes all operations dispatched through
    this transaction, including operations on its cursors.  This makes
    :py:func:`asyncio.gather` safe on the same transaction.

    Supports ``async with`` — write transactions are committed on clean exit
    and aborted on exception.
    )_txnr,   _lockNc                 C   s   || _ || _t | _d S r   )rJ   r,   r4   LockrK   )r   r<   r   r   r   r   r      s   zAsyncTransaction.__init__c                    r-   r   r   r   r   r   r   r      r.   zAsyncTransaction.__aenter__c              	      s   | j 4 I d H / |r| j  nt }|| j| jjI d H  W d   I d H  d S W d   I d H  d S 1 I d H s=w   Y  d S r   )rK   rJ   abortr4   r5   r6   r,   commit)r   r!   Z_exc_valZ_exc_tbr;   r   r   r   r       s   .zAsyncTransaction.__aexit__c                    r3   )a;  Open a cursor, returning an :py:class:`AsyncCursor`.

        Accepts the same arguments as :py:meth:`lmdb.Transaction.cursor`.
        Can be used with ``await`` or ``async with``::

            async with txn.cursor() as cur:
                await cur.first()
                items = await cur.iternext()
        c               	      s   j 4 I d H % t } | jtjjjg R i I d H }W d   I d H  n1 I d H s3w   Y  t	|jj S r   )
rK   r4   r5   r6   r,   r7   r8   rJ   cursorAsyncCursor)r;   curr=   r   r   _cursor   s   (z(AsyncTransaction.cursor.<locals>._cursorrA   )r   r>   r?   rR   r   r=   r   rO      s   

zAsyncTransaction.cursorc                 C   s4   t | j|}t|s|S |tv r|S t|| j| jS r   )rB   rJ   rC   	_TXN_SYNC_async_method_lockedr,   rK   rF   r   r   r   rI      s   zAsyncTransaction.__getattr__r   )
r$   r%   r&   r'   r(   r   r   r    rO   rI   r   r   r   r   r:      s    

r:   c                   @   sf   e Zd ZdZdZdddZdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )rP   u  Async wrapper for :py:class:`lmdb.Cursor`.

    All methods of the underlying :py:class:`~lmdb.Cursor` are available.
    Most are dispatched to an executor; ``key()``, ``value()``, and
    ``item()`` are called directly.

    Iterator methods (``iternext()``, ``iterprev()``, etc.) are consumed in
    the executor and returned as a list.

    Shares the parent transaction's :py:class:`asyncio.Lock`.

    Supports ``async with`` — the cursor is closed on exit.
    )rR   r,   rK   Nc                 C   s   || _ || _|pt | _d S r   )rR   r,   r4   rL   rK   )r   rO   r   lockr   r   r   r      s   zAsyncCursor.__init__c                    r-   r   r   r   r   r   r   r      r.   zAsyncCursor.__aenter__c                    r/   r   )rR   r0   r1   r   r   r   r       r2   zAsyncCursor.__aexit__c                       t | jj|| j| jI d H S r   )_collect_lockedrR   r
   r,   rK   r   kwr   r   r   r
         
zAsyncCursor.iternextc                    rV   r   )rW   rR   r   r,   rK   rX   r   r   r   r      rZ   zAsyncCursor.iternext_dupc                    rV   r   )rW   rR   r   r,   rK   rX   r   r   r   r      rZ   zAsyncCursor.iternext_nodupc                    rV   r   )rW   rR   r   r,   rK   rX   r   r   r   r     rZ   zAsyncCursor.iterprevc                    rV   r   )rW   rR   r   r,   rK   rX   r   r   r   r     rZ   zAsyncCursor.iterprev_dupc                    rV   r   )rW   rR   r	   r,   rK   rX   r   r   r   r	   
  rZ   zAsyncCursor.iterprev_nodupc                 C   sR   t | j|}t|s|S |tv r|S |tv r!tt|i | j| j	S t
|| j| j	S r   )rB   rR   rC   _CURSOR_SYNC_CURSOR_ITERSr7   r8   rW   r,   rK   rT   rF   r   r   r   rI     s   zAsyncCursor.__getattr__)NN)r$   r%   r&   r'   r(   r   r   r    r
   r   r   r   r   r	   rI   r   r   r   r   rP      s    
rP   c                    s   t  fdd}|S )z>Return a coroutine function that calls *method* in *executor*.c                     s0   t  }| tjg| R i |I d H S r   r4   r5   r6   r7   r8   r>   r?   r;   r   methodr   r   wrapper"  s   
z_async_method.<locals>.wrapperr7   wraps)r`   r   ra   r   r_   r   rE      s   rE   c                    s   t  fdd}|S )z6Like :func:`_async_method`, but acquires *lock* first.c               	      sl   4 I d H " t  }| tjg| R i |I d H W  d   I d H  S 1 I d H s/w   Y  d S r   r]   r^   r   rU   r`   r   r   ra   .  s   0z%_async_method_locked.<locals>.wrapperrb   )r`   r   rU   ra   r   rd   r   rT   ,  s   rT   c              	      sf   t  } fdd}|4 I dH  |||I dH W  d  I dH  S 1 I dH s,w   Y  dS )z:Call an iterator method in the executor and return a list.c                      s   t  di S )Nr   )listr   iter_methodr?   r   r   _consume=  s   z!_collect_locked.<locals>._consumeN)r4   r5   r6   )rg   r?   r   rU   r;   rh   r   rf   r   rW   9  s   0rW   r   )r'   r4   r7   	frozensetrD   rS   r[   r\   r   r   r   r:   rP   rE   rT   rW   r   r   r   r   <module>   s   

	:CJ