From 7692763f83ad1be735b0b9e9ab0af8ce666d8de8 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 11 Nov 2024 01:12:29 +0800 Subject: HALF WORK: 2024.12.17 --- tools/cru-py/cru/_path.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tools/cru-py/cru/_path.py (limited to 'tools/cru-py/cru/_path.py') diff --git a/tools/cru-py/cru/_path.py b/tools/cru-py/cru/_path.py new file mode 100644 index 0000000..a131c41 --- /dev/null +++ b/tools/cru-py/cru/_path.py @@ -0,0 +1,23 @@ +from pathlib import Path + +from ._error import CruException + + +class CruPathError(CruException): + def __init__(self, message, _path: Path, *args, **kwargs): + super().__init__(message, *args, **kwargs) + self._path = _path + + @property + def path(self) -> Path: + return self._path + + +class CruPath(Path): + def check_parents_dir(self, must_exist: bool = False) -> bool: + for p in reversed(self.parents): + if not p.exists() and not must_exist: + return False + if not p.is_dir(): + raise CruPathError("Parents path must be a dir.", self) + return True -- cgit v1.2.3