From b834953fa61d816b9a955640ad12d244316ce904 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 11 Nov 2024 01:12:29 +0800 Subject: HALF WORK: 2024.12.22 code. --- tools/cru-py/.gitignore | 6 ------ tools/cru-py/cru/app.py | 40 +++++++++++++++++++++++------------ tools/cru-py/cru/service/__main__.py | 11 ++++++++++ tools/cru-py/cru/service/_base.py | 23 ++++++++++++++++++++ tools/cru-py/cru/service/_docker.py | 24 +++++++++++++++++++++ tools/cru-py/cru/service/_manager.py | 2 ++ tools/cru-py/cru/service/_template.py | 20 ++++++++++++++++++ tools/cru-py/cru/service/docker.py | 24 --------------------- 8 files changed, 106 insertions(+), 44 deletions(-) create mode 100644 tools/cru-py/cru/service/__main__.py create mode 100644 tools/cru-py/cru/service/_base.py create mode 100644 tools/cru-py/cru/service/_docker.py create mode 100644 tools/cru-py/cru/service/_template.py delete mode 100644 tools/cru-py/cru/service/docker.py (limited to 'tools/cru-py') diff --git a/tools/cru-py/.gitignore b/tools/cru-py/.gitignore index d40cb9b..bee8a64 100644 --- a/tools/cru-py/.gitignore +++ b/tools/cru-py/.gitignore @@ -1,7 +1 @@ -.idea -venv - __pycache__ - -.continuerc.json -.continuerules 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("") diff --git a/tools/cru-py/cru/service/__main__.py b/tools/cru-py/cru/service/__main__.py new file mode 100644 index 0000000..a7add4d --- /dev/null +++ b/tools/cru-py/cru/service/__main__.py @@ -0,0 +1,11 @@ +import argparse + + +arg_parser = argparse.ArgumentParser(description="Service management") +command_subparser = arg_parser.add_subparsers(dest="command") + +template_parser = command_subparser.add_parser("template", help="Template management") +template_subparser = template_parser.add_subparsers(dest="template_command") + +template_subparser.add_parser('list', description="List templates") +template_subparser.add_parser('generate') diff --git a/tools/cru-py/cru/service/_base.py b/tools/cru-py/cru/service/_base.py new file mode 100644 index 0000000..707fb66 --- /dev/null +++ b/tools/cru-py/cru/service/_base.py @@ -0,0 +1,23 @@ +from argparse import ArgumentParser, Namespace +from typing import Protocol +from cru.app import ApplicationPath, CruApplication + + +class AppFunction(Protocol): + @property + def name(self) -> str: ... + + def add_arg_parser(self, arg_parser: ArgumentParser) -> None: ... + + def run_command(self, args: Namespace) -> None: ... + + +class AppBase(CruApplication): + def __init__(self, name: str, app_dir: str): + super().__init__(name) + self._app_dir = app_dir + self._template_dir = ApplicationPath(app_dir, "templates", True) + + @property + def app_dir(self) -> str: + return self._app_dir diff --git a/tools/cru-py/cru/service/_docker.py b/tools/cru-py/cru/service/_docker.py new file mode 100644 index 0000000..5958f4f --- /dev/null +++ b/tools/cru-py/cru/service/_docker.py @@ -0,0 +1,24 @@ +import shutil +import subprocess + +from .._util import L + + +class DockerController: + DOCKER_BIN_NAME = "docker" + + def __init__(self, docker_bin: None | str = None) -> None: + self._docker_bin = docker_bin + + @property + def docker_bin(self) -> str: + if self._docker_bin is None: + self._docker_bin = shutil.which(self.DOCKER_BIN_NAME) + return self._docker_bin + + def list_containers(self) -> L[str]: + p = subprocess.run([self.docker_bin, "container", "ls", ""], capture_output=True) + return p.stdout.decode("utf-8").splitlines() + + def restart_container(self, container_name: str) -> None: + subprocess.run([self.docker_bin, "restart", container_name]) \ No newline at end of file diff --git a/tools/cru-py/cru/service/_manager.py b/tools/cru-py/cru/service/_manager.py index 45a9e47..c1af428 100644 --- a/tools/cru-py/cru/service/_manager.py +++ b/tools/cru-py/cru/service/_manager.py @@ -1,2 +1,4 @@ class CruServiceManager: "TODO: Continue here tomorrow!" + def __init__(self): + \ No newline at end of file diff --git a/tools/cru-py/cru/service/_template.py b/tools/cru-py/cru/service/_template.py new file mode 100644 index 0000000..0ca4b63 --- /dev/null +++ b/tools/cru-py/cru/service/_template.py @@ -0,0 +1,20 @@ +from argparse import ArgumentParser, Namespace + +from ._base import AppBase, AppFunction + + +class TemplateManager(AppFunction): + def __init__(self, app: AppBase): + self._app = app + pass + + @property + def name(self): + return "template-manager" + + def add_arg_parser(self, arg_parser: ArgumentParser) -> None: + subparsers = arg_parser.add_subparsers(dest="template_command") + list_parser = subparsers.add_parser("list", help="List templates") + generate_parser = subparsers.add_parser("generate", help="Generate template") + + def run_command(self, args: Namespace) -> None: ... diff --git a/tools/cru-py/cru/service/docker.py b/tools/cru-py/cru/service/docker.py deleted file mode 100644 index 5958f4f..0000000 --- a/tools/cru-py/cru/service/docker.py +++ /dev/null @@ -1,24 +0,0 @@ -import shutil -import subprocess - -from .._util import L - - -class DockerController: - DOCKER_BIN_NAME = "docker" - - def __init__(self, docker_bin: None | str = None) -> None: - self._docker_bin = docker_bin - - @property - def docker_bin(self) -> str: - if self._docker_bin is None: - self._docker_bin = shutil.which(self.DOCKER_BIN_NAME) - return self._docker_bin - - def list_containers(self) -> L[str]: - p = subprocess.run([self.docker_bin, "container", "ls", ""], capture_output=True) - return p.stdout.decode("utf-8").splitlines() - - def restart_container(self, container_name: str) -> None: - subprocess.run([self.docker_bin, "restart", container_name]) \ No newline at end of file -- cgit v1.2.3