Added logic to handle StartupExtension classes
This commit is contained in:
parent
581013b61a
commit
97e52b39c0
@ -16,7 +16,7 @@
|
|||||||
"LicenseName": "MIT",
|
"LicenseName": "MIT",
|
||||||
"LicenseDescription": "MIT, see LICENSE for more details.",
|
"LicenseDescription": "MIT, see LICENSE for more details.",
|
||||||
"Dependencies": [
|
"Dependencies": [
|
||||||
"cpl-core>=2022.6.1"
|
"cpl-core>=2022.6.2"
|
||||||
],
|
],
|
||||||
"PythonVersion": ">=3.10",
|
"PythonVersion": ">=3.10",
|
||||||
"PythonPath": {},
|
"PythonPath": {},
|
||||||
|
@ -24,6 +24,7 @@ from .application_abc import ApplicationABC
|
|||||||
from .application_builder import ApplicationBuilder
|
from .application_builder import ApplicationBuilder
|
||||||
from .application_builder_abc import ApplicationBuilderABC
|
from .application_builder_abc import ApplicationBuilderABC
|
||||||
from .startup_abc import StartupABC
|
from .startup_abc import StartupABC
|
||||||
|
from .startup_extension_abc import StartupExtensionABC
|
||||||
|
|
||||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||||
version_info = VersionInfo(major='2022', minor='6', micro='1')
|
version_info = VersionInfo(major='2022', minor='6', micro='1')
|
||||||
|
@ -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_abc import ApplicationABC
|
||||||
from cpl_core.application.application_builder_abc import ApplicationBuilderABC
|
from cpl_core.application.application_builder_abc import ApplicationBuilderABC
|
||||||
from cpl_core.application.application_extension_abc import ApplicationExtensionABC
|
from cpl_core.application.application_extension_abc import ApplicationExtensionABC
|
||||||
from cpl_core.application.startup_abc import StartupABC
|
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.configuration.configuration import Configuration
|
||||||
from cpl_core.dependency_injection.service_collection import ServiceCollection
|
from cpl_core.dependency_injection.service_collection import ServiceCollection
|
||||||
|
|
||||||
@ -26,38 +27,47 @@ class ApplicationBuilder(ApplicationBuilderABC):
|
|||||||
self._environment = self._configuration.environment
|
self._environment = self._configuration.environment
|
||||||
self._services = ServiceCollection(self._configuration)
|
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]):
|
def use_startup(self, startup: Type[StartupABC]):
|
||||||
self._startup = startup()
|
self._startup = startup()
|
||||||
|
|
||||||
def use_extension(self, extension: Type[ApplicationExtensionABC]):
|
def use_extension(self, extension: Type[Union[ApplicationExtensionABC, StartupExtensionABC]]):
|
||||||
if extension not in self._extensions:
|
if issubclass(extension, ApplicationExtensionABC) and extension not in self._app_extensions:
|
||||||
self._extensions.append(extension)
|
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:
|
if self._startup is not None:
|
||||||
self._startup.configure_configuration(self._configuration, self._environment)
|
self._startup.configure_configuration(self._configuration, self._environment)
|
||||||
self._startup.configure_services(self._services, self._environment)
|
self._startup.configure_services(self._services, self._environment)
|
||||||
|
|
||||||
|
def build(self) -> ApplicationABC:
|
||||||
|
self._build_startup()
|
||||||
|
|
||||||
config = self._configuration
|
config = self._configuration
|
||||||
services = self._services.build_service_provider()
|
services = self._services.build_service_provider()
|
||||||
|
|
||||||
for ex in self._extensions:
|
for ex in self._app_extensions:
|
||||||
extension = ex()
|
extension = ex()
|
||||||
extension.run(config, services)
|
extension.run(config, services)
|
||||||
|
|
||||||
return self._app(config, services)
|
return self._app(config, services)
|
||||||
|
|
||||||
async def build_async(self) -> ApplicationABC:
|
async def build_async(self) -> ApplicationABC:
|
||||||
if self._startup is not None:
|
self._build_startup()
|
||||||
await self._startup.configure_configuration(self._configuration, self._environment)
|
|
||||||
await self._startup.configure_services(self._services, self._environment)
|
|
||||||
|
|
||||||
config = self._configuration
|
config = self._configuration
|
||||||
services = self._services.build_service_provider()
|
services = self._services.build_service_provider()
|
||||||
|
|
||||||
for ex in self._extensions:
|
for ex in self._app_extensions:
|
||||||
extension = ex()
|
extension = ex()
|
||||||
await extension.run(config, services)
|
await extension.run(config, services)
|
||||||
|
|
||||||
|
@ -10,8 +10,7 @@ class StartupABC(ABC):
|
|||||||
r"""ABC for the startup class"""
|
r"""ABC for the startup class"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def __init__(self, *args):
|
def __init__(self): pass
|
||||||
pass
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC) -> ConfigurationABC:
|
def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC) -> ConfigurationABC:
|
||||||
|
34
src/cpl_core/application/startup_extension_abc.py
Normal file
34
src/cpl_core/application/startup_extension_abc.py
Normal file
@ -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
|
@ -4,7 +4,7 @@
|
|||||||
"Version": {
|
"Version": {
|
||||||
"Major": "2022",
|
"Major": "2022",
|
||||||
"Minor": "6",
|
"Minor": "6",
|
||||||
"Micro": "1"
|
"Micro": "2"
|
||||||
},
|
},
|
||||||
"Author": "Sven Heidemann",
|
"Author": "Sven Heidemann",
|
||||||
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
"AuthorEmail": "sven.heidemann@sh-edraft.de",
|
||||||
|
@ -21,9 +21,13 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
"DatabaseSettings": {
|
"DatabaseSettings": {
|
||||||
"AuthPlugin": "mysql_native_password",
|
"Host": "localhost",
|
||||||
"ConnectionString": "mysql+mysqlconnector://sh_cpl:$credentials@localhost/sh_cpl",
|
"User": "sh_cpl",
|
||||||
"Credentials": "MHZhc0Y2bjhKc1VUMWV0Qw==",
|
"Password": "MHZhc0Y2bjhKc1VUMWV0Qw==",
|
||||||
"Encoding": "utf8mb4"
|
"Database": "sh_cpl",
|
||||||
|
"Charset": "utf8mb4",
|
||||||
|
"UseUnicode": "true",
|
||||||
|
"Buffered": "true",
|
||||||
|
"AuthPlugin": "mysql_native_password"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,9 +16,9 @@
|
|||||||
"LicenseName": "MIT",
|
"LicenseName": "MIT",
|
||||||
"LicenseDescription": "MIT, see LICENSE for more details.",
|
"LicenseDescription": "MIT, see LICENSE for more details.",
|
||||||
"Dependencies": [
|
"Dependencies": [
|
||||||
"sh_cpl==2021.4.0.post2"
|
"cpl_core==2022.6.1"
|
||||||
],
|
],
|
||||||
"PythonVersion": ">=3.8",
|
"PythonVersion": ">=3.10",
|
||||||
"PythonPath": {
|
"PythonPath": {
|
||||||
"linux": "../../../../../../cpl-env/bin/python3.9",
|
"linux": "../../../../../../cpl-env/bin/python3.9",
|
||||||
"win32": ""
|
"win32": ""
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
from application import Application
|
from application import Application
|
||||||
from cpl_core.application import ApplicationBuilder
|
from cpl_core.application import ApplicationBuilder
|
||||||
from general.test_extension import TestExtension
|
from test_extension import TestExtension
|
||||||
from startup import Startup
|
from startup import Startup
|
||||||
|
from test_startup_extension import TestStartupExtension
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
app_builder = ApplicationBuilder(Application)
|
app_builder = ApplicationBuilder(Application)
|
||||||
app_builder.use_startup(Startup)
|
app_builder.use_startup(Startup)
|
||||||
|
app_builder.use_extension(TestStartupExtension)
|
||||||
app_builder.use_extension(TestExtension)
|
app_builder.use_extension(TestExtension)
|
||||||
app_builder.build().run()
|
app_builder.build().run()
|
||||||
|
|
||||||
|
@ -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')
|
Loading…
Reference in New Issue
Block a user