mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 14:14:22 +01:00
Updates teos to work with the new conf file format and redefines how objects are built
Configuration parameters are now load from a .conf file (see template.conf as an example). The code has 3 config levels: default (teos/__init__.py), config file (<data_dir>/teos.conf) and command line. Most of the config parameters are only modificable trough the config file for now. The priority order is: command line, config file, default. Objects that need config parameters are now built in teosd instead of inside other classes. Therefore teosd acts like a factory and config parameters don't have to be passed around between objects. This also implies that a lot of the object creation logic has been changed. This should simplify unit testing.
This commit is contained in:
@@ -54,42 +54,9 @@ def setup_data_folder(data_folder):
|
||||
os.makedirs(data_folder, exist_ok=True)
|
||||
|
||||
|
||||
def check_conf_fields(conf_fields):
|
||||
"""
|
||||
Checks that the provided configuration field have the right type.
|
||||
|
||||
Args:
|
||||
conf_fields (:obj:`dict`): a dictionary populated with the configuration file params and the expected types.
|
||||
The format is as follows:
|
||||
|
||||
{"field0": {"value": value_from_conf_file, "type": expected_type, ...}}
|
||||
|
||||
Returns:
|
||||
:obj:`dict`: A dictionary with the same keys as the provided one, but containing only the "value" field as value
|
||||
if the provided ``conf_fields`` where correct.
|
||||
|
||||
Raises:
|
||||
ValueError: If any of the dictionary elements does not have the expected type
|
||||
"""
|
||||
|
||||
conf_dict = {}
|
||||
|
||||
for field in conf_fields:
|
||||
value = conf_fields[field]["value"]
|
||||
correct_type = conf_fields[field]["type"]
|
||||
|
||||
if (value is not None) and isinstance(value, correct_type):
|
||||
conf_dict[field] = value
|
||||
else:
|
||||
err_msg = "{} variable in config is of the wrong type".format(field)
|
||||
raise ValueError(err_msg)
|
||||
|
||||
return conf_dict
|
||||
|
||||
|
||||
def extend_paths(base_path, config_fields):
|
||||
"""
|
||||
Extends the relative paths of a given ``config_fields`` dictionary with a diven ``base_path``.
|
||||
Extends the relative paths of a given ``config_fields`` dictionary with a given ``base_path``.
|
||||
|
||||
Paths in the config file are based on DATA_PATH, this method extends them so they are all absolute.
|
||||
|
||||
@@ -98,16 +65,12 @@ def extend_paths(base_path, config_fields):
|
||||
config_fields (:obj:`dict`): a dictionary of configuration fields containing a ``path`` flag, as follows:
|
||||
{"field0": {"value": value_from_conf_file, "path": True, ...}}
|
||||
|
||||
Returns:
|
||||
:obj:`dict`: A ``config_fields`` with the flagged paths updated.
|
||||
"""
|
||||
|
||||
for key, field in config_fields.items():
|
||||
if field.get("path") is True:
|
||||
config_fields[key]["value"] = base_path + config_fields[key]["value"]
|
||||
|
||||
return config_fields
|
||||
|
||||
|
||||
def setup_logging(log_file_path, log_name_prefix):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user