
    Ki;                    j   % S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	J
r
Jr  S SKJrJr  S SKJr  SSKJr  SSKJr  \(       a  S S	KJr  \R.                  " S
5      r\" 5       rS\S'   \R8                  " 5       rSS jr\R>                  " \5        Sr SS jr! " S S\"5      r# " S S\#S9r$g)    )annotationsN)contextmanagersuppress)TYPE_CHECKINGLiteral)WeakValueDictionary   )AcquireReturnProxy)Timeout)	Generatorfilelockzset[sqlite3.Connection]_all_connectionsc                    [            [        [        5       H+  n [        [        5         U R                  5         S S S 5        M-     [        R                  5         S S S 5        g ! , (       d  f       M[  = f! , (       d  f       g = fN)_all_connections_locklistr   r   	Exceptioncloseclear)cons    V/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/filelock/_read_write.py_cleanup_connectionsr      sT    	()C)$		 %$ * 	 	 
	$$ 
	s!   "A5A# A5#
A2-A55
Bi5wc                   USL a  gU S:X  a  [         $ U S:  a  Sn[        U5      eU S:  a  [        X-
  S5      OU n[        US-  5      nU[         :  d  US:  a!  [        R                  SU [         5        [         $ U$ )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maxint_LOGGERwarning)timeoutblockingalready_waitedmsg	remaining
timeout_mss         r   timeout_for_sqliter'   (   s    5"}%%{;o4;aKG,a0WIY%&J**j1nQSZ\rs%%    c                  h   ^  \ rS rSr% SrS\S'   S\S'    SSSS.         SU 4S	 jjjjrS
rU =r$ )_ReadWriteLockMeta;   a  
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_lockTr"   is_singletonc          	       > U(       d  [         TU ]  XX4S9$ [        R                  " U5      R	                  5       nU R
                     XPR                  ;  a  [         TU ]  XX4S9nX`R                  U'   OU R                  U   nUR                  U:w  d  UR                  U:w  a-  SUR                   SUR                   SU SU 3n[        U5      eUsS S S 5        $ ! , (       d  f       g = f)Nr/   z$Singleton lock created with timeout=z, blocking=z, cannot be changed to timeout=)
super__call__pathlibPathresolver.   r-   r!   r"   r   )	cls	lock_filer!   r"   r0   
normalizedinstancer$   	__class__s	           r   r3   _ReadWriteLockMeta.__call__G   s     7#I#ee\\),446
  / 7+I+m-5z*>>*57*h.?.?8.K:8;K;K:LKX`XiXiWj k55<I[
T  !o% !  s   B
C
C) r   )
r8   str | os.PathLike[str]r!   floatr"   boolr0   rA   returnReadWriteLock)	__name__
__module____qualname____firstlineno____doc____annotations__r3   __static_attributes____classcell__)r;   s   @r   r*   r*   ;   sc     A@##
 
 !) 
   
 r(   r*   c                  t   \ rS rSr% Sr\" 5       rS\S'   \R                  " 5       r
\ SSS.       SS jjj5       r SSSS.         SS	 jjjrSS
 jrSS jr          SS jrSS jrSSS.SS jjjrSSS.SS jjjrSS.S S jjr\S!SS.S"S jjj5       r\S!SS.S"S jjj5       rS#S jrSrg)$rC   c   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,   r-   Tr"   c                   U " XUS9$ )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

rN   r=   )r7   r8   r!   r"   s       r   get_lockReadWriteLock.get_lock{   s      999r(   r/   c                  [         R                  " U5      U l        X l        X0l        [
        R                  " 5       U l        [
        R                  " 5       U l        SU l	        S U l
        S U l        [        R                  " U R                  SS9U l        [           [         R#                  U R                  5        S S S 5        g ! , (       d  f       g = f)Nr   F)check_same_thread)osfspathr8   r!   r"   	threadingLock_transaction_lock_internal_lock_lock_level_current_mode_write_thread_idsqlite3connect_conr   r   add)selfr8   r!   r"   r0   s        r   __init__ReadWriteLock.__init__   s     9- !*!1'nn.>B,0OODNNeL	"  + #""s    C
Cc                   US:X  a  U R                   R                  U5      nOU R                   R                  X5      nU(       d  [        U R                  5      S eg )Nr   )rX   acquirer   r8   )ra   r"   r!   acquireds       r   _acquire_transaction_lock'ReadWriteLock._acquire_transaction_lock   sN    b=--55h?H--55hHH$..)t3 r(   c                ~   U R                   U:w  a0  SU SU R                   S[        U 5       SU SU S3n[        U5      eUS:X  aZ  [        R
                  " 5       =oPR                  :w  a6  SU R                   S[        U 5       S	U S
U R                   3n[        U5      eU =R                  S-  sl        [        U S9$ )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)	r[   r8   idRuntimeErrorrV   	get_identr\   rZ   r
   )ra   modeopposite	directionr$   curs         r   _validate_reentrant!ReadWriteLock._validate_reentrant   s    %!$y0@BtH: V%%-Jgi[O  s##7?y':':'< <AVAVV//?{2d8* U"e#?@U@U?VX  s##A!t,,r(   c               b   [         R                  " 5       U-
  n[        X#US9nU R                  R	                  SU S35      R                  5         U R                  R	                  S5      R                  5         [         R                  " 5       U-
  n[        X#US9=ov:w  a-  U R                  R	                  SU S35      R                  5         US:X  a  SOSnU R                  R	                  U5      R                  5         US:X  a*  U R                  R	                  S	5      R                  5         g g )
N)r"   r#   zPRAGMA busy_timeout=;zPRAGMA journal_mode=MEMORY;rk   zBEGIN EXCLUSIVE TRANSACTION;zBEGIN TRANSACTION;readz'SELECT name FROM sqlite_schema LIMIT 1;)timeperf_counterr'   r_   executer   )	ra   rq   r!   r"   
start_timewaitedr&   
recomputedstmts	            r   _configure_and_begin"ReadWriteLock._configure_and_begin   s    ""$z1'SYZ
		0A>?EEG 			78>>@""$z1,WX^__JnII 4ZLBCIIK15-FZ		$%%'6> IIGHNNP r(   c                  US:X  a  SOSnUS:X  a  SOSnU R                      U R                  S:  a  U R                  XU5      sS S S 5        $  S S S 5        [        R                  " 5       nU R                  X2S9   U R                      U R                  S:  a5  U R                  XU5      sS S S 5        U R                  R                  5         $  S S S 5        U R                  XX6S9  U R                      Xl	        SU l        US:X  a  [        R                  " 5       U l        S S S 5        [        U S	9U R                  R                  5         $ ! , (       d  f       GN= f! , (       d  f       N= f! , (       d  f       NT= f! [        R                   a+  nS
[!        U5      ;  a  e [#        U R$                  5      S eS nAff = f! U R                  R                  5         f = f)Nry   rk   	downgradeupgrader   )r"   r!   )r"   r}   r	   rl   zdatabase is locked)rY   rZ   ru   rz   r{   rg   rX   releaser   r[   rV   rp   r\   r
   r]   OperationalErrorstrr   r8   )ra   rq   r!   r"   rr   rs   r}   excs           r   _acquireReadWriteLock._acquire   s   "fn7&#'6>Ky	  !#//	J ! # ! &&(
&&&J	-$$##a'33DIN %$& ""**,% ( % %%dh%^$$%)"#$ 7?,5,?,?,AD)	 % &40 ""**,7 !  %$ %$ '' 	4#3s83$..)t3	4
 ""**,se   "E;E? "E)	E? $E? 2.E. E? 
E
E+'E? .
E<8E? ?F>&F99F>>G Gc               "    U R                  SXS9$ )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

ry   rN   r   ra   r!   r"   s      r   acquire_readReadWriteLock.acquire_read   s      }}VW}@@r(   c               "    U R                  SXS9$ )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

rk   rN   r   r   s      r   acquire_writeReadWriteLock.acquire_write  s    $ }}Wg}AAr(   Fforcec                  SnU R                      U R                  S:X  a8  U(       a
   SSS5        gSU R                   S[        U 5       S3n[	        U5      eU(       a  SU l        OU =R                  S-  sl        U R                  S:X  a  SU l        SU l        SnSSS5        U(       a  U R                  R                  5         gg! , (       d  f       N1= f)	aD  
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 rj   z) that is not heldr	   T)	rY   rZ   r8   rn   ro   r[   r\   r_   rollback)ra   r   should_rollbackr$   s       r   r   ReadWriteLock.release  s        1$ !  2$..1ARPTXJVhi"3''#$   A% 1$%)"(,%"& ! II   ! s   CA+C
CNc             #     #    Uc  U R                   nUc  U R                  nU R                  XS9   Sv   U R                  5         g! U R                  5         f = f7f)aU  
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

NrN   )r!   r"   r   r   r   s      r   	read_lockReadWriteLock.read_lock7  sO      ?llG}}H'5	LLNDLLN   .AA AAAc             #     #    Uc  U R                   nUc  U R                  nU R                  XS9   Sv   U R                  5         g! U R                  5         f = f7f)aZ  
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

NrN   )r!   r"   r   r   r   s      r   
write_lockReadWriteLock.write_lockL  sO      ?llG}}H76	LLNDLLNr   c                    U R                  SS9  U R                  R                  5         [           [        R                  U R                  5        SSS5        g! , (       d  f       g= f)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   r_   r   r   r   discard)ra   s    r   r   ReadWriteLock.closea  sA     	4 		"$$TYY/ #""s    A
A')	r_   r[   rY   rZ   rX   r\   r"   r8   r!   r>   )r8   r?   r!   r@   r"   rA   rB   rC   )
r8   r?   r!   r@   r"   rA   r0   rA   rB   None)r"   rA   r!   r@   rB   r   )rq   Literal['read', 'write']rr   r   rs   r   rB   r
   )
rq   r   r!   r@   r"   rA   r}   r@   rB   r   )rq   r   r!   r@   r"   rA   rB   r
   )r!   r@   r"   rA   rB   r
   )r   rA   rB   r   r   )r!   zfloat | Noner"   zbool | NonerB   zGenerator[None]rB   r   )rD   rE   rF   rG   rH   r   r-   rI   rV   rW   r.   classmethodrP   rb   rg   ru   r   r   r   r   r   r   r   r   r   rJ   r=   r(   r   rC   rC   c   s[   ( DWCXJ@Xnn&OAC:Y]:.:9>:RV:	: :( ,
 !,), ,
 , , 
,(4- Q,Q7<QKOQ]bQ	Q2-BAD A A$BT B B( (- !: QU  ( RV  (
0r(   rC   )	metaclassr   )r!   r@   r"   rA   r#   r@   rB   r   )%
__future__r   atexitloggingrT   r4   r]   rV   rz   
contextlibr   r   typingr   r   weakrefr   _apir
   _errorr   collections.abcr   	getLoggerr   setr   rI   rW   r   r   registerr   r'   typer*   rC   r=   r(   r   <module>r      s    "   	     / ) ' $ )


J
',/E ) 1!( ! $ % + &% %PH00 H0r(   