From 6a5c83228849aee6b504e7da3ed351af3e2d2f53 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 7 Apr 2021 18:19:34 +0200 Subject: [PATCH] Added new file structure --- cpl-workspace.json | 9 +++ src/cpl/cpl.json | 57 +++++++++++++++++++ src/cpl_cli/command_handler_service.py | 47 ++++++++++++++- .../configuration/workspace_settings.py | 31 ++++++++++ .../workspace_settings_name_enum.py | 7 +++ src/cpl_cli/cpl_cli.json | 46 +++++++++++++++ src/cpl_cli/startup.py | 8 ++- 7 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 cpl-workspace.json create mode 100644 src/cpl/cpl.json create mode 100644 src/cpl_cli/configuration/workspace_settings.py create mode 100644 src/cpl_cli/configuration/workspace_settings_name_enum.py create mode 100644 src/cpl_cli/cpl_cli.json diff --git a/cpl-workspace.json b/cpl-workspace.json new file mode 100644 index 00000000..617e8345 --- /dev/null +++ b/cpl-workspace.json @@ -0,0 +1,9 @@ +{ + "Workspace": { + "DefaultProject": "cpl_cli", + "Projects": { + "cpl": "src/cpl/cpl.json", + "cpl_cli": "src/cpl_cli/cpl_cli.json" + } + } +} \ No newline at end of file diff --git a/src/cpl/cpl.json b/src/cpl/cpl.json new file mode 100644 index 00000000..4eb54c90 --- /dev/null +++ b/src/cpl/cpl.json @@ -0,0 +1,57 @@ +{ + "ProjectSettings": { + "Name": "sh_cpl", + "Version": { + "Major": "2021", + "Minor": "4", + "Micro": "post1" + }, + "Author": "Sven Heidemann", + "AuthorEmail": "sven.heidemann@sh-edraft.de", + "Description": "sh-edraft Common Python library", + "LongDescription": "sh-edraft Common Python library", + "URL": "https://www.sh-edraft.de", + "CopyrightDate": "2020 - 2021", + "CopyrightName": "sh-edraft.de", + "LicenseName": "MIT", + "LicenseDescription": "MIT, see LICENSE for more details.", + "Dependencies": [ + "colorama==0.4.4", + "mysql-connector==2.2.9", + "psutil==5.8.0", + "packaging==20.9", + "pyfiglet==0.8.post1", + "pynput==1.7.3", + "SQLAlchemy==1.4.3", + "setuptools==54.2.0", + "tabulate==0.8.9", + "termcolor==1.1.0", + "watchdog==2.0.2", + "wheel==0.36.2" + ], + "PythonVersion": ">=3.8", + "PythonPath": {}, + "Classifiers": [] + }, + "BuildSettings": { + "ProjectType": "library", + "SourcePath": "", + "OutputPath": "dist", + "Main": "", + "EntryPoint": "cpl", + "IncludePackageData": true, + "Included": [ + "*/templates" + ], + "Excluded": [ + "*/__pycache__", + "*/logs", + "*/tests" + ], + "PackageData": { + "cpl_cli": [ + "*.json" + ] + } + } +} \ No newline at end of file diff --git a/src/cpl_cli/command_handler_service.py b/src/cpl_cli/command_handler_service.py index 75bcdced..a01cff7b 100644 --- a/src/cpl_cli/command_handler_service.py +++ b/src/cpl_cli/command_handler_service.py @@ -1,9 +1,11 @@ import os +import sys from abc import ABC from cpl.configuration.configuration_abc import ConfigurationABC from cpl.console.console import Console from cpl.dependency_injection.service_provider_abc import ServiceProviderABC +from cpl_cli.configuration.workspace_settings import WorkspaceSettings from cpl_cli.error import Error from cpl_cli.command_model import CommandModel @@ -28,6 +30,9 @@ class CommandHandler(ABC): def commands(self) -> list[CommandModel]: return self._commands + def _load_json(self): + pass + def add_command(self, cmd: CommandModel): self._commands.append(cmd) @@ -43,12 +48,48 @@ class CommandHandler(ABC): """ for command in self._commands: if cmd == command.name or cmd in command.aliases: - if command.is_project_needed and not os.path.isfile(os.path.join(self._env.working_directory, 'cpl.json')): - Error.error('The command requires to be run in an CPL project, but a project could not be found.') + if command.is_project_needed and \ + not os.path.isfile(os.path.join(self._env.working_directory, 'cpl-workspace.json')): + Error.error( + 'The command requires to be run in an CPL workspace, but a workspace could not be found.' + ) return if command.is_project_needed: - self._config.add_json_file('cpl.json', optional=True, output=False) + self._config.add_json_file('cpl-workspace.json', optional=True, output=False) + workspace: WorkspaceSettings = self._config.get_configuration(WorkspaceSettings) + + if workspace is None: + Error.error( + 'The command requires to be run in an CPL workspace, but a workspace could not be found.' + ) + return + + project_name = workspace.default_project + if len(args) > 0: + project_name = args[0] + index = sys.argv.index(args[0]) + 1 + if index < len(sys.argv): + args = sys.argv[index:] + + if project_name not in workspace.projects: + Error.error( + f'Project {project_name} not found.' + ) + return + + project_json = workspace.projects[project_name] + if not os.path.isfile(os.path.join(self._env.working_directory, project_json)): + Error.error( + 'The command requires to be run in an CPL project, but a project could not be found.' + ) + return + + self._config.add_json_file(project_json, optional=True, output=False) + + self._config.environment.set_working_directory( + os.path.join(self._env.working_directory, os.path.dirname(project_json)) + ) self._services.get_service(command.command).run(args) Console.write('\n') diff --git a/src/cpl_cli/configuration/workspace_settings.py b/src/cpl_cli/configuration/workspace_settings.py new file mode 100644 index 00000000..ff40999a --- /dev/null +++ b/src/cpl_cli/configuration/workspace_settings.py @@ -0,0 +1,31 @@ +import traceback +from typing import Optional + +from cpl.configuration.configuration_model_abc import ConfigurationModelABC +from cpl.console import Console +from cpl_cli.configuration.workspace_settings_name_enum import WorkspaceSettingsNameEnum + + +class WorkspaceSettings(ConfigurationModelABC): + + def __init__(self): + ConfigurationModelABC.__init__(self) + + self._default_project: Optional[str] = None + self._projects: dict[str, str] = {} + + @property + def default_project(self) -> str: + return self._default_project + + @property + def projects(self) -> dict[str, str]: + return self._projects + + def from_dict(self, settings: dict): + try: + self._default_project = settings[WorkspaceSettingsNameEnum.default_project.value] + self._projects = settings[WorkspaceSettingsNameEnum.projects.value] + except Exception as e: + Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings') + Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') diff --git a/src/cpl_cli/configuration/workspace_settings_name_enum.py b/src/cpl_cli/configuration/workspace_settings_name_enum.py new file mode 100644 index 00000000..57bbdd20 --- /dev/null +++ b/src/cpl_cli/configuration/workspace_settings_name_enum.py @@ -0,0 +1,7 @@ +from enum import Enum + + +class WorkspaceSettingsNameEnum(Enum): + + default_project = 'DefaultProject' + projects = 'Projects' diff --git a/src/cpl_cli/cpl_cli.json b/src/cpl_cli/cpl_cli.json new file mode 100644 index 00000000..6c2c2d62 --- /dev/null +++ b/src/cpl_cli/cpl_cli.json @@ -0,0 +1,46 @@ +{ + "ProjectSettings": { + "Name": "sh_cpl-cli", + "Version": { + "Major": "2021", + "Minor": "4", + "Micro": "" + }, + "Author": "Sven Heidemann", + "AuthorEmail": "sven.heidemann@sh-edraft.de", + "Description": "sh-edraft Common Python library CLI", + "LongDescription": "sh-edraft Common Python library Command Line Interface", + "URL": "https://www.sh-edraft.de", + "CopyrightDate": "2020 - 2021", + "CopyrightName": "sh-edraft.de", + "LicenseName": "MIT", + "LicenseDescription": "MIT, see LICENSE for more details.", + "Dependencies": [ + "sh_cpl==2021.4.0" + ], + "PythonVersion": ">=3.8", + "PythonPath": {}, + "Classifiers": [] + }, + "BuildSettings": { + "ProjectType": "console", + "SourcePath": "", + "OutputPath": "dist", + "Main": "main", + "EntryPoint": "cpl", + "IncludePackageData": true, + "Included": [ + "*/templates" + ], + "Excluded": [ + "*/__pycache__", + "*/logs", + "*/tests" + ], + "PackageData": { + "cpl_cli": [ + "*.json" + ] + } + } +} \ No newline at end of file diff --git a/src/cpl_cli/startup.py b/src/cpl_cli/startup.py index f05fe10a..28661067 100644 --- a/src/cpl_cli/startup.py +++ b/src/cpl_cli/startup.py @@ -1,3 +1,5 @@ +import os + from cpl.application.startup_abc import StartupABC from cpl.configuration.console_argument import ConsoleArgument from cpl.configuration.configuration_abc import ConfigurationABC @@ -29,7 +31,7 @@ class Startup(StartupABC): self._env = self._configuration.environment self._services = services - self._env.set_runtime_directory(__file__) + self._env.set_runtime_directory(os.path.dirname(__file__)) def configure_configuration(self) -> ConfigurationABC: self._configuration.argument_error_function = Error.error @@ -56,7 +58,9 @@ class Startup(StartupABC): ConsoleArgument('', 'library', ['l', 'L'], ' ') ])) self._configuration.add_console_argument(ConsoleArgument('', 'publish', ['p', 'P'], '')) - self._configuration.add_console_argument(ConsoleArgument('', 'start', ['s', 'S'], '')) + self._configuration.add_console_argument( + ConsoleArgument('', 'start', ['s', 'S'], ' ', is_value_token_optional=True) + ) self._configuration.add_console_argument(ConsoleArgument('', 'uninstall', ['ui', 'UI'], ' ')) self._configuration.add_console_argument(ConsoleArgument('', 'update', ['u', 'U'], '')) self._configuration.add_console_argument(ConsoleArgument('', 'version', ['v', 'V'], ''))