diff --git a/src/sh_edraft/configuration/base/configuration_base.py b/src/sh_edraft/configuration/base/configuration_base.py index 40c68555..bf44a8c1 100644 --- a/src/sh_edraft/configuration/base/configuration_base.py +++ b/src/sh_edraft/configuration/base/configuration_base.py @@ -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 diff --git a/src/sh_edraft/configuration/base/configuration_model_base.py b/src/sh_edraft/configuration/base/configuration_model_base.py index 06b65ba5..9772a648 100644 --- a/src/sh_edraft/configuration/base/configuration_model_base.py +++ b/src/sh_edraft/configuration/base/configuration_model_base.py @@ -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 diff --git a/src/sh_edraft/configuration/configuration.py b/src/sh_edraft/configuration/configuration.py index 39e1767d..8c02be21 100644 --- a/src/sh_edraft/configuration/configuration.py +++ b/src/sh_edraft/configuration/configuration.py @@ -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 diff --git a/src/sh_edraft/configuration/model/__init__.py b/src/sh_edraft/configuration/model/__init__.py new file mode 100644 index 00000000..a3c9f248 --- /dev/null +++ b/src/sh_edraft/configuration/model/__init__.py @@ -0,0 +1,3 @@ +# imports: + +from .configuration_variable_name import ConfigurationVariableName diff --git a/src/sh_edraft/configuration/model/configuration_variable_name.py b/src/sh_edraft/configuration/model/configuration_variable_name.py new file mode 100644 index 00000000..c89deb88 --- /dev/null +++ b/src/sh_edraft/configuration/model/configuration_variable_name.py @@ -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] diff --git a/src/sh_edraft/environment/__init__.py b/src/sh_edraft/environment/__init__.py new file mode 100644 index 00000000..28c021f3 --- /dev/null +++ b/src/sh_edraft/environment/__init__.py @@ -0,0 +1,3 @@ +# imports: + +from .hosting_environment import HostingEnvironment diff --git a/src/sh_edraft/environment/base/__init__.py b/src/sh_edraft/environment/base/__init__.py new file mode 100644 index 00000000..fec6499d --- /dev/null +++ b/src/sh_edraft/environment/base/__init__.py @@ -0,0 +1,3 @@ +# imports: + +from .environment_base import EnvironmentBase diff --git a/src/sh_edraft/environment/base/environment_base.py b/src/sh_edraft/environment/base/environment_base.py new file mode 100644 index 00000000..9d78d598 --- /dev/null +++ b/src/sh_edraft/environment/base/environment_base.py @@ -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 diff --git a/src/sh_edraft/environment/hosting_environment.py b/src/sh_edraft/environment/hosting_environment.py new file mode 100644 index 00000000..3840a3d2 --- /dev/null +++ b/src/sh_edraft/environment/hosting_environment.py @@ -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() diff --git a/src/sh_edraft/environment/model/__init__.py b/src/sh_edraft/environment/model/__init__.py new file mode 100644 index 00000000..3204981f --- /dev/null +++ b/src/sh_edraft/environment/model/__init__.py @@ -0,0 +1,3 @@ +# imports: + +from .environment_name import EnvironmentName diff --git a/src/sh_edraft/hosting/model/environment_name.py b/src/sh_edraft/environment/model/environment_name.py similarity index 100% rename from src/sh_edraft/hosting/model/environment_name.py rename to src/sh_edraft/environment/model/environment_name.py diff --git a/src/sh_edraft/hosting/__init__.py b/src/sh_edraft/hosting/__init__.py index 2a8b8615..ec999843 100644 --- a/src/sh_edraft/hosting/__init__.py +++ b/src/sh_edraft/hosting/__init__.py @@ -1,4 +1,4 @@ # imports: from .application_host import ApplicationHost -from .hosting_environment import HostingEnvironment +from .application_runtime import ApplicationRuntime diff --git a/src/sh_edraft/hosting/application_host.py b/src/sh_edraft/hosting/application_host.py index 09eafb99..1960b419 100644 --- a/src/sh_edraft/hosting/application_host.py +++ b/src/sh_edraft/hosting/application_host.py @@ -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 diff --git a/src/sh_edraft/hosting/application_runtime.py b/src/sh_edraft/hosting/application_runtime.py index e4ce40c1..5e903bb5 100644 --- a/src/sh_edraft/hosting/application_runtime.py +++ b/src/sh_edraft/hosting/application_runtime.py @@ -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 diff --git a/src/sh_edraft/hosting/base/__init__.py b/src/sh_edraft/hosting/base/__init__.py index 2bafd17e..dfb25a31 100644 --- a/src/sh_edraft/hosting/base/__init__.py +++ b/src/sh_edraft/hosting/base/__init__.py @@ -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 diff --git a/src/sh_edraft/hosting/base/application_host_base.py b/src/sh_edraft/hosting/base/application_host_base.py index 117ec546..4ee8c0f5 100644 --- a/src/sh_edraft/hosting/base/application_host_base.py +++ b/src/sh_edraft/hosting/base/application_host_base.py @@ -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 @@ -10,19 +9,11 @@ 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 diff --git a/src/sh_edraft/hosting/base/application_runtime_base.py b/src/sh_edraft/hosting/base/application_runtime_base.py index 8d5ec5cc..fd644270 100644 --- a/src/sh_edraft/hosting/base/application_runtime_base.py +++ b/src/sh_edraft/hosting/base/application_runtime_base.py @@ -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 diff --git a/src/sh_edraft/hosting/base/environment_base.py b/src/sh_edraft/hosting/base/environment_base.py deleted file mode 100644 index 99cfaa18..00000000 --- a/src/sh_edraft/hosting/base/environment_base.py +++ /dev/null @@ -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 diff --git a/src/sh_edraft/hosting/hosting_environment.py b/src/sh_edraft/hosting/hosting_environment.py deleted file mode 100644 index e1ecc15b..00000000 --- a/src/sh_edraft/hosting/hosting_environment.py +++ /dev/null @@ -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 diff --git a/src/sh_edraft/hosting/model/__init__.py b/src/sh_edraft/hosting/model/__init__.py index b8692da2..52f86f25 100644 --- a/src/sh_edraft/hosting/model/__init__.py +++ b/src/sh_edraft/hosting/model/__init__.py @@ -1,2 +1 @@ # imports: -from .environment_name import EnvironmentName \ No newline at end of file diff --git a/src/sh_edraft/logging/model/__init__.py b/src/sh_edraft/logging/model/__init__.py index 6712752d..8c8ce494 100644 --- a/src/sh_edraft/logging/model/__init__.py +++ b/src/sh_edraft/logging/model/__init__.py @@ -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) diff --git a/src/sh_edraft/logging/model/logging_settings.py b/src/sh_edraft/logging/model/logging_settings.py index c5d1d77e..47889dea 100644 --- a/src/sh_edraft/logging/model/logging_settings.py +++ b/src/sh_edraft/logging/model/logging_settings.py @@ -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') diff --git a/src/sh_edraft/logging/model/logging_settings_name.py b/src/sh_edraft/logging/model/logging_settings_name.py index 3bd90037..323e5735 100644 --- a/src/sh_edraft/logging/model/logging_settings_name.py +++ b/src/sh_edraft/logging/model/logging_settings_name.py @@ -1,9 +1,8 @@ from enum import Enum -class LogSettingsName(Enum): +class LoggingSettingsName(Enum): - log = 'Log' path = 'Path' filename = 'Filename' console_level = 'ConsoleLogLevel' diff --git a/src/sh_edraft/service/service_provider.py b/src/sh_edraft/service/service_provider.py index 40173ea0..4207b0f5 100644 --- a/src/sh_edraft/service/service_provider.py +++ b/src/sh_edraft/service/service_provider.py @@ -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) diff --git a/src/sh_edraft/time/model/time_format_settings.py b/src/sh_edraft/time/model/time_format_settings.py index f092e27b..64508ef2 100644 --- a/src/sh_edraft/time/model/time_format_settings.py +++ b/src/sh_edraft/time/model/time_format_settings.py @@ -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') diff --git a/src/sh_edraft/time/model/time_format_settings_names.py b/src/sh_edraft/time/model/time_format_settings_names.py index 4afc21cd..3858de10 100644 --- a/src/sh_edraft/time/model/time_format_settings_names.py +++ b/src/sh_edraft/time/model/time_format_settings_names.py @@ -3,7 +3,6 @@ from enum import Enum class TimeFormatSettingsNames(Enum): - formats = 'TimeFormats' date_format = 'DateFormat' time_format = 'TimeFormat' date_time_format = 'DateTimeFormat' diff --git a/src/tests_dev/appsettings.development.json b/src/tests_dev/appsettings.development.json new file mode 100644 index 00000000..62ec6c61 --- /dev/null +++ b/src/tests_dev/appsettings.development.json @@ -0,0 +1,8 @@ +{ + "LoggingSettings": { + "Path": "logs/", + "Filename": "log_$start_time.log", + "ConsoleLogLevel": "TRACE", + "FileLogLevel": "TRACE" + } +} \ No newline at end of file diff --git a/src/tests_dev/appsettings.json b/src/tests_dev/appsettings.json new file mode 100644 index 00000000..fd8ddf6c --- /dev/null +++ b/src/tests_dev/appsettings.json @@ -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" + } +} \ No newline at end of file diff --git a/src/tests_dev/program.py b/src/tests_dev/program.py index 5c8a6356..7d4e7494 100644 --- a/src/tests_dev/program.py +++ b/src/tests_dev/program.py @@ -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')