o
    wOi                     @   s6   d dl Z d dlZd dlZe eZG dd deZdS )    Nc                   @   s   e Zd ZdZdd Zedd Zejdd Zedd ZdddZ	dd Z
dd Zdd Zd ddZedd Zdd Zdd Zdd ZdS )!LockzM
    Locking recipe for etcd, inspired by the kazoo recipe for zookeeper
    c                 C   sJ   || _ || _t j| _d|j|| _d| _	d | _
td| j| j d S )Nz{}/{}Fz#Initiating lock for %s with uuid %s)clientnameuuiduuid4hex_uuidformatlock_prefixpathis_taken	_sequence_logdebug)selfr   	lock_name r   =/home/ubuntu/.local/lib/python3.10/site-packages/etcd/lock.py__init__   s   zLock.__init__c                 C   s   | j S )z+
        The unique id of the lock
        )r   r   r   r   r   r      s   z	Lock.uuidc                 C   s0   | j }|| _ |  std || _ tdd S )Nz)The hand-set uuid was not found, refusingzInexistent UUID)r   
_find_lockr   warn
ValueError)r   valueold_uuidr   r   r   r       s   
c                 C   sP   | j s
td dS z
| j| j W dS  tjy'   td d| _ Y dS w )z2
        tells us if the lock is acquired
        zLock not takenFTz0Lock was supposedly taken, but we cannot find it)	r   r   r   r   readlock_keyetcdEtcdKeyNotFoundr   r   r   r   r   is_acquired)   s   

zLock.is_acquiredT  r   c                 C   sv   |   s'td| j | jj| j| j|dd}| |j td|j| j	 n|r4| jj| j
| j|d | j||dS )a  
        Acquire the lock.

        :param blocking Block until the lock is obtained, or timeout is reached
        :param lock_ttl The duration of the lock we acquired, set to None for eternal locks
        :param timeout The time to wait before giving up on getting a lock
        z Lock not found, writing it to %sT)ttlappendz#Lock key %s written, sequence is %s)r!   blockingtimeout)r   r   r   r   r   writer   _set_sequencekeyr   r   	_acquired)r   r$   lock_ttlr%   resr   r   r   acquire9   s   	zLock.acquirec                 C   st   | j s|   z.ztd| j | j| j W n tjy)   t	d| j Y nw W d| _
dS W d| _
dS d| _
w )z"
        Release the lock
        zReleasing existing lock %sz%Lock %s not found, nothing to releaseFN)r   r   r   r   r   r   deleter   r   infor   r   r   r   r   releaseN   s   
zLock.releasec                 C   s   | j ddd | S )z:
        You can use the lock as a contextmanager
        TN)r$   r*   )r,   r   r   r   r   	__enter__]   s   zLock.__enter__c                 C   s   |    dS )NF)r/   )r   typer   	tracebackr   r   r   __exit__d   s   zLock.__exit__c           	   
   C   s   |   \}}d| _| j|krtd d| _dS d| _|sdS |j}td| td|}	 z| jj|||j	d d}td|j|j
 | jd|d	W S  tjyb   td
| | jd|d	 Y S  tjyp } z|d }~w tjy~   td Y nw q.)NFzLock acquired!Tz"Lock not acquired, now watching %sr      )r%   indexzDetected variation for %s: %sr#   z%Key %s not present anymore, moving onzUnexpected exception)_get_lockerr   r   r   r   r(   maxr   watchmodifiedIndexactionr)   r   r   EtcdLockExpiredEtcdException	exception)	r   r$   r%   lockernearest	watch_keytrer   r   r   r)   h   s6   


zLock._acquiredc                 C   s"   | j std| jd t| j  S )NzNo sequence present./)r   r   r   strr   r   r   r   r      s   zLock.lock_keyc                 C   s   | | jdd| _d S )N rD   )replacer   lstripr   )r   r(   r   r   r   r'      s   zLock._set_sequencec                 C   s   | j rz| j| j}|j| _W dS  tjy   Y dS w | jrLz | jj| jddj	D ]}|j| jkr=| 
|j  W dS q+W dS  tjyK   Y dS w dS )NTF	recursive)r   r   r   r   r   r   r   r   r   leavesr'   r(   )r   r+   rB   r   r   r   r      s,   zLock._find_lockc                    s   dd | j j| jddjD }| js|   tdd |D td z4	| j
  dkr<td d d fW S td	d  d
   d t fdd|D fW S  tye   tdw )Nc                 S   s   g | ]}|qS r   r   ).0r+   r   r   r   
<listcomp>   s    z$Lock._get_locker.<locals>.<listcomp>TrI   c                 S   s   g | ]}|j qS r   r(   )rL   rB   r   r   r   rM      s    zLock keys found: %sr   z(No key before our one, we are the lockerzLocker: %s, key to watch: %sr4   c                 3   s&    | ]}|j  d   kr|V  qdS )r4   NrN   )rL   xilr   r   	<genexpr>   s   $ z#Lock._get_locker.<locals>.<genexpr>zLock not found)r   r   r   rK   r   r   sortedr   r   r5   r   nextr   r   r;   )r   resultsr   rP   r   r6      s"   
"
zLock._get_lockerN)Tr    r   )Tr   )__name__
__module____qualname____doc__r   propertyr   setterr   r,   r/   r0   r3   r)   r   r'   r   r6   r   r   r   r   r      s&    





r   )loggingr   r   	getLoggerrW   r   objectr   r   r   r   r   <module>   s
    
