diff options
Diffstat (limited to 'tools/cru-py/cru')
| -rw-r--r-- | tools/cru-py/cru/app.py | 40 | ||||
| -rw-r--r-- | tools/cru-py/cru/service/__main__.py | 11 | ||||
| -rw-r--r-- | tools/cru-py/cru/service/_base.py | 23 | ||||
| -rw-r--r-- | tools/cru-py/cru/service/_docker.py (renamed from tools/cru-py/cru/service/docker.py) | 0 | ||||
| -rw-r--r-- | tools/cru-py/cru/service/_manager.py | 2 | ||||
| -rw-r--r-- | tools/cru-py/cru/service/_template.py | 20 | 
6 files changed, 82 insertions, 14 deletions
| 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 index 5958f4f..5958f4f 100644 --- a/tools/cru-py/cru/service/docker.py +++ b/tools/cru-py/cru/service/_docker.py 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: ... | 
