o
    2j;!                     @  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   U/var/www/html/whisper/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dAddZedBddZedCddZ	edDdd Z
dEd&d'Zd>dd(dFd*d+Zd>dd(dFd,d-Zd.d/dGd1d2ZedHdd(dId6d7ZedHdd(dId8d9ZdJd:d;ZdJd<d=ZdS )Kr   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``. When ``None`` a dedicated single-thread executor is created
        and owned by this lock, ensuring every operation runs on the same thread (required for SQLite affinity); it
        is shut down by :meth:`close`. A caller-supplied executor is used as-is and never shut down here, so when no
        executor is passed remember to call :meth:`close` to release the owned one.

    .. 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   <   s   

zAsyncReadWriteLock.__init__strc                 C     | j jS )z$:returns: the path to the lock file.)r2   r*   r   r   r   r   r*   K      zAsyncReadWriteLock.lock_filec                 C  r7   )z:returns: the default timeout.)r2   r,   r   r   r   r   r,   P   r8   zAsyncReadWriteLock.timeoutc                 C  r7   )z1:returns: whether blocking is enabled by default.)r2   r&   r   r   r   r   r&   U   r8   zAsyncReadWriteLock.blockingc                 C     | j S )z<:returns: the event loop (or ``None`` for the running loop).)r3   r   r   r   r   r(   Z      zAsyncReadWriteLock.loopfutures.Executorc                 C  r9   )zi:returns: the executor used for ``run_in_executor`` (a dedicated single-thread one if none was supplied).)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   asyncioget_running_looprun_in_executorr5   	functoolspartial)r   r<   r>   r@   r(   r   r   r   _rund   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

        rG   Nr   )rF   r2   acquire_readr   r   r,   r&   r   r   r   rI   h      
zAsyncReadWriteLock.acquire_readc                  rH   )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

        rG   Nr   )rF   r2   acquire_writer   rJ   r   r   r   rL   z   rK   z AsyncReadWriteLock.acquire_writeFforcerN   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``

        rM   N)rF   r2   r   )r   rN   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

        NrG   )r2   r,   r&   rI   r   rJ   r   r   r   	read_lock      "zAsyncReadWriteLock.read_lockc                C rR   )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

        NrG   )r2   r,   r&   rL   r   rJ   r   r   r   
write_lock   rT   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)rF   r2   closer4   r5   shutdownr   r   r   r   rX      s
   zAsyncReadWriteLock.closec                 C  s"   t | ddr| jjdd d S d S )Nr4   FrV   )getattrr5   rY   r   r   r   r   __del__   s   zAsyncReadWriteLock.__del__)r%   )r*   r+   r,   r-   r&   r.   r'   r.   r(   r/   r)   r0   r   r   )r   r6   )r   r-   )r   r.   )r   r/   )r   r;   )r<   r=   r>   r?   r@   r?   r   r?   )r,   r-   r&   r.   r   r   )rN   r.   r   r   r   )r,   rO   r&   rP   r   rQ   )r   r   )r!   r"   r#   r$   r   propertyr*   r,   r&   r(   r)   rF   rI   rL   r   r   rS   rU   rX   r[   r   r   r   r   r   &   s:    

r   )r$   
__future__r   rA   rD   concurrent.futuresr   
contextlibr   typingr   _read_writer   oscollections.abcr   r	   
concurrentr
   typesr   r   r   __all__r   r   r   r   <module>   s&     1