
    \jO                        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
rddlZdd	lmZmZ dd
lmZ ddlmZ  G d d      Z G d d      ZddgZy)zEAsync wrapper around :class:`ReadWriteLock` for use with ``asyncio``.    )annotationsN)ThreadPoolExecutor)asynccontextmanager)TYPE_CHECKING   )ReadWriteLock)AsyncGeneratorCallable)futures)TracebackTypec                  8    e Zd ZdZddZddZ	 	 	 	 	 	 	 	 ddZy)	 AsyncAcquireReadWriteReturnProxyzEContext-aware object that releases the async read/write lock on exit.c                    || _         y Nlock)selfr   s     D/root/env/lib/python3.12/site-packages/filelock/_async_read_write.py__init__z)AsyncAcquireReadWriteReturnProxy.__init__   s	    	    c                "   K   | j                   S wr   r   r   s    r   
__aenter__z+AsyncAcquireReadWriteReturnProxy.__aenter__   s     yys   c                T   K   | j                   j                          d {    y 7 wr   )r   release)r   exc_type	exc_value	tracebacks       r   	__aexit__z*AsyncAcquireReadWriteReturnProxy.__aexit__   s      ii!!!s   (&(N)r   AsyncReadWriteLockreturnNone)r!   r    )r   ztype[BaseException] | Noner   zBaseException | Noner   zTracebackType | Noner!   r"   )__name__
__module____qualname____doc__r   r   r    r   r   r   r      s:    O"," (" (	"
 
"r   r   c                     e Zd ZdZ	 dddddd	 	 	 	 	 	 	 	 	 	 	 	 	 ddZedd       Zedd       Zedd       Zedd	       Z	edd
       Z
ddZdddddZdddddZddddZedddd d       Zedddd d       Zd!dZy)"r    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executorc               v    t        ||||      | _        || _        |d u | _        |xs t	        d      | _        y )N)r)   r*   r   )max_workers)r   _lock_loop_owns_executorr   	_executor)r   	lock_filetimeoutr)   r*   r+   r,   s          r   r   zAsyncReadWriteLock.__init__9   s=     #9gWcd

&$.!F%7A%Fr   c                .    | j                   j                  S )z$:returns: the path to the lock file.)r/   r3   r   s    r   r3   zAsyncReadWriteLock.lock_fileH   s     zz###r   c                .    | j                   j                  S )z:returns: the default timeout.)r/   r4   r   s    r   r4   zAsyncReadWriteLock.timeoutM   s     zz!!!r   c                .    | j                   j                  S )z1:returns: whether blocking is enabled by default.)r/   r)   r   s    r   r)   zAsyncReadWriteLock.blockingR   s     zz"""r   c                    | j                   S )z<:returns: the event loop (or ``None`` for the running loop).)r0   r   s    r   r+   zAsyncReadWriteLock.loopW   s     zzr   c                    | j                   S )z5:returns: the executor (or ``None`` for the default).)r2   r   s    r   r,   zAsyncReadWriteLock.executor\   s     ~~r   c                   K   | j                   xs t        j                         }|j                  | j                  t        j                  |g|i |       d {   S 7 wr   )r0   asyncioget_running_looprun_in_executorr2   	functoolspartial)r   funcargskwargsr+   s        r   _runzAsyncReadWriteLock._runa   sQ     zz7W557))$..):K:KD:bSW:b[a:bccccs   AA"A A"r)   c                  K   | j                  | j                  j                  ||       d{    t        |       S 7 w)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   r/   acquire_readr   r   r4   r)   s      r   rF   zAsyncReadWriteLock.acquire_reade   s;      ii

//8iLLL/T:: 	M   ,A?Ac                  K   | j                  | j                  j                  ||       d{    t        |       S 7 w)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   r/   acquire_writer   rG   s      r   rJ   z AsyncReadWriteLock.acquire_writew   s;      ii

00'HiMMM/T:: 	NrH   Fforcec               n   K   | j                  | j                  j                  |       d{    y7 w)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``

        rK   N)rC   r/   r   )r   rL   s     r   r   zAsyncReadWriteLock.release   s(      ii

**%i888s   +535c              "  K   || j                   j                  }|| j                   j                  }| j                  ||       d{    	 d | j	                          d{    y7 #7 # | j	                          d{  7   w xY w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   )r/   r4   r)   rF   r   rG   s      r   	read_lockzAsyncReadWriteLock.read_lock   s{      ?jj((Gzz**H(;;;	!,,.  	 	< !$,,.  H   AB	A-
BA1 B'A/(B/B1BBBBc              "  K   || j                   j                  }|| j                   j                  }| j                  ||       d{    	 d | j	                          d{    y7 #7 # | j	                          d{  7   w xY ww)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   )r/   r4   r)   rJ   r   rG   s      r   
write_lockzAsyncReadWriteLock.write_lock   s{      ?jj((Gzz**H  8 <<<	!,,.  	 	= !$,,.  rP   c                   K   | j                  | j                  j                         d{    | j                  r| j                  j                  d       yy7 .w)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   r/   closer1   r2   shutdownr   s    r   rU   zAsyncReadWriteLock.close   sL      ii

(()))NN###/  	*s   )AA/A))r3   zstr | os.PathLike[str]r4   floatr)   boolr*   rY   r+    asyncio.AbstractEventLoop | Noner,   futures.Executor | Noner!   r"   )r!   str)r!   rX   )r!   rY   )r!   rZ   )r!   r[   )r@   zCallable[..., object]rA   objectrB   r]   r!   r]   )r4   rX   r)   rY   r!   r   )rL   rY   r!   r"   r   )r4   zfloat | Noner)   zbool | Noner!   zAsyncGenerator[None])r!   r"   )r#   r$   r%   r&   r   propertyr3   r4   r)   r+   r,   rC   rF   rJ   r   r   rO   rR   rU   r'   r   r   r    r    &   s:   * G
 !15,0G)G G
 G G /G *G 
G $ $ " " # #    d;$ ;$;4 ;$ .3 9 !W[ ! !( !X\ ! !(	0r   r    )r&   
__future__r   r;   r>   concurrent.futuresr   
contextlibr   typingr   _read_writer   oscollections.abcr	   r
   
concurrentr   typesr   r   r    __all__r'   r   r   <module>ri      sR    K "   1 *   &8"#" "$c0 c0N 'r   