From d2dc57bc76b925b706e2a79fa51fcc721c2a4d8d Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 10 Dec 2020 18:11:05 +0100 Subject: [PATCH] Improved database module structure --- src/sh_edraft/database/__init__.py | 2 -- src/sh_edraft/database/connection/__init__.py | 3 +++ .../database/{ => connection}/base/__init__.py | 0 .../base/database_connection_base.py | 3 +++ .../{ => connection}/database_connection.py | 10 +++++++--- src/sh_edraft/database/model/__init__.py | 4 ++++ .../database/model/database_settings.py | 5 ----- src/sh_edraft/hosting/application_host.py | 11 +++++++++-- .../hosting/base/application_host_base.py | 2 +- src/sh_edraft/logging/logger.py | 2 ++ src/sh_edraft/service/__init__.py | 1 - src/sh_edraft/service/base/__init__.py | 1 - src/sh_edraft/service/providing/__init__.py | 1 + src/tests_dev/appsettings.development.json | 5 +++++ src/tests_dev/program.py | 16 ++++++++++++---- 15 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 src/sh_edraft/database/connection/__init__.py rename src/sh_edraft/database/{ => connection}/base/__init__.py (100%) rename src/sh_edraft/database/{ => connection}/base/database_connection_base.py (74%) rename src/sh_edraft/database/{ => connection}/database_connection.py (83%) diff --git a/src/sh_edraft/database/__init__.py b/src/sh_edraft/database/__init__.py index eae4c82d..8b137891 100644 --- a/src/sh_edraft/database/__init__.py +++ b/src/sh_edraft/database/__init__.py @@ -1,3 +1 @@ -# imports: -from .database_connection import DatabaseConnection diff --git a/src/sh_edraft/database/connection/__init__.py b/src/sh_edraft/database/connection/__init__.py new file mode 100644 index 00000000..eae4c82d --- /dev/null +++ b/src/sh_edraft/database/connection/__init__.py @@ -0,0 +1,3 @@ +# imports: + +from .database_connection import DatabaseConnection diff --git a/src/sh_edraft/database/base/__init__.py b/src/sh_edraft/database/connection/base/__init__.py similarity index 100% rename from src/sh_edraft/database/base/__init__.py rename to src/sh_edraft/database/connection/base/__init__.py diff --git a/src/sh_edraft/database/base/database_connection_base.py b/src/sh_edraft/database/connection/base/database_connection_base.py similarity index 74% rename from src/sh_edraft/database/base/database_connection_base.py rename to src/sh_edraft/database/connection/base/database_connection_base.py index 9307c9b1..f97859bc 100644 --- a/src/sh_edraft/database/base/database_connection_base.py +++ b/src/sh_edraft/database/connection/base/database_connection_base.py @@ -8,3 +8,6 @@ class DatabaseConnectionBase(ServiceBase): @abstractmethod def __init__(self): ServiceBase.__init__(self) + + @abstractmethod + def use_mysql(self, connection_string: str): pass diff --git a/src/sh_edraft/database/database_connection.py b/src/sh_edraft/database/connection/database_connection.py similarity index 83% rename from src/sh_edraft/database/database_connection.py rename to src/sh_edraft/database/connection/database_connection.py index 31db403a..7d02c382 100644 --- a/src/sh_edraft/database/database_connection.py +++ b/src/sh_edraft/database/connection/database_connection.py @@ -3,7 +3,7 @@ from typing import Optional from sqlalchemy import engine, create_engine from sqlalchemy.orm import session, sessionmaker -from sh_edraft.database.base.database_connection_base import DatabaseConnectionBase +from sh_edraft.database.connection.base.database_connection_base import DatabaseConnectionBase from sh_edraft.database.model.database_settings import DatabaseSettings from sh_edraft.utils.console import Console @@ -19,9 +19,13 @@ class DatabaseConnection(DatabaseConnectionBase): self._session: Optional[session] = None self._credentials: Optional[str] = None - def create(self): + self.create() + + def create(self): pass + + def use_mysql(self, connection_string: str): try: - self._engine = create_engine(self._db_settings.decrypted_connection_string) + self._engine = create_engine(connection_string) if self._db_settings.encoding is not None: self._engine.encoding = self._db_settings.encoding diff --git a/src/sh_edraft/database/model/__init__.py b/src/sh_edraft/database/model/__init__.py index e69de29b..c13a207e 100644 --- a/src/sh_edraft/database/model/__init__.py +++ b/src/sh_edraft/database/model/__init__.py @@ -0,0 +1,4 @@ +# imports: + +from .database_settings import DatabaseSettings +from .database_settings_name import DatabaseSettingsName diff --git a/src/sh_edraft/database/model/database_settings.py b/src/sh_edraft/database/model/database_settings.py index 8c2def60..a8df384d 100644 --- a/src/sh_edraft/database/model/database_settings.py +++ b/src/sh_edraft/database/model/database_settings.py @@ -3,7 +3,6 @@ from typing import Optional from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase from sh_edraft.database.model.database_settings_name import DatabaseSettingsName -from sh_edraft.utils.credential_manager import CredentialManager from sh_edraft.utils.console import Console @@ -26,10 +25,6 @@ class DatabaseSettings(ConfigurationModelBase): def connection_string(self, connection_string: str): self._connection_string = connection_string - @property - def decrypted_connection_string(self) -> str: - return CredentialManager.build_string(self._connection_string, self._credentials) - @property def credentials(self) -> str: return self._credentials diff --git a/src/sh_edraft/hosting/application_host.py b/src/sh_edraft/hosting/application_host.py index e5e6b362..fa4b2182 100644 --- a/src/sh_edraft/hosting/application_host.py +++ b/src/sh_edraft/hosting/application_host.py @@ -5,18 +5,25 @@ from sh_edraft.configuration.base.configuration_base import ConfigurationBase from sh_edraft.hosting.base.application_runtime_base import ApplicationRuntimeBase from sh_edraft.hosting.application_runtime import ApplicationRuntime from sh_edraft.hosting.base.application_host_base import ApplicationHostBase -from sh_edraft.service.service_provider import ServiceProvider -from sh_edraft.service.base.service_provider_base import ServiceProviderBase +from sh_edraft.service.providing.service_provider import ServiceProvider +from sh_edraft.service.providing.base.service_provider_base import ServiceProviderBase class ApplicationHost(ApplicationHostBase): def __init__(self): ApplicationHostBase.__init__(self) + + # Init self._config = Configuration() self._app_runtime = ApplicationRuntime(self._config) self._services = ServiceProvider(self._app_runtime) + # Create + self._config.create() + self._services.create() + + # Set vars self._start_time: datetime = datetime.now() self._end_time: datetime = datetime.now() diff --git a/src/sh_edraft/hosting/base/application_host_base.py b/src/sh_edraft/hosting/base/application_host_base.py index 4ee8c0f5..fb816ccb 100644 --- a/src/sh_edraft/hosting/base/application_host_base.py +++ b/src/sh_edraft/hosting/base/application_host_base.py @@ -2,7 +2,7 @@ from abc import ABC, abstractmethod from sh_edraft.configuration.base.configuration_base import ConfigurationBase from sh_edraft.hosting.base.application_runtime_base import ApplicationRuntimeBase -from sh_edraft.service.base.service_provider_base import ServiceProviderBase +from sh_edraft.service.providing.base.service_provider_base import ServiceProviderBase class ApplicationHostBase(ABC): diff --git a/src/sh_edraft/logging/logger.py b/src/sh_edraft/logging/logger.py index f2159ad2..a043b21b 100644 --- a/src/sh_edraft/logging/logger.py +++ b/src/sh_edraft/logging/logger.py @@ -28,6 +28,8 @@ class Logger(LoggerBase): self._level = self._log_settings.level self._console = self._log_settings.console + self.create() + def _get_datetime_now(self) -> str: try: return datetime.datetime.now().strftime(self._time_format_settings.date_time_format) diff --git a/src/sh_edraft/service/__init__.py b/src/sh_edraft/service/__init__.py index 52003f0b..abfc6044 100644 --- a/src/sh_edraft/service/__init__.py +++ b/src/sh_edraft/service/__init__.py @@ -20,7 +20,6 @@ __version__ = '2020.12.5' from collections import namedtuple # imports: -from .service_provider import ServiceProvider VersionInfo = namedtuple('VersionInfo', 'major minor micro') version_info = VersionInfo(major=2020, minor=12, micro=5) diff --git a/src/sh_edraft/service/base/__init__.py b/src/sh_edraft/service/base/__init__.py index 6ce7f45b..d3d865d7 100644 --- a/src/sh_edraft/service/base/__init__.py +++ b/src/sh_edraft/service/base/__init__.py @@ -21,7 +21,6 @@ from collections import namedtuple # imports: from .service_base import ServiceBase -from .service_provider_base import ServiceProviderBase VersionInfo = namedtuple('VersionInfo', 'major minor micro') version_info = VersionInfo(major=2020, minor=12, micro=5) diff --git a/src/sh_edraft/service/providing/__init__.py b/src/sh_edraft/service/providing/__init__.py index b8bf2077..7817c7f3 100644 --- a/src/sh_edraft/service/providing/__init__.py +++ b/src/sh_edraft/service/providing/__init__.py @@ -1,3 +1,4 @@ # imports: +from .service_provider import ServiceProvider from .service_provider import ServiceProviderBase diff --git a/src/tests_dev/appsettings.development.json b/src/tests_dev/appsettings.development.json index 62ec6c61..3a54f3cc 100644 --- a/src/tests_dev/appsettings.development.json +++ b/src/tests_dev/appsettings.development.json @@ -4,5 +4,10 @@ "Filename": "log_$start_time.log", "ConsoleLogLevel": "TRACE", "FileLogLevel": "TRACE" + }, + "DatabaseSettings": { + "ConnectionString": "mysql+mysqlconnector://sh_cpl:$credentials@localhost/sh_cpl", + "Credentials": "MHZhc0Y2bjhKc1VUMWV0Qw==", + "Encoding": "utf8mb4" } } \ No newline at end of file diff --git a/src/tests_dev/program.py b/src/tests_dev/program.py index 2ee1b09b..d780d0c7 100644 --- a/src/tests_dev/program.py +++ b/src/tests_dev/program.py @@ -1,11 +1,15 @@ from typing import Optional from sh_edraft.configuration.base import ConfigurationBase +from sh_edraft.database.connection import DatabaseConnection +from sh_edraft.database.connection.base import DatabaseConnectionBase +from sh_edraft.database.model import DatabaseSettings from sh_edraft.hosting import ApplicationHost from sh_edraft.hosting.base import ApplicationBase from sh_edraft.logging import Logger from sh_edraft.logging.base import LoggerBase -from sh_edraft.service.base import ServiceProviderBase +from sh_edraft.service.providing.base import ServiceProviderBase +from sh_edraft.utils.credential_manager import CredentialManager class Program(ApplicationBase): @@ -24,7 +28,6 @@ class Program(ApplicationBase): self._services = self._app_host.services def create_configuration(self): - self._configuration.create() self._configuration.add_environment_variables('PYTHON_') self._configuration.add_environment_variables('CPL_') self._configuration.add_argument_variables() @@ -33,10 +36,15 @@ class Program(ApplicationBase): self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True) def create_services(self): - self._services.create() + # Create and connect to database + db_settings: DatabaseSettings = self._configuration.get_configuration(DatabaseSettings) + self._services.add_singleton(DatabaseConnectionBase, DatabaseConnection) + db: DatabaseConnectionBase = self._services.get_service(DatabaseConnectionBase) + db.use_mysql(CredentialManager.build_string(db_settings.connection_string, db_settings.credentials)) + + # Add and create logger self._services.add_singleton(LoggerBase, Logger) self._logger = self._services.get_service(LoggerBase) - self._logger.create() def main(self): self._logger.header(f'{self._configuration.environment.application_name}:')