From 5ddc2dc72d987fc770727d1223ff0efb2d390454 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 23 Nov 2021 09:25:51 +0100 Subject: [PATCH] Added menus --- src/multi_install.sh | 5 +++ src/multi_install/__init__.py | 2 +- src/multi_install/abc/__init__.py | 1 + src/multi_install/abc/menu_service_abc.py | 19 +++++++++ src/multi_install/config/__init__.py | 1 + src/multi_install/config/os_settings.py | 24 ++++++++++++ src/multi_install/service/__init__.py | 1 + src/multi_install/service/menu_service.py | 47 +++++++++++++++++++++++ src/multi_install_cli/application.py | 8 +++- src/multi_install_cli/appsettings.json | 7 ++++ src/multi_install_cli/startup.py | 10 +++++ 11 files changed, 122 insertions(+), 3 deletions(-) create mode 100755 src/multi_install.sh create mode 100644 src/multi_install/abc/__init__.py create mode 100644 src/multi_install/abc/menu_service_abc.py create mode 100644 src/multi_install/config/__init__.py create mode 100644 src/multi_install/config/os_settings.py create mode 100644 src/multi_install/service/__init__.py create mode 100644 src/multi_install/service/menu_service.py diff --git a/src/multi_install.sh b/src/multi_install.sh new file mode 100755 index 0000000..7225f9d --- /dev/null +++ b/src/multi_install.sh @@ -0,0 +1,5 @@ +#!/bin/bash +# -*- coding: utf-8 -*- +export PYTHONPATH=./:$PYTHONPATH + +python3.9 multi_install_cli/main.py \ No newline at end of file diff --git a/src/multi_install/__init__.py b/src/multi_install/__init__.py index ad5eca3..425ab6c 100644 --- a/src/multi_install/__init__.py +++ b/src/multi_install/__init__.py @@ -1 +1 @@ -# imports: +# imports diff --git a/src/multi_install/abc/__init__.py b/src/multi_install/abc/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/multi_install/abc/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/multi_install/abc/menu_service_abc.py b/src/multi_install/abc/menu_service_abc.py new file mode 100644 index 0000000..cdf5593 --- /dev/null +++ b/src/multi_install/abc/menu_service_abc.py @@ -0,0 +1,19 @@ +from abc import ABC, abstractmethod + + +class MenuServiceABC(ABC): + + @abstractmethod + def __init__(self): pass + + @abstractmethod + def run(self): pass + + @abstractmethod + def select_os(self) -> str: pass + + @abstractmethod + def main_menu(self) -> str: pass + + @abstractmethod + def select_collection(self) -> str: pass diff --git a/src/multi_install/config/__init__.py b/src/multi_install/config/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/multi_install/config/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/multi_install/config/os_settings.py b/src/multi_install/config/os_settings.py new file mode 100644 index 0000000..ca70e9d --- /dev/null +++ b/src/multi_install/config/os_settings.py @@ -0,0 +1,24 @@ +import traceback + +from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC +from cpl_core.console import Console + + +class OSSettings(ConfigurationModelABC): + + def __init__(self): + ConfigurationModelABC.__init__(self) + + self._operating_systems = [] + + @property + def operating_systems(self) -> list[str]: + return self._operating_systems + + def from_dict(self, settings: dict): + try: + for os in settings['OperatingSystems']: + self._operating_systems.append(os) + 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/multi_install/service/__init__.py b/src/multi_install/service/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/multi_install/service/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/multi_install/service/menu_service.py b/src/multi_install/service/menu_service.py new file mode 100644 index 0000000..09dc5c7 --- /dev/null +++ b/src/multi_install/service/menu_service.py @@ -0,0 +1,47 @@ +from cpl_core.console import Console +from multi_install.abc.menu_service_abc import MenuServiceABC +from multi_install.config.os_settings import OSSettings + + +class MenuService(MenuServiceABC): + + def __init__(self, os_settings: OSSettings): + self._os_settings = os_settings + + self._is_running = True + + def run(self): + while self._is_running: + os = self.select_os() + if os == 'Exit': + self._is_running = False + break + + main_menu = self.main_menu() + if main_menu == 'Exit': + self._is_running = False + break + + def select_os(self) -> str: + options = self._os_settings.operating_systems + options.append('Exit') + return Console.select('>', 'Select OS:', options) + + def main_menu(self) -> str: + options = [ + 'Install collection', + 'Add collection', + 'Remove collection', + + 'Install application', + 'Add application', + 'Remove application', + 'Add application' + ] + options.append('Exit') + return Console.select('>', 'Select option:', options) + + def select_collection(self) -> str: + options = [] + options.append('Exit') + return Console.select('>', 'Select collection:', options) diff --git a/src/multi_install_cli/application.py b/src/multi_install_cli/application.py index 34a68c8..5a9cd2d 100644 --- a/src/multi_install_cli/application.py +++ b/src/multi_install_cli/application.py @@ -3,14 +3,18 @@ from cpl_core.configuration import ConfigurationABC from cpl_core.console import Console from cpl_core.dependency_injection import ServiceProviderABC +from multi_install.abc.menu_service_abc import MenuServiceABC + class Application(ApplicationABC): def __init__(self, config: ConfigurationABC, services: ServiceProviderABC): ApplicationABC.__init__(self, config, services) + + self._menu_service: MenuServiceABC = services.get_service(MenuServiceABC) def configure(self): pass - + def main(self): - Console.write_line('Hello World') + self._menu_service.run() \ No newline at end of file diff --git a/src/multi_install_cli/appsettings.json b/src/multi_install_cli/appsettings.json index 629e6eb..0c7b26e 100644 --- a/src/multi_install_cli/appsettings.json +++ b/src/multi_install_cli/appsettings.json @@ -11,5 +11,12 @@ "Filename": "log_$start_time.log", "ConsoleLogLevel": "ERROR", "FileLogLevel": "WARN" + }, + + "OS": { + "OperatingSystems": [ + "Fedora", + "Ubuntu" + ] } } diff --git a/src/multi_install_cli/startup.py b/src/multi_install_cli/startup.py index 359c03d..ba56160 100644 --- a/src/multi_install_cli/startup.py +++ b/src/multi_install_cli/startup.py @@ -1,7 +1,11 @@ +import os from cpl_core.application import StartupABC from cpl_core.configuration import ConfigurationABC from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC from cpl_core.environment import ApplicationEnvironment +from multi_install.abc.menu_service_abc import MenuServiceABC + +from multi_install.service.menu_service import MenuService class Startup(StartupABC): @@ -10,7 +14,13 @@ class Startup(StartupABC): StartupABC.__init__(self) def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC: + environment.set_working_directory(os.path.dirname(__file__)) + + configuration.add_json_file('appsettings.json') + return configuration def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC: + services.add_singleton(MenuServiceABC, MenuService) + return services.build_service_provider()