
    qiy                        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
 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)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     L/opt/pipecat/venv/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               H    t        ||||      | _        || _        || _        y )N)r(   r)   )r   _lock_loop	_executor)r   	lock_filetimeoutr(   r)   r*   r+   s          r   r   zAsyncReadWriteLock.__init__8   s%     #9gWcd

!r   c                .    | j                   j                  S )z$:returns: the path to the lock file.)r-   r0   r   s    r   r0   zAsyncReadWriteLock.lock_fileF   s     zz###r   c                .    | j                   j                  S )z:returns: the default timeout.)r-   r1   r   s    r   r1   zAsyncReadWriteLock.timeoutK   s     zz!!!r   c                .    | j                   j                  S )z1:returns: whether blocking is enabled by default.)r-   r(   r   s    r   r(   zAsyncReadWriteLock.blockingP   s     zz"""r   c                    | j                   S )z<:returns: the event loop (or ``None`` for the running loop).)r.   r   s    r   r*   zAsyncReadWriteLock.loopU   s     zzr   c                    | j                   S )z5:returns: the executor (or ``None`` for the default).)r/   r   s    r   r+   zAsyncReadWriteLock.executorZ   s     ~~r   c                   K   | j                   xs t        j                         }|j                  | j                  t        j                  |g|i |       d {   S 7 wr   )r.   asyncioget_running_looprun_in_executorr/   	functoolspartial)r   funcargskwargsr*   s        r   _runzAsyncReadWriteLock._run_   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

        rA   Nr   )r@   r-   acquire_readr   r   r1   r(   s      r   rC   zAsyncReadWriteLock.acquire_readc   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

        rA   Nr   )r@   r-   acquire_writer   rD   s      r   rG   z AsyncReadWriteLock.acquire_writeu   s;      ii

00'HiMMM/T:: 	NrE   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``

        rH   N)r@   r-   r   )r   rI   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

        NrA   )r-   r1   r(   rC   r   rD   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

        NrA   )r-   r1   r(   rG   r   rD   s      r   
write_lockzAsyncReadWriteLock.write_lock   s{      ?jj((Gzz**H  8 <<<	!,,.  	 	= !$,,.  rM   c                j   K   | j                  | j                  j                         d{    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.

        N)r@   r-   closer   s    r   rQ   zAsyncReadWriteLock.close   s$      ii

(()))s   )313))r0   zstr | os.PathLike[str]r1   floatr(   boolr)   rT   r*    asyncio.AbstractEventLoop | Noner+   futures.Executor | Noner    r!   )r    str)r    rS   )r    rT   )r    rU   )r    rV   )r=   zCallable[..., object]r>   objectr?   rX   r    rX   )r1   rS   r(   rT   r    r   )rI   rT   r    r!   r   )r1   zfloat | Noner(   zbool | Noner    zAsyncGenerator[None])r    r!   )r"   r#   r$   r%   r   propertyr0   r1   r(   r*   r+   r@   rC   rG   r   r   rL   rO   rQ   r&   r   r   r   r   %   s1   * "
 !15,0")" "
 " " /" *" 
" $ $ " " # #    d;$ ;$;4 ;$ .3 9 !W[ ! !( !X\ ! !(*r   r   )r%   
__future__r   r8   r;   
contextlibr   typingr   _read_writer   oscollections.abcr   r	   
concurrentr
   typesr   r   r   __all__r&   r   r   <module>rc      sO    K "   *   &8"#" "$`* `*H 'r   