From ead0eae5f0085884ce5d7b9dc3e34406e4860c50 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 26 Nov 2020 11:20:21 +0100 Subject: [PATCH] Improved hosting --- .../configuration/base/configuration_base.py | 1 - src/sh_edraft/configuration/configuration.py | 2 ++ src/sh_edraft/hosting/__init__.py | 1 + src/sh_edraft/hosting/application_host.py | 18 ++++++++++--- src/sh_edraft/hosting/base/__init__.py | 1 + .../hosting/base/application_host_base.py | 16 ++++++++++++ .../hosting/base/environment_base.py | 25 +++++++++++++++++++ src/sh_edraft/hosting/hosting_environment.py | 20 +++++++++++++++ src/sh_edraft/hosting/model/__init__.py | 2 ++ .../hosting/model/environment_name.py | 9 +++++++ src/sh_edraft/logging/base/logger_base.py | 8 +----- src/sh_edraft/logging/logger.py | 11 ++++---- .../publishing/base/publisher_base.py | 7 +----- src/sh_edraft/publishing/model/__init__.py | 2 ++ src/sh_edraft/publishing/publisher.py | 5 +++- src/sh_edraft/service/service_provider.py | 10 +++++--- src/tests/logging/logger.py | 6 ++--- src/tests/publishing/publisher.py | 9 +++---- .../service_providing/service_provider.py | 8 +++--- 19 files changed, 124 insertions(+), 37 deletions(-) create mode 100644 src/sh_edraft/hosting/base/environment_base.py create mode 100644 src/sh_edraft/hosting/hosting_environment.py create mode 100644 src/sh_edraft/hosting/model/environment_name.py diff --git a/src/sh_edraft/configuration/base/configuration_base.py b/src/sh_edraft/configuration/base/configuration_base.py index 4db03091..03d74dd6 100644 --- a/src/sh_edraft/configuration/base/configuration_base.py +++ b/src/sh_edraft/configuration/base/configuration_base.py @@ -10,7 +10,6 @@ class ConfigurationBase(ServiceBase): @abstractmethod def __init__(self): ServiceBase.__init__(self) - self._config: dict[type, object] = {} @property @abstractmethod diff --git a/src/sh_edraft/configuration/configuration.py b/src/sh_edraft/configuration/configuration.py index 67140807..b3743aff 100644 --- a/src/sh_edraft/configuration/configuration.py +++ b/src/sh_edraft/configuration/configuration.py @@ -9,6 +9,8 @@ class Configuration(ConfigurationBase): def __init__(self): super().__init__() + self._config: dict[type, object] = {} + @property def config(self): return self._config diff --git a/src/sh_edraft/hosting/__init__.py b/src/sh_edraft/hosting/__init__.py index ceeb2b0e..2a8b8615 100644 --- a/src/sh_edraft/hosting/__init__.py +++ b/src/sh_edraft/hosting/__init__.py @@ -1,3 +1,4 @@ # imports: from .application_host import ApplicationHost +from .hosting_environment import HostingEnvironment diff --git a/src/sh_edraft/hosting/application_host.py b/src/sh_edraft/hosting/application_host.py index f9b8fbf9..0316a2f2 100644 --- a/src/sh_edraft/hosting/application_host.py +++ b/src/sh_edraft/hosting/application_host.py @@ -1,17 +1,29 @@ from datetime import datetime from sh_edraft.hosting.base.application_host_base import ApplicationHostBase +from sh_edraft.hosting.base.environment_base import EnvironmentBase from sh_edraft.service.service_provider import ServiceProvider class ApplicationHost(ApplicationHostBase): - - def __init__(self): + + def __init__(self, name: str, env: EnvironmentBase): ApplicationHostBase.__init__(self) - self._services = ServiceProvider() + self._name: str = name + self._environment: EnvironmentBase = env + + self._services = ServiceProvider(self) 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 services(self): return self._services diff --git a/src/sh_edraft/hosting/base/__init__.py b/src/sh_edraft/hosting/base/__init__.py index 96c21b77..cc2f2b01 100644 --- a/src/sh_edraft/hosting/base/__init__.py +++ b/src/sh_edraft/hosting/base/__init__.py @@ -1,2 +1,3 @@ # imports: from .application_host_base import ApplicationHostBase +from .environment_base import EnvironmentBase diff --git a/src/sh_edraft/hosting/base/application_host_base.py b/src/sh_edraft/hosting/base/application_host_base.py index 20dbd9a5..3e735665 100644 --- a/src/sh_edraft/hosting/base/application_host_base.py +++ b/src/sh_edraft/hosting/base/application_host_base.py @@ -1,17 +1,28 @@ from abc import ABC, abstractmethod from datetime import datetime +from sh_edraft.hosting.base.environment_base import EnvironmentBase + class ApplicationHostBase(ABC): @abstractmethod def __init__(self): pass + + @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 @property @@ -19,4 +30,9 @@ class ApplicationHostBase(ABC): 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/hosting/base/environment_base.py b/src/sh_edraft/hosting/base/environment_base.py new file mode 100644 index 00000000..99cfaa18 --- /dev/null +++ b/src/sh_edraft/hosting/base/environment_base.py @@ -0,0 +1,25 @@ +from abc import ABC, abstractmethod + +from sh_edraft.hosting.model.environment_name import EnvironmentName + + +class EnvironmentBase(ABC): + + @abstractmethod + def __init__(self): pass + + @property + @abstractmethod + def name(self) -> EnvironmentName: pass + + @name.setter + @abstractmethod + def name(self, name: EnvironmentName): pass + + @property + @abstractmethod + def content_root_path(self) -> str: pass + + @content_root_path.setter + @abstractmethod + def content_root_path(self, content_root_path: str): pass diff --git a/src/sh_edraft/hosting/hosting_environment.py b/src/sh_edraft/hosting/hosting_environment.py new file mode 100644 index 00000000..c771cf81 --- /dev/null +++ b/src/sh_edraft/hosting/hosting_environment.py @@ -0,0 +1,20 @@ +from sh_edraft.hosting.base.environment_base import EnvironmentBase +from sh_edraft.hosting.model.environment_name import EnvironmentName + + +class HostingEnvironment(EnvironmentBase): + + def __init__(self, name: EnvironmentName, crp: str): + EnvironmentBase.__init__(self) + + self._name = name + self._content_root_path = crp + + @property + def name(self) -> EnvironmentName: + return self._name + + @property + def content_root_path(self) -> str: + return self._content_root_path + diff --git a/src/sh_edraft/hosting/model/__init__.py b/src/sh_edraft/hosting/model/__init__.py index e69de29b..b8692da2 100644 --- a/src/sh_edraft/hosting/model/__init__.py +++ b/src/sh_edraft/hosting/model/__init__.py @@ -0,0 +1,2 @@ +# imports: +from .environment_name import EnvironmentName \ No newline at end of file diff --git a/src/sh_edraft/hosting/model/environment_name.py b/src/sh_edraft/hosting/model/environment_name.py new file mode 100644 index 00000000..18abcbb5 --- /dev/null +++ b/src/sh_edraft/hosting/model/environment_name.py @@ -0,0 +1,9 @@ +from enum import Enum + + +class EnvironmentName(Enum): + + production = 'production' + staging = 'staging' + testing = 'testing' + development = 'development' diff --git a/src/sh_edraft/logging/base/logger_base.py b/src/sh_edraft/logging/base/logger_base.py index 47949898..2dc5de50 100644 --- a/src/sh_edraft/logging/base/logger_base.py +++ b/src/sh_edraft/logging/base/logger_base.py @@ -1,20 +1,14 @@ from abc import abstractmethod -from sh_edraft.hosting.application_host import ApplicationHost -from sh_edraft.logging.model.logging_settings import LoggingSettings from sh_edraft.service.base.service_base import ServiceBase -from sh_edraft.time.model.time_format_settings import TimeFormatSettings class LoggerBase(ServiceBase): @abstractmethod - def __init__(self, logging_settings: LoggingSettings, time_format: TimeFormatSettings): + def __init__(self): ServiceBase.__init__(self) - self._log_settings: LoggingSettings = logging_settings - self._time_format_settings: TimeFormatSettings = time_format - @abstractmethod def header(self, string: str): pass diff --git a/src/sh_edraft/logging/logger.py b/src/sh_edraft/logging/logger.py index f7a0a361..6305725a 100644 --- a/src/sh_edraft/logging/logger.py +++ b/src/sh_edraft/logging/logger.py @@ -1,10 +1,9 @@ import datetime import os import traceback -from collections import Callable from string import Template -from sh_edraft.hosting.application_host import ApplicationHost +from sh_edraft.hosting.base import ApplicationHostBase from sh_edraft.logging.base.logger_base import LoggerBase from sh_edraft.logging.model import LoggingSettings from sh_edraft.logging.model.logging_level import LoggingLevel @@ -14,10 +13,12 @@ from sh_edraft.utils.console import Console class Logger(LoggerBase): - def __init__(self, logging_settings: LoggingSettings, time_format: TimeFormatSettings, app_host: ApplicationHost): - LoggerBase.__init__(self, logging_settings, time_format) + def __init__(self, logging_settings: LoggingSettings, time_format: TimeFormatSettings, app_host: ApplicationHostBase): + LoggerBase.__init__(self) - self._app_host: ApplicationHost = app_host + self._log_settings: LoggingSettings = logging_settings + self._time_format_settings: TimeFormatSettings = time_format + self._app_host = app_host self._log = Template(self._log_settings.filename).substitute( date_time_now=self._app_host.date_time_now.strftime(self._time_format_settings.date_time_format), diff --git a/src/sh_edraft/publishing/base/publisher_base.py b/src/sh_edraft/publishing/base/publisher_base.py index f2038018..f3f899ca 100644 --- a/src/sh_edraft/publishing/base/publisher_base.py +++ b/src/sh_edraft/publishing/base/publisher_base.py @@ -1,19 +1,14 @@ from abc import abstractmethod -from sh_edraft.logging.base.logger_base import LoggerBase -from sh_edraft.publishing.model.publish_settings_model import PublishSettingsModel from sh_edraft.service.base.service_base import ServiceBase class PublisherBase(ServiceBase): @abstractmethod - def __init__(self, logger: LoggerBase, publish_settings: PublishSettingsModel): + def __init__(self): ServiceBase.__init__(self) - self._logger: LoggerBase = logger - self._publish_settings: PublishSettingsModel = publish_settings - @property @abstractmethod def source_path(self) -> str: pass diff --git a/src/sh_edraft/publishing/model/__init__.py b/src/sh_edraft/publishing/model/__init__.py index 91e2bd18..4017b71c 100644 --- a/src/sh_edraft/publishing/model/__init__.py +++ b/src/sh_edraft/publishing/model/__init__.py @@ -22,6 +22,8 @@ from collections import namedtuple # imports: from .template import Template from .template_enum import TemplateEnum +from .publish_settings_model import PublishSettingsModel +from .publish_settings_name import PublishSettingsName VersionInfo = namedtuple('VersionInfo', 'major minor micro') version_info = VersionInfo(major=2020, minor=12, micro=5) diff --git a/src/sh_edraft/publishing/publisher.py b/src/sh_edraft/publishing/publisher.py index 1e7f55e6..573c11f5 100644 --- a/src/sh_edraft/publishing/publisher.py +++ b/src/sh_edraft/publishing/publisher.py @@ -11,7 +11,10 @@ from sh_edraft.publishing.model.template import Template class Publisher(PublisherBase): def __init__(self, logger: LoggerBase, publish_settings: PublishSettingsModel): - super().__init__(logger, publish_settings) + PublisherBase.__init__(self) + + self._logger: LoggerBase = logger + self._publish_settings: PublishSettingsModel = publish_settings @property def source_path(self) -> str: diff --git a/src/sh_edraft/service/service_provider.py b/src/sh_edraft/service/service_provider.py index 66cb567e..2467ec46 100644 --- a/src/sh_edraft/service/service_provider.py +++ b/src/sh_edraft/service/service_provider.py @@ -11,8 +11,9 @@ from sh_edraft.service.base.service_base import ServiceBase class ServiceProvider(ServiceProviderBase): - def __init__(self): + def __init__(self, app_host: ApplicationHostBase): super().__init__() + self._app_host: ApplicationHostBase = app_host self._config = Configuration() @property @@ -27,10 +28,13 @@ 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, ServiceBase): + if issubclass(parameter.annotation, ApplicationHostBase): + params.append(self._app_host) + + elif issubclass(parameter.annotation, ServiceBase): params.append(self.get_service(parameter.annotation)) - elif issubclass(parameter.annotation, ConfigurationModelBase) or issubclass(parameter.annotation, ApplicationHostBase): + elif issubclass(parameter.annotation, ConfigurationModelBase): params.append(self._config.get_config_by_type(parameter.annotation)) return service(*params) diff --git a/src/tests/logging/logger.py b/src/tests/logging/logger.py index 37ea1346..d7222426 100644 --- a/src/tests/logging/logger.py +++ b/src/tests/logging/logger.py @@ -1,10 +1,10 @@ import os import shutil import unittest -from datetime import datetime from string import Template -from sh_edraft.hosting import ApplicationHost +from sh_edraft.hosting import ApplicationHost, HostingEnvironment +from sh_edraft.hosting.model import EnvironmentName from sh_edraft.logging import Logger from sh_edraft.logging.model import LoggingSettings from sh_edraft.time.model import TimeFormatSettings @@ -13,7 +13,7 @@ from sh_edraft.time.model import TimeFormatSettings class LoggerTest(unittest.TestCase): def setUp(self): - self._app_host = ApplicationHost() + self._app_host = ApplicationHost('CPL_Test', HostingEnvironment(EnvironmentName.testing, './')) self._services = self._app_host.services self._services.create() diff --git a/src/tests/publishing/publisher.py b/src/tests/publishing/publisher.py index ff64d0ae..cb721a41 100644 --- a/src/tests/publishing/publisher.py +++ b/src/tests/publishing/publisher.py @@ -2,14 +2,13 @@ import os import shutil import unittest -from sh_edraft.hosting import ApplicationHost +from sh_edraft.hosting import ApplicationHost, HostingEnvironment +from sh_edraft.hosting.model import EnvironmentName from sh_edraft.logging import Logger -from sh_edraft.logging.base import LoggerBase from sh_edraft.logging.model import LoggingSettings from sh_edraft.publishing import Publisher -from sh_edraft.publishing.base import PublisherBase from sh_edraft.publishing.model import Template -from sh_edraft.publishing.model.publish_settings_model import PublishSettingsModel +from sh_edraft.publishing.model import PublishSettingsModel from sh_edraft.coding.model import Version from sh_edraft.time.model import TimeFormatSettings @@ -79,7 +78,7 @@ class PublisherTest(unittest.TestCase): def setUp(self): self._config() - self._app_host = ApplicationHost() + self._app_host = ApplicationHost('CPL_Test', HostingEnvironment(EnvironmentName.testing, './')) self._logger = Logger(self._log_settings, self._time_format_settings, self._app_host) self._logger.create() diff --git a/src/tests/service_providing/service_provider.py b/src/tests/service_providing/service_provider.py index 1c136976..a6626763 100644 --- a/src/tests/service_providing/service_provider.py +++ b/src/tests/service_providing/service_provider.py @@ -1,12 +1,14 @@ import unittest from sh_edraft.hosting import ApplicationHost +from sh_edraft.hosting import HostingEnvironment +from sh_edraft.hosting.model import EnvironmentName from sh_edraft.logging import Logger from sh_edraft.logging.base import LoggerBase from sh_edraft.logging.model import LoggingSettings from sh_edraft.publishing import Publisher from sh_edraft.publishing.base import PublisherBase -from sh_edraft.publishing.model.publish_settings_model import PublishSettingsModel +from sh_edraft.publishing.model import PublishSettingsModel from sh_edraft.service import ServiceProvider from sh_edraft.service.base import ServiceBase from sh_edraft.time.model import TimeFormatSettings @@ -15,7 +17,7 @@ from sh_edraft.time.model import TimeFormatSettings class ServiceProviderTest(unittest.TestCase): def setUp(self): - self._app_host = ApplicationHost() + self._app_host = ApplicationHost('CPL_Test', HostingEnvironment(EnvironmentName.testing, './')) self._services = self._app_host.services self._services.create() @@ -59,7 +61,7 @@ class ServiceProviderTest(unittest.TestCase): def test_create(self): print(f'{__name__}.test_create:') - provider = ServiceProvider() + provider = ServiceProvider(self._app_host) self.assertIsNotNone(provider) provider.create() self.assertIsNotNone(provider)