diff options
author | crupest <crupest@outlook.com> | 2024-11-11 01:12:29 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2024-12-18 18:31:27 +0800 |
commit | 7b4d49e4bbdff6ddf1f8f7e937130e700024d5e9 (patch) | |
tree | b45e6cb4312b050ac4e1ccf51106d44af9201b40 /tools/cru-py/cru/paths.py | |
parent | ca88aee42b741110d42683db826caf61b642abea (diff) | |
download | crupest-7b4d49e4bbdff6ddf1f8f7e937130e700024d5e9.tar.gz crupest-7b4d49e4bbdff6ddf1f8f7e937130e700024d5e9.tar.bz2 crupest-7b4d49e4bbdff6ddf1f8f7e937130e700024d5e9.zip |
HALF WORK: 2024.12.17
Diffstat (limited to 'tools/cru-py/cru/paths.py')
-rw-r--r-- | tools/cru-py/cru/paths.py | 64 |
1 files changed, 0 insertions, 64 deletions
diff --git a/tools/cru-py/cru/paths.py b/tools/cru-py/cru/paths.py deleted file mode 100644 index cdd97fe..0000000 --- a/tools/cru-py/cru/paths.py +++ /dev/null @@ -1,64 +0,0 @@ -import os -from pathlib import Path - -from .error import CruException - - -class ApplicationPathError(CruException): - def __init__(self, message: str, p: str | Path, *args, **kwargs): - super().__init__(message, *args, path=str(p), **kwargs) - - -def check_parents_dir(p: str | Path, /, must_exist: bool = False) -> bool: - p = Path(p) if isinstance(p, str) else p - for p in reversed(p.parents): - if not p.exists() and not must_exist: - return False - if not p.is_dir(): - raise ApplicationPathError("Parents path should be a dir.", p) - return True - - -class ApplicationPath: - def __init__(self, p: str | Path, is_dir: bool) -> None: - self._path = Path(p) if isinstance(p, str) else p - self._is_dir = is_dir - - @property - def path(self) -> Path: - return self._path - - @property - def is_dir(self) -> bool: - return self._is_dir - - def check_parents(self, must_exist: bool = False) -> bool: - return check_parents_dir(self._path.parent, must_exist) - - def check_self(self, must_exist: bool = False) -> bool: - if not self.check_parents(must_exist): - return False - if not self.path.exists(): - if not must_exist: - return False - raise ApplicationPathError("Mot exist.", self.path) - if self.is_dir: - if not self.path.is_dir(): - raise ApplicationPathError("Should be a directory, but not.", self.path) - else: - return False - else: - if not self.path.is_file(): - raise ApplicationPathError("Should be a file, but not.", self.path) - else: - return False - - def ensure(self, create_file: bool = False) -> None: - e = self.check_self(False) - if not e: - os.makedirs(self.path.parent, exist_ok=True) - if self.is_dir: - os.mkdir(self.path) - elif create_file: - with open(self.path, "w") as f: - f.write("") |