
    ώi;                    j   U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
mZ d dlmZmZ d dlmZ ddlmZ ddlmZ erd d	lmZ  ej        d
          Z e            Zded<    ej                    ZddZ ej        e           dZ ddZ! G d de"          Z# G d de#          Z$dS )    )annotationsN)contextmanagersuppress)TYPE_CHECKINGLiteral)WeakValueDictionary   )AcquireReturnProxy)Timeout)	Generatorfilelockzset[sqlite3.Connection]_all_connectionsreturnNonec                 &   t           5  t          t                    D ]B} t          t                    5  |                                  d d d            n# 1 swxY w Y   Ct                                           d d d            d S # 1 swxY w Y   d S N)_all_connections_locklistr   r   	Exceptioncloseclear)cons    G/var/www/icac/venv/lib/python3.11/site-packages/filelock/_read_write.py_cleanup_connectionsr      s   	 ! !()) 	 	C)$$  		                 	! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !s4   +BABABABB
B
i5wtimeoutfloatblockingboolalready_waitedintc               &   |du rdS | dk    rt           S | dk     rd}t          |          | dk    rt          | |z
  d          n| }t          |dz            }|t           k    s|dk     r(t                              d| t                      t           S |S )NFr   z+timeout must be a non-negative number or -1i  z7timeout %s is too large for SQLite, using %s ms instead)_MAX_SQLITE_TIMEOUT_MS
ValueErrormaxr    _LOGGERwarning)r   r   r   msg	remaining
timeout_mss         r   timeout_for_sqliter+   (   s    5q"}}%%{{;oo4;aKKGn,a000WIY%&&J***j1nnQSZ\rsss%%    c                  F     e Zd ZU dZded<   ded<   	 ddddd fdZ xZS )_ReadWriteLockMetaa"  
    Metaclass that handles singleton resolution when is_singleton=True.

    Singleton logic lives here rather than in ReadWriteLock.get_lock so that ``ReadWriteLock(path)`` transparently
    returns cached instances without a 2-arg ``super()`` call that type checkers cannot verify.

    0WeakValueDictionary[pathlib.Path, ReadWriteLock]
_instanceszthreading.Lock_instances_lockr"   Tr   is_singleton	lock_filestr | os.PathLike[str]r   r   r   r   r3   r   ReadWriteLockc          	        |s%t                                          ||||          S t          j        |                                          }| j        5  || j        vr0t                                          ||||          }|| j        |<   n| j        |         }|j        |k    s|j        |k    r'd|j         d|j         d| d| }t          |          |cd d d            S # 1 swxY w Y   d S )Nr2   z$Singleton lock created with timeout=z, blocking=z, cannot be changed to timeout=)
super__call__pathlibPathresolver1   r0   r   r   r$   )	clsr4   r   r   r3   
normalizedinstancer(   	__class__s	           r   r9   z_ReadWriteLockMeta.__call__G   sy     	f77##IwXd#eee\),,4466
  	 	// 77++Iw`l+mm-5z**>*57**h.?8.K.KT8;K T TX`Xi T T5<T TIQT T  !oo%	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   BC((C,/C,r"   )
r4   r5   r   r   r   r   r3   r   r   r6   )__name__
__module____qualname____doc____annotations__r9   __classcell__)r@   s   @r   r.   r.   ;   s           A@@@####
 
 !           r,   r.   c                     e Zd ZU dZ e            Zded<    ej                    Z	e
	 d+ddd,d            Z	 d+dddd-dZd.dZd/dZd0dZd1dZd+ddd2dZd+ddd2dZd d!d3d#Zed4d$dd5d(            Zed4d$dd5d)            Zd6d*Zd$S )7r6   a  
    Cross-process read-write lock backed by SQLite.

    Allows concurrent shared readers or a single exclusive writer. The lock is reentrant within the same mode (multiple
    ``acquire_read`` calls nest, as do multiple ``acquire_write`` calls from the same thread), but upgrading from read
    to write or downgrading from write to read raises :class:`RuntimeError`. Write locks are pinned to the thread that
    acquired them.

    By default, ``is_singleton=True``: calling ``ReadWriteLock(path)`` with the same resolved path returns the same
    instance. The lock file must use a ``.db`` extension (SQLite database).

    :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 instances for the same resolved path

    .. versionadded:: 3.21.0

    r/   r0   r"   Tr   r4   r5   r   r   r   r   r   c                    | |||          S )a	  
        Return the singleton :class:`ReadWriteLock` for *lock_file*.

        :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

        :returns: the singleton lock instance

        :raises ValueError: if an instance already exists for this path with different *timeout* or *blocking* values

        rI    )r=   r4   r   r   s       r   get_lockzReadWriteLock.get_lock{   s      s9g9999r,   r2   r3   r   c                  t          j        |          | _        || _        || _        t          j                    | _        t          j                    | _        d| _	        d | _
        d | _        t          j        | j        d          | _        t          5  t                               | j                   d d d            d S # 1 swxY w Y   d S )Nr   F)check_same_thread)osfspathr4   r   r   	threadingLock_transaction_lock_internal_lock_lock_level_current_mode_write_thread_idsqlite3connect_conr   r   add)selfr4   r   r   r3   s        r   __init__zReadWriteLock.__init__   s     9-- !*!1!1'n..>B,0ODNeLLL	" 	, 	,  +++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s    CCCc                   |s| j                             d          }n>|dk    r| j                             d          }n| j                             d|          }|st          | j                  d d S )NFrI   r"   Tr   r   )rS   acquirer   r4   )r\   r   r   acquireds       r   _acquire_transaction_lockz'ReadWriteLock._acquire_transaction_lock   s     	V-55u5EEHH]]-55t5DDHH-55tW5UUH 	4$.))t3	4 	4r,   modeLiteral['read', 'write']oppositestr	directionr
   c                n   | j         |k    r3d| d| j         dt          |            d| d| d}t          |          |dk    rRt	          j                    x}| j        k    r4d| j         dt          |            d	| d
| j         }t          |          | xj        dz  c_        t          |           S )NzCannot acquire z	 lock on  (lock id: z): already holding a z lock (z not allowed)writezCannot acquire write lock on z) from thread z while it is held by thread r	   lock)	rV   r4   idRuntimeErrorrQ   	get_identrW   rU   r
   )r\   rc   re   rg   r(   curs         r   _validate_reentrantz!ReadWriteLock._validate_reentrant   s*   %%O$ O O O OBtHH O O%-O O6?O O O  s###7??y':'<'< <AVVVX X X2d88 X X"X X@D@UX X  s###A!t,,,,r,   
start_timec                  t          j                    |z
  }t          |||          }| j                            d| d                                           | j                            d                                           t          j                    |z
  }t          |||          x}|k    r0| j                            d| d                                           |dk    rdnd}| j                            |                                           |dk    r.| j                            d	                                           d S d S )
N)r   r   zPRAGMA busy_timeout=;zPRAGMA journal_mode=MEMORY;rj   zBEGIN EXCLUSIVE TRANSACTION;zBEGIN TRANSACTION;readz'SELECT name FROM sqlite_schema LIMIT 1;)timeperf_counterr+   rZ   executer   )	r\   rc   r   r   rr   waitedr*   
recomputedstmts	            r   _configure_and_beginz"ReadWriteLock._configure_and_begin   sR    "$$z1'(SYZZZ
	>>>>??EEGGG 		788>>@@@"$$z1,WxX^____JdnnnIBZBBBCCIIKKK15--FZ	$%%'''6>> IGHHNNPPPPP >r,   c                  |dk    rdnd}|dk    rdnd}| j         5  | j        dk    r#|                     |||          cd d d            S 	 d d d            n# 1 swxY w Y   t          j                    }|                     ||           	 | j         5  | j        dk    r<|                     |||          cd d d            | j                                         S 	 d d d            n# 1 swxY w Y   |                     ||||           | j         5  || _	        d| _        |dk    rt          j                    | _        d d d            n# 1 swxY w Y   t          | 	          | j                                         S # t          j        $ r,}d
t!          |          vr t#          | j                  d d }~ww xY w# | j                                         w xY w)Nru   rj   	downgradeupgrader   r_   )r   rr   r	   rk   zdatabase is locked)rT   rU   rq   rv   rw   rb   rS   releaser|   rV   rQ   ro   rW   r
   rX   OperationalErrorrf   r   r4   )r\   rc   r   r   re   rg   rr   excs           r   _acquirezReadWriteLock._acquire   s=   "fnn77&#'6>>KKy	  	K 	K!##//h	JJ	K 	K 	K 	K 	K 	K 	K 	K#	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K &((
&&'&JJJ	-$ O O#a''33D(INNO O O O O O O& "**,,,,% (O O O O O O O O O O O O O O O %%dGhS]%^^^$ B B%)"#$ 7??,5,?,A,AD)	B B B B B B B B B B B B B B B &4000 "**,,,, ' 	4 	4 	4#3s8833$.))t3	4
 "**,,,,s   "AAAF "C*7F F *C..F 1C.2#F -EF EF EF F='F88F==G   Gc               2    |                      d||          S )a  
        Acquire a shared read lock.

        If this instance already holds a read lock, the lock level is incremented (reentrant). Attempting to acquire a
        read lock while holding a write lock raises :class:`RuntimeError` (downgrade not allowed).

        :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 a 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

        ru   rI   r   r\   r   r   s      r   acquire_readzReadWriteLock.acquire_read   s      }}VWx}@@@r,   c               2    |                      d||          S )a  
        Acquire an exclusive write lock.

        If this instance already holds a write lock from the same thread, the lock level is incremented (reentrant).
        Attempting to acquire a write lock while holding a read lock raises :class:`RuntimeError` (upgrade not allowed).
        Write locks are pinned to the acquiring thread: a different thread trying to re-enter also raises
        :class:`RuntimeError`.

        :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 a 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

        rj   rI   r   r   s      r   acquire_writezReadWriteLock.acquire_write  s    $ }}Wg}AAAr,   Fforcer   c               v   d}| j         5  | j        dk    r:|r	 ddd           dS d| j         dt          |            d}t	          |          |rd| _        n| xj        dz  c_        | j        dk    rd| _        d| _        d}ddd           n# 1 swxY w Y   |r| j                                         dS dS )	al  
        Release one level of the current lock.

        When the lock level reaches zero the underlying SQLite transaction is rolled back, releasing the database 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``

        Fr   NzCannot release a lock on ri   z) that is not heldr	   T)	rT   rU   r4   rm   rn   rV   rW   rZ   rollback)r\   r   should_rollbackr(   s       r   r   zReadWriteLock.release  sR       	' 	'1$$ 	' 	' 	' 	' 	' 	' 	' 	' j$.iiRPTXXiii"3''' &#$    A%  1$$%)"(,%"&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'  	!I     	! 	!s   BABBBNfloat | Nonebool | NoneGenerator[None]c             #     K   || j         }|| j        }|                     ||           	 dV  |                                  dS # |                                  w xY w)a}  
        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

        NrI   )r   r   r   r   r   s      r   	read_lockzReadWriteLock.read_lock8  sk       ?lG}H'H555	EEELLNNNNNDLLNNNN   A Ac             #     K   || j         }|| j        }|                     ||           	 dV  |                                  dS # |                                  w xY w)a  
        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

        NrI   )r   r   r   r   r   s      r   
write_lockzReadWriteLock.write_lockM  sk       ?lG}H7X666	EEELLNNNNNDLLNNNNr   c                    |                      d           | j                                         t          5  t                              | j                   ddd           dS # 1 swxY w Y   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.

        Tr   N)r   rZ   r   r   r   discard)r\   s    r   r   zReadWriteLock.closeb  s     	4   	" 	0 	0$$TY///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s    A$$A(+A(rA   )r4   r5   r   r   r   r   r   r6   )
r4   r5   r   r   r   r   r3   r   r   r   )r   r   r   r   r   r   )rc   rd   re   rf   rg   rf   r   r
   )
rc   rd   r   r   r   r   rr   r   r   r   )rc   rd   r   r   r   r   r   r
   )r   r   r   r   r   r
   )r   r   r   r   r   )r   r   r   r   r   r   r   r   )rB   rC   rD   rE   r   r0   rF   rQ   rR   r1   classmethodrL   r]   rb   rq   r|   r   r   r   r   r   r   r   r   rK   r,   r   r6   r6   c   s         ( DWCVCXCXJXXXX$in&&OAC:Y]: : : : : [:( ,
 !, , , , , ,(4 4 4 4- - - - Q Q Q Q2- - - -BAD A A A A A A$BT B B B B B B( (- ! ! ! ! ! !: QU      ^( RV      ^(
0 
0 
0 
0 
0 
0r,   r6   )	metaclassr   )r   r   r   r   r   r   r   r    )%
__future__r   atexitloggingrO   r:   rX   rQ   rv   
contextlibr   r   typingr   r   weakrefr   _apir
   _errorr   collections.abcr   	getLoggerr&   setr   rF   rR   r   r   registerr#   r+   typer.   r6   rK   r,   r   <module>r      s   " " " " " " "   				        / / / / / / / / ) ) ) ) ) ) ) ) ' ' ' ' ' ' $ $ $ $ $ $       *))))))
'
J
'
',/CEE  1 1 1 1&	(( ! ! ! ! $ % % % +    &% % % % % % % %PI0 I0 I0 I0 I00 I0 I0 I0 I0 I0 I0r,   