From 3903f853683b3f35425e28ee8dce830f4122c6f2 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 2 Oct 2021 10:57:54 +0200 Subject: [PATCH] Added logic to handle extensions before running the app --- .../application/application_builder.py | 20 +++++++++++++++++-- .../application/application_extension_abc.py | 13 ++++++++++++ .../custom/general/src/general/application.py | 3 ++- src/tests/custom/general/src/general/main.py | 2 ++ .../general/src/general/test_extension.py | 13 ++++++++++++ 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/cpl_core/application/application_extension_abc.py create mode 100644 src/tests/custom/general/src/general/test_extension.py diff --git a/src/cpl_core/application/application_builder.py b/src/cpl_core/application/application_builder.py index ee3f52a6..27ccb762 100644 --- a/src/cpl_core/application/application_builder.py +++ b/src/cpl_core/application/application_builder.py @@ -1,9 +1,12 @@ -from typing import Type, Optional +import types +from typing import Type, Optional, Callable from cpl_core.application.application_abc import ApplicationABC from cpl_core.application.application_builder_abc import ApplicationBuilderABC +from cpl_core.application.application_extension_abc import ApplicationExtensionABC from cpl_core.application.startup_abc import StartupABC from cpl_core.configuration.configuration import Configuration +from cpl_core.console import Console from cpl_core.dependency_injection.service_collection import ServiceCollection @@ -25,12 +28,25 @@ class ApplicationBuilder(ApplicationBuilderABC): self._environment = self._configuration.environment self._services = ServiceCollection(self._configuration) + self._extensions: list[Callable] = [] + def use_startup(self, startup: Type[StartupABC]): self._startup = startup() + def use_extension(self, extension: Type[ApplicationExtensionABC]): + if extension not in self._extensions: + self._extensions.append(extension) + def build(self) -> ApplicationABC: if self._startup is not None: self._startup.configure_configuration(self._configuration, self._environment) self._startup.configure_services(self._services, self._environment) - return self._app(self._configuration, self._services.build_service_provider()) + config = self._configuration + services = self._services.build_service_provider() + + for ex in self._extensions: + extension = ex() + extension.run(config, services) + + return self._app(config, services) diff --git a/src/cpl_core/application/application_extension_abc.py b/src/cpl_core/application/application_extension_abc.py new file mode 100644 index 00000000..0a6dfc20 --- /dev/null +++ b/src/cpl_core/application/application_extension_abc.py @@ -0,0 +1,13 @@ +from abc import ABC, abstractmethod + +from cpl_core.configuration import ConfigurationABC +from cpl_core.dependency_injection import ServiceProviderABC + + +class ApplicationExtensionABC(ABC): + + @abstractmethod + def __init__(self): pass + + @abstractmethod + def run(self, config: ConfigurationABC, services: ServiceProviderABC): pass diff --git a/src/tests/custom/general/src/general/application.py b/src/tests/custom/general/src/general/application.py index a42536af..9e2d4ef0 100644 --- a/src/tests/custom/general/src/general/application.py +++ b/src/tests/custom/general/src/general/application.py @@ -37,7 +37,8 @@ class Application(ApplicationABC): self._mailer = self._services.get_service(EMailClientABC) def main(self): - self._logger.header(f'{self._configuration.environment.application_name}:') + if self._configuration.environment.application_name != '': + self._logger.header(f'{self._configuration.environment.application_name}:') self._logger.debug(__name__, f'Host: {self._configuration.environment.host_name}') self._logger.debug(__name__, f'Environment: {self._configuration.environment.environment_name}') self._logger.debug(__name__, f'Customer: {self._configuration.environment.customer}') diff --git a/src/tests/custom/general/src/general/main.py b/src/tests/custom/general/src/general/main.py index f6515f12..339c0723 100644 --- a/src/tests/custom/general/src/general/main.py +++ b/src/tests/custom/general/src/general/main.py @@ -1,11 +1,13 @@ from cpl_core.application.application_builder import ApplicationBuilder from application import Application +from general.test_extension import TestExtension from startup import Startup def main(): app_builder = ApplicationBuilder(Application) app_builder.use_startup(Startup) + app_builder.use_extension(TestExtension) app_builder.build().run() diff --git a/src/tests/custom/general/src/general/test_extension.py b/src/tests/custom/general/src/general/test_extension.py new file mode 100644 index 00000000..b314c665 --- /dev/null +++ b/src/tests/custom/general/src/general/test_extension.py @@ -0,0 +1,13 @@ +from cpl_core.application.application_extension_abc import ApplicationExtensionABC +from cpl_core.configuration import ConfigurationABC +from cpl_core.console import Console +from cpl_core.dependency_injection import ServiceProviderABC + + +class TestExtension(ApplicationExtensionABC): + + def __init__(self): + ApplicationExtensionABC.__init__(self) + + def run(self, config: ConfigurationABC, services: ServiceProviderABC): + Console.write_line('Hello World from App Extension')