aboutsummaryrefslogtreecommitdiff
path: root/tools/cru-py/cru
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2024-11-11 01:12:29 +0800
committerYuqian Yang <crupest@crupest.life>2025-01-18 17:49:34 +0800
commit56139950094d6eae43bba6ce8a0b4ab7d063aa41 (patch)
tree366f4a57adc81948c09b249fb6f804357aedc509 /tools/cru-py/cru
parent41f735ed9ddc9d96d27f7e1f2a6ca34af9cce9d9 (diff)
downloadcrupest-56139950094d6eae43bba6ce8a0b4ab7d063aa41.tar.gz
crupest-56139950094d6eae43bba6ce8a0b4ab7d063aa41.tar.bz2
crupest-56139950094d6eae43bba6ce8a0b4ab7d063aa41.zip
HALF WORK: 2024.1.18 - 2
Diffstat (limited to 'tools/cru-py/cru')
-rw-r--r--tools/cru-py/cru/service/_app.py3
-rw-r--r--tools/cru-py/cru/service/_base.py28
2 files changed, 29 insertions, 2 deletions
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")