Ë
     êñiÛ;  ã                  ób  — 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j8                  «       Zdd„Z ej>                  e«       dZ dd„Z! G d„ de"«      Z# G d„ de#¬«      Z$y)é    )ÚannotationsN)ÚcontextmanagerÚsuppress)ÚTYPE_CHECKINGÚLiteral)ÚWeakValueDictionaryé   )ÚAcquireReturnProxy)ÚTimeout)Ú	GeneratorÚfilelockzset[sqlite3.Connection]Ú_all_connectionsc                 óò   — t         5  t        t        «      D ]*  } t        t        «      5  | j                  «        d d d «       Œ, t        j                  «        d d d «       y # 1 sw Y   ŒSxY w# 1 sw Y   y xY w©N)Ú_all_connections_lockÚlistr   r   Ú	ExceptionÚcloseÚclear)Úcons    úV/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/filelock/_read_write.pyÚ_cleanup_connectionsr      sm   € Ü	ñ !ÜÔ(Ó)ò 	ˆCÜœ)Ó$ñ Ø—	‘	”÷ð ð	ô 	×ÑÔ ÷	!ð !÷ð ú÷!ð !ús!   ‡"A-©A!ºA-Á!A*Á&A-Á-A6iÿ“5wc               óð   — |du ry| dk(  rt         S | dk  rd}t        |«      ‚| dkD  rt        | |z
  d«      n| }t        |dz  «      }|t         kD  s|dk  r!t        j                  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ÚmaxÚintÚ_LOGGERÚwarning)ÚtimeoutÚblockingÚalready_waitedÚmsgÚ	remainingÚ
timeout_mss         r   Útimeout_for_sqliter'   (   s†   € Ø�5ÑØà�"‚}Ü%Ð%à�‚{Ø;ˆÜ˜‹oÐà4;¸a²K”�G˜nÑ,¨aÔ0ÀW€IÜ�Y Ñ%Ó&€JØÔ*Ò*¨j¸1ªnÜ�‰ÐQÐSZÔ\rÔsÜ%Ð%ØÐó    c                  óX   ‡ — 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_lockT©r"   Úis_singletonc          	     óÒ  •— |st         ‰| �  ||||¬«      S t        j                  |«      j	                  «       }| j
                  5  || j                  vr#t         ‰| �  ||||¬«      }|| j                  |<   n| j                  |   }|j                  |k7  s|j                  |k7  r-d|j                  › d|j                  › d|› d|› �}t        |«      ‚|cd d d «       S # 1 sw Y   y xY w)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"   r/   Ú
normalizedÚinstancer$   Ú	__class__s	           €r   r2   z_ReadWriteLockMeta.__call__G   sú   ø€ ñ Ü‘7Ñ# I¨wÀÐXdÐ#ÓeÐeä—\‘\ )Ó,×4Ñ4Ó6ˆ
Ø× Ñ ñ 	Ø §¡Ñ/Ü ™7Ñ+¨I°wÈÐ`lÐ+Óm�Ø-5�—‘˜zÒ*àŸ>™>¨*Ñ5�à×Ñ 7Ò*¨h×.?Ñ.?À8Ò.Kà:¸8×;KÑ;KÐ:LÈKÐX`×XiÑXiÐWjð k5Ø5<°I¸[ÈÈ
ðTð ô ! “oÐ%Ø÷	÷ 	ò 	ús   ÁBCÃC&©r   )
r7   ústr | os.PathLike[str]r!   Úfloatr"   Úboolr/   r>   ÚreturnÚReadWriteLock)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r2   Ú__classcell__)r:   s   @r   r*   r*   ;   sc   ø… ñð 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	 	 	 	 	 	 	 	 	 	 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y)"r@   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               ó   —  | |||¬«      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

        rH   © )r6   r7   r!   r"   s       r   Úget_lockzReadWriteLock.get_lock{   s   € ñ  �9˜g°Ô9Ð9r(   r.   c               ó¢  — t        j                  |«      | _        || _        || _        t        j                  «       | _        t        j                  «       | _        d| _	        d | _
        d | _        t        j                  | j                  d¬«      | _        t        5  t         j#                  | j                  «       d d d «       y # 1 sw Y   y xY w)Nr   F)Úcheck_same_thread)ÚosÚfspathr7   r!   r"   Ú	threadingÚLockÚ_transaction_lockÚ_internal_lockÚ_lock_levelÚ_current_modeÚ_write_thread_idÚsqlite3ÚconnectÚ_conr   r   Úadd)Úselfr7   r!   r"   r/   s        r   Ú__init__zReadWriteLock.__init__�   s˜   € ô Ÿ™ 9Ó-ˆŒØˆŒØ ˆŒÜ!*§¡Ó!1ˆÔÜ'Ÿn™nÓ.ˆÔØˆÔØ>BˆÔØ,0ˆÔÜ—O‘O D§N¡NÀeÔLˆŒ	Ü"ñ 	,Ü× Ñ  §¡Ô+÷	,÷ 	,ñ 	,ús   Â CÃCc               óð   — |s| j                   j                  d¬«      }n?|dk(  r| j                   j                  d¬«      }n| j                   j                  d|¬«      }|st        | j                  «      d ‚y )NFrH   r   T©r"   r!   )rR   Úacquirer   r7   )r[   r"   r!   Úacquireds       r   Ú_acquire_transaction_lockz'ReadWriteLock._acquire_transaction_lock¡   sr   € ÙØ×-Ñ-×5Ñ5¸uÐ5ÓE‰HØ˜Š]Ø×-Ñ-×5Ñ5¸tÐ5ÓD‰Hà×-Ñ-×5Ñ5¸tÈWÐ5ÓUˆHÙÜ˜$Ÿ.™.Ó)¨tÐ3ð r(   c                ó~  — | j                   |k7  r0d|› d| j                  › dt        | «      › d|› d|› d�}t        |«      ‚|dk(  rYt	        j
                  «       x}| j                  k7  r6d| 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)	rU   r7   ÚidÚRuntimeErrorrP   Ú	get_identrV   rT   r
   )r[   ÚmodeÚoppositeÚ	directionr$   Úcurs         r   Ú_validate_reentrantz!ReadWriteLock._validate_reentrant«   sÜ   € Ø×Ñ Ò%à! $  y°·±Ð0@ÀÌBÈtËHÈ:ð V%Ø%- J¨g°i°[ÀðOð ô ˜sÓ#Ð#Ø�7Š?¤y×':Ñ':Ó'<Ð < À×AVÑAVÒVà/°·±Ð/?¸{Ì2ÈdË8È*ð UØ"˜eÐ#?À×@UÑ@UÐ?VðXð ô ˜sÓ#Ð#Ø×Ò˜AÑÕÜ! tÔ,Ð,r(   c               ój  — t        j                  «       |z
  }t        |||¬«      }| j                  j	                  d|› d�«      j                  «        | j                  j	                  d«      j                  «        t        j                  «       |z
  }t        |||¬«      x}|k7  r-| j                  j	                  d|› d�«      j                  «        |dk(  rdnd}| j                  j	                  |«      j                  «        |dk(  r*| j                  j	                  d	«      j                  «        y y )
N)r"   r#   zPRAGMA busy_timeout=ú;zPRAGMA journal_mode=MEMORY;rd   zBEGIN EXCLUSIVE TRANSACTION;zBEGIN TRANSACTION;Úreadz'SELECT name FROM sqlite_schema LIMIT 1;)ÚtimeÚperf_counterr'   rY   Úexecuter   )	r[   rj   r!   r"   Ú
start_timeÚwaitedr&   Ú
recomputedÚstmts	            r   Ú_configure_and_beginz"ReadWriteLock._configure_and_begin»   s  € ô ×"Ñ"Ó$ zÑ1ˆÜ'¨¸(ÐSYÔZˆ
Ø�	‰	×ÑÐ0°°¸AÐ>Ó?×EÑEÔGð 	�	‰	×ÑÐ7Ó8×>Ñ>Ô@ä×"Ñ"Ó$ zÑ1ˆÜ,¨W¸xÐX^Ô_Ð_ˆJÐdnÒnØ�I‰I×ÑÐ 4°Z°LÀÐBÓC×IÑIÔKØ15¸²Ñ-ÐFZˆØ�	‰	×Ñ˜$Ó×%Ñ%Ô'Ø�6Š>ð �I‰I×ÑÐGÓH×NÑNÕPð r(   c               ó¬  — |dk(  rdnd}|dk(  rdnd}| j                   5  | j                  dkD  r| j                  |||«      cd d d «       S 	 d d d «       t        j                  «       }| j                  ||¬«       	 | j                   5  | j                  dkD  r6| j                  |||«      cd d d «       | j                  j                  «        S 	 d d d «       | j                  ||||¬«       | j                   5  || _	        d| _        |dk(  rt        j                  «       | _        d d d «       t        | ¬	«      | j                  j                  «        S # 1 sw Y   �Œ	xY w# 1 sw Y   Œ’xY w# 1 sw Y   ŒHxY w# t        j                  $ r)}d
t!        |«      vr‚ t#        | j$                  «      d ‚d }~ww xY w# | j                  j                  «        w xY w)Nrq   rd   Ú	downgradeÚupgrader   r^   )r"   ru   r	   re   zdatabase is locked)rS   rT   rn   rr   rs   ra   rR   Úreleasery   rU   rP   ri   rV   r
   rW   ÚOperationalErrorÚstrr   r7   )r[   rj   r!   r"   rk   rl   ru   Úexcs           r   Ú_acquirezReadWriteLock._acquireÔ   sÊ  € Ø" fšn‘7°&ˆØ#'¨6¢>‘K°yˆ	à× Ñ ñ 	KØ×Ñ !Ò#Ø×/Ñ/°°hÀ	ÓJ÷	Kñ 	KØ#÷	Kô ×&Ñ&Ó(ˆ
Ø×&Ñ&°À'Ð&ÔJð	-à×$Ñ$ñ OØ×#Ñ# aÒ'Ø×3Ñ3°D¸(ÀIÓN÷Oð Oð& ×"Ñ"×*Ñ*Õ,ð% (÷Oð ×%Ñ% d¨G¸hÐS]Ð%Ô^à×$Ñ$ñ BØ%)�Ô"Ø#$�Ô Ø˜7’?Ü,5×,?Ñ,?Ó,A�DÔ)÷	Bô &¨4Ô0ð ×"Ñ"×*Ñ*Õ,÷7	Kñ 	Kú÷Oð Oú÷Bð Bûô ×'Ñ'ò 	4Ø#¬3¨s«8Ñ3ØÜ˜$Ÿ.™.Ó)¨tÐ3ûð	4ûð
 ×"Ñ"×*Ñ*Õ,úse   Ÿ"EÁ<E8 Â"E Â*	E8 Ã)E8 Ã8-E,Ä%E8 ÅEÅ E)Å%E8 Å,E5Å1E8 Å8F4Æ$F/Æ/F4Æ4F7 Æ7Gc               ó*   — | j                  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

        rq   rH   ©r�   ©r[   r!   r"   s      r   Úacquire_readzReadWriteLock.acquire_readõ   s   € ð  �}‰}˜V W°xˆ}Ó@Ð@r(   c               ó*   — | j                  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

        rd   rH   rƒ   r„   s      r   Úacquire_writezReadWriteLock.acquire_write  s   € ð$ �}‰}˜W g¸ˆ}ÓAÐAr(   F©Úforcec               ó„  — d}| j                   5  | j                  dk(  r3|r
	 ddd«       yd| j                  › dt        | «      › d�}t	        |«      ‚|rd| _        n| xj                  dz  c_        | j                  dk(  rd| _        d| _        d}ddd«       |r| j                  j                  «        yy# 1 sw Y   Œ'xY w)	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 rc   z) that is not heldr	   T)	rS   rT   r7   rg   rh   rU   rV   rY   Úrollback)r[   r‰   Úshould_rollbackr$   s       r   r}   zReadWriteLock.release  sÊ   € ð  ˆØ× Ñ ñ 	'Ø×Ñ 1Ò$ÙØ÷	'ð 	'ð 2°$·.±.Ð1AÀÌRÐPTËXÈJÐVhÐi�Ü" 3Ó'Ð'ÙØ#$�Õ à× Ò  AÑ%Õ Ø×Ñ 1Ò$Ø%)�Ô"Ø(,�Ô%Ø"&�÷	'ñ Ø�I‰I×ÑÕ ð ÷	'ð 	'ús   �B6«A%B6Â6B?Nc             #  ó¾   K  — |€| j                   }|€| j                  }| j                  ||¬«       	 d–— | j                  «        y# | j                  «        w xY w­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

        NrH   )r!   r"   r…   r}   r„   s      r   Ú	read_lockzReadWriteLock.read_lock8  sR   è ø€ ð ˆ?Ø—l‘lˆGØÐØ—}‘}ˆHØ×Ñ˜'¨HÐÔ5ð	Ûà�L‰L�NøˆD�L‰L�Nüó   ‚0A³A ·AÁAÁAc             #  ó¾   K  — |€| j                   }|€| j                  }| j                  ||¬«       	 d–— | j                  «        y# | j                  «        w xY w­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

        NrH   )r!   r"   r‡   r}   r„   s      r   Ú
write_lockzReadWriteLock.write_lockM  sR   è ø€ ð ˆ?Ø—l‘lˆGØÐØ—}‘}ˆHØ×Ñ˜7¨XÐÔ6ð	Ûà�L‰L�NøˆD�L‰L�Nür�   c                óÐ   — | j                  d¬«       | j                  j                  «        t        5  t        j                  | j                  «       ddd«       y# 1 sw Y   yxY w)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}   rY   r   r   r   Údiscard)r[   s    r   r   zReadWriteLock.closeb  sJ   € ð 	�‰˜4ˆÔ Ø�	‰	�‰ÔÜ"ñ 	0Ü×$Ñ$ T§Y¡YÔ/÷	0÷ 	0ñ 	0ús   ³ AÁA%r;   )r7   r<   r!   r=   r"   r>   r?   r@   )
r7   r<   r!   r=   r"   r>   r/   r>   r?   ÚNone)r"   r>   r!   r=   r?   r”   )rj   úLiteral['read', 'write']rk   r   rl   r   r?   r
   )
rj   r•   r!   r=   r"   r>   ru   r=   r?   r”   )rj   r•   r!   r=   r"   r>   r?   r
   )r!   r=   r"   r>   r?   r
   )r‰   r>   r?   r”   r   )r!   zfloat | Noner"   zbool | Noner?   zGenerator[None]©r?   r”   )rA   rB   rC   rD   r   r,   rE   rP   rQ   r-   ÚclassmethodrK   r\   ra   rn   ry   r�   r…   r‡   r}   r   rŽ   r‘   r   rJ   r(   r   r@   r@   c   sQ  … ññ( DWÓCX€JÐ@ÓXØ$�i—n‘nÓ&€OààACð:ØY]ñ:Ø.ð:Ø9>ð:ØRVð:à	ò:ó ð:ð( ð,ð
 Ø!ñ,à)ð,ð ð,ð
 ð,ð ð,ð 
ó,ó(4ó-ð QØ,ðQØ7<ðQØKOðQØ]bðQà	óQó2-ðBAÀDõ Að$BÀTõ Bð( (-õ !ð: ðÐQUô ó ðð( ðÐRVô ó ðô(
0r(   r@   )Ú	metaclassr–   )r!   r=   r"   r>   r#   r=   r?   r   )%Ú
__future__r   ÚatexitÚloggingrN   r3   rW   rP   rr   Ú
contextlibr   r   Útypingr   r   Úweakrefr   Ú_apir
   Ú_errorr   Úcollections.abcr   Ú	getLoggerr   Úsetr   rE   rQ   r   r   Úregisterr   r'   Útyper*   r@   rJ   r(   r   ú<module>r¦      s    ðÞ "ã Û Û 	Û Û Û Û ß /ß )Ý 'å $Ý áÝ)à
ˆ'×
Ñ
˜JÓ
'€á,/«EÐ Ð)Ó 1Ø&˜	Ÿ™Ó(Ð ó!ð €‡�Ð$Ô %ð +Ð óô&%˜ô %ôPI0Ð0ö I0r(   