
     j,                        d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
mZ ddlmZmZmZmZmZmZ ddlmZ e	rdd	lmZmZ dd
lmZ ddlmZ ddlmZ  e
d      Z ed      Z G d d      Z  G d d      Z!ddgZ"y)zIAsync wrapper around :class:`SoftReadWriteLock` for use with ``asyncio``.    )annotationsN)asynccontextmanager)TYPE_CHECKING	ParamSpecTypeVar)_BackendOutcome_capture_call_drain_future_future_result_raise_cancelled_error_wait_until_done   )SoftReadWriteLock)AsyncGeneratorCallable)futures)TracebackType)AcquireReturnProxy_P_Rc            	      `   e Zd ZdZ	 ddd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
ed!ddd"d       Zed!ddd"d       Z	 d!dd	 	 	 	 	 d#dZ	 d!dd	 	 	 	 	 d#dZddd$dZd%dZd%dZd&dZd'dZ	 	 	 	 	 	 	 	 d(dZy))AsyncSoftReadWriteLocka  
    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()``. The
    underlying :class:`SoftReadWriteLock` handles reentrancy, upgrade/downgrade rules, fork handling, heartbeat and
    TTL stale detection, and singleton behavior.

    :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executorc          	         t        j                         | _        t        |||||||      | _        || _        |	| _        y )N)r   r   r   r   r   )osgetpid_creator_pidr   _lock_loop	_executor)
self	lock_filetimeoutr   r   r   r   r   r   r   s
             d/home/min/hermes_workspace/demo_review/venv/lib/python3.12/site-packages/filelock/_soft_rw/_async.py__init__zAsyncSoftReadWriteLock.__init__7   sC     IIK&%1+'

 
!    c                .    | j                   j                  S )z4The path to the lock file passed to the constructor.)r$   r(   r'   s    r*   r(   z AsyncSoftReadWriteLock.lock_fileQ   s     zz###r,   c                .    | j                   j                  S )z\The default timeout applied when ``acquire_read`` / ``acquire_write`` is called without one.)r$   r)   r.   s    r*   r)   zAsyncSoftReadWriteLock.timeoutV   s     zz!!!r,   c                .    | j                   j                  S )zYWhether ``acquire_*`` defaults to blocking; ``False`` makes contention raise immediately.)r$   r   r.   s    r*   r   zAsyncSoftReadWriteLock.blocking[   s     zz"""r,   c                    | j                   S )zNThe event loop used for ``run_in_executor``, or ``None`` for the running loop.)r%   r.   s    r*   r   zAsyncSoftReadWriteLock.loop`   s     zzr,   c                    | j                   S )zPThe executor used for ``run_in_executor``, or ``None`` for the default executor.)r&   r.   s    r*   r   zAsyncSoftReadWriteLock.executore   s     ~~r,   r   c                 K   | 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.

        :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

        r3   N)acquire_readreleaser'   r)   r   s      r*   	read_lockz AsyncSoftReadWriteLock.read_lockj   sR      (;;;	!,,.  	 	< !$,,.  >   A=AA A?AAAAAAc                 K   | 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.

        :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

        r3   N)acquire_writer6   r7   s      r*   
write_lockz!AsyncSoftReadWriteLock.write_lock|   sR        8 <<<	!,,.  	 	= !$,,.  r9   c                  K   | j                          | j                  t        j                  | j                  j
                  ||             d{    t        |       S 7 w)a.  
        Acquire a shared read lock.

        See :meth:`SoftReadWriteLock.acquire_read` for reentrancy / upgrade / fork semantics. The blocking work runs
        inside ``run_in_executor`` so other coroutines on the same loop keep progressing 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

        r3   Nlock)_raise_if_inherited_run_acquire	functoolspartialr$   r5   $AsyncAcquireSoftReadWriteReturnProxyr7   s      r*   r5   z#AsyncSoftReadWriteLock.acquire_read   sS     & 	  "	 1 1$**2I2I7]e fggg3>> 	h   AA$A"A$c                  K   | j                          | j                  t        j                  | j                  j
                  ||             d{    t        |       S 7 w)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

        r3   Nr>   )r@   rA   rB   rC   r$   r;   rD   r7   s      r*   r;   z$AsyncSoftReadWriteLock.acquire_write   sS     & 	  "	 1 1$**2J2JG^f ghhh3>> 	irE   Fforcec                  K   | j                   t        j                         k(  r0| j                  | j                  j
                  |       d{    yy7 w)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``

        rG   N)r#   r!   r"   _runr$   r6   )r'   rH   s     r*   r6   zAsyncSoftReadWriteLock.release   sD      		+))DJJ..e)<<< ,<s   AAAAc                   K   | j                   t        j                         k(  r.| j                  | j                  j
                         d{    yy7 w)zRRelease any held lock and release the underlying filesystem resources. Idempotent.N)r#   r!   r"   rJ   r$   closer.   s    r*   rL   zAsyncSoftReadWriteLock.close   s>     		+))DJJ,,--- ,-s   A
AAAc                |    | j                   t        j                         k7  rd| j                   d}t	        |      y )NzAsyncSoftReadWriteLock on z4 was inherited across fork; construct a new instance)r#   r!   r"   r(   RuntimeError)r'   msgs     r*   r@   z*AsyncSoftReadWriteLock._raise_if_inherited   s:    		+.t~~.>>rsCs## ,r,   c                  K   | j                  |      }	 t        |       d {    t        |       y 7 # t        j                  $ r}	 t	        |       d {  7   n"# t
        $ r}t        ||       Y d }~nd }~ww xY w	 t	        | j                  | j                  j                               d {  7    # t
        $ r}t        ||       Y d }~ d }~ww xY wd }~ww xY wwN)
_submitr   asyncioCancelledErrorr
   BaseExceptionr   r$   r6   r   )r'   acquireacquire_futurecancellationerrors        r*   rA   z#AsyncSoftReadWriteLock._run_acquire   s     
 g.	">222 	~& 3%% 		<#N333  <&|U;;<<#DLL1C1C$DEEE  ! <&|U;;<		s   C$6 46 C$6 C!
AAAC	A>(A94C9A>>C1B:3B64B:9C:	CCCCCC!!C$c                  K    | j                   |g|i |}	 t        |       d {    t        |      S 7 # t        j                  $ r=}	 t	        |       d {  7    # t
        $ r}t        ||       Y d }~ d }~ww xY wd }~ww xY wwrQ   )rR   r   rS   rT   r
   rU   r   r   )r'   funcargskwargsfuturerX   rY   s          r*   rJ   zAsyncSoftReadWriteLock._run   s      d4T4V4	"6*** f%% +%% 	<#F+++  ! <&|U;;<	sa   B: 8: B: B
A#AA#"B#	B,A=8B=BBB

Bc           	         | j                   xs t        j                         }|j                  | j                  t
        t        j                  |g|i |      S rQ   )r%   rS   get_running_looprun_in_executorr&   r	   rB   rC   )r'   r[   r\   r]   r   s        r*   rR   zAsyncSoftReadWriteLock._submit   sL     zz7W557##DNNM9CTCTUYCk\`CkdjCkllr,   ))r(   zstr | os.PathLike[str]r)   floatr   boolr   rd   r   rc   r   float | Noner   rc   r    asyncio.AbstractEventLoop | Noner   futures.Executor | NonereturnNone)rh   str)rh   rc   )rh   rd   )rh   rf   )rh   rg   rQ   )r)   re   r   bool | Nonerh   zAsyncGenerator[None])r)   re   r   rk   rh   rD   )rH   rd   rh   ri   )rh   ri   )rV   z Callable[[], AcquireReturnProxy]rh   ri   )r[   Callable[_P, _R]r\   _P.argsr]   	_P.kwargsrh   r   )r[   rl   r\   rm   r]   rn   rh   z#asyncio.Future[_BackendOutcome[_R]])__name__
__module____qualname____doc__r+   propertyr(   r)   r   r   r   r   r8   r<   r5   r;   r6   rL   r@   rA   rJ   rR    r,   r*   r   r   !   s   0 "
 !$((,#15,0")" "
 " " "" &" " /" *" 
"4 $ $ " " # #     !W[ ! !" !X\ ! !$ '+?GK?#?9D?	-?0 '+?GK?#?9D?	-?. .3 
=.
$
'(&m$m-4m@Im	,mr,   r   c                  8    e Zd ZdZddZddZ	 	 	 	 	 	 	 	 ddZy)	rD   zTAsync context-aware object that releases an :class:`AsyncSoftReadWriteLock` on exit.c                    || _         y rQ   r>   )r'   r?   s     r*   r+   z-AsyncAcquireSoftReadWriteReturnProxy.__init__   s	    	r,   c                "   K   | j                   S wrQ   r>   r.   s    r*   
__aenter__z/AsyncAcquireSoftReadWriteReturnProxy.__aenter__  s     yys   c                T   K   | j                   j                          d {    y 7 wrQ   )r?   r6   )r'   exc_type	exc_value	tracebacks       r*   	__aexit__z.AsyncAcquireSoftReadWriteReturnProxy.__aexit__  s      ii!!!s   (&(N)r?   r   rh   ri   )rh   r   )rz   ztype[BaseException] | Noner{   zBaseException | Noner|   zTracebackType | Nonerh   ri   )ro   rp   rq   rr   r+   rx   r}   rt   r,   r*   rD   rD      s:    ^"," (" (	"
 
"r,   rD   )#rr   
__future__r   rS   rB   r!   
contextlibr   typingr   r   r   filelock._asyncr   r	   r
   r   r   r   _syncr   collections.abcr   r   
concurrentr   typesr   filelock._apir   r   r   r   rD   __all__rt   r,   r*   <module>r      su    O "   	 * 4 4  %8"#0t_T]Wm Wmt" "& +r,   