From 56139950094d6eae43bba6ce8a0b4ab7d063aa41 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 11 Nov 2024 01:12:29 +0800 Subject: HALF WORK: 2024.1.18 - 2 --- tools/cru-py/cru/service/_app.py | 3 ++- tools/cru-py/cru/service/_base.py | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) (limited to 'tools/cru-py') diff --git a/tools/cru-py/cru/service/_app.py b/tools/cru-py/cru/service/_app.py index a656e3b..9533b5c 100644 --- a/tools/cru-py/cru/service/_app.py +++ b/tools/cru-py/cru/service/_app.py @@ -1,4 +1,4 @@ -from ._base import AppBase, CommandDispatcher, AppInitializer, OWNER_NAME +from ._base import AppBase, CommandDispatcher, AppInitializer, PathCommandProvider, OWNER_NAME from ._config import ConfigManager from ._data import DataManager from ._template import TemplateManager @@ -7,6 +7,7 @@ from ._template import TemplateManager class App(AppBase): def __init__(self): super().__init__(f"{OWNER_NAME}-service") + self.add_feature(PathCommandProvider()) self.add_feature(AppInitializer()) self.add_feature(DataManager()) self.add_feature(ConfigManager()) diff --git a/tools/cru-py/cru/service/_base.py b/tools/cru-py/cru/service/_base.py index 1ada93b..913455d 100644 --- a/tools/cru-py/cru/service/_base.py +++ b/tools/cru-py/cru/service/_base.py @@ -113,6 +113,10 @@ class AppPath(ABC): ) -> AppFeaturePath: return self.app.add_path(name, is_dir, self, id, description) + @property + def app_relative_path(self) -> CruPath: + return self.full_path.relative_to(self.app.root.full_path) + class AppFeaturePath(AppPath): def __init__( @@ -168,7 +172,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) + self._full_path = CruPath(path).resolve() class AppFeatureProvider(ABC): @@ -203,6 +207,28 @@ class AppCommandFeatureProvider(AppFeatureProvider): DATA_DIR_NAME = "data" +class PathCommandProvider(AppCommandFeatureProvider): + def __init__(self) -> None: + super().__init__("path-command-provider") + + def setup(self): + pass + + def get_command_info(self): + return ("path", "Get information about paths used by app.") + + def setup_arg_parser(self, arg_parser: ArgumentParser) -> None: + subparsers = arg_parser.add_subparsers( + dest="path_command", required=True, metavar="PATH_COMMAND" + ) + _list_parser = subparsers.add_parser("list", help="list all paths.") + + def run_command(self, args: Namespace) -> None: + if args.path_command == "list": + for path in self.app.paths: + print(f"{path.app_relative_path.as_posix()}: {path.description}") + + class CommandDispatcher(AppFeatureProvider): def __init__(self) -> None: super().__init__("command-dispatcher") -- cgit v1.2.3