o
    ôjÇF  ã                   @  sÀ  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mZ d dlm	Z	 d dl
mZ d dlmZmZmZ d dlmZmZmZmZ d dlmZ d	d
lmZmZ d	dlmZ d	dlmZ d	dlmZm Z m!Z!m"Z" d	dl#m$Z$ d	dl%m&Z&m'Z' er—d dl(m)Z)m*Z* d	dlm+Z+m,Z, ej-dkr‘d dlm.Z. nd dl/m.Z. ed Z0ed Z1dZ2de3d< e	dd�G dd„ dƒƒZ4e	dd�G dd„ dƒƒZ5e	G dd „ d ƒƒZ6G d!d"„ d"eƒZ7e	dd�G d#d$„ d$ƒƒZ8g d%¢Z9dS )&é    )ÚannotationsN)Úsuppress)Ú	dataclass)Úisfinite)ÚEventÚThreadÚcurrent_thread)ÚTYPE_CHECKINGÚFinalÚLiteralÚcast)Ú
WeakMethodé   )Ú_check_timeout_maxÚ_seconds)ÚLeaseSettingsMismatch)Úowner_is_stale)ÚMarkerSoftFileLockÚ	OwnerModeÚOwnerRecordÚparse_marker)Ú_read_lock_file)Úbreak_lock_fileÚtouch)ÚCallableÚMapping)ÚLockOptionsÚ_ExtraValue)é   é   )ÚUnpack)úmarker-missingúowner-changedúrefresh-failedZevicted)ÚokÚ	transientr!   r"   g      >@zFinal[float]Ú_DEFAULT_LEASE_DURATIONT)Úfrozenc                   @  s6   e Zd ZU dZded< ded< ded< dZded	< dS )
ÚLeaseCompromisez6Why a held lease stopped being this process's to hold.ÚstrÚ	lock_fileÚtokenÚCompromiseReasonÚreasonNúOSError | NoneÚerror)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r/   © r5   r5   ú`/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/filelock/_lease.pyr(   '   s   
 r(   c                   @  s"   e Zd ZU dZded< ded< dS )Ú
_HeartbeatzPA running heartbeat and the event that stops it, which only ever exist together.r   Úthreadr   ÚstopN)r0   r1   r2   r3   r4   r5   r5   r5   r6   r7   1   s   
 r7   c                   @  s6   e Zd ZU dZdZded< dZded< dZded< dS )	Ú_LeaseClaimzNThe state of one claim: its token, its heartbeat, and how that claim was lost.Nú
str | Noner+   úLeaseCompromise | NoneÚ
compromisez_Heartbeat | NoneÚ	heartbeat)r0   r1   r2   r3   r+   r4   r=   r>   r5   r5   r5   r6   r:   9   s
   
 r:   c                      sø   e Zd ZU dZdZded< dZded< dZd	ed
< edddœdF‡ fdd„Z	dG‡ fdd„Z
edHd d!„ƒZedId"d#„ƒZedJd%d&„ƒZedKd(d)„ƒZdL‡ fd*d+„ZdL‡ fd,d-„ZdM‡ fd/d0„ZdL‡ fd1d2„ZdNd4d5„ZdOd<d=„ZdLd>d?„ZdPdDdE„Z‡  ZS )QÚSoftFileLeaseaC  
    Existence lock whose claim expires, so a peer may take it while the previous holder still runs.

    A lease trades mutual exclusion for progress. The holder publishes a claim and refreshes it every
    ``heartbeat_interval`` seconds; a contender takes the marker once it has seen no refresh for ``lease_duration``
    seconds of its own monotonic clock, so clock skew between hosts cannot expire a live claim. Nothing stops the
    expired holder: it keeps running, and it keeps using whatever the lock protects. Treat the lease as a hint about
    who *should* be working, not as a guarantee that only one worker is.

    To make a protected resource reject a superseded holder, that resource must be linearizable and must fence on a
    monotonic generation it controls. :attr:`token` names a claim; it does not fence one. Where overlap is unacceptable,
    use :class:`StrictSoftFileLock <filelock.StrictSoftFileLock>` instead.

    Every contender for a path must agree on ``lease_duration``. A contender that finds a claim published under a
    different duration raises :class:`LeaseSettingsMismatch <filelock.LeaseSettingsMismatch>` rather than apply its own
    expiry to a peer that never agreed to it.

    Expiry reclaims less on Windows, which refuses to rename or delete a file another process holds open. A peer there
    takes an expired claim only once the previous holder's process exits and its handle closes; a holder that lives on
    but stops refreshing keeps the marker. Unix reclaims the marker either way.

    ``on_compromise`` fires from the heartbeat thread when a refresh fails, or when the marker vanishes or names another
    owner. The holder should stop touching the protected resource when it runs. Because it runs on that thread, a
    ``release()`` inside it only takes effect when the lease was built with ``thread_local=False``; the default
    thread-local context hides the claim from every thread but the one that acquired it, so the release does nothing.
    Signal the acquiring thread instead when the context stays thread-local.

    .. versionadded:: 3.30.0

    Úleaser   Ú_owner_modeFÚboolÚ_lifetime_supportedz(lease_duration sets when a lease expiresr)   Ú_lifetime_unsupported_reasonN)Úlease_durationÚheartbeat_intervalÚon_compromiser*   ústr | os.PathLike[str]rE   ÚfloatrF   úfloat | NonerG   ú(Callable[[LeaseCompromise], None] | NoneÚkwargsúUnpack[LockOptions]ÚreturnÚNonec                  s¸   t td|ƒ }ƒr|dkrd|›�}t|ƒ‚|du r|d ntd|ƒ}d|  k r-|k s7n d|›�}t|ƒ‚td|ƒ tƒ j|fi |¤Ž || _|| _|| _|  	¡ sZt
ƒ | j_dS dS )aÂ  
        Create a lease.

        :param lease_duration: seconds a contender must see the marker go unrefreshed before it may take the claim.
            Every contender for the path must pass the same value.
        :param heartbeat_interval: seconds between refreshes. Defaults to a third of ``lease_duration``, leaving room
            for two missed refreshes before a peer may take the claim. Must be shorter than ``lease_duration`` and at
            most :data:`threading.TIMEOUT_MAX`.
        :param on_compromise: called from the heartbeat thread with a :class:`LeaseCompromise` when the claim is lost.
        :param kwargs: every other :class:`BaseFileLock <filelock.BaseFileLock>` option, ``timeout`` and ``mode`` among
            them. The metaclass passes them all by keyword, and taking them here lets
            :class:`AsyncSoftFileLease <filelock.AsyncSoftFileLease>` add the async plumbing a fixed signature would
            hide.

        rE   r   z0lease_duration must be positive and finite, got Nr   rF   zBheartbeat_interval must be positive and below lease_duration, got )r   r   Ú
ValueErrorr   ÚsuperÚ__init__Ú_lease_durationÚ_heartbeat_intervalÚ_on_compromiseÚis_thread_localr:   Ú_contextÚlease_claim)Úselfr*   rE   rF   rG   rL   Úmsg©Ú	__class__r5   r6   rR   h   s    
ÿ

ÿzSoftFileLease.__init__úMapping[str, _ExtraValue]údict[str, tuple[str, str]]c                  s¾   t ƒ  |¡}tdtd| dt¡ƒƒ}|| jkr"t|ƒt| jƒf|d< | d¡ }d u r0| jd ntdtd|ƒƒ}|| jkrHt|ƒt| jƒf|d< | d¡ }| j	ur]t|ƒt| j	ƒf|d< |S )NrE   rI   rF   r   rG   )
rQ   Ú_singleton_extra_mismatchesr   r   Úgetr&   rS   r)   rT   rU   )rY   rL   Z
mismatchesrE   ZpassedrF   rG   r[   r5   r6   r_   “   s   ÿ

ÿý
z)SoftFileLease._singleton_extra_mismatchesr:   c                 C  s&   |   ¡ }|j }d u rtƒ  }|_|S ©N)Ú_hold_staterX   r:   )rY   ÚstateÚclaimr5   r5   r6   Ú_claim§   s   zSoftFileLease._claimc                 C  s   | j S )zEThe staleness in seconds after which a contender may take this claim.)rS   ©rY   r5   r5   r6   rE   ¯   s   zSoftFileLease.lease_durationr;   c                 C  ó   | j jS )z½
        The token naming the claim this process published.

        :returns: the token while the lease is held, ``None`` otherwise. It identifies a claim; it does not fence one.

        )re   r+   rf   r5   r5   r6   r+   ´   s   zSoftFileLease.tokenr<   c                 C  rg   )aá  
        The loss of claim the heartbeat observed.

        The value outlives ``release()``, so a holder can check after leaving the ``with`` block whether its work may
        have overlapped a successor; the next acquisition resets it, as :attr:`SoftReadWriteLock.compromise
        <filelock.SoftReadWriteLock.compromise>` does.

        :returns: the :class:`LeaseCompromise` of the latest claim, or ``None`` while that claim holds or held to its
            release

        )re   r=   rf   r5   r5   r6   r=   ¾   s   zSoftFileLease.compromisec                   sˆ   | j }|  ¡  t d¡ |_}d |_ztƒ  ¡  W n ty$   d |_‚ w | j	j
 }d ur?| j	j }d ur?|  ||||¡ d S d |_d S )Né   )re   Ú_stop_heartbeatÚsecretsÚ	token_hexr+   r=   rQ   Ú_acquireÚBaseExceptionrW   Úlock_file_fdÚlock_file_fd_identityÚ_start_heartbeat)rY   rd   r+   ÚfdÚidentityr[   r5   r6   rl   Í   s    þ
þ
zSoftFileLease._acquirec                   s   |   ¡  d | j_tƒ  ¡  d S ra   )ri   re   r+   rQ   Ú_releaserf   r[   r5   r6   rs   á   s   zSoftFileLease._releaser   c                   s   t ƒ  ¡ j| jj| jd�S )N)r+   rE   )rQ   Ú_published_recordÚ_replacere   r+   rS   rf   r[   r5   r6   rt   æ   s   zSoftFileLease._published_recordc                   s  |   ¡  }d u rtƒ  ¡  d S |\}}}|jdkr1|j| jkr1| j› d|j›d| j›d�}t|ƒ‚tt	ƒ�L |jdkrTt
|j|j|jƒrTt| j||ƒ 	 W d   ƒ d S |jdkrq|  ||¡| jkryt| j||ƒ W d   ƒ d S W d   ƒ d S W d   ƒ d S 1 s„w   Y  d S )Nr@   z holds a lease of z s but this contender configured z:s; every contender for a path must agree on lease_durationÚunknown)Ú
_read_peerrQ   Ú_try_break_stale_lockÚmoderE   rS   r*   r   r   ÚOSErrorr   ÚpidÚhostnameÚstartr   Z_marker_unchanged_for)rY   ÚpeerÚownerÚmtimeÚinorZ   r[   r5   r6   rx   é   s,   

ÿÿ
ûùú"úz#SoftFileLease._try_break_stale_lockú%tuple[OwnerRecord, float, int] | Nonec                 C  sh   t ttƒ�% t| jƒ\}}}t|ƒ }d ur"|||fW  d   ƒ S W d   ƒ d S 1 s-w   Y  d S ra   )r   rz   rP   r   r*   r   )rY   Úcontentr€   r�   r   r5   r5   r6   rw     s   ý
þüzSoftFileLease._read_peerrd   rq   Úintrr   útuple[int, int]r+   c                   sh   t ƒ ‰ tt| j‡ fdd„ƒ| j||||ˆ | j| jd�	}t|jdt	 
¡ › �dd�}t|ˆ ƒ|_| ¡  d S )Nc                   s   ˆ   ¡ S ra   )Úset)Ú_©r9   r5   r6   Ú<lambda>  s    z0SoftFileLease._start_heartbeat.<locals>.<lambda>)	Úreportr*   rd   rq   rr   r+   r9   ÚintervalÚdurationzfilelock-lease-T)ÚtargetÚnameÚdaemon)r   Ú_RefreshLoopr   Ú_report_compromiser*   rT   rS   r   ÚrunÚosÚgetpidr7   r>   r}   )rY   rd   rq   rr   r+   Úloopr8   r5   rˆ   r6   rp     s   õzSoftFileLease._start_heartbeatc                 C  sh   | j }|j }d u rd S |j ¡  d |_|jjd ur.|jtƒ ur0t ¡ s2|jj	| j
d� d S d S d S d S )N)Útimeout)re   r>   r9   r†   r8   Úidentr   ÚsysÚis_finalizingÚjoinrT   )rY   rd   r>   r5   r5   r6   ri   %  s   
 ÿzSoftFileLease._stop_heartbeatr-   r,   r/   r.   c                 C  s2   t | j|||d�|_| jd ur|  |j¡ d S d S )N)r*   r+   r-   r/   )r(   r*   r=   rU   )rY   rd   r-   r/   r+   r5   r5   r6   r‘   0  s   

ÿz SoftFileLease._report_compromise)r*   rH   rE   rI   rF   rJ   rG   rK   rL   rM   rN   rO   )rL   r]   rN   r^   )rN   r:   )rN   rI   )rN   r;   )rN   r<   ©rN   rO   )rN   r   )rN   r‚   )
rd   r:   rq   r„   rr   r…   r+   r)   rN   rO   )
rd   r:   r-   r,   r/   r.   r+   r)   rN   rO   )r0   r1   r2   r3   rA   r4   rC   rD   r&   rR   r_   Úpropertyre   rE   r+   r=   rl   rs   rt   rx   rw   rp   ri   r‘   Ú__classcell__r5   r5   r[   r6   r?   B   s4   
 ú+	


r?   c                   @  sn   e Zd ZU dZded< ded< ded< ded	< d
ed< ded< ded< ded< ded< ddd„Zddd„ZdS )r�   zYWhat a heartbeat thread needs to refresh one claim, holding the lease itself only weakly.zPWeakMethod[Callable[[_LeaseClaim, CompromiseReason, OSError | None, str], None]]rŠ   r)   r*   r:   rd   r„   rq   r…   rr   r+   r   r9   rI   r‹   rŒ   rN   rO   c                 C  sª   t  ¡ d}}| j | j¡sS|  ¡ \}}|dkr t  ¡ d}}q|dkr)|s)d}q|dkr:t  ¡ | | j| j k r:q|  ¡  }d urQ|| j|dkrKdn||| j	ƒ d S d S )NFr$   r!   Tr%   r#   )
ÚtimeÚ	monotonicr9   Úwaitr‹   Ú_refreshrŒ   rŠ   rd   r+   )rY   Zlast_successÚmissingÚoutcomer/   rŠ   r5   r5   r6   r’   M  s    óz_RefreshLoop.runú&tuple[_RefreshOutcome, OSError | None]c              
   C  sÂ   zt  | j¡}W n+ ty } z
d|fW  Y d }~S d }~w ty3 } z
d|fW  Y d }~S d }~ww |j|jf| jkr?dS zt| j| j	d� W dS  ty` } z
d|fW  Y d }~S d }~ww )Nr!   r%   )r"   N)rq   )r$   N)
r“   Úlstatr*   ÚFileNotFoundErrorrz   Úst_devÚst_inorr   r   rq   )rY   Ústr/   r5   r5   r6   r¡   `  s$   €€ÿþ€ÿz_RefreshLoop._refreshNr›   )rN   r¤   )r0   r1   r2   r3   r4   r’   r¡   r5   r5   r5   r6   r�   ?  s   
 
r�   )r,   r(   r?   r:   ):Ú
__future__r   r“   rj   r˜   rž   Ú
contextlibr   Údataclassesr   Úmathr   Ú	threadingr   r   r   Útypingr	   r
   r   r   Úweakrefr   Ú_apir   r   Ú_errorr   Ú	_identityr   Ú_markerr   r   r   r   Ú_softr   Ú_utilr   r   Úcollections.abcr   r   r   r   Úversion_infor    Útyping_extensionsr,   Z_RefreshOutcomer&   r4   r(   r7   r:   r?   r�   Ú__all__r5   r5   r5   r6   Ú<module>   sJ    
	 ~1