Improved application host

This commit is contained in:
Sven Heidemann 2021-03-04 06:53:38 +01:00
parent 2c2c1ede21
commit 960607a60d
7 changed files with 73 additions and 26 deletions

View File

@ -22,13 +22,14 @@
<select />
</component>
<component name="ChangeListManager">
<list default="true" id="7e2256bc-a6b8-4880-83a6-8b0e3372d0a4" name="Default Changelist" comment="Improved cli">
<change afterPath="$PROJECT_DIR$/src/cpl_cli/error.py" afterDir="false" />
<list default="true" id="7e2256bc-a6b8-4880-83a6-8b0e3372d0a4" name="Default Changelist" comment="Smaller bugfixes">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl/application/application_abc.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl/application/application_abc.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl/application/application_host.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl/application/application_host.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl_cli/cli.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl_cli/cli.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl_cli/command_handler.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl_cli/command_handler.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl/application/application_host_abc.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl/application/application_host_abc.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl/configuration/configuration.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl/configuration/configuration.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl/configuration/configuration_abc.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl/configuration/configuration_abc.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl_cli/startup.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl_cli/startup.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -100,7 +101,7 @@
<recent name="$PROJECT_DIR$/src/cpl/database" />
</key>
</component>
<component name="RunManager" selected="Python.cli help">
<component name="RunManager" selected="Python.main cli">
<configuration name="cli build" type="PythonConfigurationType" factoryName="Python">
<module name="sh_common_py_lib" />
<option name="INTERPRETER_OPTIONS" value="" />
@ -489,13 +490,6 @@
<option name="presentableId" value="Default" />
<updated>1605881914521</updated>
</task>
<task id="LOCAL-00040" summary="Improved publisher">
<created>1608048544558</created>
<option name="number" value="00040" />
<option name="presentableId" value="LOCAL-00040" />
<option name="project" value="LOCAL" />
<updated>1608048544558</updated>
</task>
<task id="LOCAL-00041" summary="Improved publisher">
<created>1608049672925</created>
<option name="number" value="00041" />
@ -832,7 +826,14 @@
<option name="project" value="LOCAL" />
<updated>1614797335732</updated>
</task>
<option name="localTasksCounter" value="89" />
<task id="LOCAL-00089" summary="Smaller bugfixes">
<created>1614836560513</created>
<option name="number" value="00089" />
<option name="presentableId" value="LOCAL-00089" />
<option name="project" value="LOCAL" />
<updated>1614836560514</updated>
</task>
<option name="localTasksCounter" value="90" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
@ -848,7 +849,6 @@
<option name="oldMeFiltersMigrated" value="true" />
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="Improved service providing" />
<MESSAGE value="Added email client" />
<MESSAGE value="Published" />
<MESSAGE value="Improved publisher" />
@ -873,7 +873,8 @@
<MESSAGE value="Added version and help cli command" />
<MESSAGE value="Improved help command" />
<MESSAGE value="Improved cli" />
<option name="LAST_COMMIT_MESSAGE" value="Improved cli" />
<MESSAGE value="Smaller bugfixes" />
<option name="LAST_COMMIT_MESSAGE" value="Smaller bugfixes" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>

View File

@ -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()

View File

@ -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

View File

@ -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

View File

@ -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')

View File

@ -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

View File

@ -8,6 +8,7 @@ from cpl.dependency_injection.service_provider_base import ServiceProviderABC
from cpl_cli.command_handler import CommandHandler
from cpl_cli.commands.help import Help
from cpl_cli.commands.version import Version
from cpl_cli.error import Error
class Startup(StartupABC):
@ -23,6 +24,9 @@ class Startup(StartupABC):
self._app_host = ApplicationHost()
self._configuration = self._app_host.configuration
self._services = self._app_host.services
self._app_host.console_argument_error_function(Error.error)
return self._app_host
def create_configuration(self) -> ConfigurationABC: