Improved database module structure

This commit is contained in:
Sven Heidemann 2020-12-10 18:11:05 +01:00
parent 6977b9ae05
commit d2dc57bc76
15 changed files with 47 additions and 19 deletions

View File

@ -1,3 +1 @@
# imports:
from .database_connection import DatabaseConnection

View File

@ -0,0 +1,3 @@
# imports:
from .database_connection import DatabaseConnection

View File

@ -8,3 +8,6 @@ class DatabaseConnectionBase(ServiceBase):
@abstractmethod @abstractmethod
def __init__(self): def __init__(self):
ServiceBase.__init__(self) ServiceBase.__init__(self)
@abstractmethod
def use_mysql(self, connection_string: str): pass

View File

@ -3,7 +3,7 @@ from typing import Optional
from sqlalchemy import engine, create_engine from sqlalchemy import engine, create_engine
from sqlalchemy.orm import session, sessionmaker 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.database.model.database_settings import DatabaseSettings
from sh_edraft.utils.console import Console from sh_edraft.utils.console import Console
@ -19,9 +19,13 @@ class DatabaseConnection(DatabaseConnectionBase):
self._session: Optional[session] = None self._session: Optional[session] = None
self._credentials: Optional[str] = None self._credentials: Optional[str] = None
def create(self): self.create()
def create(self): pass
def use_mysql(self, connection_string: str):
try: 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: if self._db_settings.encoding is not None:
self._engine.encoding = self._db_settings.encoding self._engine.encoding = self._db_settings.encoding

View File

@ -0,0 +1,4 @@
# imports:
from .database_settings import DatabaseSettings
from .database_settings_name import DatabaseSettingsName

View File

@ -3,7 +3,6 @@ from typing import Optional
from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase
from sh_edraft.database.model.database_settings_name import DatabaseSettingsName 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 from sh_edraft.utils.console import Console
@ -26,10 +25,6 @@ class DatabaseSettings(ConfigurationModelBase):
def connection_string(self, connection_string: str): def connection_string(self, connection_string: str):
self._connection_string = connection_string self._connection_string = connection_string
@property
def decrypted_connection_string(self) -> str:
return CredentialManager.build_string(self._connection_string, self._credentials)
@property @property
def credentials(self) -> str: def credentials(self) -> str:
return self._credentials return self._credentials

View File

@ -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.base.application_runtime_base import ApplicationRuntimeBase
from sh_edraft.hosting.application_runtime import ApplicationRuntime from sh_edraft.hosting.application_runtime import ApplicationRuntime
from sh_edraft.hosting.base.application_host_base import ApplicationHostBase from sh_edraft.hosting.base.application_host_base import ApplicationHostBase
from sh_edraft.service.service_provider import ServiceProvider from sh_edraft.service.providing.service_provider import ServiceProvider
from sh_edraft.service.base.service_provider_base import ServiceProviderBase from sh_edraft.service.providing.base.service_provider_base import ServiceProviderBase
class ApplicationHost(ApplicationHostBase): class ApplicationHost(ApplicationHostBase):
def __init__(self): def __init__(self):
ApplicationHostBase.__init__(self) ApplicationHostBase.__init__(self)
# Init
self._config = Configuration() self._config = Configuration()
self._app_runtime = ApplicationRuntime(self._config) self._app_runtime = ApplicationRuntime(self._config)
self._services = ServiceProvider(self._app_runtime) self._services = ServiceProvider(self._app_runtime)
# Create
self._config.create()
self._services.create()
# Set vars
self._start_time: datetime = datetime.now() self._start_time: datetime = datetime.now()
self._end_time: datetime = datetime.now() self._end_time: datetime = datetime.now()

View File

@ -2,7 +2,7 @@ from abc import ABC, abstractmethod
from sh_edraft.configuration.base.configuration_base import ConfigurationBase from sh_edraft.configuration.base.configuration_base import ConfigurationBase
from sh_edraft.hosting.base.application_runtime_base import ApplicationRuntimeBase 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): class ApplicationHostBase(ABC):

View File

@ -28,6 +28,8 @@ class Logger(LoggerBase):
self._level = self._log_settings.level self._level = self._log_settings.level
self._console = self._log_settings.console self._console = self._log_settings.console
self.create()
def _get_datetime_now(self) -> str: def _get_datetime_now(self) -> str:
try: try:
return datetime.datetime.now().strftime(self._time_format_settings.date_time_format) return datetime.datetime.now().strftime(self._time_format_settings.date_time_format)

View File

@ -20,7 +20,6 @@ __version__ = '2020.12.5'
from collections import namedtuple from collections import namedtuple
# imports: # imports:
from .service_provider import ServiceProvider
VersionInfo = namedtuple('VersionInfo', 'major minor micro') VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major=2020, minor=12, micro=5) version_info = VersionInfo(major=2020, minor=12, micro=5)

View File

@ -21,7 +21,6 @@ from collections import namedtuple
# imports: # imports:
from .service_base import ServiceBase from .service_base import ServiceBase
from .service_provider_base import ServiceProviderBase
VersionInfo = namedtuple('VersionInfo', 'major minor micro') VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major=2020, minor=12, micro=5) version_info = VersionInfo(major=2020, minor=12, micro=5)

View File

@ -1,3 +1,4 @@
# imports: # imports:
from .service_provider import ServiceProvider
from .service_provider import ServiceProviderBase from .service_provider import ServiceProviderBase

View File

@ -4,5 +4,10 @@
"Filename": "log_$start_time.log", "Filename": "log_$start_time.log",
"ConsoleLogLevel": "TRACE", "ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE" "FileLogLevel": "TRACE"
},
"DatabaseSettings": {
"ConnectionString": "mysql+mysqlconnector://sh_cpl:$credentials@localhost/sh_cpl",
"Credentials": "MHZhc0Y2bjhKc1VUMWV0Qw==",
"Encoding": "utf8mb4"
} }
} }

View File

@ -1,11 +1,15 @@
from typing import Optional from typing import Optional
from sh_edraft.configuration.base import ConfigurationBase 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 import ApplicationHost
from sh_edraft.hosting.base import ApplicationBase from sh_edraft.hosting.base import ApplicationBase
from sh_edraft.logging import Logger from sh_edraft.logging import Logger
from sh_edraft.logging.base import LoggerBase 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): class Program(ApplicationBase):
@ -24,7 +28,6 @@ class Program(ApplicationBase):
self._services = self._app_host.services self._services = self._app_host.services
def create_configuration(self): def create_configuration(self):
self._configuration.create()
self._configuration.add_environment_variables('PYTHON_') self._configuration.add_environment_variables('PYTHON_')
self._configuration.add_environment_variables('CPL_') self._configuration.add_environment_variables('CPL_')
self._configuration.add_argument_variables() 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) self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True)
def create_services(self): 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._services.add_singleton(LoggerBase, Logger)
self._logger = self._services.get_service(LoggerBase) self._logger = self._services.get_service(LoggerBase)
self._logger.create()
def main(self): def main(self):
self._logger.header(f'{self._configuration.environment.application_name}:') self._logger.header(f'{self._configuration.environment.application_name}:')