Ë
    ùÇ:j‰<  ã                  ó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    úi/home/mcse/projects/srt_converter/srt-converter-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                  óV  — 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„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                ó¢  — |dk(  rdnd}|dk(  rdnd}| 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 )NÚreadÚwriteÚ	downgradeÚupgradezCannot acquire z	 lock on ú (lock id: z): already holding a z lock (z not allowed)z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ö   € Ø" fšn‘7°&ˆØ#'¨6¢>‘K°yˆ	Ø×Ñ Ò%à! $  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;rc   z'SELECT name FROM sqlite_schema LIMIT 1;)ÚtimeÚperf_counterr'   rY   Úexecuter   )	r[   rm   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               ó  — | j                   5  | j                  dkD  r| j                  |«      cd d d «       S 	 d d d «       t        j                  «       }| j                  ||¬«       	 | j                  ||||¬«      | j                  j                  «        S # 1 sw Y   Œ`xY w# t        j                  $ r)}dt        |«      vr‚ t        | j                  «      d ‚d }~ww xY w# | j                  j                  «        w xY w)Nr   r^   ©r"   rw   zdatabase is locked)rS   rT   rq   rt   ru   ra   Ú_do_acquire_innerrR   ÚreleaserW   ÚOperationalErrorÚstrr   r7   )r[   rm   r!   r"   rw   Úexcs         r   Ú_acquirezReadWriteLock._acquireÖ   sé   € Ø× Ñ ñ 	6Ø×Ñ !Ò#Ø×/Ñ/°Ó5÷	6ñ 	6Ø#÷	6ô ×&Ñ&Ó(ˆ
Ø×&Ñ&°À'Ð&ÔJð	-Ø×)Ñ)¨$°À(ÐWaÐ)Óbð ×"Ñ"×*Ñ*Õ,÷	6ð 	6ûô ×'Ñ'ò 	4Ø#¬3¨s«8Ñ3ØÜ˜$Ÿ.™.Ó)¨tÐ3ûð	4ûð
 ×"Ñ"×*Ñ*Õ,ús/   � BÁ(B# ÂB Â#CÂ6$CÃCÃC" Ã"C>c               ót  — | j                   5  | j                  dkD  r| j                  |«      cd d d «       S 	 d d d «       | j                  ||||¬«       | j                   5  || _        d| _        |dk(  rt        j                  «       | _        d d d «       t        | ¬«      S # 1 sw Y   ŒkxY w# 1 sw Y   Œ!xY w)Nr   r}   r	   rd   rh   )	rS   rT   rq   r{   rU   rP   rl   rV   r
   )r[   rm   r!   r"   rw   s        r   r~   zReadWriteLock._do_acquire_inneræ   s·   € ð × Ñ ñ 	6Ø×Ñ !Ò#Ø×/Ñ/°Ó5÷	6ñ 	6Ø#÷	6ð 	×!Ñ! $¨¸(ÈzÐ!ÔZØ× Ñ ñ 	>Ø!%ˆDÔØ ˆDÔØ�wŠÜ(1×(;Ñ(;Ó(=�Ô%÷		>ô
 " tÔ,Ð,÷	6ð 	6ú÷	>ð 	>ús   � B"Á!-B.Â"B+Â.B7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

        rc   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 rg   z) that is not heldr	   T)	rS   rT   r7   rj   rk   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_lock=  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_lockR  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.closeg  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—   )rm   úLiteral['read', 'write']r?   r
   )
rm   r˜   r!   r=   r"   r>   rw   r=   r?   r—   )rm   r˜   r!   r=   r"   r>   r?   r
   )
rm   r˜   r!   r=   r"   r>   rw   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   rq   r{   rƒ   r~   rˆ   rŠ   r   r   r‘   r”   r   rJ   r(   r   r@   r@   c   s…  … ññ( DWÓCX€JÐ@ÓXØ$�i—n‘nÓ&€OààACð:ØY]ñ:Ø.ð:Ø9>ð:ØRVð:à	ò:ó ð:ð( ð,ð
 Ø!ñ,à)ð,ð ð,ð
 ð,ð ð,ð 
ó,ó(4ó-ð$QØ,ðQØ7<ðQØKOðQØ]bðQà	óQó2-ð -à&ð-ð ð-ð
 ð-ð ð-ð 
ó-ð(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   rt   Ú
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N0Ð0ö N0r(   