
    i "                        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dS )zIAsync wrapper around :class:`SoftReadWriteLock` for use with ``asyncio``.    )annotationsN)asynccontextmanager)TYPE_CHECKING   )SoftReadWriteLock)AsyncGeneratorCallable)futures)TracebackTypec                  *    e Zd ZdZddZddZddZdS )$AsyncAcquireSoftReadWriteReturnProxyzTAsync context-aware object that releases an :class:`AsyncSoftReadWriteLock` on exit.lockAsyncSoftReadWriteLockreturnNonec                    || _         d S Nr   )selfr   s     Y/usr/local/lib/hermes-agent/venv/lib/python3.11/site-packages/filelock/_soft_rw/_async.py__init__z-AsyncAcquireSoftReadWriteReturnProxy.__init__   s    			    c                   K   | j         S r   r   r   s    r   
__aenter__z/AsyncAcquireSoftReadWriteReturnProxy.__aenter__   s      yr   exc_typetype[BaseException] | None	exc_valueBaseException | None	tracebackTracebackType | Nonec                H   K   | j                                          d {V  d S r   )r   release)r   r   r   r    s       r   	__aexit__z.AsyncAcquireSoftReadWriteReturnProxy.__aexit__   s4       i!!!!!!!!!!!r   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      sV        ^^      " " " " " "r   r   c            	      *   e Zd ZdZ	 d3ddddddddd4dZed5d            Zed6d            Zed7d            Zed8d            Z	ed9d            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d?d2ZdS )@r   a  
    Async wrapper around :class:`SoftReadWriteLock` for ``asyncio`` applications.

    The sync class's blocking filesystem operations run on a thread pool via ``loop.run_in_executor()``.
    Reentrancy, upgrade/downgrade rules, fork handling, heartbeat and TTL stale detection, and singleton
    behavior are delegated to the underlying :class:`SoftReadWriteLock`.

    :param lock_file: path to the lock file; sidecar state/write/readers live next to it
    :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
    :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately on contention
    :param is_singleton: if ``True``, reuse existing :class:`SoftReadWriteLock` instances per resolved path
    :param heartbeat_interval: seconds between heartbeat refreshes; default 30 s
    :param stale_threshold: seconds of mtime inactivity before a marker is stale; defaults to ``3 * heartbeat_interval``
    :param poll_interval: seconds between acquire retries under contention; default 0.25 s
    :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.27.0

    Tg      >@Ng      ?)blockingis_singletonheartbeat_intervalstale_thresholdpoll_intervalloopexecutor	lock_filestr | os.PathLike[str]timeoutfloatr,   boolr-   r.   r/   float | Noner0   r1    asyncio.AbstractEventLoop | Noner2   futures.Executor | Noner   r   c          	     X    t          |||||||          | _        || _        |	| _        d S )N)r,   r-   r.   r/   r0   )r   _lock_loop	_executor)
r   r3   r5   r,   r-   r.   r/   r0   r1   r2   s
             r   r   zAsyncSoftReadWriteLock.__init__;   sC     '%1+'
 
 

 
!r   strc                    | j         j        S )z>:returns: the path to the lock file passed to the constructor.)r<   r3   r   s    r   r3   z AsyncSoftReadWriteLock.lock_fileT   s     z##r   c                    | j         j        S )zf:returns: the default timeout applied when ``acquire_read`` / ``acquire_write`` is called without one.)r<   r5   r   s    r   r5   zAsyncSoftReadWriteLock.timeoutY   s     z!!r   c                    | j         j        S )zc:returns: whether ``acquire_*`` defaults to blocking; ``False`` makes contention raise immediately.)r<   r,   r   s    r   r,   zAsyncSoftReadWriteLock.blocking^   s     z""r   c                    | j         S )zX:returns: the event loop used for ``run_in_executor``, or ``None`` for the running loop.)r=   r   s    r   r1   zAsyncSoftReadWriteLock.loopc   s     zr   c                    | j         S )zZ:returns: the executor used for ``run_in_executor``, or ``None`` for the default executor.)r>   r   s    r   r2   zAsyncSoftReadWriteLock.executorh   s     ~r   r,   bool | Noner   c               v   K   |                      | j        j        ||           d{V  t          |           S )aC  
        Acquire a shared read lock.

        See :meth:`SoftReadWriteLock.acquire_read` for the full reentrancy / upgrade / fork semantics. The blocking
        work runs inside ``run_in_executor`` so other coroutines on the same loop continue to progress while this
        call waits.

        :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

        :returns: a proxy usable as an async context manager to release the lock

        :raises RuntimeError: if a write lock is already held, if this instance was invalidated by
            :func:`os.fork`, or if :meth:`close` was called
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        rE   Nr   )_runr<   acquire_readr   r   r5   r,   s      r   rI   z#AsyncSoftReadWriteLock.acquire_readm   sH      ( ii
/8iLLLLLLLLL3>>>>r   c               v   K   |                      | j        j        ||           d{V  t          |           S )a  
        Acquire an exclusive write lock.

        See :meth:`SoftReadWriteLock.acquire_write` for the two-phase writer-preferring semantics. The blocking
        work runs inside ``run_in_executor``.

        :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

        :returns: a proxy usable as an async context manager to release the lock

        :raises RuntimeError: if a read lock is already held, if a write lock is held by a different thread, if
            this instance was invalidated by :func:`os.fork`, or if :meth:`close` was called
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        rE   Nr   )rH   r<   acquire_writer   rJ   s      r   rL   z$AsyncSoftReadWriteLock.acquire_write   sH      & ii
0'HiMMMMMMMMM3>>>>r   FforcerN   c               X   K   |                      | j        j        |           d{V  dS )z
        Release one level of the current lock.

        :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)rH   r<   r#   )r   rN   s     r   r#   zAsyncSoftReadWriteLock.release   s;       ii
*%i88888888888r   AsyncGenerator[None]c                 K   |                      ||           d{V  	 dW V  |                                  d{V  dS # |                                  d{V  w xY w)a  
        Async context manager that acquires and releases a shared read lock.

        :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

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

        rE   N)rI   r#   rJ   s      r   	read_lockz AsyncSoftReadWriteLock.read_lock   s       (;;;;;;;;;	!EEEE,,..         $,,..           A Ac                 K   |                      ||           d{V  	 dW V  |                                  d{V  dS # |                                  d{V  w xY w)a  
        Async context manager that acquires and releases an exclusive write lock.

        :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

        :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

        rE   N)rL   r#   rJ   s      r   
write_lockz!AsyncSoftReadWriteLock.write_lock   s         8 <<<<<<<<<	!EEEE,,..         $,,..        rS   c                T   K   |                      | j        j                   d{V  dS )zRRelease any held lock and release the underlying filesystem resources. Idempotent.N)rH   r<   closer   s    r   rW   zAsyncSoftReadWriteLock.close   s5      ii
()))))))))))r   funcCallable[..., object]argsobjectkwargsc                   K   | j         pt          j                    }|                    | j        t          j        |g|R i |           d {V S r   )r=   asyncioget_running_looprun_in_executorr>   	functoolspartial)r   rX   rZ   r\   r1   s        r   rH   zAsyncSoftReadWriteLock._run   sb      z7W577))$.):KD:bSW:b:b:b[a:b:bcccccccccr   )r+   )r3   r4   r5   r6   r,   r7   r-   r7   r.   r6   r/   r8   r0   r6   r1   r9   r2   r:   r   r   )r   r?   )r   r6   )r   r7   )r   r9   )r   r:   r   )r5   r8   r,   rF   r   r   )rN   r7   r   r   )r5   r8   r,   rF   r   rP   )r   r   )rX   rY   rZ   r[   r\   r[   r   r[   )r%   r&   r'   r(   r   propertyr3   r5   r,   r1   r2   rI   rL   r#   r   rR   rU   rW   rH   r)   r   r   r   r   %   s
        0 "
 !$((,#15,0" " " " " "2 $ $ $ X$ " " " X" # # # X#    X    X
 '+?GK? ? ? ? ? ?0 '+?GK? ? ? ? ? ?, .3 	9 	9 	9 	9 	9 	9 !W[ ! ! ! ! ! !" !X\ ! ! ! ! ! !"* * * *d d d d d dr   r   )r(   
__future__r   r^   ra   
contextlibr   typingr   _syncr   oscollections.abcr   r	   
concurrentr
   typesr   r   r   __all__r)   r   r   <module>rm      s)   O O " " " " " "      * * * * * *             $ $ $ $ $ $ $III88888888""""""######" " " " " " " "$jd jd jd jd jd jd jd jd\ +r   