From 97e52b39c032b32bdc6f60bf6e3a56c8c1e351d8 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 30 Apr 2022 17:43:34 +0200 Subject: [PATCH 1/2] Added logic to handle StartupExtension classes --- src/cpl_cli/cpl_cli.json | 2 +- src/cpl_core/application/__init__.py | 1 + .../application/application_builder.py | 32 +++++++++++------ src/cpl_core/application/startup_abc.py | 3 +- .../application/startup_extension_abc.py | 34 +++++++++++++++++++ src/cpl_core/cpl_core.json | 2 +- ...son => appsettings.edrafts-pc-ubuntu.json} | 12 ++++--- .../custom/general/src/general/general.json | 4 +-- src/tests/custom/general/src/general/main.py | 4 ++- .../src/general/test_startup_extension.py | 17 ++++++++++ 10 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 src/cpl_core/application/startup_extension_abc.py rename src/tests/custom/general/src/general/{appsettings.edrafts-pc.json => appsettings.edrafts-pc-ubuntu.json} (69%) create mode 100644 src/tests/custom/general/src/general/test_startup_extension.py diff --git a/src/cpl_cli/cpl_cli.json b/src/cpl_cli/cpl_cli.json index 9694bc91..c543514f 100644 --- a/src/cpl_cli/cpl_cli.json +++ b/src/cpl_cli/cpl_cli.json @@ -16,7 +16,7 @@ "LicenseName": "MIT", "LicenseDescription": "MIT, see LICENSE for more details.", "Dependencies": [ - "cpl-core>=2022.6.1" + "cpl-core>=2022.6.2" ], "PythonVersion": ">=3.10", "PythonPath": {}, diff --git a/src/cpl_core/application/__init__.py b/src/cpl_core/application/__init__.py index cab819c0..1ecef6be 100644 --- a/src/cpl_core/application/__init__.py +++ b/src/cpl_core/application/__init__.py @@ -24,6 +24,7 @@ from .application_abc import ApplicationABC from .application_builder import ApplicationBuilder from .application_builder_abc import ApplicationBuilderABC from .startup_abc import StartupABC +from .startup_extension_abc import StartupExtensionABC VersionInfo = namedtuple('VersionInfo', 'major minor micro') version_info = VersionInfo(major='2022', minor='6', micro='1') diff --git a/src/cpl_core/application/application_builder.py b/src/cpl_core/application/application_builder.py index c434c6ec..379e1216 100644 --- a/src/cpl_core/application/application_builder.py +++ b/src/cpl_core/application/application_builder.py @@ -1,9 +1,10 @@ -from typing import Type, Optional, Callable +from typing import Type, Optional, Callable, Union 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.application.startup_extension_abc import StartupExtensionABC from cpl_core.configuration.configuration import Configuration from cpl_core.dependency_injection.service_collection import ServiceCollection @@ -26,38 +27,47 @@ class ApplicationBuilder(ApplicationBuilderABC): self._environment = self._configuration.environment self._services = ServiceCollection(self._configuration) - self._extensions: list[Callable] = [] + self._app_extensions: list[Callable] = [] + self._startup_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 use_extension(self, extension: Type[Union[ApplicationExtensionABC, StartupExtensionABC]]): + if issubclass(extension, ApplicationExtensionABC) and extension not in self._app_extensions: + self._app_extensions.append(extension) + elif issubclass(extension, StartupExtensionABC) and extension not in self._startup_extensions: + self._startup_extensions.append(extension) + + def _build_startup(self): + for ex in self._startup_extensions: + extension = ex() + extension.configure_configuration(self._configuration, self._environment) + extension.configure_services(self._services, self._environment) - 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) + def build(self) -> ApplicationABC: + self._build_startup() + config = self._configuration services = self._services.build_service_provider() - for ex in self._extensions: + for ex in self._app_extensions: extension = ex() extension.run(config, services) return self._app(config, services) async def build_async(self) -> ApplicationABC: - if self._startup is not None: - await self._startup.configure_configuration(self._configuration, self._environment) - await self._startup.configure_services(self._services, self._environment) + self._build_startup() config = self._configuration services = self._services.build_service_provider() - for ex in self._extensions: + for ex in self._app_extensions: extension = ex() await extension.run(config, services) diff --git a/src/cpl_core/application/startup_abc.py b/src/cpl_core/application/startup_abc.py index 1d1af6d6..1c25dbf7 100644 --- a/src/cpl_core/application/startup_abc.py +++ b/src/cpl_core/application/startup_abc.py @@ -10,8 +10,7 @@ class StartupABC(ABC): r"""ABC for the startup class""" @abstractmethod - def __init__(self, *args): - pass + def __init__(self): pass @abstractmethod def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC) -> ConfigurationABC: diff --git a/src/cpl_core/application/startup_extension_abc.py b/src/cpl_core/application/startup_extension_abc.py new file mode 100644 index 00000000..5ca19cde --- /dev/null +++ b/src/cpl_core/application/startup_extension_abc.py @@ -0,0 +1,34 @@ +from abc import ABC, abstractmethod + +from cpl_core.configuration.configuration_abc import ConfigurationABC +from cpl_core.dependency_injection.service_collection_abc import ServiceCollectionABC +from cpl_core.environment.application_environment_abc import ApplicationEnvironmentABC + + +class StartupExtensionABC(ABC): + r"""ABC for startup extension classes""" + + @abstractmethod + def __init__(self): pass + + @abstractmethod + def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): + r"""Creates configuration of application + + Parameter + --------- + config: :class:`cpl_core.configuration.configuration_abc.ConfigurationABC` + env: :class:`cpl_core.environment.application_environment_abc` + """ + pass + + @abstractmethod + def configure_services(self, service: ServiceCollectionABC, env: ApplicationEnvironmentABC): + r"""Creates service provider + + Parameter + --------- + services: :class:`cpl_core.dependency_injection.service_collection_abc` + env: :class:`cpl_core.environment.application_environment_abc` + """ + pass diff --git a/src/cpl_core/cpl_core.json b/src/cpl_core/cpl_core.json index aaff74a4..e171d838 100644 --- a/src/cpl_core/cpl_core.json +++ b/src/cpl_core/cpl_core.json @@ -4,7 +4,7 @@ "Version": { "Major": "2022", "Minor": "6", - "Micro": "1" + "Micro": "2" }, "Author": "Sven Heidemann", "AuthorEmail": "sven.heidemann@sh-edraft.de", diff --git a/src/tests/custom/general/src/general/appsettings.edrafts-pc.json b/src/tests/custom/general/src/general/appsettings.edrafts-pc-ubuntu.json similarity index 69% rename from src/tests/custom/general/src/general/appsettings.edrafts-pc.json rename to src/tests/custom/general/src/general/appsettings.edrafts-pc-ubuntu.json index afae3437..5d40a040 100644 --- a/src/tests/custom/general/src/general/appsettings.edrafts-pc.json +++ b/src/tests/custom/general/src/general/appsettings.edrafts-pc-ubuntu.json @@ -21,9 +21,13 @@ }, "DatabaseSettings": { - "AuthPlugin": "mysql_native_password", - "ConnectionString": "mysql+mysqlconnector://sh_cpl:$credentials@localhost/sh_cpl", - "Credentials": "MHZhc0Y2bjhKc1VUMWV0Qw==", - "Encoding": "utf8mb4" + "Host": "localhost", + "User": "sh_cpl", + "Password": "MHZhc0Y2bjhKc1VUMWV0Qw==", + "Database": "sh_cpl", + "Charset": "utf8mb4", + "UseUnicode": "true", + "Buffered": "true", + "AuthPlugin": "mysql_native_password" } } \ No newline at end of file diff --git a/src/tests/custom/general/src/general/general.json b/src/tests/custom/general/src/general/general.json index 6bed03dd..448345cd 100644 --- a/src/tests/custom/general/src/general/general.json +++ b/src/tests/custom/general/src/general/general.json @@ -16,9 +16,9 @@ "LicenseName": "MIT", "LicenseDescription": "MIT, see LICENSE for more details.", "Dependencies": [ - "sh_cpl==2021.4.0.post2" + "cpl_core==2022.6.1" ], - "PythonVersion": ">=3.8", + "PythonVersion": ">=3.10", "PythonPath": { "linux": "../../../../../../cpl-env/bin/python3.9", "win32": "" diff --git a/src/tests/custom/general/src/general/main.py b/src/tests/custom/general/src/general/main.py index 2835d115..67191b28 100644 --- a/src/tests/custom/general/src/general/main.py +++ b/src/tests/custom/general/src/general/main.py @@ -1,12 +1,14 @@ from application import Application from cpl_core.application import ApplicationBuilder -from general.test_extension import TestExtension +from test_extension import TestExtension from startup import Startup +from test_startup_extension import TestStartupExtension def main(): app_builder = ApplicationBuilder(Application) app_builder.use_startup(Startup) + app_builder.use_extension(TestStartupExtension) app_builder.use_extension(TestExtension) app_builder.build().run() diff --git a/src/tests/custom/general/src/general/test_startup_extension.py b/src/tests/custom/general/src/general/test_startup_extension.py new file mode 100644 index 00000000..87b179ea --- /dev/null +++ b/src/tests/custom/general/src/general/test_startup_extension.py @@ -0,0 +1,17 @@ +from cpl_core.application import StartupExtensionABC +from cpl_core.configuration import ConfigurationABC +from cpl_core.console import Console +from cpl_core.dependency_injection import ServiceCollectionABC +from cpl_core.environment import ApplicationEnvironmentABC + + +class TestStartupExtension(StartupExtensionABC): + + def __init__(self): + StartupExtensionABC.__init__(self) + + def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): + Console.write_line('config') + + def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): + Console.write_line('services') -- 2.45.2 From dcd85831836717a30c7226bd3204e774bd8dd928 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 30 Apr 2022 17:44:15 +0200 Subject: [PATCH 2/2] Build packages --- scripts/build.sh | 2 +- src/cpl_core/__init__.py | 4 ++-- src/cpl_core/application/__init__.py | 4 ++-- src/cpl_core/configuration/__init__.py | 4 ++-- src/cpl_core/console/__init__.py | 4 ++-- src/cpl_core/database/__init__.py | 4 ++-- src/cpl_core/database/connection/__init__.py | 4 ++-- src/cpl_core/database/context/__init__.py | 4 ++-- src/cpl_core/dependency_injection/__init__.py | 4 ++-- src/cpl_core/environment/__init__.py | 4 ++-- src/cpl_core/logging/__init__.py | 4 ++-- src/cpl_core/mailing/__init__.py | 4 ++-- src/cpl_core/time/__init__.py | 4 ++-- src/cpl_core/utils/__init__.py | 4 ++-- 14 files changed, 27 insertions(+), 27 deletions(-) diff --git a/scripts/build.sh b/scripts/build.sh index 5599dad4..96d00ded 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,6 +1,6 @@ #!/bin/bash # activate venv -source /home/sven/Nextcloud_Sven/Schreibtisch/git_sh-edraft_de/sh_cpl/cpl-env/bin/activate +source /home/sven/Nextcloud_Sven/Schreibtisch/git_sh-edraft_de/sh_cpl/venv/bin/activate # CPL cd /home/sven/Nextcloud_Sven/Schreibtisch/git_sh-edraft_de/sh_cpl/src/cpl_core diff --git a/src/cpl_core/__init__.py b/src/cpl_core/__init__.py index 744a4252..e741b291 100644 --- a/src/cpl_core/__init__.py +++ b/src/cpl_core/__init__.py @@ -15,11 +15,11 @@ __title__ = 'cpl_core' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de' -__version__ = '2022.6.1' +__version__ = '2022.6.2' from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='6', micro='1') +version_info = VersionInfo(major='2022', minor='6', micro='2') diff --git a/src/cpl_core/application/__init__.py b/src/cpl_core/application/__init__.py index 1ecef6be..155bb71f 100644 --- a/src/cpl_core/application/__init__.py +++ b/src/cpl_core/application/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_core.application' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de' -__version__ = '2022.6.1' +__version__ = '2022.6.2' from collections import namedtuple @@ -27,4 +27,4 @@ from .startup_abc import StartupABC from .startup_extension_abc import StartupExtensionABC VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='6', micro='1') +version_info = VersionInfo(major='2022', minor='6', micro='2') diff --git a/src/cpl_core/configuration/__init__.py b/src/cpl_core/configuration/__init__.py index 0af264af..de358f22 100644 --- a/src/cpl_core/configuration/__init__.py +++ b/src/cpl_core/configuration/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_core.configuration' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de' -__version__ = '2022.6.1' +__version__ = '2022.6.2' from collections import namedtuple @@ -27,4 +27,4 @@ from .configuration_variable_name_enum import ConfigurationVariableNameEnum from .console_argument import ConsoleArgument VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='6', micro='1') +version_info = VersionInfo(major='2022', minor='6', micro='2') diff --git a/src/cpl_core/console/__init__.py b/src/cpl_core/console/__init__.py index f5ca0e3c..928c0904 100644 --- a/src/cpl_core/console/__init__.py +++ b/src/cpl_core/console/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_core.console' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de' -__version__ = '2022.6.1' +__version__ = '2022.6.2' from collections import namedtuple @@ -27,4 +27,4 @@ from .foreground_color_enum import ForegroundColorEnum from .spinner_thread import SpinnerThread VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='6', micro='1') +version_info = VersionInfo(major='2022', minor='6', micro='2') diff --git a/src/cpl_core/database/__init__.py b/src/cpl_core/database/__init__.py index e09cd083..55bc3f3c 100644 --- a/src/cpl_core/database/__init__.py +++ b/src/cpl_core/database/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_core.database' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de' -__version__ = '2022.6.1' +__version__ = '2022.6.2' from collections import namedtuple @@ -25,4 +25,4 @@ from .database_settings import DatabaseSettings from .table_abc import TableABC VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='6', micro='1') +version_info = VersionInfo(major='2022', minor='6', micro='2') diff --git a/src/cpl_core/database/connection/__init__.py b/src/cpl_core/database/connection/__init__.py index 7bef8077..accbfaa6 100644 --- a/src/cpl_core/database/connection/__init__.py +++ b/src/cpl_core/database/connection/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_core.database.connection' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de' -__version__ = '2022.6.1' +__version__ = '2022.6.2' from collections import namedtuple @@ -24,4 +24,4 @@ from .database_connection import DatabaseConnection from .database_connection_abc import DatabaseConnectionABC VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='6', micro='1') +version_info = VersionInfo(major='2022', minor='6', micro='2') diff --git a/src/cpl_core/database/context/__init__.py b/src/cpl_core/database/context/__init__.py index eab72301..e1684eea 100644 --- a/src/cpl_core/database/context/__init__.py +++ b/src/cpl_core/database/context/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_core.database.context' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de' -__version__ = '2022.6.1' +__version__ = '2022.6.2' from collections import namedtuple @@ -24,4 +24,4 @@ from .database_context import DatabaseContext from .database_context_abc import DatabaseContextABC VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='6', micro='1') +version_info = VersionInfo(major='2022', minor='6', micro='2') diff --git a/src/cpl_core/dependency_injection/__init__.py b/src/cpl_core/dependency_injection/__init__.py index 3a9c61f0..044ec621 100644 --- a/src/cpl_core/dependency_injection/__init__.py +++ b/src/cpl_core/dependency_injection/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_core.dependency_injection' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de' -__version__ = '2022.6.1' +__version__ = '2022.6.2' from collections import namedtuple @@ -28,4 +28,4 @@ from .service_provider import ServiceProvider from .service_provider_abc import ServiceProviderABC VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='6', micro='1') +version_info = VersionInfo(major='2022', minor='6', micro='2') diff --git a/src/cpl_core/environment/__init__.py b/src/cpl_core/environment/__init__.py index 737a7427..3c96a3bf 100644 --- a/src/cpl_core/environment/__init__.py +++ b/src/cpl_core/environment/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_core.environment' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de' -__version__ = '2022.6.1' +__version__ = '2022.6.2' from collections import namedtuple @@ -25,4 +25,4 @@ from .environment_name_enum import EnvironmentNameEnum from .application_environment import ApplicationEnvironment VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='6', micro='1') +version_info = VersionInfo(major='2022', minor='6', micro='2') diff --git a/src/cpl_core/logging/__init__.py b/src/cpl_core/logging/__init__.py index a83095ee..3e341963 100644 --- a/src/cpl_core/logging/__init__.py +++ b/src/cpl_core/logging/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_core.logging' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de' -__version__ = '2022.6.1' +__version__ = '2022.6.2' from collections import namedtuple @@ -27,4 +27,4 @@ from .logging_settings import LoggingSettings from .logging_settings_name_enum import LoggingSettingsNameEnum VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='6', micro='1') +version_info = VersionInfo(major='2022', minor='6', micro='2') diff --git a/src/cpl_core/mailing/__init__.py b/src/cpl_core/mailing/__init__.py index 63847174..0eeefc95 100644 --- a/src/cpl_core/mailing/__init__.py +++ b/src/cpl_core/mailing/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_core.mailing' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de' -__version__ = '2022.6.1' +__version__ = '2022.6.2' from collections import namedtuple @@ -27,4 +27,4 @@ from .email_client_settings import EMailClientSettings from .email_client_settings_name_enum import EMailClientSettingsNameEnum VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='6', micro='1') +version_info = VersionInfo(major='2022', minor='6', micro='2') diff --git a/src/cpl_core/time/__init__.py b/src/cpl_core/time/__init__.py index 793097bf..4dea9c68 100644 --- a/src/cpl_core/time/__init__.py +++ b/src/cpl_core/time/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_core.time' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de' -__version__ = '2022.6.1' +__version__ = '2022.6.2' from collections import namedtuple @@ -24,4 +24,4 @@ from .time_format_settings import TimeFormatSettings from .time_format_settings_names_enum import TimeFormatSettingsNamesEnum VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='6', micro='1') +version_info = VersionInfo(major='2022', minor='6', micro='2') diff --git a/src/cpl_core/utils/__init__.py b/src/cpl_core/utils/__init__.py index 9ca3cc47..5d9e5653 100644 --- a/src/cpl_core/utils/__init__.py +++ b/src/cpl_core/utils/__init__.py @@ -15,7 +15,7 @@ __title__ = 'cpl_core.utils' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de' -__version__ = '2022.6.1' +__version__ = '2022.6.2' from collections import namedtuple @@ -25,4 +25,4 @@ from .string import String from .pip import Pip VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2022', minor='6', micro='1') +version_info = VersionInfo(major='2022', minor='6', micro='2') -- 2.45.2