o
    /iy                     @  s   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 dZG dd dZddgZdS )zEAsync wrapper around :class:`ReadWriteLock` for use with ``asyncio``.    )annotationsN)asynccontextmanager)TYPE_CHECKING   )ReadWriteLock)AsyncGeneratorCallable)futures)TracebackTypec                   @  s.   e Zd ZdZdddZddd	ZdddZdS ) AsyncAcquireReadWriteReturnProxyzEContext-aware object that releases the async read/write lock on exit.lockAsyncReadWriteLockreturnNonec                 C  s
   || _ d S Nr   )selfr    r   T/home/kim/smarthome/.venv/lib/python3.10/site-packages/filelock/_async_read_write.py__init__   s   
z)AsyncAcquireReadWriteReturnProxy.__init__c                   s   | j S r   r   r   r   r   r   
__aenter__   s   z+AsyncAcquireReadWriteReturnProxy.__aenter__exc_typetype[BaseException] | None	exc_valueBaseException | None	tracebackTracebackType | Nonec                   s   | j  I d H  d S r   )r   release)r   r   r   r   r   r   r   	__aexit__   s   z*AsyncAcquireReadWriteReturnProxy.__aexit__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   r      s
    

r   c                   @  s   e Zd ZdZ	d;dddddd<ddZed=ddZed>ddZed?ddZed@ddZ	edAddZ
dBd%d&Zd;dd'dCd)d*Zd;dd'dCd+d,Zd-d.dDd0d1ZedEdd'dFd5d6ZedEdd'dFd7d8ZdGd9d:ZdS )Hr   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executor	lock_filestr | os.PathLike[str]timeoutfloatr%   boolr&   r'    asyncio.AbstractEventLoop | Noner(   futures.Executor | Noner   r   c                C  s"   t ||||d| _|| _|| _d S )N)r%   r&   )r   _lock_loop	_executor)r   r)   r+   r%   r&   r'   r(   r   r   r   r   8   s   

zAsyncReadWriteLock.__init__strc                 C     | j jS )z$:returns: the path to the lock file.)r0   r)   r   r   r   r   r)   F      zAsyncReadWriteLock.lock_filec                 C  r4   )z:returns: the default timeout.)r0   r+   r   r   r   r   r+   K   r5   zAsyncReadWriteLock.timeoutc                 C  r4   )z1:returns: whether blocking is enabled by default.)r0   r%   r   r   r   r   r%   P   r5   zAsyncReadWriteLock.blockingc                 C     | j S )z<:returns: the event loop (or ``None`` for the running loop).)r1   r   r   r   r   r'   U      zAsyncReadWriteLock.loopc                 C  r6   )z5:returns: the executor (or ``None`` for the default).)r2   r   r   r   r   r(   Z   r7   zAsyncReadWriteLock.executorfuncCallable[..., object]argsobjectkwargsc                   s8   | j pt }|| jtj|g|R i |I d H S r   )r1   asyncioZget_running_loopZrun_in_executorr2   	functoolspartial)r   r8   r:   r<   r'   r   r   r   _run_   s   (zAsyncReadWriteLock._runr%   r   c                  &   | j | jj||dI dH  t| dS )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@   r0   acquire_readr   r   r+   r%   r   r   r   rC   c      
zAsyncReadWriteLock.acquire_readc                  rB   )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@   r0   acquire_writer   rD   r   r   r   rF   u   rE   z AsyncReadWriteLock.acquire_writeFforcerH   c                  s   | j | jj|dI dH  dS )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``

        rG   N)r@   r0   r   )r   rH   r   r   r   r      s   zAsyncReadWriteLock.releasefloat | Nonebool | NoneAsyncGenerator[None]c                C b   |du r	| j j}|du r| j j}| j||dI dH  zdV  W |  I dH  dS |  I dH  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   )r0   r+   r%   rC   r   rD   r   r   r   	read_lock      "zAsyncReadWriteLock.read_lockc                C rL   )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   )r0   r+   r%   rF   r   rD   r   r   r   
write_lock   rN   zAsyncReadWriteLock.write_lockc                   s   |  | jjI dH  dS )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@   r0   closer   r   r   r   rP      s   zAsyncReadWriteLock.close)r$   )r)   r*   r+   r,   r%   r-   r&   r-   r'   r.   r(   r/   r   r   )r   r3   )r   r,   )r   r-   )r   r.   )r   r/   )r8   r9   r:   r;   r<   r;   r   r;   )r+   r,   r%   r-   r   r   )rH   r-   r   r   r   )r+   rI   r%   rJ   r   rK   )r   r   )r    r!   r"   r#   r   propertyr)   r+   r%   r'   r(   r@   rC   rF   r   r   rM   rO   rP   r   r   r   r   r   %   s8    
r   )r#   
__future__r   r=   r>   
contextlibr   typingr   Z_read_writer   oscollections.abcr   r   Z
concurrentr	   typesr
   r   r   __all__r   r   r   r   <module>   s$     %