PK!ǡ11ezlock/__init__.py__version__ = '0.1.2' from .lock import Lock PK!q! 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! Arguments ========= 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!gֶ  "ezlock-0.1.2.dist-info/LICENSE.txtCopyright (c) 2018 Hugh Ramsden Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HڽTUezlock-0.1.2.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!H'&q4ezlock-0.1.2.dist-info/METADATATo0~_qlk:R*&V!V/&Vc;J9;IǴX~|}wܠ"IS3E(LW&۱g5J ۦphIUW[I{0$u¤!.L gFMMQ*Jf,3{csh5:wcƗ$RB)=P,T7; 6ҢKn[&Ms~q J8' 'i _iK#$֚"T%hHy*g}q;]feiɕOmMOhڻ5cJgO>e 9&um:!x]-YiɎQsY{Tq&.(6~/$5pP$@ âӑ%+1 ~DQ_#v8-*n8lƑpՂ i-?qVQ;X2ꌸISGs 7apԻ^ޓ |x7PK!H)ezlock-0.1.2.dist-info/RECORDu;r@޳)VXQ@\A>9}&8/*ВvpȴP(8mѪ&a{eB)Wjb$8K~%;~ OO$wdv_n?ࠫGF\ _ܙK˴6*6U sk_Ǽf-9S$mss=+}a5͒Py5"n/FrJHC.zldS16m^zb6)"]JK ȵD#,h 3PK!ǡ11ezlock/__init__.pyPK!q! aezlock/lock.pyPK!gֶ  "' ezlock-0.1.2.dist-info/LICENSE.txtPK!HڽTUezlock-0.1.2.dist-info/WHEELPK!H'&q4ezlock-0.1.2.dist-info/METADATAPK!H)ezlock-0.1.2.dist-info/RECORDPK'