
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
U
    UV$                     @   s  d Z 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eedsJej	e_
eejdsbejjej_dddd	d
ddddddddgZG dd deZG dd deZG dd deZG dd	 d	eZG dd
 d
eZG dd deZG dd deZG dd deZG dd deZG dd deZdd Zd d Zd!d Zd"d Zd(d#dZeed$rjd%d&l m!Z" e"j#Z$nd%d'l m%Z& e&j'Z$e$Z(dS ))a  
lockfile.py - Platform-independent advisory file locks.

Requires Python 2.5 unless you apply 2.4.diff
Locking is done on a per-thread basis instead of a per-process basis.

Usage:

>>> lock = LockFile('somefile')
>>> try:
...     lock.acquire()
... except AlreadyLocked:
...     print 'somefile', 'is locked already.'
... except LockFailed:
...     print 'somefile', 'can\'t be locked.'
... else:
...     print 'got lock'
got lock
>>> print lock.is_locked()
True
>>> lock.release()

>>> lock = LockFile('somefile')
>>> print lock.is_locked()
False
>>> with lock:
...    print lock.is_locked()
True
>>> print lock.is_locked()
False

>>> lock = LockFile('somefile')
>>> # It is okay to lock twice from the same thread...
>>> with lock:
...     lock.acquire()
...
>>> # Though no counter is kept, so you can't unlock multiple times...
>>> print lock.is_locked()
False

Exceptions:

    Error - base class for other exceptions
        LockError - base class for all locking exceptions
            AlreadyLocked - Another thread or process already holds the lock
            LockFailed - Lock failed for some other reason
        UnlockError - base class for all unlocking exceptions
            AlreadyUnlocked - File was not locked.
            NotMyLock - File was locked but not by the current thread/process
    )absolute_importNcurrent_threadget_nameError	LockErrorLockTimeoutAlreadyLocked
LockFailedUnlockError	NotLocked	NotMyLockLinkFileLockMkdirFileLockSQLiteFileLockLockBaselockedc                   @   s   e Zd ZdZdS )r   zw
    Base class for other exceptions.

    >>> try:
    ...   raise Error
    ... except Exception:
    ...   pass
    N__name__
__module____qualname____doc__ r   r   B/opt/alt/python38/lib/python3.8/site-packages/lockfile/__init__.pyr   J   s   c                   @   s   e Zd ZdZdS )r   z
    Base class for error arising from attempts to acquire the lock.

    >>> try:
    ...   raise LockError
    ... except Error:
    ...   pass
    Nr   r   r   r   r   r   V   s   c                   @   s   e Zd ZdZdS )r   zRaised when lock creation fails within a user-defined period of time.

    >>> try:
    ...   raise LockTimeout
    ... except LockError:
    ...   pass
    Nr   r   r   r   r   r   b   s   c                   @   s   e Zd ZdZdS )r   zSome other thread/process is locking the file.

    >>> try:
    ...   raise AlreadyLocked
    ... except LockError:
    ...   pass
    Nr   r   r   r   r   r   m   s   c                   @   s   e Zd ZdZdS )r	   zLock file creation failed for some other reason.

    >>> try:
    ...   raise LockFailed
    ... except LockError:
    ...   pass
    Nr   r   r   r   r   r	   x   s   c                   @   s   e Zd ZdZdS )r
   z
    Base class for errors arising from attempts to release the lock.

    >>> try:
    ...   raise UnlockError
    ... except Error:
    ...   pass
    Nr   r   r   r   r   r
      s   c                   @   s   e Zd ZdZdS )r   zRaised when an attempt is made to unlock an unlocked file.

    >>> try:
    ...   raise NotLocked
    ... except UnlockError:
    ...   pass
    Nr   r   r   r   r   r      s   c                   @   s   e Zd ZdZdS )r   zRaised when an attempt is made to unlock a file someone else locked.

    >>> try:
    ...   raise NotMyLock
    ... except UnlockError:
    ...   pass
    Nr   r   r   r   r   r      s   c                   @   s>   e Zd Zdd ZdddZdd Zdd	 Zd
d Zdd ZdS )_SharedBasec                 C   s
   || _ d S )N)path)selfr   r   r   r   __init__   s    z_SharedBase.__init__Nc                 C   s   t ddS )a  
        Acquire the lock.

        * If timeout is omitted (or None), wait forever trying to lock the
          file.

        * If timeout > 0, try to acquire the lock for that many seconds.  If
          the lock period expires and the file is still locked, raise
          LockTimeout.

        * If timeout <= 0, raise AlreadyLocked immediately if the file is
          already locked.
        implement in subclassNNotImplemented)r   timeoutr   r   r   acquire   s    z_SharedBase.acquirec                 C   s   t ddS )zX
        Release the lock.

        If the file is not locked, raise NotLocked.
        r   Nr   r   r   r   r   release   s    z_SharedBase.releasec                 C   s   |    | S )*
        Context manager support.
        )r!   r"   r   r   r   	__enter__   s    z_SharedBase.__enter__c                 G   s   |    dS )r$   N)r#   )r   Z_excr   r   r   __exit__   s    z_SharedBase.__exit__c                 C   s   d| j j| jf S )Nz<%s: %r>)	__class__r   r   r"   r   r   r   __repr__   s    z_SharedBase.__repr__)N)	r   r   r   r   r!   r#   r%   r&   r(   r   r   r   r   r      s   
r   c                       sB   e Zd ZdZd fdd	Zdd Zdd	 Zd
d Zdd Z  Z	S )r   z.Base class for platform-specific lock classes.TNc              	      s   t t| | tj|d | _t | _	t
 | _|rbt }t|dt|}d|d@  | _nd| _tj| j}tj|d| j	| j| jt| jf | _|| _dS )zi
        >>> lock = LockBase('somefile')
        >>> lock = LockBase('somefile', threaded=False)
        z.lockidentz-%xl     z	%s%s.%s%sN)superr   r   osr   abspathZ	lock_filesocketgethostnamehostnamegetpidpid	threadingr   getattrhashtnamedirnamejoinunique_namer    )r   r   threadedr    tr)   r7   r'   r   r   r      s$    

	zLockBase.__init__c                 C   s   t ddS )z9
        Tell whether or not the file is locked.
        r   Nr   r"   r   r   r   	is_locked   s    zLockBase.is_lockedc                 C   s   t ddS )zA
        Return True if this object is locking the file.
        r   Nr   r"   r   r   r   i_am_locking   s    zLockBase.i_am_lockingc                 C   s   t ddS )zN
        Remove a lock.  Useful if a locking thread failed to unlock.
        r   Nr   r"   r   r   r   
break_lock  s    zLockBase.break_lockc                 C   s   d| j j| j| jf S )Nz<%s: %r -- %r>)r'   r   r9   r   r"   r   r   r   r(     s    zLockBase.__repr__)TN)
r   r   r   r   r   r=   r>   r?   r(   __classcell__r   r   r<   r   r      s   !c                 O   sP   t jd| tdd t|d ts.|dd  }t|dkrF|sFd|d< | ||S )Nz1Import from %s module instead of lockfile package   )
stacklevelr      Tr:   )warningswarnDeprecationWarning
isinstancestrlen)clsmodargskwdsr   r   r   
_fl_helper  s    
 rN   c                  O   s    ddl m} t|jdf| |S )zFactory function provided for backwards compatibility.

    Do not use in new code.  Instead, import LinkLockFile from the
    lockfile.linklockfile module.
    rC   linklockfilezlockfile.linklockfile)r*   rP   rN   LinkLockFile)rL   rM   rP   r   r   r   r     s    
c                  O   s    ddl m} t|jdf| |S )zFactory function provided for backwards compatibility.

    Do not use in new code.  Instead, import MkdirLockFile from the
    lockfile.mkdirlockfile module.
    rC   mkdirlockfilezlockfile.mkdirlockfile)r*   rS   rN   MkdirLockFile)rL   rM   rS   r   r   r   r   %  s    
c                  O   s    ddl m} t|jdf| |S )zFactory function provided for backwards compatibility.

    Do not use in new code.  Instead, import SQLiteLockFile from the
    lockfile.mkdirlockfile module.
    rC   )sqlitelockfilezlockfile.sqlitelockfile)r*   rU   rN   ZSQLiteLockFile)rL   rM   rU   r   r   r   r   0  s    
c                    s    fdd}|S )a  Decorator which enables locks for decorated function.

    Arguments:
     - path: path for lockfile.
     - timeout (optional): Timeout for acquiring lock.

     Usage:
         @locked('/var/run/myname', timeout=0)
         def myname(...):
             ...
    c                    s   t   fdd}|S )Nc                     s2   t d}|  z | |W S |  X d S )N)r    )FileLockr!   r#   )rL   kwargslock)funcr   r    r   r   wrapperH  s
    z&locked.<locals>.decor.<locals>.wrapper)	functoolswraps)rY   rZ   r   r    )rY   r   decorG  s    zlocked.<locals>.decorr   )r   r    r^   r   r]   r   r   ;  s    
linkrC   rO   rR   )N))r   
__future__r   r[   r,   r.   r3   rD   hasattrcurrentThreadr   ThreadgetNamer   __all__	Exceptionr   r   r   r   r	   r
   r   r   objectr   r   rN   r   r   r   r   r*   rP   Z_llfrQ   ZLockFilerS   Z_mlfrT   rV   r   r   r   r   <module>   sT   3
      -:
