PK!11ezlock/__init__.py__version__ = '0.1.1' from .lock import Lock PK!j` ezlock/lock.pyfrom pathlib import Path import os import time class LockError(Exception): pass class Lock: def __init__(self, path='.lock'): """ Lock object that keeps track of a file found at `self.path`. If there is a file found at `self.path`, the lock is considered... locked! Parameters ========== path : str, Path path to write the lock file to (will be converted to `pathlib.Path`). Defaults to '.lock'. """ self.path = Path(path) @property def name(self): """ name written to lock to prove ownership """ return 'pid:{}, obj:{}'.format(os.getpid(), id(self)) @property def locked(self): """ Does the lock-file at `self.path` exist? """ return self.path.exists() @property def mine(self): """ Was the lock created by this object? """ try: return self.path.read_text() == self.name except FileNotFoundError: raise LockError("Attempted to check ownership on lock that doesn't exist") def acquire(self): """ Create the lock-file, and stamp on it that it was made by me! """ if self.locked: raise LockError("Attempted to acquire on already locked lock!") self.path.write_text(self.name) def release(self, force=False, rerelease=True): """ Release the lock. Will get upset if the lock isn't `self.mine` but can override by setting `force=True`. Arguments ========= force : bool force releasing the lock, even if not `self.mine`. (default `False`) rerelease : when `True` will not complain if attempting to release and already released lock. (default `True`) """ if not self.locked: if not rerelease: raise LockError("Attempted to release an already released lock") return None if not self.mine and not force: raise LockError("Attempted to release a lock that wasn't mine, can set `force=True`") pid = self.path.read_text() def wait(self, dt=0.01): """ Wait until lock is released. Arguments ========= dt : float how long to wait between checking for `self.locked` """ while self.locked: time.sleep(dt) def __enter__(self): self.acquire() def __exit__(self): self.release() def __bool__(self): return self.locked PK!HڽTUezlock-0.1.1.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!Hc-#yezlock-0.1.1.dist-info/METADATA 0E| (n:&ѤY; g35$'A;+y&,rō6-|o9[mXΓ4`/IЀ$` g cHvOJ'q_lQAjwީB[`Uo$њ6/akcPK!H`9Zezlock-0.1.1.dist-info/RECORDuMo0ỿ(=L! b嫈sߖd&aWJb)UK=! ;"`}kmPdX6 8a!iE*8*uMVٱ]]HPueHT.UͱEzK;t=^\wYZu*G8tPC{FP`Zf`>QAq(kqò*i p}ۓg163vxo0PK!11ezlock/__init__.pyPK!j` aezlock/lock.pyPK!HڽTU' ezlock-0.1.1.dist-info/WHEELPK!Hc-#y ezlock-0.1.1.dist-info/METADATAPK!H`9Z ezlock-0.1.1.dist-info/RECORDPK^