o
    #jO                     @  s   d Z ddlm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 e
rBddlZdd	lmZmZ dd
lmZ ddlmZ G dd dZG dd dZddgZdS )zEAsync wrapper around :class:`ReadWriteLock` for use with ``asyncio``.    )annotationsN)ThreadPoolExecutor)asynccontextmanager)TYPE_CHECKING   )ReadWriteLock)AsyncGeneratorCallable)futures)TracebackTypec                   @  s.   e Zd ZdZdddZddd	ZdddZdS ) AsyncAcquireReadWriteReturnProxyzEContext-aware object that releases the async read/write lock on exit.lockAsyncReadWriteLockreturnNonec                 C  s
   || _ d S Nr   )selfr    r   [/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/filelock/_async_read_write.py__init__   s   
z)AsyncAcquireReadWriteReturnProxy.__init__c                   s   | j S r   r   r   r   r   r   
__aenter__   s   z+AsyncAcquireReadWriteReturnProxy.__aenter__exc_typetype[BaseException] | None	exc_valueBaseException | None	tracebackTracebackType | Nonec                   s   | j  I d H  d S r   )r   release)r   r   r   r   r   r   r   	__aexit__   s   z*AsyncAcquireReadWriteReturnProxy.__aexit__N)r   r   r   r   )r   r   )r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__r   r   r    r   r   r   r   r      s
    

r   c                   @  s   e Zd ZdZ	d;dddddd<ddZed=ddZed>ddZed?ddZed@ddZ	edAddZ
dBd%d&Zd;dd'dCd)d*Zd;dd'dCd+d,Zd-d.dDd0d1ZedEdd'dFd5d6ZedEdd'dFd7d8ZdGd9d:ZdS )Hr   a  
    Async wrapper around :class:`ReadWriteLock` for use in ``asyncio`` applications.

    Because Python's :mod:`sqlite3` module has no async API, all blocking SQLite operations are dispatched to a thread
    pool via ``loop.run_in_executor()``. Reentrancy, upgrade/downgrade rules, and singleton behavior are delegated
    to the underlying :class:`ReadWriteLock`.

    :param lock_file: path to the SQLite database file used as the lock
    :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
    :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately when the lock is unavailable
    :param is_singleton: if ``True``, reuse existing :class:`ReadWriteLock` instances for the same resolved path
    :param loop: event loop for ``run_in_executor``; ``None`` uses the running loop
    :param executor: executor for ``run_in_executor``; ``None`` uses the default executor

    .. versionadded:: 3.21.0

    TN)blockingis_singletonloopexecutor	lock_filestr | os.PathLike[str]timeoutfloatr&   boolr'   r(    asyncio.AbstractEventLoop | Noner)   futures.Executor | Noner   r   c                C  s6   t ||||d| _|| _|d u | _|ptdd| _d S )N)r&   r'   r   )max_workers)r   _lock_loop_owns_executorr   	_executor)r   r*   r,   r&   r'   r(   r)   r   r   r   r   9   s   

zAsyncReadWriteLock.__init__strc                 C     | j jS )z$:returns: the path to the lock file.)r2   r*   r   r   r   r   r*   H      zAsyncReadWriteLock.lock_filec                 C  r7   )z:returns: the default timeout.)r2   r,   r   r   r   r   r,   M   r8   zAsyncReadWriteLock.timeoutc                 C  r7   )z1:returns: whether blocking is enabled by default.)r2   r&   r   r   r   r   r&   R   r8   zAsyncReadWriteLock.blockingc                 C     | j S )z<:returns: the event loop (or ``None`` for the running loop).)r3   r   r   r   r   r(   W      zAsyncReadWriteLock.loopc                 C  r9   )z5:returns: the executor (or ``None`` for the default).)r5   r   r   r   r   r)   \   r:   zAsyncReadWriteLock.executorfuncCallable[..., object]argsobjectkwargsc                   s8   | j pt }|| jtj|g|R i |I d H S r   )r3   asyncioZget_running_loopZrun_in_executorr5   	functoolspartial)r   r;   r=   r?   r(   r   r   r   _runa   s   (zAsyncReadWriteLock._runr&   r   c                  &   | j | jj||dI dH  t| dS )a7  
        Acquire a shared read lock.

        See :meth:`ReadWriteLock.acquire_read` for full semantics.

        :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately when the lock is unavailable

        :returns: a proxy that can be used as an async context manager to release the lock

        :raises RuntimeError: if a write lock is already held on this instance
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        rD   Nr   )rC   r2   acquire_readr   r   r,   r&   r   r   r   rF   e      
zAsyncReadWriteLock.acquire_readc                  rE   )aZ  
        Acquire an exclusive write lock.

        See :meth:`ReadWriteLock.acquire_write` for full semantics.

        :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately when the lock is unavailable

        :returns: a proxy that can be used as an async context manager to release the lock

        :raises RuntimeError: if a read lock is already held, or a write lock is held by a different thread
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        rD   Nr   )rC   r2   acquire_writer   rG   r   r   r   rI   w   rH   z AsyncReadWriteLock.acquire_writeFforcerK   c                  s   | j | jj|dI dH  dS )a2  
        Release one level of the current lock.

        See :meth:`ReadWriteLock.release` for full semantics.

        :param force: if ``True``, release the lock completely regardless of the current lock level

        :raises RuntimeError: if no lock is currently held and *force* is ``False``

        rJ   N)rC   r2   r   )r   rK   r   r   r   r      s   zAsyncReadWriteLock.releasefloat | Nonebool | NoneAsyncGenerator[None]c                C b   |du r	| j j}|du r| j j}| j||dI dH  zdV  W |  I dH  dS |  I dH  w )a  
        Async context manager that acquires and releases a shared read lock.

        Falls back to instance defaults for *timeout* and *blocking* when ``None``.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately; ``None`` uses the instance default

        NrD   )r2   r,   r&   rF   r   rG   r   r   r   	read_lock      "zAsyncReadWriteLock.read_lockc                C rO   )a  
        Async context manager that acquires and releases an exclusive write lock.

        Falls back to instance defaults for *timeout* and *blocking* when ``None``.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately; ``None`` uses the instance default

        NrD   )r2   r,   r&   rI   r   rG   r   r   r   
write_lock   rQ   zAsyncReadWriteLock.write_lockc                   s2   |  | jjI dH  | jr| jjdd dS dS )z
        Release the lock (if held) and close the underlying SQLite connection.

        After calling this method, the lock instance is no longer usable.

        NF)wait)rC   r2   closer4   r5   shutdownr   r   r   r   rT      s
   zAsyncReadWriteLock.close)r%   )r*   r+   r,   r-   r&   r.   r'   r.   r(   r/   r)   r0   r   r   )r   r6   )r   r-   )r   r.   )r   r/   )r   r0   )r;   r<   r=   r>   r?   r>   r   r>   )r,   r-   r&   r.   r   r   )rK   r.   r   r   r   )r,   rL   r&   rM   r   rN   )r   r   )r!   r"   r#   r$   r   propertyr*   r,   r&   r(   r)   rC   rF   rI   r   r   rP   rR   rT   r   r   r   r   r   &   s8    
r   )r$   
__future__r   r@   rA   concurrent.futuresr   
contextlibr   typingr   Z_read_writer   oscollections.abcr   r	   Z
concurrentr
   typesr   r   r   __all__r   r   r   r   <module>   s&     (