Added logic to load config from json & improved hosting and service providing

This commit is contained in:
Sven Heidemann 2020-11-28 15:13:54 +01:00
parent ff577b121e
commit 44ffb4e574
29 changed files with 285 additions and 141 deletions

View File

@ -3,6 +3,7 @@ from collections import Callable
from typing import Type
from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase
from sh_edraft.environment.base.environment_base import EnvironmentBase
class ConfigurationBase(ABC):
@ -10,11 +11,24 @@ class ConfigurationBase(ABC):
@abstractmethod
def __init__(self): pass
@property
@abstractmethod
def add_config_by_type(self, key_type: type, value: object): pass
def environment(self) -> EnvironmentBase: pass
@abstractmethod
def get_config_by_type(self, search_type: Type[ConfigurationModelBase]) -> Callable[ConfigurationModelBase]: pass
def add_environment_variables(self, prefix: str): pass
@abstractmethod
def add_argument_variables(self): pass
@abstractmethod
def add_json_file(self, name: str, optional: bool = None): pass
@abstractmethod
def add_configuration(self, key_type: type, value: object): pass
@abstractmethod
def get_configuration(self, search_type: Type[ConfigurationModelBase]) -> Callable[ConfigurationModelBase]: pass
@abstractmethod
def create(self): pass

View File

@ -3,5 +3,8 @@ from abc import ABC, abstractmethod
class ConfigurationModelBase(ABC):
@abstractmethod
def __init__(self): pass
@abstractmethod
def from_dict(self, settings: dict): pass

View File

@ -1,25 +1,113 @@
from collections import Callable
import json
import os
import sys
from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase
from sh_edraft.configuration.base.configuration_base import ConfigurationBase
from sh_edraft.configuration.model.configuration_variable_name import ConfigurationVariableName
from sh_edraft.environment.base.environment_base import EnvironmentBase
from sh_edraft.environment.hosting_environment import HostingEnvironment
from sh_edraft.utils import Console
class Configuration(ConfigurationBase):
def __init__(self):
super().__init__()
ConfigurationBase.__init__(self)
self._config: dict[type, object] = {}
self._hosting_environment = HostingEnvironment()
self._config: dict[type, ConfigurationModelBase] = {}
def create(self): pass
@property
def environment(self) -> EnvironmentBase:
return self._hosting_environment
def add_config_by_type(self, key_type: type, value: object):
@staticmethod
def _print_info(name: str, message: str):
Console.write_line(f'[{name}] {message}', 'green')
@staticmethod
def _print_warn(name: str, message: str):
Console.write_line(f'[{name}] {message}', 'yellow')
@staticmethod
def _print_error(name: str, message: str):
Console.write_line(f'[{name}] {message}', 'red')
def _set_variable(self, name: str, value: str):
if name == ConfigurationVariableName.environment.value:
self._hosting_environment.environment_name = value
elif name == ConfigurationVariableName.name.value:
self._hosting_environment.application_name = value
elif name == ConfigurationVariableName.customer.value:
self._hosting_environment.customer = value
def add_environment_variables(self, prefix: str):
for variable in ConfigurationVariableName.to_list():
var_name = f'{prefix}{variable}'
if var_name in [key.upper() for key in os.environ.keys()]:
self._set_variable(variable, os.environ[var_name])
def add_argument_variables(self):
for arg in sys.argv[1:]:
try:
argument = arg.split('--')[1].split('=')[0].upper()
value = arg.split('=')[1]
if argument not in ConfigurationVariableName.to_list():
raise Exception(f'Invalid argument name: {argument}')
self._set_variable(argument, value)
except Exception as e:
self._print_error(__name__, f'Invalid argument: {arg} -> {e}')
exit()
def add_json_file(self, name: str, optional: bool = None):
if self._hosting_environment.content_root_path.endswith('/') and not name.startswith('/'):
file_path = f'{self._hosting_environment.content_root_path}{name}'
else:
file_path = f'{self._hosting_environment.content_root_path}/{name}'
if not os.path.isfile(file_path):
if not optional:
self._print_error(__name__, f'File not found: {file_path}')
exit()
self._print_warn(__name__, f'Not Loaded config file: {file_path}')
return None
config_from_file = self._load_json_file(file_path)
for sub in ConfigurationModelBase.__subclasses__():
for key, value in config_from_file.items():
if sub.__name__ == key:
configuration = sub()
configuration.from_dict(value)
self.add_configuration(sub, configuration)
def _load_json_file(self, file: str) -> dict:
try:
# open config file, create if not exists
with open(file, encoding='utf-8') as cfg:
# load json
json_cfg = json.load(cfg)
self._print_info(__name__, f'Loaded config file: {file}')
return json_cfg
except Exception as e:
self._print_error(__name__, f'Cannot load config file: {file}! -> {e}')
return {}
def add_configuration(self, key_type: type, value: object):
self._config[key_type] = value
def get_config_by_type(self, search_type: type) -> Callable[ConfigurationModelBase]:
def get_configuration(self, search_type: type) -> ConfigurationModelBase:
if search_type not in self._config:
raise Exception(f'Config model by type {search_type} not found')
for config_model in self._config:
if config_model == search_type:
return self._config[config_model]
def create(self):
pass

View File

@ -0,0 +1,3 @@
# imports:
from .configuration_variable_name import ConfigurationVariableName

View File

@ -0,0 +1,12 @@
from enum import Enum
class ConfigurationVariableName(Enum):
environment = 'ENVIRONMENT'
name = 'NAME'
customer = 'CUSTOMER'
@staticmethod
def to_list():
return [var.value for var in ConfigurationVariableName]

View File

@ -0,0 +1,3 @@
# imports:
from .hosting_environment import HostingEnvironment

View File

@ -0,0 +1,3 @@
# imports:
from .environment_base import EnvironmentBase

View File

@ -0,0 +1,45 @@
from abc import ABC, abstractmethod
from sh_edraft.environment.model.environment_name import EnvironmentName
class EnvironmentBase(ABC):
@abstractmethod
def __init__(self): pass
@property
@abstractmethod
def environment_name(self) -> EnvironmentName: pass
@environment_name.setter
@abstractmethod
def environment_name(self, environment_name: EnvironmentName): pass
@property
@abstractmethod
def application_name(self) -> str: pass
@application_name.setter
@abstractmethod
def application_name(self, application_name: str): pass
@property
@abstractmethod
def customer(self) -> str: pass
@customer.setter
@abstractmethod
def customer(self, customer: str): 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
@property
@abstractmethod
def host_name(self) -> str: pass

View File

@ -0,0 +1,52 @@
from socket import gethostname
from typing import Optional
from sh_edraft.environment.base.environment_base import EnvironmentBase
from sh_edraft.environment.model.environment_name import EnvironmentName
class HostingEnvironment(EnvironmentBase):
def __init__(self, name: EnvironmentName = EnvironmentName.production, crp: str = './'):
EnvironmentBase.__init__(self)
self._environment_name: Optional[EnvironmentName] = name
self._app_name: Optional[str] = None
self._customer: Optional[str] = None
self._content_root_path: Optional[str] = crp
@property
def environment_name(self) -> EnvironmentName:
return self._environment_name
@environment_name.setter
def environment_name(self, environment_name: EnvironmentName):
self._environment_name = environment_name
@property
def application_name(self) -> str:
return self._app_name if self._app_name is not None else ''
@application_name.setter
def application_name(self, application_name: str):
self._app_name = application_name
@property
def customer(self) -> str:
return self._customer if self._customer is not None else ''
@customer.setter
def customer(self, customer: str):
self._customer = customer
@property
def content_root_path(self) -> str:
return self._content_root_path
@content_root_path.setter
def content_root_path(self, content_root_path: str):
self._content_root_path = content_root_path
@property
def host_name(self):
return gethostname()

View File

@ -0,0 +1,3 @@
# imports:
from .environment_name import EnvironmentName

View File

@ -1,4 +1,4 @@
# imports:
from .application_host import ApplicationHost
from .hosting_environment import HostingEnvironment
from .application_runtime import ApplicationRuntime

View File

@ -4,8 +4,6 @@ from datetime import datetime
from sh_edraft.configuration.configuration import Configuration
from sh_edraft.configuration.base.configuration_base import ConfigurationBase
from sh_edraft.hosting.base.application_runtime_base import ApplicationRuntimeBase
from sh_edraft.hosting.base.environment_base import EnvironmentBase
from sh_edraft.hosting.hosting_environment import HostingEnvironment
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
@ -14,31 +12,19 @@ from sh_edraft.service.base.service_provider_base import ServiceProviderBase
class ApplicationHost(ApplicationHostBase):
def __init__(self, name: str):
def __init__(self):
ApplicationHostBase.__init__(self)
self._name: str = name
self._args: list[str] = sys.argv
self._config = Configuration()
self._environment = HostingEnvironment()
self._app_runtime = ApplicationRuntime(self._config, self._environment)
self._app_runtime = ApplicationRuntime(self._config)
self._services = ServiceProvider(self._app_runtime)
self._start_time: datetime = datetime.now()
self._end_time: datetime = datetime.now()
@property
def name(self) -> str:
return self._name
@property
def configuration(self) -> ConfigurationBase:
return self._config
@property
def environment(self) -> EnvironmentBase:
return self._environment
@property
def application_runtime(self) -> ApplicationRuntimeBase:
return self._app_runtime

View File

@ -1,24 +1,18 @@
from datetime import datetime
from sh_edraft.configuration.base.configuration_base import ConfigurationBase
from sh_edraft.hosting.base.environment_base import EnvironmentBase
from sh_edraft.hosting.base.application_runtime_base import ApplicationRuntimeBase
class ApplicationRuntime(ApplicationRuntimeBase):
def __init__(self, config: ConfigurationBase, environment: EnvironmentBase):
def __init__(self, config: ConfigurationBase):
ApplicationRuntimeBase.__init__(self)
self._environment = environment
self._app_configuration = config
self._start_time: datetime = datetime.now()
self._end_time: datetime = datetime.now()
@property
def environment(self) -> EnvironmentBase:
return self._environment
@property
def configuration(self) -> ConfigurationBase:
return self._app_configuration

View File

@ -1,4 +1,4 @@
# imports:
from .application_host_base import ApplicationHostBase
from .environment_base import EnvironmentBase
from .application_base import ApplicationBase
from .application_host_base import ApplicationHostBase
from .application_runtime_base import ApplicationRuntimeBase

View File

@ -2,7 +2,6 @@ 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.hosting.base.environment_base import EnvironmentBase
from sh_edraft.service.base.service_provider_base import ServiceProviderBase
@ -11,18 +10,10 @@ class ApplicationHostBase(ABC):
@abstractmethod
def __init__(self): pass
@property
@abstractmethod
def name(self) -> str: pass
@property
@abstractmethod
def configuration(self) -> ConfigurationBase: pass
@property
@abstractmethod
def environment(self) -> EnvironmentBase: pass
@property
@abstractmethod
def application_runtime(self) -> ApplicationRuntimeBase: pass

View File

@ -2,7 +2,6 @@ from abc import ABC, abstractmethod
from datetime import datetime
from sh_edraft.configuration.base.configuration_base import ConfigurationBase
from sh_edraft.hosting.base.environment_base import EnvironmentBase
class ApplicationRuntimeBase(ABC):
@ -10,10 +9,6 @@ class ApplicationRuntimeBase(ABC):
@abstractmethod
def __init__(self): pass
@property
@abstractmethod
def environment(self) -> EnvironmentBase: pass
@property
@abstractmethod
def configuration(self) -> ConfigurationBase: pass

View File

@ -1,25 +0,0 @@
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

View File

@ -1,29 +0,0 @@
from typing import Optional
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 = EnvironmentName.production, crp: str = './'):
EnvironmentBase.__init__(self)
self._name: Optional[EnvironmentName] = name
self._content_root_path: Optional[str] = crp
@property
def name(self) -> EnvironmentName:
return self._name
@name.setter
def name(self, name: EnvironmentName):
self._name = name
@property
def content_root_path(self) -> str:
return self._content_root_path
@content_root_path.setter
def content_root_path(self, content_root_path: str):
self._content_root_path = content_root_path

View File

@ -1,2 +1 @@
# imports:
from .environment_name import EnvironmentName

View File

@ -22,6 +22,7 @@ from collections import namedtuple
# imports:
from .logging_level import LoggingLevel
from .logging_settings import LoggingSettings
from .logging_settings_name import LoggingSettingsName
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major=2020, minor=12, micro=5)

View File

@ -2,7 +2,7 @@ import traceback
from typing import Optional
from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase
from sh_edraft.logging.model.logging_settings_name import LogSettingsName
from sh_edraft.logging.model.logging_settings_name import LoggingSettingsName
from sh_edraft.utils.console import Console
from sh_edraft.logging.model.logging_level import LoggingLevel
@ -11,7 +11,6 @@ class LoggingSettings(ConfigurationModelBase):
def __init__(self):
ConfigurationModelBase.__init__(self)
self._path: Optional[str] = None
self._filename: Optional[str] = None
self._console: Optional[LoggingLevel] = None
@ -51,10 +50,10 @@ class LoggingSettings(ConfigurationModelBase):
def from_dict(self, settings: dict):
try:
self._path = settings[LogSettingsName.path.value]
self._filename = settings[LogSettingsName.filename.value]
self._console = LoggingLevel[settings[LogSettingsName.console_level.value]]
self._level = LoggingLevel[settings[LogSettingsName.file_level.value]]
self._path = settings[LoggingSettingsName.path.value]
self._filename = settings[LoggingSettingsName.filename.value]
self._console = LoggingLevel[settings[LoggingSettingsName.console_level.value]]
self._level = LoggingLevel[settings[LoggingSettingsName.file_level.value]]
except Exception as e:
Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {LogSettingsName.log.value} settings', 'red')
Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings', 'red')
Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}', 'red')

View File

@ -1,9 +1,8 @@
from enum import Enum
class LogSettingsName(Enum):
class LoggingSettingsName(Enum):
log = 'Log'
path = 'Path'
filename = 'Filename'
console_level = 'ConsoleLogLevel'

View File

@ -33,7 +33,7 @@ class ServiceProvider(ServiceProviderBase):
params.append(self.get_service(parameter.annotation))
elif issubclass(parameter.annotation, ConfigurationModelBase):
params.append(self._app_runtime.configuration.get_config_by_type(parameter.annotation))
params.append(self._app_runtime.configuration.get_configuration(parameter.annotation))
return service(*params)

View File

@ -9,6 +9,7 @@ from sh_edraft.utils.console import Console
class TimeFormatSettings(ConfigurationModelBase):
def __init__(self):
ConfigurationModelBase.__init__(self)
self._date_format: Optional[str] = None
self._time_format: Optional[str] = None
self._date_time_format: Optional[str] = None
@ -55,5 +56,5 @@ class TimeFormatSettings(ConfigurationModelBase):
self._date_time_format = settings[TimeFormatSettingsNames.date_time_format.value]
self._date_time_log_format = settings[TimeFormatSettingsNames.date_time_log_format.value]
except Exception as e:
Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {TimeFormatSettingsNames.formats.value} settings')
Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}', 'red')

View File

@ -3,7 +3,6 @@ from enum import Enum
class TimeFormatSettingsNames(Enum):
formats = 'TimeFormats'
date_format = 'DateFormat'
time_format = 'TimeFormat'
date_time_format = 'DateTimeFormat'

View File

@ -0,0 +1,8 @@
{
"LoggingSettings": {
"Path": "logs/",
"Filename": "log_$start_time.log",
"ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE"
}
}

View File

@ -0,0 +1,15 @@
{
"TimeFormatSettings": {
"DateFormat": "%Y-%m-%d",
"TimeFormat": "%H:%M:%S",
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
},
"LoggingSettings": {
"Path": "logs/",
"Filename": "log_$start_time.log",
"ConsoleLogLevel": "ERROR",
"FileLogLevel": "WARN"
}
}

View File

@ -3,12 +3,9 @@ from typing import Optional
from sh_edraft.configuration.base import ConfigurationBase
from sh_edraft.hosting import ApplicationHost
from sh_edraft.hosting.base import ApplicationBase
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.service.base import ServiceProviderBase
from sh_edraft.time.model import TimeFormatSettings
class Program(ApplicationBase):
@ -22,40 +19,28 @@ class Program(ApplicationBase):
self._configuration: Optional[ConfigurationBase] = None
def create_application_host(self):
self._app_host = ApplicationHost('CPL_DEV_Test')
self._app_host = ApplicationHost()
self._services = self._app_host.services
self._configuration = self._app_host.configuration
self._app_host.environment.name = EnvironmentName.development
def create_configuration(self):
self._configuration.create()
log_settings = LoggingSettings()
log_settings.from_dict({
"Path": "logs/",
"Filename": "log_$start_time.log",
"ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE"
})
time_format_settings = TimeFormatSettings()
time_format_settings.from_dict({
"DateFormat": "%Y-%m-%d",
"TimeFormat": "%H:%M:%S",
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
})
self._configuration.add_config_by_type(LoggingSettings, log_settings)
self._configuration.add_config_by_type(TimeFormatSettings, time_format_settings)
self._configuration.add_environment_variables('PYTHON_')
self._configuration.add_environment_variables('CPL_')
self._configuration.add_argument_variables()
self._configuration.add_json_file(f'appsettings.json')
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.environment_name}.json')
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True)
def create_services(self):
self._services.create()
self._services.add_singleton(LoggerBase, Logger)
logger: Logger = self._services.get_service(LoggerBase)
logger.create()
logger.header(self._app_host.name)
logger.header(f'{self._configuration.environment.application_name}:')
logger.debug(__name__, f'Host: {self._configuration.environment.host_name}')
logger.debug(__name__, f'Environment: {self._configuration.environment.environment_name}')
logger.debug(__name__, f'Customer: {self._configuration.environment.customer}')
def main(self):
print('RUN')