From c6d1dce577011f223a69a7e2c0e661b8033aa3d6 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 26 Nov 2020 18:58:08 +0100 Subject: [PATCH] Improved service providing and app hosting --- .../configuration/base/configuration_base.py | 14 ++++--- src/sh_edraft/hosting/application_host.py | 41 ++++++------------- src/sh_edraft/hosting/application_runtime.py | 38 +++++++++++++++++ src/sh_edraft/hosting/base/__init__.py | 1 + .../hosting/base/application_base.py | 3 ++ .../hosting/base/application_host_base.py | 23 +++-------- .../hosting/base/application_runtime_base.py | 34 +++++++++++++++ .../service/base/service_provider_base.py | 4 -- src/sh_edraft/service/service_provider.py | 25 ++++------- src/tests_dev/main.py | 1 + src/tests_dev/program.py | 13 +++--- 11 files changed, 118 insertions(+), 79 deletions(-) create mode 100644 src/sh_edraft/hosting/application_runtime.py create mode 100644 src/sh_edraft/hosting/base/application_runtime_base.py diff --git a/src/sh_edraft/configuration/base/configuration_base.py b/src/sh_edraft/configuration/base/configuration_base.py index 03d74dd6..06c8bfe8 100644 --- a/src/sh_edraft/configuration/base/configuration_base.py +++ b/src/sh_edraft/configuration/base/configuration_base.py @@ -1,15 +1,14 @@ -from abc import abstractmethod +from abc import abstractmethod, ABC from collections import Callable +from typing import Type from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase -from sh_edraft.service.base.service_base import ServiceBase -class ConfigurationBase(ServiceBase): +class ConfigurationBase(ABC): @abstractmethod - def __init__(self): - ServiceBase.__init__(self) + def __init__(self): pass @property @abstractmethod @@ -19,4 +18,7 @@ class ConfigurationBase(ServiceBase): def add_config_by_type(self, key_type: type, value: object): pass @abstractmethod - def get_config_by_type(self, search_type: ConfigurationModelBase) -> Callable[ConfigurationModelBase]: pass + def get_config_by_type(self, search_type: Type[ConfigurationModelBase]) -> Callable[ConfigurationModelBase]: pass + + @abstractmethod + def create(self): pass diff --git a/src/sh_edraft/hosting/application_host.py b/src/sh_edraft/hosting/application_host.py index e9a4ecc4..edfa6613 100644 --- a/src/sh_edraft/hosting/application_host.py +++ b/src/sh_edraft/hosting/application_host.py @@ -1,11 +1,12 @@ import sys from datetime import datetime -from sh_edraft.hosting.hosting_environment import HostingEnvironment +from sh_edraft.configuration.configuration import Configuration +from sh_edraft.configuration.base.configuration_base import ConfigurationBase +from sh_edraft.hosting.application_runtime import ApplicationRuntime from sh_edraft.hosting.base.application_host_base import ApplicationHostBase -from sh_edraft.hosting.base.environment_base import EnvironmentBase -from sh_edraft.service.base import ServiceProviderBase from sh_edraft.service.service_provider import ServiceProvider +from sh_edraft.service.base.service_provider_base import ServiceProviderBase class ApplicationHost(ApplicationHostBase): @@ -13,41 +14,25 @@ class ApplicationHost(ApplicationHostBase): def __init__(self, name: str): ApplicationHostBase.__init__(self) self._name: str = name - self._environment = HostingEnvironment() - self._args: list[str] = sys.argv - self._services = ServiceProvider(self) + + self._config = Configuration() + self._app_runtime = ApplicationRuntime(self._config) + self._services = ServiceProvider(self._app_runtime) + self._start_time: datetime = datetime.now() self._end_time: datetime = datetime.now() @property def name(self) -> str: return self._name - - @property - def environment(self) -> EnvironmentBase: - return self._environment @property - def end_time(self) -> datetime: - return self._end_time + def configuration(self) -> ConfigurationBase: + return self._config - @end_time.setter - def end_time(self, end_time: datetime): - self._end_time = end_time - - @property - def start_time(self) -> datetime: - return self._start_time - - @start_time.setter - def start_time(self, start_time: datetime): - self._start_time = start_time - - @property - def date_time_now(self) -> datetime: - return datetime.now() - @property def services(self) -> ServiceProviderBase: return self._services + + def create(self): pass diff --git a/src/sh_edraft/hosting/application_runtime.py b/src/sh_edraft/hosting/application_runtime.py new file mode 100644 index 00000000..e94ca901 --- /dev/null +++ b/src/sh_edraft/hosting/application_runtime.py @@ -0,0 +1,38 @@ +from datetime import datetime + +from sh_edraft.configuration.base import ConfigurationBase +from sh_edraft.hosting.base.application_runtime_base import ApplicationRuntimeBase + + +class ApplicationRuntime(ApplicationRuntimeBase): + + def __init__(self, config: ConfigurationBase): + ApplicationRuntimeBase.__init__(self) + + self._configuration = config + self._start_time: datetime = datetime.now() + self._end_time: datetime = datetime.now() + + @property + def configuration(self) -> ConfigurationBase: + return self._configuration + + @property + def start_time(self) -> datetime: + return self._start_time + + @start_time.setter + def start_time(self, start_time: datetime): + self._start_time = start_time + + @property + def end_time(self) -> datetime: + return self._end_time + + @end_time.setter + def end_time(self, end_time: datetime): + self._end_time = end_time + + @property + def date_time_now(self) -> datetime: + return datetime.now() diff --git a/src/sh_edraft/hosting/base/__init__.py b/src/sh_edraft/hosting/base/__init__.py index cc2f2b01..2bafd17e 100644 --- a/src/sh_edraft/hosting/base/__init__.py +++ b/src/sh_edraft/hosting/base/__init__.py @@ -1,3 +1,4 @@ # imports: from .application_host_base import ApplicationHostBase from .environment_base import EnvironmentBase +from .application_base import ApplicationBase diff --git a/src/sh_edraft/hosting/base/application_base.py b/src/sh_edraft/hosting/base/application_base.py index 96997c6b..00f72997 100644 --- a/src/sh_edraft/hosting/base/application_base.py +++ b/src/sh_edraft/hosting/base/application_base.py @@ -6,6 +6,9 @@ class ApplicationBase(ABC): @abstractmethod def __init__(self): pass + @abstractmethod + def create_application_host(self): pass + @abstractmethod def create_configuration(self): pass diff --git a/src/sh_edraft/hosting/base/application_host_base.py b/src/sh_edraft/hosting/base/application_host_base.py index 3e735665..0ed3b6e4 100644 --- a/src/sh_edraft/hosting/base/application_host_base.py +++ b/src/sh_edraft/hosting/base/application_host_base.py @@ -1,7 +1,7 @@ from abc import ABC, abstractmethod -from datetime import datetime -from sh_edraft.hosting.base.environment_base import EnvironmentBase +from sh_edraft.configuration.base.configuration_base import ConfigurationBase +from sh_edraft.service.base.service_provider_base import ServiceProviderBase class ApplicationHostBase(ABC): @@ -12,27 +12,14 @@ class ApplicationHostBase(ABC): @property @abstractmethod def name(self) -> str: pass - - @property - @abstractmethod - def environment(self) -> EnvironmentBase: pass @property @abstractmethod - def start_time(self) -> datetime: pass - - @start_time.setter - @abstractmethod - def start_time(self, start_time: datetime): pass + def configuration(self) -> ConfigurationBase: pass @property @abstractmethod - def end_time(self): pass + def services(self) -> ServiceProviderBase: pass - @end_time.setter @abstractmethod - def end_time(self, end_time: datetime): pass - - @property - @abstractmethod - def date_time_now(self) -> datetime: pass + def create(self): pass diff --git a/src/sh_edraft/hosting/base/application_runtime_base.py b/src/sh_edraft/hosting/base/application_runtime_base.py new file mode 100644 index 00000000..c7d364d6 --- /dev/null +++ b/src/sh_edraft/hosting/base/application_runtime_base.py @@ -0,0 +1,34 @@ +from abc import ABC, abstractmethod +from datetime import datetime + +from sh_edraft.configuration.base import ConfigurationBase + + +class ApplicationRuntimeBase(ABC): + + @abstractmethod + def __init__(self): pass + + @property + @abstractmethod + def configuration(self) -> ConfigurationBase: pass + + @property + @abstractmethod + def start_time(self) -> datetime: pass + + @start_time.setter + @abstractmethod + def start_time(self, start_time: datetime): pass + + @property + @abstractmethod + def end_time(self): pass + + @end_time.setter + @abstractmethod + def end_time(self, end_time: datetime): pass + + @property + @abstractmethod + def date_time_now(self) -> datetime: pass diff --git a/src/sh_edraft/service/base/service_provider_base.py b/src/sh_edraft/service/base/service_provider_base.py index fe5e0f91..d9dfa000 100644 --- a/src/sh_edraft/service/base/service_provider_base.py +++ b/src/sh_edraft/service/base/service_provider_base.py @@ -11,10 +11,6 @@ class ServiceProviderBase(ServiceBase): def __init__(self): ServiceBase.__init__(self) - @property - @abstractmethod - def config(self): pass - @abstractmethod def add_transient(self, service_type: Type[ServiceBase], service: Type[ServiceBase]): pass diff --git a/src/sh_edraft/service/service_provider.py b/src/sh_edraft/service/service_provider.py index 5c5b0819..36548cfe 100644 --- a/src/sh_edraft/service/service_provider.py +++ b/src/sh_edraft/service/service_provider.py @@ -2,28 +2,22 @@ from collections import Callable from inspect import signature, Parameter from typing import Type -from sh_edraft.configuration.configuration import Configuration -from sh_edraft.hosting.base.application_host_base import ApplicationHostBase from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase +from sh_edraft.hosting.base.application_runtime_base import ApplicationRuntimeBase from sh_edraft.service.base.service_provider_base import ServiceProviderBase from sh_edraft.service.base.service_base import ServiceBase class ServiceProvider(ServiceProviderBase): - def __init__(self, app_host: ApplicationHostBase): - super().__init__() - self._app_host: ApplicationHostBase = app_host - self._config = Configuration() + def __init__(self, app_runtime: ApplicationRuntimeBase): + ServiceProviderBase.__init__(self) + self._app_runtime: ApplicationRuntimeBase = app_runtime self._transient_services: dict[Type[ServiceBase], Type[ServiceBase]] = {} self._scoped_services: dict[Type[ServiceBase], Type[ServiceBase]] = {} self._singleton_services: dict[Type[ServiceBase], ServiceBase] = {} - @property - def config(self): - return self._config - def create(self): pass def _create_instance(self, service: Callable[ServiceBase]) -> ServiceBase: @@ -32,21 +26,16 @@ class ServiceProvider(ServiceProviderBase): for param in sig.parameters.items(): parameter = param[1] if parameter.name != 'self' and parameter.annotation != Parameter.empty: - if issubclass(parameter.annotation, ApplicationHostBase): - params.append(self._app_host) + if issubclass(parameter.annotation, ApplicationRuntimeBase): + params.append(self._app_runtime) elif issubclass(parameter.annotation, ServiceBase): params.append(self.get_service(parameter.annotation)) elif issubclass(parameter.annotation, ConfigurationModelBase): - params.append(self._config.get_config_by_type(parameter.annotation)) + params.append(self._app_runtime.configuration.get_config_by_type(parameter.annotation)) return service(*params) - # try: - # instance.init(args) - # return instance - # except Exception as e: - # print(colored(f'Argument error\n{e}', 'red')) def add_transient(self, service_type: Type[ServiceBase], service: Type[ServiceBase]): self._transient_services[service_type] = service diff --git a/src/tests_dev/main.py b/src/tests_dev/main.py index 02718764..cfb45c60 100644 --- a/src/tests_dev/main.py +++ b/src/tests_dev/main.py @@ -2,6 +2,7 @@ from tests_dev.program import Program if __name__ == '__main__': program = Program() + program.create_application_host() program.create_configuration() program.create_services() program.main() diff --git a/src/tests_dev/program.py b/src/tests_dev/program.py index 59f95aa8..69d2796b 100644 --- a/src/tests_dev/program.py +++ b/src/tests_dev/program.py @@ -1,5 +1,7 @@ +from typing import Optional + from sh_edraft.hosting import ApplicationHost -from sh_edraft.hosting.base.application_base import ApplicationBase +from sh_edraft.hosting.base import ApplicationBase class Program(ApplicationBase): @@ -7,15 +9,16 @@ class Program(ApplicationBase): def __init__(self): ApplicationBase.__init__(self) + self._app_host: Optional[ApplicationHost] = None + + def create_application_host(self): self._app_host = ApplicationHost('CPL_DEV_Test') - self._config = self._app_host.services.config - self._services = self._app_host.services def create_configuration(self): - self._config.create() + self._app_host.configuration.create() def create_services(self): - self._services.create() + self._app_host.services.create() def main(self): print('RUN')