class file_lock(contextlib.AbstractContextManager): """File lock based on filelock package.""" def __init__(self, path: str, ignore_remote_protocol: bool = True, lock_file: str = ".lock") -> None: if not isinstance(path, (str, os.PathLike, pathlib.Path)): self.lock = None else: path = os.path.join( path, lock_file) if os.path.isdir(path) else f"{path}./{lock_file}" if ignore_remote_protocol and has_remote_protocol(path): self.lock = None else: self.lock = FileLock(path, timeout=-1) def __enter__(self, *args, **kwargs): if self.lock: return self.lock.__enter__(*args, **kwargs) def __exit__(self, *args, **kwargs): if self.lock: return self.lock.__exit__(*args, **kwargs)