Added logic to load config from json & improved hosting and service providing
This commit is contained in:
parent
ff577b121e
commit
44ffb4e574
@ -3,6 +3,7 @@ from collections import Callable
|
|||||||
from typing import Type
|
from typing import Type
|
||||||
|
|
||||||
from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase
|
from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase
|
||||||
|
from sh_edraft.environment.base.environment_base import EnvironmentBase
|
||||||
|
|
||||||
|
|
||||||
class ConfigurationBase(ABC):
|
class ConfigurationBase(ABC):
|
||||||
@ -10,11 +11,24 @@ class ConfigurationBase(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def __init__(self): pass
|
def __init__(self): pass
|
||||||
|
|
||||||
|
@property
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def add_config_by_type(self, key_type: type, value: object): pass
|
def environment(self) -> EnvironmentBase: pass
|
||||||
|
|
||||||
@abstractmethod
|
@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
|
@abstractmethod
|
||||||
def create(self): pass
|
def create(self): pass
|
||||||
|
@ -3,5 +3,8 @@ from abc import ABC, abstractmethod
|
|||||||
|
|
||||||
class ConfigurationModelBase(ABC):
|
class ConfigurationModelBase(ABC):
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def __init__(self): pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def from_dict(self, settings: dict): pass
|
def from_dict(self, settings: dict): pass
|
||||||
|
@ -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_model_base import ConfigurationModelBase
|
||||||
from sh_edraft.configuration.base.configuration_base import ConfigurationBase
|
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):
|
class Configuration(ConfigurationBase):
|
||||||
|
|
||||||
def __init__(self):
|
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
|
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:
|
if search_type not in self._config:
|
||||||
raise Exception(f'Config model by type {search_type} not found')
|
raise Exception(f'Config model by type {search_type} not found')
|
||||||
|
|
||||||
for config_model in self._config:
|
for config_model in self._config:
|
||||||
if config_model == search_type:
|
if config_model == search_type:
|
||||||
return self._config[config_model]
|
return self._config[config_model]
|
||||||
|
|
||||||
|
def create(self):
|
||||||
|
pass
|
||||||
|
3
src/sh_edraft/configuration/model/__init__.py
Normal file
3
src/sh_edraft/configuration/model/__init__.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# imports:
|
||||||
|
|
||||||
|
from .configuration_variable_name import ConfigurationVariableName
|
@ -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]
|
3
src/sh_edraft/environment/__init__.py
Normal file
3
src/sh_edraft/environment/__init__.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# imports:
|
||||||
|
|
||||||
|
from .hosting_environment import HostingEnvironment
|
3
src/sh_edraft/environment/base/__init__.py
Normal file
3
src/sh_edraft/environment/base/__init__.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# imports:
|
||||||
|
|
||||||
|
from .environment_base import EnvironmentBase
|
45
src/sh_edraft/environment/base/environment_base.py
Normal file
45
src/sh_edraft/environment/base/environment_base.py
Normal 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
|
52
src/sh_edraft/environment/hosting_environment.py
Normal file
52
src/sh_edraft/environment/hosting_environment.py
Normal 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()
|
3
src/sh_edraft/environment/model/__init__.py
Normal file
3
src/sh_edraft/environment/model/__init__.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# imports:
|
||||||
|
|
||||||
|
from .environment_name import EnvironmentName
|
@ -1,4 +1,4 @@
|
|||||||
# imports:
|
# imports:
|
||||||
|
|
||||||
from .application_host import ApplicationHost
|
from .application_host import ApplicationHost
|
||||||
from .hosting_environment import HostingEnvironment
|
from .application_runtime import ApplicationRuntime
|
||||||
|
@ -4,8 +4,6 @@ from datetime import datetime
|
|||||||
from sh_edraft.configuration.configuration import Configuration
|
from sh_edraft.configuration.configuration import Configuration
|
||||||
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.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.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.service_provider import ServiceProvider
|
||||||
@ -14,31 +12,19 @@ from sh_edraft.service.base.service_provider_base import ServiceProviderBase
|
|||||||
|
|
||||||
class ApplicationHost(ApplicationHostBase):
|
class ApplicationHost(ApplicationHostBase):
|
||||||
|
|
||||||
def __init__(self, name: str):
|
def __init__(self):
|
||||||
ApplicationHostBase.__init__(self)
|
ApplicationHostBase.__init__(self)
|
||||||
self._name: str = name
|
|
||||||
self._args: list[str] = sys.argv
|
|
||||||
|
|
||||||
self._config = Configuration()
|
self._config = Configuration()
|
||||||
self._environment = HostingEnvironment()
|
self._app_runtime = ApplicationRuntime(self._config)
|
||||||
self._app_runtime = ApplicationRuntime(self._config, self._environment)
|
|
||||||
self._services = ServiceProvider(self._app_runtime)
|
self._services = ServiceProvider(self._app_runtime)
|
||||||
|
|
||||||
self._start_time: datetime = datetime.now()
|
self._start_time: datetime = datetime.now()
|
||||||
self._end_time: datetime = datetime.now()
|
self._end_time: datetime = datetime.now()
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self) -> str:
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def configuration(self) -> ConfigurationBase:
|
def configuration(self) -> ConfigurationBase:
|
||||||
return self._config
|
return self._config
|
||||||
|
|
||||||
@property
|
|
||||||
def environment(self) -> EnvironmentBase:
|
|
||||||
return self._environment
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def application_runtime(self) -> ApplicationRuntimeBase:
|
def application_runtime(self) -> ApplicationRuntimeBase:
|
||||||
return self._app_runtime
|
return self._app_runtime
|
||||||
|
@ -1,24 +1,18 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from sh_edraft.configuration.base.configuration_base import ConfigurationBase
|
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
|
from sh_edraft.hosting.base.application_runtime_base import ApplicationRuntimeBase
|
||||||
|
|
||||||
|
|
||||||
class ApplicationRuntime(ApplicationRuntimeBase):
|
class ApplicationRuntime(ApplicationRuntimeBase):
|
||||||
|
|
||||||
def __init__(self, config: ConfigurationBase, environment: EnvironmentBase):
|
def __init__(self, config: ConfigurationBase):
|
||||||
ApplicationRuntimeBase.__init__(self)
|
ApplicationRuntimeBase.__init__(self)
|
||||||
|
|
||||||
self._environment = environment
|
|
||||||
self._app_configuration = config
|
self._app_configuration = config
|
||||||
self._start_time: datetime = datetime.now()
|
self._start_time: datetime = datetime.now()
|
||||||
self._end_time: datetime = datetime.now()
|
self._end_time: datetime = datetime.now()
|
||||||
|
|
||||||
@property
|
|
||||||
def environment(self) -> EnvironmentBase:
|
|
||||||
return self._environment
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def configuration(self) -> ConfigurationBase:
|
def configuration(self) -> ConfigurationBase:
|
||||||
return self._app_configuration
|
return self._app_configuration
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# imports:
|
# imports:
|
||||||
from .application_host_base import ApplicationHostBase
|
|
||||||
from .environment_base import EnvironmentBase
|
|
||||||
from .application_base import ApplicationBase
|
from .application_base import ApplicationBase
|
||||||
|
from .application_host_base import ApplicationHostBase
|
||||||
|
from .application_runtime_base import ApplicationRuntimeBase
|
||||||
|
@ -2,7 +2,6 @@ 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.hosting.base.environment_base import EnvironmentBase
|
|
||||||
from sh_edraft.service.base.service_provider_base import ServiceProviderBase
|
from sh_edraft.service.base.service_provider_base import ServiceProviderBase
|
||||||
|
|
||||||
|
|
||||||
@ -10,19 +9,11 @@ class ApplicationHostBase(ABC):
|
|||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def __init__(self): pass
|
def __init__(self): pass
|
||||||
|
|
||||||
@property
|
|
||||||
@abstractmethod
|
|
||||||
def name(self) -> str: pass
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def configuration(self) -> ConfigurationBase: pass
|
def configuration(self) -> ConfigurationBase: pass
|
||||||
|
|
||||||
@property
|
|
||||||
@abstractmethod
|
|
||||||
def environment(self) -> EnvironmentBase: pass
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def application_runtime(self) -> ApplicationRuntimeBase: pass
|
def application_runtime(self) -> ApplicationRuntimeBase: pass
|
||||||
|
@ -2,7 +2,6 @@ from abc import ABC, abstractmethod
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from sh_edraft.configuration.base.configuration_base import ConfigurationBase
|
from sh_edraft.configuration.base.configuration_base import ConfigurationBase
|
||||||
from sh_edraft.hosting.base.environment_base import EnvironmentBase
|
|
||||||
|
|
||||||
|
|
||||||
class ApplicationRuntimeBase(ABC):
|
class ApplicationRuntimeBase(ABC):
|
||||||
@ -10,10 +9,6 @@ class ApplicationRuntimeBase(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def __init__(self): pass
|
def __init__(self): pass
|
||||||
|
|
||||||
@property
|
|
||||||
@abstractmethod
|
|
||||||
def environment(self) -> EnvironmentBase: pass
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def configuration(self) -> ConfigurationBase: pass
|
def configuration(self) -> ConfigurationBase: pass
|
||||||
|
@ -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
|
|
@ -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
|
|
@ -1,2 +1 @@
|
|||||||
# imports:
|
# imports:
|
||||||
from .environment_name import EnvironmentName
|
|
@ -22,6 +22,7 @@ from collections import namedtuple
|
|||||||
# imports:
|
# imports:
|
||||||
from .logging_level import LoggingLevel
|
from .logging_level import LoggingLevel
|
||||||
from .logging_settings import LoggingSettings
|
from .logging_settings import LoggingSettings
|
||||||
|
from .logging_settings_name import LoggingSettingsName
|
||||||
|
|
||||||
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)
|
||||||
|
@ -2,7 +2,7 @@ import traceback
|
|||||||
from typing import Optional
|
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.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.utils.console import Console
|
||||||
from sh_edraft.logging.model.logging_level import LoggingLevel
|
from sh_edraft.logging.model.logging_level import LoggingLevel
|
||||||
|
|
||||||
@ -11,7 +11,6 @@ class LoggingSettings(ConfigurationModelBase):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
ConfigurationModelBase.__init__(self)
|
ConfigurationModelBase.__init__(self)
|
||||||
|
|
||||||
self._path: Optional[str] = None
|
self._path: Optional[str] = None
|
||||||
self._filename: Optional[str] = None
|
self._filename: Optional[str] = None
|
||||||
self._console: Optional[LoggingLevel] = None
|
self._console: Optional[LoggingLevel] = None
|
||||||
@ -51,10 +50,10 @@ class LoggingSettings(ConfigurationModelBase):
|
|||||||
|
|
||||||
def from_dict(self, settings: dict):
|
def from_dict(self, settings: dict):
|
||||||
try:
|
try:
|
||||||
self._path = settings[LogSettingsName.path.value]
|
self._path = settings[LoggingSettingsName.path.value]
|
||||||
self._filename = settings[LogSettingsName.filename.value]
|
self._filename = settings[LoggingSettingsName.filename.value]
|
||||||
self._console = LoggingLevel[settings[LogSettingsName.console_level.value]]
|
self._console = LoggingLevel[settings[LoggingSettingsName.console_level.value]]
|
||||||
self._level = LoggingLevel[settings[LogSettingsName.file_level.value]]
|
self._level = LoggingLevel[settings[LoggingSettingsName.file_level.value]]
|
||||||
except Exception as e:
|
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')
|
Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}', 'red')
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
class LogSettingsName(Enum):
|
class LoggingSettingsName(Enum):
|
||||||
|
|
||||||
log = 'Log'
|
|
||||||
path = 'Path'
|
path = 'Path'
|
||||||
filename = 'Filename'
|
filename = 'Filename'
|
||||||
console_level = 'ConsoleLogLevel'
|
console_level = 'ConsoleLogLevel'
|
||||||
|
@ -33,7 +33,7 @@ class ServiceProvider(ServiceProviderBase):
|
|||||||
params.append(self.get_service(parameter.annotation))
|
params.append(self.get_service(parameter.annotation))
|
||||||
|
|
||||||
elif issubclass(parameter.annotation, ConfigurationModelBase):
|
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)
|
return service(*params)
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ from sh_edraft.utils.console import Console
|
|||||||
class TimeFormatSettings(ConfigurationModelBase):
|
class TimeFormatSettings(ConfigurationModelBase):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
ConfigurationModelBase.__init__(self)
|
||||||
self._date_format: Optional[str] = None
|
self._date_format: Optional[str] = None
|
||||||
self._time_format: Optional[str] = None
|
self._time_format: Optional[str] = None
|
||||||
self._date_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_format = settings[TimeFormatSettingsNames.date_time_format.value]
|
||||||
self._date_time_log_format = settings[TimeFormatSettingsNames.date_time_log_format.value]
|
self._date_time_log_format = settings[TimeFormatSettingsNames.date_time_log_format.value]
|
||||||
except Exception as e:
|
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')
|
Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}', 'red')
|
||||||
|
@ -3,7 +3,6 @@ from enum import Enum
|
|||||||
|
|
||||||
class TimeFormatSettingsNames(Enum):
|
class TimeFormatSettingsNames(Enum):
|
||||||
|
|
||||||
formats = 'TimeFormats'
|
|
||||||
date_format = 'DateFormat'
|
date_format = 'DateFormat'
|
||||||
time_format = 'TimeFormat'
|
time_format = 'TimeFormat'
|
||||||
date_time_format = 'DateTimeFormat'
|
date_time_format = 'DateTimeFormat'
|
||||||
|
8
src/tests_dev/appsettings.development.json
Normal file
8
src/tests_dev/appsettings.development.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"LoggingSettings": {
|
||||||
|
"Path": "logs/",
|
||||||
|
"Filename": "log_$start_time.log",
|
||||||
|
"ConsoleLogLevel": "TRACE",
|
||||||
|
"FileLogLevel": "TRACE"
|
||||||
|
}
|
||||||
|
}
|
15
src/tests_dev/appsettings.json
Normal file
15
src/tests_dev/appsettings.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
@ -3,12 +3,9 @@ from typing import Optional
|
|||||||
from sh_edraft.configuration.base import ConfigurationBase
|
from sh_edraft.configuration.base import ConfigurationBase
|
||||||
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.hosting.model import EnvironmentName
|
|
||||||
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.logging.model import LoggingSettings
|
|
||||||
from sh_edraft.service.base import ServiceProviderBase
|
from sh_edraft.service.base import ServiceProviderBase
|
||||||
from sh_edraft.time.model import TimeFormatSettings
|
|
||||||
|
|
||||||
|
|
||||||
class Program(ApplicationBase):
|
class Program(ApplicationBase):
|
||||||
@ -22,40 +19,28 @@ class Program(ApplicationBase):
|
|||||||
self._configuration: Optional[ConfigurationBase] = None
|
self._configuration: Optional[ConfigurationBase] = None
|
||||||
|
|
||||||
def create_application_host(self):
|
def create_application_host(self):
|
||||||
self._app_host = ApplicationHost('CPL_DEV_Test')
|
self._app_host = ApplicationHost()
|
||||||
self._services = self._app_host.services
|
self._services = self._app_host.services
|
||||||
self._configuration = self._app_host.configuration
|
self._configuration = self._app_host.configuration
|
||||||
|
|
||||||
self._app_host.environment.name = EnvironmentName.development
|
|
||||||
|
|
||||||
def create_configuration(self):
|
def create_configuration(self):
|
||||||
self._configuration.create()
|
self._configuration.create()
|
||||||
|
self._configuration.add_environment_variables('PYTHON_')
|
||||||
log_settings = LoggingSettings()
|
self._configuration.add_environment_variables('CPL_')
|
||||||
log_settings.from_dict({
|
self._configuration.add_argument_variables()
|
||||||
"Path": "logs/",
|
self._configuration.add_json_file(f'appsettings.json')
|
||||||
"Filename": "log_$start_time.log",
|
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.environment_name}.json')
|
||||||
"ConsoleLogLevel": "TRACE",
|
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True)
|
||||||
"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)
|
|
||||||
|
|
||||||
def create_services(self):
|
def create_services(self):
|
||||||
self._services.create()
|
self._services.create()
|
||||||
self._services.add_singleton(LoggerBase, Logger)
|
self._services.add_singleton(LoggerBase, Logger)
|
||||||
logger: Logger = self._services.get_service(LoggerBase)
|
logger: Logger = self._services.get_service(LoggerBase)
|
||||||
logger.create()
|
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):
|
def main(self):
|
||||||
print('RUN')
|
print('RUN')
|
||||||
|
Loading…
Reference in New Issue
Block a user