diff options
author | crupest <crupest@outlook.com> | 2024-11-11 01:12:29 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2025-01-20 23:41:34 +0800 |
commit | 672492caff469ea135d5e1338d4406d2b20bab16 (patch) | |
tree | 4ae61ce81e170c5208123d1b0a63bba20c6119f1 | |
parent | 90a72d2d7ed4a5eadfe36f62d81f26ca3324e49b (diff) | |
download | crupest-672492caff469ea135d5e1338d4406d2b20bab16.tar.gz crupest-672492caff469ea135d5e1338d4406d2b20bab16.tar.bz2 crupest-672492caff469ea135d5e1338d4406d2b20bab16.zip |
HALF WORK: 2024.1.20 - 5
-rw-r--r-- | tools/cru-py/cru/__init__.py | 3 | ||||
-rw-r--r-- | tools/cru-py/cru/_path.py | 23 | ||||
-rw-r--r-- | tools/cru-py/cru/service/_base.py | 23 | ||||
-rw-r--r-- | tools/cru-py/cru/template.py | 13 | ||||
-rw-r--r-- | tools/manage | 16 |
5 files changed, 36 insertions, 42 deletions
diff --git a/tools/cru-py/cru/__init__.py b/tools/cru-py/cru/__init__.py index 66732ea..17799a9 100644 --- a/tools/cru-py/cru/__init__.py +++ b/tools/cru-py/cru/__init__.py @@ -19,7 +19,6 @@ from ._const import ( from ._func import CruFunction from ._iter import CruIterable, CruIterator from ._event import CruEvent, CruEventHandlerToken -from ._path import CruPath, CruPathError from ._type import CruTypeSet, CruTypeCheckError @@ -56,8 +55,6 @@ __all__ = [ "CruIterator", "CruEvent", "CruEventHandlerToken", - "CruPath", - "CruPathError", "CruTypeSet", "CruTypeCheckError", ] diff --git a/tools/cru-py/cru/_path.py b/tools/cru-py/cru/_path.py deleted file mode 100644 index a131c41..0000000 --- a/tools/cru-py/cru/_path.py +++ /dev/null @@ -1,23 +0,0 @@ -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 diff --git a/tools/cru-py/cru/service/_base.py b/tools/cru-py/cru/service/_base.py index 6f2a61a..ad813c9 100644 --- a/tools/cru-py/cru/service/_base.py +++ b/tools/cru-py/cru/service/_base.py @@ -7,7 +7,7 @@ import os from pathlib import Path from typing import TypeVar, overload -from cru import CruException, CruLogicError, CruPath +from cru import CruException, CruLogicError _Feature = TypeVar("_Feature", bound="AppFeatureProvider") @@ -64,14 +64,19 @@ class AppPath(ABC): @property @abstractmethod - def full_path(self) -> CruPath: ... + def full_path(self) -> Path: ... @property def full_path_str(self) -> str: return str(self.full_path) def check_parents(self, must_exist: bool = False) -> bool: - return self.full_path.check_parents_dir(must_exist) + for p in reversed(self.full_path.parents): + if not p.exists() and not must_exist: + return False + if not p.is_dir(): + raise AppPathError("Parents' path must be a dir.", self.full_path) + return True def check_self(self, must_exist: bool = False) -> bool: if not self.check_parents(must_exist): @@ -112,7 +117,7 @@ class AppPath(ABC): return self.app.add_path(name, is_dir, self, id, description) @property - def app_relative_path(self) -> CruPath: + def app_relative_path(self) -> Path: return self.full_path.relative_to(self.app.root.full_path) @@ -143,15 +148,15 @@ class AppFeaturePath(AppPath): return self.parent.app @property - def full_path(self) -> CruPath: - return CruPath(self.parent.full_path, self.name).resolve() + def full_path(self) -> Path: + return Path(self.parent.full_path, self.name).resolve() class AppRootPath(AppPath): def __init__(self, app: AppBase): super().__init__("root", True, "Application root path.") self._app = app - self._full_path: CruPath | None = None + self._full_path: Path | None = None @property def parent(self) -> None: @@ -162,7 +167,7 @@ class AppRootPath(AppPath): return self._app @property - def full_path(self) -> CruPath: + def full_path(self) -> Path: if self._full_path is None: raise AppError("App root path is not set yet.") return self._full_path @@ -170,7 +175,7 @@ class AppRootPath(AppPath): def setup(self, path: os.PathLike) -> None: if self._full_path is not None: raise AppError("App root path is already set.") - self._full_path = CruPath(path).resolve() + self._full_path = Path(path).resolve() class AppFeatureProvider(ABC): diff --git a/tools/cru-py/cru/template.py b/tools/cru-py/cru/template.py index 9f0b560..6749cab 100644 --- a/tools/cru-py/cru/template.py +++ b/tools/cru-py/cru/template.py @@ -4,7 +4,6 @@ import os.path from pathlib import Path from string import Template -from ._path import CruPath from ._iter import CruIterator from ._error import CruException @@ -82,7 +81,7 @@ class TemplateTree: wrongly handled. """ self._prefix = prefix - self._files: list[tuple[CruPath, CruTemplate]] = [] + self._files: list[tuple[Path, CruTemplate]] = [] self._source = source self._template_file_suffix = template_file_suffix self._load() @@ -92,7 +91,7 @@ class TemplateTree: return self._prefix @property - def templates(self) -> list[tuple[CruPath, CruTemplate]]: + def templates(self) -> list[tuple[Path, CruTemplate]]: return self._files @property @@ -104,13 +103,13 @@ class TemplateTree: return self._template_file_suffix @staticmethod - def _scan_files(root_path: str) -> list[CruPath]: - result: list[CruPath] = [] + def _scan_files(root_path: str) -> list[Path]: + result: list[Path] = [] for root, _dirs, files in os.walk(root_path): for file in files: path = Path(root, file) path = path.relative_to(root_path) - result.append(CruPath(path)) + result.append(Path(path)) return result def _load(self) -> None: @@ -141,7 +140,7 @@ class TemplateTree: self, destination: str, variables: Mapping[str, str], dry_run: bool ) -> None: for file, template in self.templates: - des = CruPath(destination) / file + des = Path(destination) / file if self.template_file_suffix is not None and des.name.endswith( self.template_file_suffix ): diff --git a/tools/manage b/tools/manage new file mode 100644 index 0000000..39eb16a --- /dev/null +++ b/tools/manage @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +set -e + +python3.11 --version >NUL 2>&1 || ( + echo Error: failed to run Python with py -3 --version. + exit 1 +) + +script_dir=$(dirname "$0") +project_dir=$(realpath "$script_dir/..") + +cd "$project_dir" + +export PYTHONPATH="$project_dir/tools/cru-py:$PYTHONPATH" +python3.11 -m cru.service --project-dir "$project_dir" "$@" |