Improved configuration

This commit is contained in:
Sven Heidemann 2021-03-04 18:12:51 +01:00
parent 01ef965180
commit 6452226b50
2 changed files with 13 additions and 7 deletions

View File

@ -134,7 +134,7 @@ class Configuration(ConfigurationABC):
exit()
def add_json_file(self, name: str, optional: bool = None):
def add_json_file(self, name: str, optional: bool = None, output: bool = False):
if self._hosting_environment.content_root_path.endswith('/') and not name.startswith('/'):
file_path = f'{self._hosting_environment.content_root_path}{name}'
else:
@ -142,13 +142,17 @@ class Configuration(ConfigurationABC):
if not os.path.isfile(file_path):
if not optional:
if output:
self._print_error(__name__, f'File not found: {file_path}')
exit()
if output:
self._print_warn(__name__, f'Not Loaded config file: {file_path}')
return None
config_from_file = self._load_json_file(file_path)
config_from_file = self._load_json_file(file_path, output)
for sub in ConfigurationModelABC.__subclasses__():
for key, value in config_from_file.items():
if sub.__name__ == key:
@ -156,13 +160,15 @@ class Configuration(ConfigurationABC):
configuration.from_dict(value)
self.add_configuration(sub, configuration)
def _load_json_file(self, file: str) -> dict:
def _load_json_file(self, file: str, output: bool) -> dict:
try:
# open config file, create if not exists
with open(file, encoding='utf-8') as cfg:
# load json
json_cfg = json.load(cfg)
if output:
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}')

View File

@ -37,7 +37,7 @@ class ConfigurationABC(ABC):
def add_console_arguments(self): pass
@abstractmethod
def add_json_file(self, name: str, optional: bool = None): pass
def add_json_file(self, name: str, optional: bool = None, output: bool = False): pass
@abstractmethod
def add_configuration(self, key_type: type, value: object): pass