o
    7Di$                     @   sr  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s%ej	e_
eejds1ejjej_g d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%rd&d'l m!Z" e"j#Z$e$Z(dS 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                   @      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   J/var/www/Datamplify/venv/lib/python3.10/site-packages/lockfile/__init__.pyr   J       r   c                   @   r   )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   r   r   c                   @   r   )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       r   c                   @   r   )r   zSome other thread/process is locking the file.

    >>> try:
    ...   raise AlreadyLocked
    ... except LockError:
    ...   pass
    Nr   r   r   r   r   r   m   r   r   c                   @   r   )r	   zLock file creation failed for some other reason.

    >>> try:
    ...   raise LockFailed
    ... except LockError:
    ...   pass
    Nr   r   r   r   r   r	   x   r   r	   c                   @   r   )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
      r   r
   c                   @   r   )r   zRaised when an attempt is made to unlock an unlocked file.

    >>> try:
    ...   raise NotLocked
    ... except UnlockError:
    ...   pass
    Nr   r   r   r   r   r      r   r   c                   @   r   )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      r   r   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      t d)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 subclassNotImplemented)r   timeoutr   r   r   acquire   s   z_SharedBase.acquirec                 C   r!   )zX
        Release the lock.

        If the file is not locked, raise NotLocked.
        r"   r#   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   _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__r   )	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
 | _|r1t }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   abspath	lock_filesocketgethostnamehostnamegetpidpid	threadingr   getattrhashtnamedirnamejoinunique_namer%   )r   r   threadedr%   tr/   r>   r-   r   r   r       s$   

	
zLockBase.__init__c                 C   r!   )z9
        Tell whether or not the file is locked.
        r"   r#   r'   r   r   r   	is_locked      zLockBase.is_lockedc                 C   r!   )zA
        Return True if this object is locking the file.
        r"   r#   r'   r   r   r   i_am_locking   rE   zLockBase.i_am_lockingc                 C   r!   )zN
        Remove a lock.  Useful if a locking thread failed to unlock.
        r"   r#   r'   r   r   r   
break_lock  rE   zLockBase.break_lockc                 C   s   d| j j| j| jf S )Nz<%s: %r -- %r>)r-   r   r@   r   r'   r   r   r   r.     s   zLockBase.__repr__)TN)
r   r   r   r   r    rD   rF   rG   r.   __classcell__r   r   rC   r   r      s    !r   c                 O   sT   t jd| tdd t|d ts|dd  }t|dkr#|s#d|d< | |i |S )Nz1Import from %s module instead of lockfile package   )
stacklevelr      TrA   )warningswarnDeprecationWarning
isinstancestrlen)clsmodargskwdsr   r   r   
_fl_helper  s   
rV   c                  O   &   ddl m} t|jdg| R i |S )zFactory function provided for backwards compatibility.

    Do not use in new code.  Instead, import LinkLockFile from the
    lockfile.linklockfile module.
    rK   linklockfilezlockfile.linklockfile)r0   rY   rV   LinkLockFile)rT   rU   rY   r   r   r   r        
r   c                  O   rW   )zFactory function provided for backwards compatibility.

    Do not use in new code.  Instead, import MkdirLockFile from the
    lockfile.mkdirlockfile module.
    rK   mkdirlockfilezlockfile.mkdirlockfile)r0   r]   rV   MkdirLockFile)rT   rU   r]   r   r   r   r   %  r[   r   c                  O   rW   )zFactory function provided for backwards compatibility.

    Do not use in new code.  Instead, import SQLiteLockFile from the
    lockfile.mkdirlockfile module.
    rK   )sqlitelockfilezlockfile.sqlitelockfile)r0   r_   rV   SQLiteLockFile)rT   rU   r_   r   r   r   r   0  r[   r   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                     s8   t d}|  z | i |W |  S |  w )N)r%   )FileLockr&   r(   )rT   kwargslock)funcr   r%   r   r   wrapperH  s
   z&locked.<locals>.decor.<locals>.wrapper)	functoolswraps)rd   re   r   r%   )rd   r   decorG  s   zlocked.<locals>.decorr   )r   r%   ri   r   rh   r   r   ;  s   
r   linkrK   rX   r\   r   ))r   
__future__r   rf   r2   r5   r:   rL   hasattrcurrentThreadr   ThreadgetNamer   __all__	Exceptionr   r   r   r   r	   r
   r   r   objectr   r   rV   r   r   r   r   r0   rY   _llfrZ   LockFiler]   _mlfr^   ra   r   r   r   r   <module>   sD   3
-:

