Improved database module structure
This commit is contained in:
parent
6977b9ae05
commit
d2dc57bc76
@ -1,3 +1 @@
|
||||
# imports:
|
||||
|
||||
from .database_connection import DatabaseConnection
|
||||
|
3
src/sh_edraft/database/connection/__init__.py
Normal file
3
src/sh_edraft/database/connection/__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
# imports:
|
||||
|
||||
from .database_connection import DatabaseConnection
|
@ -8,3 +8,6 @@ class DatabaseConnectionBase(ServiceBase):
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
ServiceBase.__init__(self)
|
||||
|
||||
@abstractmethod
|
||||
def use_mysql(self, connection_string: str): pass
|
@ -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
|
@ -0,0 +1,4 @@
|
||||
# imports:
|
||||
|
||||
from .database_settings import DatabaseSettings
|
||||
from .database_settings_name import DatabaseSettingsName
|
@ -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
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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):
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -1,3 +1,4 @@
|
||||
# imports:
|
||||
from .service_provider import ServiceProvider
|
||||
|
||||
from .service_provider import ServiceProviderBase
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
@ -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}:')
|
||||
|
Loading…
Reference in New Issue
Block a user