Ë
    ùÇ:j;!  ã                  ó¸   — 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     úo/home/mcse/projects/srt_converter/srt-converter-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   è ø€ Ø�y‰yÐù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   è ø€ ð �i‰i×ÑÓ!×!Ò!ú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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``. 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Ú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__<   s=   € ô # 9¨gÀÐWcÔdˆŒ
ØˆŒ
Ø&¨$Ð.ˆÔØ!ÒFÔ%7ÀAÔ%Fˆ�r   c                ó.   — | j                   j                  S )z$:returns: the path to the lock file.)r/   r3   r   s    r   r3   zAsyncReadWriteLock.lock_fileK   s   € ð �z‰z×#Ñ#Ð#r   c                ó.   — | j                   j                  S )z:returns: the default timeout.)r/   r4   r   s    r   r4   zAsyncReadWriteLock.timeoutP   s   € ð �z‰z×!Ñ!Ð!r   c                ó.   — | j                   j                  S )z1:returns: whether blocking is enabled by default.)r/   r)   r   s    r   r)   zAsyncReadWriteLock.blockingU   s   € ð �z‰z×"Ñ"Ð"r   c                ó   — | j                   S )z<:returns: the event loop (or ``None`` for the running loop).)r0   r   s    r   r+   zAsyncReadWriteLock.loopZ   s   € ð �z‰zÐr   c                ó   — | j                   S )zi:returns: the executor used for ``run_in_executor`` (a dedicated single-thread one if none was supplied).)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._rund   sQ   è ø€ Ø�z‰zÒ7œW×5Ñ5Ó7ˆØ×)Ñ)¨$¯.©.¼)×:KÑ:KÈDÐ:bÐSWÒ:bÐ[aÑ:bÓc×cÐcÐcús   ‚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_readh   s;   è ø€ ð �i‰i˜Ÿ
™
×/Ñ/°À8ˆiÓ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_writez   s;   è ø€ ð �i‰i˜Ÿ
™
×0Ñ0°'ÀHˆiÓ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(   è ø€ ð �i‰i˜Ÿ
™
×*Ñ*°%ˆiÓ8×8Ò8ús   ‚+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{   è ø€ ð ˆ?Ø—j‘j×(Ñ(ˆGØÐØ—z‘z×*Ñ*ˆ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{   è ø€ ð ˆ?Ø—j‘j×(Ñ(ˆGØÐØ—z‘z×*Ñ*ˆ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   rV   zAsyncReadWriteLock.closeÃ   sL   è ø€ ð �i‰i˜Ÿ
™
×(Ñ(Ó)×)Ð)Ø×ÒØ�N‰N×#Ñ#¨Ð#Õ/ð ð 	*ús   ‚)A«A¬/Ac                óX   — t        | dd«      r| j                  j                  d¬«       y y )Nr1   FrT   )Úgetattrr2   rW   r   s    r   Ú__del__zAsyncReadWriteLock.__del__Î   s+   € ô �4Ð)¨5Ô1Ø�N‰N×#Ñ#¨Ð#Õ/ð 2r   )éÿÿÿÿ)r3   zstr | os.PathLike[str]r4   Úfloatr)   Úboolr*   r]   r+   ú asyncio.AbstractEventLoop | Noner,   zfutures.Executor | Noner!   r"   )r!   Ústr)r!   r\   )r!   r]   )r!   r^   )r!   zfutures.Executor)r@   zCallable[..., object]rA   ÚobjectrB   r`   r!   r`   )r4   r\   r)   r]   r!   r   )rL   r]   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   rV   rZ   r'   r   r   r    r    &   s?  „ ñð0 ðGð
 Ø!Ø15Ø,0ñGà)ðGð ðGð
 ðGð ðGð /ðGð *ðGð 
óGð ò$ó ð$ð ò"ó ð"ð ò#ó ð#ð òó ðð òó ðódð;È$õ ;ð$;È4õ ;ð$ .3õ 9ð ð!ÐW[ô !ó ð!ð( ð!ÐX\ô !ó ð!ó(	0ô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>rl      sR   ðÙ Kå "ã Û Ý 1Ý *Ý  å &áÛß8Ý"Ý#÷"ñ "÷$l0ñ l0ð` 'Øð�r   