o
    c=                     @   s  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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 d dlmZ d d	lmZ eeZd
d ZG dd dZG dd dZdeeef fddZdeeef fddZdeeef fddZdS )    )contextmanagerN)Path)Callable)Optional)Union)_utils)_interprocess_mechanism)%_interprocess_reader_writer_mechanismc              
   C   sp   	 zt |  W dS  ty7 } z"|jtjkr%t j| s W Y d }~dS |jtjkr2W Y d }~dS  d }~ww )NFT)osmakedirsOSErrorerrnoEEXISTpathisdirZEISDIR)r   e r   ZG:\Development\the-witcher-3-mod-manager\.venv\lib\site-packages\fasteners\process_lock.py_ensure_tree%   s   r   c                   @   s   e Zd Z	 dZdZejdfdeee	f de
egdf deej fddZd	d
 Zdd Z				d#dedededee def
ddZdd Zdd Zdd Zdd Zdd Zdd  Zd!d" ZdS )$InterProcessLock皙?{Gz?Nr   
sleep_funcloggerc                 C   s2   	 d | _ t|| _d| _|| _t|t| _d S )NF)	lockfiler   canonicalize_pathr   acquiredr   pick_first_not_noneLOGr   selfr   r   r   r   r   r   __init__@   s   	zInterProcessLock.__init__c              
   C   sr   z|    W dS  ty8 } z%|jtjtjfv r)|r| r%W Y d }~dS t t	d| j
|d d }~ww )NFz9Unable to acquire lock on `%(path)s` due to %(exception)s)r   	exceptionT)trylockIOErrorr   EACCESEAGAINexpiredr   
RetryAgain	threadingThreadErrorr   )r    blockingwatchr   r   r   r   _try_acquireS   s   
zInterProcessLock._try_acquirec                 C   sX   t j| j}|rt|}|r| jtjd| | jd u s!| jj	r*t
| jd| _d S d S )NCreated lock base path `%s`a)r
   r   dirnamer   r   logr   BLATHERr   closedopenr    basedirZmade_basedirr   r   r   _do_openg   s   
zInterProcessLock._do_openTr+   delay	max_delaytimeoutreturnc                 C   s   	 |dk r	t d|d ur|dk rt d||kr|}|   tj|d}tj||| j|d}| || j||}W d    n1 sCw   Y  |sLdS d| _| j	tj
d| j| |j dS )	Nr   +Delay must be greater than or equal to zero-Timeout must be greater than or equal to zerodurationr   r,   FTHAcquired file lock `%s` after waiting %0.3fs [%s attempts were required])
ValueErrorr7   r   	StopWatchRetryr   r-   r   r   r1   r2   r   elapsedattempts)r    r+   r8   r9   r:   r,   rgottenr   r   r   acquiret   s0   

zInterProcessLock.acquirec                 C   s"   | j d ur| j   d | _ d S d S N)r   closer    r   r   r   	_do_close   s   


zInterProcessLock._do_closec                 C   s    |   }|std| j | S )NFUnable to acquire a file lock on `%s` (when used as a context manager))rI   r)   r*   r   )r    rH   r   r   r   	__enter__   s   zInterProcessLock.__enter__c              
   C   s   	 | j s	tdz|   W n ty, } zd| jf}| j| t||d }~ww d| _ z|   W n t	yH   | jd| j Y d S w | j
tjd| j d S )Nz#Unable to release an unaquired lock1Could not unlock the acquired lock opened on `%s`F.Could not close the file handle opened on `%s`*Unlocked and closed file lock open on `%s`)r   r)   r*   unlock	Exceptionr   r   r"   rM   r$   r1   r   r2   )r    r   msgr   r   r   release   s.   



zInterProcessLock.releasec                 C   s   |    d S rJ   )rV   )r    exc_typeexc_valexc_tbr   r   r   __exit__   s   zInterProcessLock.__exit__c                 C   s   t j| jS rJ   )r
   r   existsrL   r   r   r   r[      s   zInterProcessLock.existsc                 C      t | j d S rJ   )r   r#   r   rL   r   r   r   r#         zInterProcessLock.trylockc                 C   r\   rJ   )r   rS   r   rL   r   r   r   rS      r]   zInterProcessLock.unlockTr   r   N)__name__
__module____qualname__	MAX_DELAYDELAY_INCREMENTtimesleepr   r   strr   floatr   loggingLoggerr!   r-   r7   boolrI   rM   rO   rV   rZ   r[   r#   rS   r   r   r   r   r   :   sJ    


,	r   c                   @   s   e Zd Z	 dZdZejdfdeee	f de
egdf deej fddZed#d	d
Zed#ddZdd Zdd Z				d$dededededef
ddZ				d$dededededef
ddZ			d%ddZdd Zdd  Zd!d" ZdS )&InterProcessReaderWriterLockr   r   Nr   r   r   c                 C   s,   	 d | _ t|| _|| _t|t| _d S rJ   )r   r   r   r   r   r   r   r   r   r   r   r   r!      s
   	z%InterProcessReaderWriterLock.__init__c                 c   s6    	 | j d||d d z
d V  W |   d S |   w )NTr+   r8   r9   r:   )acquire_read_lockrelease_read_lock)r    r8   r9   r   r   r   	read_lock   s   z&InterProcessReaderWriterLock.read_lockc                 c   sJ    	 | j d||d d}|std| j z
d V  W |   d S |   w )NTrl   rN   )acquire_write_lockr)   r*   r   release_write_lock)r    r8   r9   rH   r   r   r   
write_lock   s   z'InterProcessReaderWriterLock.write_lockc              
   C   s`   z	t | j|}W n ty } z
td| j|d }~ww |r$dS |r*| r,dS t	
 )Nz'Unable to acquire lock on {} due to {}!TF)r	   r#   r   rT   r)   r*   formatr   r'   r   r(   )r    r+   r,   	exclusiverH   r   r   r   r   r-     s   z)InterProcessReaderWriterLock._try_acquirec                 C   sP   t j| j}|rt|}|r| jtjd| | jd u r&t	
| j| _d S d S )Nr.   )r
   r   r0   r   r   r1   r   r2   r   r	   
get_handler5   r   r   r   r7     s   

z%InterProcessReaderWriterLock._do_openTr+   r8   r9   r:   r;   c                 C      	 | j ||||ddS )NFrt   _acquirer    r+   r8   r9   r:   r   r   r   rm        z.InterProcessReaderWriterLock.acquire_read_lockc                 C   rv   )NTrw   rx   rz   r   r   r   rp   6  r{   z/InterProcessReaderWriterLock.acquire_write_lockc           	      C   s   |dk rt d|d ur|dk rt d||kr|}|   tj|d}tj||| j|d}| || j|||}W d    n1 sCw   Y  |sLdS | jtj	d| j
| |j dS )	Nr   r<   r=   r>   r@   FrA   T)rB   r7   r   rC   rD   r   r-   r   r1   r2   r   rE   rF   )	r    r+   r8   r9   r:   rt   r,   rG   rH   r   r   r   ry   N  s,   

z%InterProcessReaderWriterLock._acquirec                 C   s$   | j d urt| j  d | _ d S d S rJ   )r   r	   close_handlerL   r   r   r   rM   g  s   

z&InterProcessReaderWriterLock._do_closec                 C      	 zt | j W n ty   | jd| j Y d S w z|   W n ty4   | jd| j Y d S w | jt	j
d| j d S NrP   rQ   rR   r	   rS   r   r$   r   r"   r   rM   r1   r   r2   rL   r   r   r   rq   l  &   


z/InterProcessReaderWriterLock.release_write_lockc                 C   r}   r~   r   rL   r   r   r   rn   ~  r   z.InterProcessReaderWriterLock.release_read_lock)r   r   r^   )Tr   r   NT)r_   r`   ra   rb   rc   rd   re   r   r   rf   r   rg   r   rh   ri   r!   r   ro   rr   r-   r7   rj   rm   rp   ry   rM   rq   rn   r   r   r   r   rk      sp    





rk   r   c                       	 t |   fdd}|S )Nc                       t   fdd}|S )Nc                     :       | i |W  d    S 1 sw   Y  d S rJ   )rr   argskwargsflockr   r   wrapper     
$z=interprocess_write_locked.<locals>.decorator.<locals>.wrapper	functoolswrapsr   r   r   r   r   	decorator     z,interprocess_write_locked.<locals>.decoratorrk   r   r   r   r   r   interprocess_write_locked     r   c                    r   )Nc                    r   )Nc                     r   rJ   )ro   r   r   r   r   r     r   z<interprocess_read_locked.<locals>.decorator.<locals>.wrapperr   r   r   r   r   r     r   z+interprocess_read_locked.<locals>.decoratorr   r   r   r   r   interprocess_read_locked  r   r   c                    r   )Nc                    r   )Nc                     s6     | i |W  d    S 1 sw   Y  d S rJ   r   r   r   r   r   r     s   $z7interprocess_locked.<locals>.decorator.<locals>.wrapperr   r   r   r   r   r     r   z&interprocess_locked.<locals>.decorator)r   r   r   r   r   interprocess_locked  r   r   )
contextlibr   r   r   rh   r
   pathlibr   r)   rd   typingr   r   r   	fastenersr   fasteners.process_mechanismr   r	   	getLoggerr_   r   r   r   rk   rf   r   r   r   r   r   r   r   <module>   s.   
  A