diff --git a/docs/cli.md b/docs/cli.md index 1ec439a0..f2f2291e 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -2,5 +2,8 @@ prefix: cpl commands: new: app + base class - model \ No newline at end of file + configmodel + enum + service \ No newline at end of file diff --git a/src/setup.py b/src/setup.py index 5f656653..b99cc603 100644 --- a/src/setup.py +++ b/src/setup.py @@ -19,7 +19,7 @@ setuptools.setup( ], entry_points={ 'console_scripts': [ - 'cpl = sh_edraft.cli.cpl_cli:CPLCli.main' + 'cpl = sh_edraft.cli.cpl_cli.cli:main' ] } ) diff --git a/src/sh_edraft/cli/cpl_cli.py b/src/sh_edraft/cli/cpl_cli.py deleted file mode 100644 index 11bfe8e2..00000000 --- a/src/sh_edraft/cli/cpl_cli.py +++ /dev/null @@ -1,5 +0,0 @@ -class CPLCli: - - @staticmethod - def main(): - print('Hello world') diff --git a/src/sh_edraft/cli/cpl_cli/__init__.py b/src/sh_edraft/cli/cpl_cli/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/sh_edraft/cli/cpl_cli/cli.py b/src/sh_edraft/cli/cpl_cli/cli.py new file mode 100644 index 00000000..49974db2 --- /dev/null +++ b/src/sh_edraft/cli/cpl_cli/cli.py @@ -0,0 +1,33 @@ +import sys + +from sh_edraft.cli.cpl_cli.cli_commands import CLICommands + + +class CLI: + + def __init__(self): + self._commands: dict = {} + + def setup(self): + self._commands[CLICommands.new.__name__] = CLICommands.new + + def main(self): + args = sys.argv[1:] + + try: + cmd = self._commands[args[0]] + cmd(args[1:]) + except KeyError: + CLICommands.unexpected_command(args[0]) + except Exception as e: + print(e) + + +def main(): + cli = CLI() + cli.setup() + cli.main() + + +if __name__ == '__main__': + main() diff --git a/src/sh_edraft/cli/cpl_cli/cli_commands.py b/src/sh_edraft/cli/cpl_cli/cli_commands.py new file mode 100644 index 00000000..53986d34 --- /dev/null +++ b/src/sh_edraft/cli/cpl_cli/cli_commands.py @@ -0,0 +1,87 @@ +import os + + +class CLICommands: + + @classmethod + def new(cls, args: list[str]): + if not os.path.isdir(f'./templates/{args[0]}'): + cls.unexpected_command(args[0]) + + sub_args = args[1:] + + if len(sub_args) != 1: + cls.unexpected_argument(sub_args[1]) + + full_path = sub_args[0] + name = os.path.basename(full_path) + path = os.path.dirname(full_path) + + if args[0] in ['base', 'class', 'configmodel', 'enum', 'service']: + if not os.path.isdir(path): + os.makedirs(path) + else: + if not os.path.isdir(full_path): + os.makedirs(full_path) + + for r, d, f in os.walk(f'./templates/{args[0]}'): + for file in f: + template_content = '' + with open(f'{r}/{file}') as template: + template_content = template.read() + template.close() + + file = file.replace('txt', 'py') + if args[0] in ['base', 'class', 'configmodel', 'enum', 'service']: + suffix = None + + if args[0] == 'base': + suffix = 'base' + + elif args[0] == 'configmodel': + suffix = 'settings' + + elif args[0] == 'service': + suffix = 'service' + + if suffix is not None: + file_path = f'{path}/{name}_{suffix}.py' + else: + file_path = f'{path}/{name}.py' + else: + file_path = f'{full_path}/{file}' + + with open(file_path, 'w+') as pyfile: + if name[0].islower(): + name = f'{name[0].upper()}{name[1:]}' + + if args[0] == 'configmodel': + template_content = template_content.replace('$Name', f'{name}Settings') + pyfile.write(template_content) + + elif args[0] == 'service': + template_content = template_content.replace('$Name', f'{name}Service') + template_content = template_content.replace('$Base', f'{name}Base') + pyfile.write(template_content) + + else: + template_content = template_content.replace('$Name', name) + pyfile.write(template_content) + + pyfile.close() + + @staticmethod + def help(): + print('Commands:') + + @classmethod + def unexpected_command(cls, command: str): + print(f'Unexpected command {command}') + cls.help() + exit() + + @classmethod + def unexpected_argument(cls, argument: str): + print(f'Unexpected argument {argument}') + cls.help() + exit() diff --git a/src/sh_edraft/cli/cpl_cli/templates/app/__init__.txt b/src/sh_edraft/cli/cpl_cli/templates/app/__init__.txt new file mode 100644 index 00000000..2e12875a --- /dev/null +++ b/src/sh_edraft/cli/cpl_cli/templates/app/__init__.txt @@ -0,0 +1 @@ +# imports: \ No newline at end of file diff --git a/src/sh_edraft/cli/cpl_cli/templates/app/main.txt b/src/sh_edraft/cli/cpl_cli/templates/app/main.txt new file mode 100644 index 00000000..081d79dd --- /dev/null +++ b/src/sh_edraft/cli/cpl_cli/templates/app/main.txt @@ -0,0 +1,8 @@ +from program import Program + +if __name__ == '__main__': + program = Program() + program.create_application_host() + program.create_configuration() + program.create_services() + program.main() diff --git a/src/sh_edraft/cli/cpl_cli/templates/app/program.txt b/src/sh_edraft/cli/cpl_cli/templates/app/program.txt new file mode 100644 index 00000000..890c6b73 --- /dev/null +++ b/src/sh_edraft/cli/cpl_cli/templates/app/program.txt @@ -0,0 +1,43 @@ +from typing import Optional + +from sh_edraft.configuration.base import ConfigurationBase +from sh_edraft.hosting import ApplicationHost +from sh_edraft.hosting.base import ApplicationBase +from sh_edraft.logging import Logger +from sh_edraft.logging.base import LoggerBase +from sh_edraft.service.providing.base import ServiceProviderBase + + +class Program(ApplicationBase): + + def __init__(self): + ApplicationBase.__init__(self) + + self._app_host: Optional[ApplicationHost] = None + self._services: Optional[ServiceProviderBase] = None + self._configuration: Optional[ConfigurationBase] = None + self._logger: Optional[LoggerBase] = None + + def create_application_host(self): + self._app_host = ApplicationHost() + self._configuration = self._app_host.configuration + self._services = self._app_host.services + + def create_configuration(self): + self._configuration.add_environment_variables('PYTHON_') + self._configuration.add_environment_variables('CPL_') + self._configuration.add_argument_variables() + self._configuration.add_json_file(f'appsettings.json') + self._configuration.add_json_file(f'appsettings.{self._configuration.environment.environment_name}.json', optional=True) + self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True) + + def create_services(self): + # Add and create logger + self._services.add_singleton(LoggerBase, Logger) + self._logger = self._services.get_service(LoggerBase) + + def main(self): + self._logger.header(f'{self._configuration.environment.application_name}:') + self._logger.debug(__name__, f'Host: {self._configuration.environment.host_name}') + self._logger.debug(__name__, f'Environment: {self._configuration.environment.environment_name}') + self._logger.debug(__name__, f'Customer: {self._configuration.environment.customer}') diff --git a/src/sh_edraft/cli/cpl_cli/templates/base/base.txt b/src/sh_edraft/cli/cpl_cli/templates/base/base.txt new file mode 100644 index 00000000..1940e8ab --- /dev/null +++ b/src/sh_edraft/cli/cpl_cli/templates/base/base.txt @@ -0,0 +1,10 @@ +from abc import ABC, abstractmethod + + +class $Name(ABC): + + @abstractmethod + def __init__(self): pass + + @abstractmethod + def create(self): pass diff --git a/src/sh_edraft/cli/cpl_cli/templates/class/class.txt b/src/sh_edraft/cli/cpl_cli/templates/class/class.txt new file mode 100644 index 00000000..d22b7581 --- /dev/null +++ b/src/sh_edraft/cli/cpl_cli/templates/class/class.txt @@ -0,0 +1,4 @@ +class $Name: + + def __init__(self): + pass diff --git a/src/sh_edraft/cli/cpl_cli/templates/configmodel/model.txt b/src/sh_edraft/cli/cpl_cli/templates/configmodel/model.txt new file mode 100644 index 00000000..ca37a816 --- /dev/null +++ b/src/sh_edraft/cli/cpl_cli/templates/configmodel/model.txt @@ -0,0 +1,20 @@ +import traceback + +from sh_edraft.configuration.base import ConfigurationModelBase +from sh_edraft.console import Console +from sh_edraft.console.model import ForegroundColor + + +class $Name(ConfigurationModelBase): + + def __init__(self): + ConfigurationModelBase.__init__(self) + + def from_dict(self, settings: dict): + try: + pass + except Exception as e: + Console.set_foreground_color(ForegroundColor.red) + Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings') + Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') + Console.set_foreground_color(ForegroundColor.default) diff --git a/src/sh_edraft/cli/cpl_cli/templates/enum/enum.txt b/src/sh_edraft/cli/cpl_cli/templates/enum/enum.txt new file mode 100644 index 00000000..a7aef900 --- /dev/null +++ b/src/sh_edraft/cli/cpl_cli/templates/enum/enum.txt @@ -0,0 +1,6 @@ +from enum import Enum + + +class $Name(Enum): + + pass diff --git a/src/sh_edraft/cli/cpl_cli/templates/module/__init__.py b/src/sh_edraft/cli/cpl_cli/templates/module/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/sh_edraft/cli/cpl_cli/templates/service/service.txt b/src/sh_edraft/cli/cpl_cli/templates/service/service.txt new file mode 100644 index 00000000..d2add199 --- /dev/null +++ b/src/sh_edraft/cli/cpl_cli/templates/service/service.txt @@ -0,0 +1,6 @@ +class $Name($Base): + + def __init__(self): + TestBase.__init__(self) + + self.create()