U
    -e5                     @   sl  d Z ddlZddlZddlZddlZzddlZW n ek
rH   dZY nX zddlZW n ek
rn   dZY nX zddlZW n ek
r   dZY nX ze	 W n e
k
r   eZ	Y nX ddddddgZd	Zdad
d ZG dd de	ZG dd dZG dd dZG dd deZG dd deZG dd deZdZerDeZn$erPeZneZedk	rhed dS )zD
A platform independent file lock that supports the with-statement.
    NTimeoutBaseFileLockWindowsFileLockUnixFileLockSoftFileLockFileLockz3.0.12c                   C   s   t ptta t S )z0Returns the logger instance used in this module.)_loggerlogging	getLogger__name__ r   r   X/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/datasets/utils/filelock.pyloggerP   s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   zN
    Raised when the lock could not be acquired in *timeout*
    seconds.
    c                 C   s
   || _ dS  N	lock_file)selfr   r   r   r   __init___   s    zTimeout.__init__c                 C   s   d| j  d}|S )NzThe file lock 'z' could not be acquired.r   )r   tempr   r   r   __str__e   s    zTimeout.__str__N)r   
__module____qualname____doc__r   r   r   r   r   r   r   Y   s   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )_Acquire_ReturnProxyc                 C   s
   || _ d S Nlock)r   r   r   r   r   r   w   s    z_Acquire_ReturnProxy.__init__c                 C   s   | j S r   r   r   r   r   r   	__enter__{   s    z_Acquire_ReturnProxy.__enter__c                 C   s   | j   d S r   )r   releaser   exc_type	exc_value	tracebackr   r   r   __exit__~   s    
z_Acquire_ReturnProxy.__exit__N)r   r   r   r   r   r%   r   r   r   r   r   v   s   r   c                   @   s   e Zd ZdZd ddZedd Zedd	 Zejd
d	 Zdd Z	dd Z
edd Zd!ddZd"ddZdd Zdd Zdd ZeeedddZdS )#r   z3
    Implements the base class of a file lock.
    Nc                 C   sB   |dk	r|nd}|  ||}|| _d| _|| _t | _d| _dS )r   N   r   )hash_filename_if_too_long
_lock_file_lock_file_fdtimeout	threadingLock_thread_lock_lock_counterr   r   r+   max_filename_lengthr   r   r   r      s    
zBaseFileLock.__init__c                 C   s   | j S )z,
        The path to the lock file.
        )r)   r   r   r   r   r      s    zBaseFileLock.lock_filec                 C   s   | j S )a}  
        You can set a default timeout for the filelock. It will be used as
        fallback value in the acquire method, if no timeout value (*None*) is
        given.

        If you want to disable the timeout, set it to a negative value.

        A timeout of 0 means, that there is exactly one attempt to acquire the
        file lock.

        *New in version 2.0.0*
        )_timeoutr   r   r   r   r+      s    zBaseFileLock.timeoutc                 C   s   t || _dS r   )floatr2   )r   valuer   r   r   r+      s    
c                 C   s
   t  dS )z
        Platform dependent. If the file lock could be
        acquired, self._lock_file_fd holds the file descriptor
        of the lock file.
        NNotImplementedErrorr   r   r   r   _acquire   s    zBaseFileLock._acquirec                 C   s
   t  dS )zH
        Releases the lock and sets self._lock_file_fd to None.
        Nr5   r   r   r   r   _release   s    zBaseFileLock._releasec                 C   s
   | j dk	S )z{
        True, if the object holds the file lock.

            This was previously a method and is now a property.
        N)r*   r   r   r   r   	is_locked   s    zBaseFileLock.is_locked皙?c                 C   sJ  |dkr| j }| j |  jd7  _W 5 Q R X t| }| j}t }z| j, | jstt d| d|  | 	  W 5 Q R X | jrt d| d|  q
qF|dkrt | |krt d| d|  t
| jqFt d| d	| d
| d t| qFW n2   | j td| jd | _W 5 Q R X  Y nX t| dS )a  
        Acquires the file lock or fails with a :exc:`Timeout` error.

        ```py
        # You can use this method in the context manager (recommended)
        with lock.acquire():
            pass

        # Or use an equivalent try-finally construct:
        lock.acquire()
        try:
            pass
        finally:
            lock.release()
        ```

        :arg float timeout:
            The maximum time waited for the file lock.
            If ``timeout < 0``, there is no timeout and this method will
            block until the lock could be acquired.
            If ``timeout`` is None, the default :attr:`~timeout` is used.

        :arg float poll_intervall:
            We check once in *poll_intervall* seconds if we can acquire the
            file lock.

        :raises Timeout:
            if the lock could not be acquired in *timeout* seconds.

            This method returns now a *proxy* object instead of *self*,
            so that it can be used in a with statement without side effects.
        N   zAttempting to acquire lock  on Lock z acquired on r   zTimeout on acquiring lock z not acquired on z
, waiting z seconds ...r   )r+   r.   r/   idr)   timer9   r   debugr7   r   sleepmaxr   )r   r+   Zpoll_intervalllock_idlock_filename
start_timer   r   r   acquire   s6    "zBaseFileLock.acquireFc              	   C   s   | j t | jrv|  jd8  _| jdks*|rvt| }| j}t d| d|  |   d| _t d| d|  W 5 Q R X dS )aV  
        Releases the file lock.

        Please note, that the lock is only completly released, if the lock
        counter is 0.

        Also note, that the lock file itself is not automatically deleted.

        :arg bool force:
            If true, the lock counter is ignored and the lock is released in
            every case.
        r;   r   zAttempting to release lock r<   r=   z released on N)r.   r9   r/   r>   r)   r   r@   r8   )r   forcerC   rD   r   r   r   r    #  s    "zBaseFileLock.releasec                 C   s   |    | S r   )rF   r   r   r   r   r   ?  s    zBaseFileLock.__enter__c                 C   s   |    d S r   r    r!   r   r   r   r%   C  s    zBaseFileLock.__exit__c                 C   s   | j dd d S )NT)rG   rH   r   r   r   r   __del__G  s    zBaseFileLock.__del__)path
max_lengthreturnc                 C   sr   t j|}t||krj|dkrjt j|}tt|}|d |t| d  d | d }t j||S |S d S )Nr      z...z.lock)osrJ   basenamelendirnamestrhashjoin)r   rJ   rK   filenamerQ   Zhashed_filenameZnew_filenamer   r   r   r(   K  s    $z&BaseFileLock.hash_filename_if_too_long)r&   N)Nr:   )F)r   r   r   r   r   propertyr   r+   setterr7   r8   r9   rF   r    r   r%   rI   rR   intr(   r   r   r   r   r      s$   



	

G
c                       s2   e Zd ZdZd
 fdd	Zdd Zdd	 Z  ZS )r   ze
    Uses the :func:`msvcrt.locking` function to hard lock the lock file on
    windows systems.
    r&   Nc                    s2   ddl m} t j|||d d|| j | _d S )Nr;   )relative_to_absolute_pathr+   r1   z\\?\)Z
file_utilsrY   superr   r   r)   )r   r   r+   r1   rY   	__class__r   r   r   `  s    zWindowsFileLock.__init__c                 C   sx   t jt jB t jB }zt | j|}W n tk
r8   Y n<X zt|tj	d W n tk
rl   t 
| Y nX || _d S Nr;   )rN   O_RDWRO_CREATO_TRUNCopenr)   OSErrormsvcrtlockingZLK_NBLCKcloser*   r   Z	open_modefdr   r   r   r7   f  s    zWindowsFileLock._acquirec                 C   sP   | j }d | _ t|tjd t| zt| j W n tk
rJ   Y nX d S r^   )	r*   rd   re   ZLK_UNLCKrN   rf   remover)   rc   r   rh   r   r   r   r8   v  s    
zWindowsFileLock._release)r&   Nr   r   r   r   r   r7   r8   __classcell__r   r   r\   r   r   Z  s   c                       s2   e Zd ZdZd
 fdd	Zdd Zdd	 Z  ZS )r   zR
    Uses the :func:`fcntl.flock` to hard lock the lock file on unix systems.
    r&   Nc                    s*   t t j|j}t j|||d d S )NrZ   )rN   statvfsrJ   rQ   	f_namemaxr[   r   r0   r\   r   r   r     s    zUnixFileLock.__init__c                 C   sb   t jt jB t jB }t | j|}zt|tjtj	B  W n t
k
rV   t | Y nX || _d S r   )rN   r_   r`   ra   rb   r)   fcntlflockZLOCK_EXZLOCK_NBrc   rf   r*   rg   r   r   r   r7     s    zUnixFileLock._acquirec                 C   s(   | j }d | _ t|tj t| d S r   )r*   ro   rp   ZLOCK_UNrN   rf   rj   r   r   r   r8     s
    
zUnixFileLock._release)r&   Nrk   r   r   r\   r   r     s   c                   @   s    e Zd ZdZdd Zdd ZdS )r   z8
    Simply watches the existence of the lock file.
    c                 C   sJ   t jt jB t jB t jB }zt | j|}W n tk
r>   Y nX || _d S r   )	rN   O_WRONLYr`   O_EXCLra   rb   r)   rc   r*   rg   r   r   r   r7     s    zSoftFileLock._acquirec                 C   s<   t | j d | _zt | j W n tk
r6   Y nX d S r   )rN   rf   r*   ri   r)   rc   r   r   r   r   r8     s    zSoftFileLock._releaseN)r   r   r   r   r7   r8   r   r   r   r   r     s   
z only soft file lock is available)r   r	   rN   r,   r?   warningsImportErrorrd   ro   TimeoutError	NameErrorrc   __all____version__r   r   r   r   r   r   r   r   r   warnr   r   r   r   <module>   sZ   



		 X/%!
