From 82ffdfe27891fe6e60ccc1747b1f15ee0fadccf2 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 10 Mar 2021 22:29:42 +0100 Subject: [PATCH] Added generate abc command --- src/cpl_cli/cli.py | 2 + src/cpl_cli/command/generate.py | 99 +++++++++++++++++++ src/cpl_cli/startup.py | 9 ++ src/cpl_cli/templates/generate/__init__.py | 0 .../templates/generate/abc_template.py | 22 +++++ 5 files changed, 132 insertions(+) create mode 100644 src/cpl_cli/templates/generate/__init__.py create mode 100644 src/cpl_cli/templates/generate/abc_template.py diff --git a/src/cpl_cli/cli.py b/src/cpl_cli/cli.py index 49be2723..02446c11 100644 --- a/src/cpl_cli/cli.py +++ b/src/cpl_cli/cli.py @@ -2,6 +2,7 @@ from typing import Optional from cpl.application.application_abc import ApplicationABC from cpl_cli.command.build import Build +from cpl_cli.command.generate import Generate from cpl_cli.command.new import New from cpl_cli.command.publish import Publish from cpl_cli.command_handler import CommandHandler @@ -22,6 +23,7 @@ class CLI(ApplicationABC): self._command_handler: CommandHandler = self._services.get_service(CommandHandler) self._command_handler.add_command(CommandModel('build', ['h', 'B'], Build, True)) + self._command_handler.add_command(CommandModel('generate', ['g', 'G'], Generate, True)) self._command_handler.add_command(CommandModel('help', ['h', 'H'], Help, False)) self._command_handler.add_command(CommandModel('new', ['n', 'N'], New, False)) self._command_handler.add_command(CommandModel('publish', ['p', 'P'], Publish, True)) diff --git a/src/cpl_cli/command/generate.py b/src/cpl_cli/command/generate.py index e69de29b..9afbf14e 100644 --- a/src/cpl_cli/command/generate.py +++ b/src/cpl_cli/command/generate.py @@ -0,0 +1,99 @@ +import os + +from cpl.application.application_abc import ApplicationRuntimeABC +from cpl.configuration.configuration_abc import ConfigurationABC +from cpl.console import ForegroundColor +from cpl.console.console import Console +from cpl.utils.string import String +from cpl_cli.command_abc import CommandABC +from cpl_cli.templates.generate.abc_template import ABCTemplate + + +class Generate(CommandABC): + + def __init__(self, configuration: ConfigurationABC, runtime: ApplicationRuntimeABC): + CommandABC.__init__(self) + + self._config = configuration + self._runtime = runtime + + @staticmethod + def _help(message: str): + Console.error(message) + + schematics = [ + ['abc (a|A)'], + ['class (c|C)'], + ['configmodel (cm|CM)'], + ['enum (e|E)'], + ['service (s|S)'], + ] + Console.write_line('Available Schematics:') + for name in schematics: + Console.write(f'\n\t{name} ') + + @staticmethod + def _create_file(file_path: str, value: str): + with open(file_path, 'w') as template: + template.write(value) + template.close() + + def _generate_abc(self, name: str): + rel_path = '' + if '/' in name: + parts = name.split('/') + rel_path = '/'.join(parts[:-1]) + name = parts[len(parts) - 1] + + file_path = os.path.join(self._runtime.working_directory, rel_path, f'{String.convert_to_snake_case(name)}.py') + print(file_path) + if not os.path.isdir(os.path.dirname(file_path)): + os.makedirs(os.path.dirname(file_path)) + + if os.path.isfile(file_path): + Console.error('ABC already exists!') + exit() + + message = f'Creating {self._runtime.working_directory}/{rel_path}/{String.convert_to_snake_case(name)}.py' + if rel_path == '': + message = f'Creating {self._runtime.working_directory}/{String.convert_to_snake_case(name)}.py' + + Console.spinner( + message, + self._create_file, + file_path, + ABCTemplate.get_abc_py(name), + text_foreground_color=ForegroundColor.green, + spinner_foreground_color=ForegroundColor.cyan + ) + + def run(self, args: list[str]): + if len(args) == 0: + self._help('Usage: cpl generate [options]') + exit() + + schematic = args[0] + name = self._config.get_configuration(schematic) + if name is None: + name = Console.read(f'Name for the {args[0]}: ') + + if schematic == 'abc': + self._generate_abc(name) + + elif schematic == 'class': + print(schematic) + + elif schematic == 'configmodel': + print(schematic) + + elif schematic == 'enum': + print(schematic) + + elif schematic == 'service': + print(schematic) + + else: + Console.error('') + exit() + + Console.write('\n') diff --git a/src/cpl_cli/startup.py b/src/cpl_cli/startup.py index b5eab567..e2f8d5cd 100644 --- a/src/cpl_cli/startup.py +++ b/src/cpl_cli/startup.py @@ -7,6 +7,7 @@ from cpl.configuration.console_argument import ConsoleArgument from cpl.configuration.configuration_abc import ConfigurationABC from cpl.dependency_injection.service_provider_abc import ServiceProviderABC from cpl_cli.command.build import Build +from cpl_cli.command.generate import Generate from cpl_cli.command.new import New from cpl_cli.command.publish import Publish from cpl_cli.command_handler import CommandHandler @@ -42,6 +43,13 @@ class Startup(StartupABC): self._configuration.add_environment_variables('CPL_') self._configuration.add_json_file('cpl.json', optional=True, output=False) self._configuration.add_console_argument(ConsoleArgument('', 'build', ['b', 'B'], '')) + self._configuration.add_console_argument(ConsoleArgument('', 'generate', ['g', 'G'], '', [ + ConsoleArgument('', 'abc', ['a', 'A'], ' '), + ConsoleArgument('', 'class', ['c', 'C'], ' '), + ConsoleArgument('', 'configmodel', ['cm', 'CM'], ' '), + ConsoleArgument('', 'enum', ['e', 'E'], ' '), + ConsoleArgument('', 'service', ['s', 'S'], ' ') + ])) self._configuration.add_console_argument(ConsoleArgument('', 'help', ['h', 'H'], '')) self._configuration.add_console_argument(ConsoleArgument('', 'new', ['n', 'N'], '', [ ConsoleArgument('', 'console', ['c', 'C'], ' ') @@ -58,6 +66,7 @@ class Startup(StartupABC): self._services.add_transient(PublisherABC, Publisher) self._services.add_transient(Build) + self._services.add_transient(Generate) self._services.add_transient(Help) self._services.add_transient(New) self._services.add_transient(Publish) diff --git a/src/cpl_cli/templates/generate/__init__.py b/src/cpl_cli/templates/generate/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/cpl_cli/templates/generate/abc_template.py b/src/cpl_cli/templates/generate/abc_template.py new file mode 100644 index 00000000..9a2cd56f --- /dev/null +++ b/src/cpl_cli/templates/generate/abc_template.py @@ -0,0 +1,22 @@ +import textwrap +from string import Template + + +class ABCTemplate: + + @staticmethod + def get_abc_py(name: str) -> str: + string = textwrap.dedent("""\ + from abc import ABC, abstractmethod + + + class $Name(ABC): + + @abstractmethod + def __init__(self): pass + + """) + + return Template(string).substitute( + Name=name + )