diff options
author | crupest <crupest@outlook.com> | 2024-11-11 01:12:29 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2024-12-22 17:45:13 +0800 |
commit | b834953fa61d816b9a955640ad12d244316ce904 (patch) | |
tree | 4197980a04943b42941d3fce7cb16d1f7ea400bd /tools/cru-py/cru/app.py | |
parent | 7e85627eb0d0126baf1e698ffdeae0f3c5fc693d (diff) | |
download | crupest-b834953fa61d816b9a955640ad12d244316ce904.tar.gz crupest-b834953fa61d816b9a955640ad12d244316ce904.tar.bz2 crupest-b834953fa61d816b9a955640ad12d244316ce904.zip |
HALF WORK: 2024.12.22
code.
Diffstat (limited to 'tools/cru-py/cru/app.py')
-rw-r--r-- | tools/cru-py/cru/app.py | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/tools/cru-py/cru/app.py b/tools/cru-py/cru/app.py index 6a60926..c9e57f4 100644 --- a/tools/cru-py/cru/app.py +++ b/tools/cru-py/cru/app.py @@ -21,45 +21,57 @@ class ApplicationPathError(CruException): class ApplicationPath: - def __init__(self, p: str | Path, is_dir: bool) -> None: - self._path = CruPath(p) + def __init__(self, app_dir: str, subpath: str, is_dir: bool) -> None: + self._app_dir = app_dir + self._subpath = subpath + self._full_path = CruPath(app_dir, subpath) self._is_dir = is_dir @property - def path(self) -> CruPath: - return self._path + def app_dir(self) -> str: + return self._app_dir + + @property + def subpath(self) -> str: + return self._subpath + + @property + def full_path(self) -> CruPath: + return self._full_path @property def is_dir(self) -> bool: return self._is_dir def check_parents(self, must_exist: bool = False) -> bool: - return self._path.check_parents_dir(must_exist) + return self.full_path.check_parents_dir(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 self.full_path.exists(): if not must_exist: return False - raise ApplicationPathError("Not exist.", self.path) + raise ApplicationPathError("Not exist.", self.full_path) if self.is_dir: - if not self.path.is_dir(): - raise ApplicationPathError("Should be a directory, but not.", self.path) + if not self.full_path.is_dir(): + raise ApplicationPathError( + "Should be a directory, but not.", self.full_path + ) else: return False else: - if not self.path.is_file(): - raise ApplicationPathError("Should be a file, but not.", self.path) + if not self.full_path.is_file(): + raise ApplicationPathError("Should be a file, but not.", self.full_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) + os.makedirs(self.full_path.parent, exist_ok=True) if self.is_dir: - os.mkdir(self.path) + os.mkdir(self.full_path) elif create_file: - with open(self.path, "w") as f: + with open(self.full_path, "w") as f: f.write("") |