From 88362006ef5ce18aada110c1892098cb7eaf8191 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 16 Dec 2020 13:38:08 +0100 Subject: [PATCH] Improved cli --- src/sh_edraft/cli/command/__init__.py | 0 src/sh_edraft/cli/command/base/__init__.py | 0 .../cli/command/base/command_base.py | 10 +++ src/sh_edraft/cli/cpl_cli/cli.py | 19 ++--- src/sh_edraft/cli/cpl_cli/cli_commands.py | 6 +- .../cli/cpl_cli/commands/__init__.py | 0 src/sh_edraft/cli/cpl_cli/commands/help.py | 10 +++ src/sh_edraft/cli/cpl_cli/commands/new.py | 84 +++++++++++++++++++ src/sh_edraft/cli/interpreter/__init__.py | 0 src/sh_edraft/cli/interpreter/interpreter.py | 28 +++++++ 10 files changed, 146 insertions(+), 11 deletions(-) create mode 100644 src/sh_edraft/cli/command/__init__.py create mode 100644 src/sh_edraft/cli/command/base/__init__.py create mode 100644 src/sh_edraft/cli/command/base/command_base.py create mode 100644 src/sh_edraft/cli/cpl_cli/commands/__init__.py create mode 100644 src/sh_edraft/cli/cpl_cli/commands/help.py create mode 100644 src/sh_edraft/cli/cpl_cli/commands/new.py create mode 100644 src/sh_edraft/cli/interpreter/__init__.py create mode 100644 src/sh_edraft/cli/interpreter/interpreter.py diff --git a/src/sh_edraft/cli/command/__init__.py b/src/sh_edraft/cli/command/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/sh_edraft/cli/command/base/__init__.py b/src/sh_edraft/cli/command/base/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/sh_edraft/cli/command/base/command_base.py b/src/sh_edraft/cli/command/base/command_base.py new file mode 100644 index 00000000..bc63204b --- /dev/null +++ b/src/sh_edraft/cli/command/base/command_base.py @@ -0,0 +1,10 @@ +from abc import ABC, abstractmethod + + +class CommandBase(ABC): + + @abstractmethod + def __init__(self): pass + + @abstractmethod + def run(self, args: list[str]): pass diff --git a/src/sh_edraft/cli/cpl_cli/cli.py b/src/sh_edraft/cli/cpl_cli/cli.py index a8ff912b..b5d24415 100644 --- a/src/sh_edraft/cli/cpl_cli/cli.py +++ b/src/sh_edraft/cli/cpl_cli/cli.py @@ -1,25 +1,24 @@ import sys -from sh_edraft.cli.cpl_cli.cli_commands import CLICommands +from sh_edraft.cli.cpl_cli.commands.help import Help +from sh_edraft.cli.cpl_cli.commands.new import New +from sh_edraft.cli.interpreter.interpreter import Interpreter class CLI: def __init__(self): - self._commands: dict = {} + self._interpreter = Interpreter() def setup(self): - self._commands[CLICommands.new.__name__] = CLICommands.new - self._commands[CLICommands.help.__name__] = CLICommands.help + self._interpreter.add_command(New()) + self._interpreter.add_command(Help()) def main(self): - args = sys.argv[1:] - + print('CPL CLI:') + string = ' '.join(sys.argv[1:]) try: - cmd = self._commands[args[0]] - cmd(args[1:]) - except KeyError: - CLICommands.unexpected_command(args[0]) + self._interpreter.interpret(string) except Exception as e: print(e) diff --git a/src/sh_edraft/cli/cpl_cli/cli_commands.py b/src/sh_edraft/cli/cpl_cli/cli_commands.py index cd0b6c21..1d8d6765 100644 --- a/src/sh_edraft/cli/cpl_cli/cli_commands.py +++ b/src/sh_edraft/cli/cpl_cli/cli_commands.py @@ -60,7 +60,11 @@ class CLICommands: if name[0].islower(): name = f'{name[0].upper()}{name[1:]}' - if args[0] == 'configmodel': + if args[0] == 'base': + template_content = template_content.replace('$Name', f'{name}Base') + pyfile.write(template_content) + + elif args[0] == 'configmodel': template_content = template_content.replace('$Name', f'{name}Settings') pyfile.write(template_content) diff --git a/src/sh_edraft/cli/cpl_cli/commands/__init__.py b/src/sh_edraft/cli/cpl_cli/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/sh_edraft/cli/cpl_cli/commands/help.py b/src/sh_edraft/cli/cpl_cli/commands/help.py new file mode 100644 index 00000000..3a723f14 --- /dev/null +++ b/src/sh_edraft/cli/cpl_cli/commands/help.py @@ -0,0 +1,10 @@ +from sh_edraft.cli.command.base.command_base import CommandBase + + +class Help(CommandBase): + + def __init__(self): + CommandBase.__init__(self) + + def run(self, args: list[str]): + print('Commands:') diff --git a/src/sh_edraft/cli/cpl_cli/commands/new.py b/src/sh_edraft/cli/cpl_cli/commands/new.py new file mode 100644 index 00000000..697d43c9 --- /dev/null +++ b/src/sh_edraft/cli/cpl_cli/commands/new.py @@ -0,0 +1,84 @@ +import os + +from sh_edraft.cli.command.base.command_base import CommandBase + + +class New(CommandBase): + + def __init__(self): + CommandBase.__init__(self) + + def run(self, args: list[str]): + rel_path = f'{os.path.dirname(__file__)}/../' + if not os.path.isdir(f'{rel_path}/templates/{args[0]}'): + print(f'Unexpected argument {args[0]}') + + sub_args = args[1:] + + if len(sub_args) != 1: + print(f'Unexpected argument {sub_args[1]}') + + if not (sub_args[0].startswith('.') or sub_args[0].startswith('/')): + full_path = f'./{sub_args[0]}' + else: + 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'{rel_path}/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] == 'base': + template_content = template_content.replace('$Name', f'{name}Base') + pyfile.write(template_content) + + elif 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() diff --git a/src/sh_edraft/cli/interpreter/__init__.py b/src/sh_edraft/cli/interpreter/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/sh_edraft/cli/interpreter/interpreter.py b/src/sh_edraft/cli/interpreter/interpreter.py new file mode 100644 index 00000000..7ee549ed --- /dev/null +++ b/src/sh_edraft/cli/interpreter/interpreter.py @@ -0,0 +1,28 @@ +from sh_edraft.cli.command.base.command_base import CommandBase + + +class Interpreter: + + def __init__(self): + self._commands: list[CommandBase] = [] + + def add_command(self, command: CommandBase): + self._commands.append(command) + + def remove_command(self, command: CommandBase): + self._commands.remove(command) + + def interpret(self, input_string: str): + input_list = input_string.split(' ') + commands = [type(cmd).__name__.lower() for cmd in self._commands] + command = input_list[0] + args = input_list[1:] + print(command) + if command in commands: + cmd = next((cmd for cmd in self._commands if type(cmd).__name__.lower() == command), None) + if cmd is not None: + cmd.run(args) + else: + print(f'Unexpected command {command}') + else: + print(f'Unexpected command {command}')