Improved application host
This commit is contained in:
		| @@ -1,4 +1,3 @@ | ||||
| import atexit | ||||
| from abc import ABC, abstractmethod | ||||
| from typing import Type, Optional | ||||
|  | ||||
| @@ -21,10 +20,6 @@ class ApplicationABC(ABC): | ||||
|     def use_startup(self, startup: Type[StartupABC]): | ||||
|         self._startup = startup() | ||||
|  | ||||
|     @staticmethod | ||||
|     def output_at_exit(): | ||||
|         atexit.register(Console.close) | ||||
|  | ||||
|     def build(self): | ||||
|         if self._startup is not None: | ||||
|             self._app_host = self._startup.create_application_host() | ||||
|   | ||||
| @@ -1,3 +1,5 @@ | ||||
| import atexit | ||||
| from collections import Callable | ||||
| from datetime import datetime | ||||
|  | ||||
| from cpl.application.application_host_abc import ApplicationHostABC | ||||
| @@ -5,6 +7,7 @@ from cpl.application.application_runtime import ApplicationRuntime | ||||
| from cpl.application.application_runtime_abc import ApplicationRuntimeABC | ||||
| from cpl.configuration.configuration import Configuration | ||||
| from cpl.configuration.configuration_abc import ConfigurationABC | ||||
| from cpl.console.console import Console | ||||
| from cpl.dependency_injection.service_provider import ServiceProvider | ||||
| from cpl.dependency_injection.service_provider_base import ServiceProviderABC | ||||
|  | ||||
| @@ -35,4 +38,9 @@ class ApplicationHost(ApplicationHostABC): | ||||
|     def services(self) -> ServiceProviderABC: | ||||
|         return self._services | ||||
|  | ||||
|     def create(self): pass | ||||
|     @staticmethod | ||||
|     def output_at_exit(): | ||||
|         atexit.register(Console.close) | ||||
|  | ||||
|     def console_argument_error_function(self, function: Callable): | ||||
|         self._config.argument_error_function = function | ||||
|   | ||||
| @@ -1,4 +1,5 @@ | ||||
| from abc import ABC, abstractmethod | ||||
| from collections import Callable | ||||
|  | ||||
| from cpl.application.application_runtime_abc import ApplicationRuntimeABC | ||||
| from cpl.configuration.configuration_abc import ConfigurationABC | ||||
| @@ -21,3 +22,10 @@ class ApplicationHostABC(ABC): | ||||
|     @property | ||||
|     @abstractmethod | ||||
|     def services(self) -> ServiceProviderABC: pass | ||||
|  | ||||
|     @staticmethod | ||||
|     @abstractmethod | ||||
|     def output_at_exit(): pass | ||||
|  | ||||
|     @abstractmethod | ||||
|     def console_argument_error_function(self, function: Callable): pass | ||||
|   | ||||
| @@ -2,7 +2,7 @@ import json | ||||
| import os | ||||
| import sys | ||||
| from collections import Callable | ||||
| from typing import Union, Type | ||||
| from typing import Union, Type, Optional | ||||
|  | ||||
| from cpl.configuration.configuration_abc import ConfigurationABC | ||||
| from cpl.configuration.configuration_model_abc import ConfigurationModelABC | ||||
| @@ -26,6 +26,8 @@ class Configuration(ConfigurationABC): | ||||
|         self._argument_types: list[ConsoleArgument] = [] | ||||
|         self._additional_arguments: list[str] = [] | ||||
|  | ||||
|         self._argument_error_function: Optional[Callable] = None | ||||
|  | ||||
|     @property | ||||
|     def environment(self) -> EnvironmentABC: | ||||
|         return self._hosting_environment | ||||
| @@ -34,6 +36,14 @@ class Configuration(ConfigurationABC): | ||||
|     def additional_arguments(self) -> list[str]: | ||||
|         return self._additional_arguments | ||||
|  | ||||
|     @property | ||||
|     def argument_error_function(self) -> Optional[Callable]: | ||||
|         return self._argument_error_function | ||||
|  | ||||
|     @argument_error_function.setter | ||||
|     def argument_error_function(self, argument_error_function: Callable): | ||||
|         self._argument_error_function = argument_error_function | ||||
|  | ||||
|     @staticmethod | ||||
|     def _print_info(name: str, message: str): | ||||
|         Console.set_foreground_color(ForegroundColor.green) | ||||
| @@ -106,10 +116,22 @@ class Configuration(ConfigurationABC): | ||||
|                         break | ||||
|  | ||||
|                 if not is_done: | ||||
|                     self._print_error(__name__, f'Invalid argument: {arg}') | ||||
|                     message = f'Invalid argument: {arg}' | ||||
|  | ||||
|                     if self._argument_error_function is not None: | ||||
|                         self._argument_error_function(message) | ||||
|                     else: | ||||
|                         self._print_error(__name__, message) | ||||
|  | ||||
|                     exit() | ||||
|             except Exception as e: | ||||
|                 self._print_error(__name__, f'Invalid argument: {arg} -> {e}') | ||||
|                 message = f'Invalid argument: {arg} -> {e}' | ||||
|  | ||||
|                 if self._argument_error_function is not None: | ||||
|                     self._argument_error_function(message) | ||||
|                 else: | ||||
|                     self._print_error(__name__, message) | ||||
|  | ||||
|                 exit() | ||||
|  | ||||
|     def add_json_file(self, name: str, optional: bool = None): | ||||
| @@ -149,7 +171,8 @@ class Configuration(ConfigurationABC): | ||||
|     def add_configuration(self, key_type: type, value: ConfigurationModelABC): | ||||
|         self._config[key_type] = value | ||||
|  | ||||
|     def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[str, Callable[ConfigurationModelABC]]: | ||||
|     def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[ | ||||
|         str, Callable[ConfigurationModelABC]]: | ||||
|         if search_type not in self._config: | ||||
|             raise Exception(f'Config model by type {search_type} not found') | ||||
|  | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| from abc import abstractmethod, ABC | ||||
| from collections import Callable | ||||
| from typing import Type, Union | ||||
| from typing import Type, Union, Optional | ||||
|  | ||||
| from cpl.configuration.configuration_model_abc import ConfigurationModelABC | ||||
| from cpl.environment.environment_abc import EnvironmentABC | ||||
| @@ -19,6 +19,14 @@ class ConfigurationABC(ABC): | ||||
|     @abstractmethod | ||||
|     def additional_arguments(self) -> list[str]: pass | ||||
|  | ||||
|     @property | ||||
|     @abstractmethod | ||||
|     def argument_error_function(self) -> Optional[Callable]: pass | ||||
|  | ||||
|     @argument_error_function.setter | ||||
|     @abstractmethod | ||||
|     def argument_error_function(self, argument_error_function: Callable): pass | ||||
|  | ||||
|     @abstractmethod | ||||
|     def add_environment_variables(self, prefix: str): pass | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user