mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 14:14:22 +01:00
Moves extend paths from tools to ConfigLoader
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
from common.tools import extend_paths
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigLoader:
|
class ConfigLoader:
|
||||||
"""
|
"""
|
||||||
@@ -71,7 +69,7 @@ class ConfigLoader:
|
|||||||
self.conf_fields[k]["value"] = v
|
self.conf_fields[k]["value"] = v
|
||||||
|
|
||||||
# Extend relative paths
|
# Extend relative paths
|
||||||
extend_paths(self.data_dir, self.conf_fields)
|
self.extend_paths()
|
||||||
|
|
||||||
# Sanity check fields and build config dictionary
|
# Sanity check fields and build config dictionary
|
||||||
config = self.create_config_dict()
|
config = self.create_config_dict()
|
||||||
@@ -103,3 +101,14 @@ class ConfigLoader:
|
|||||||
raise ValueError(err_msg)
|
raise ValueError(err_msg)
|
||||||
|
|
||||||
return conf_dict
|
return conf_dict
|
||||||
|
|
||||||
|
def extend_paths(self):
|
||||||
|
"""
|
||||||
|
Extends the relative paths of the ``conf_fields`` dictionary with ``data_dir``.
|
||||||
|
|
||||||
|
If an absolute path is given, it'll remain the same.
|
||||||
|
"""
|
||||||
|
|
||||||
|
for key, field in self.conf_fields.items():
|
||||||
|
if field.get("path") is True and isinstance(field.get("value"), str):
|
||||||
|
self.conf_fields[key]["value"] = os.path.join(self.data_dir, self.conf_fields[key]["value"])
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
from common.constants import LOCATOR_LEN_HEX
|
from common.constants import LOCATOR_LEN_HEX
|
||||||
|
|
||||||
|
|
||||||
@@ -50,26 +51,7 @@ def setup_data_folder(data_folder):
|
|||||||
data_folder (:obj:`str`): the path of the folder
|
data_folder (:obj:`str`): the path of the folder
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not os.path.isdir(data_folder):
|
Path(data_folder).mkdir(parents=True, exist_ok=True)
|
||||||
os.makedirs(data_folder, exist_ok=True)
|
|
||||||
|
|
||||||
|
|
||||||
def extend_paths(base_path, config_fields):
|
|
||||||
"""
|
|
||||||
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.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
base_path (:obj:`str`): the base path to prepend the other paths.
|
|
||||||
config_fields (:obj:`dict`): a dictionary of configuration fields containing a ``path`` flag, as follows:
|
|
||||||
{"field0": {"value": value_from_conf_file, "path": True, ...}}
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
for key, field in config_fields.items():
|
|
||||||
if field.get("path") is True:
|
|
||||||
config_fields[key]["value"] = os.path.join(base_path, config_fields[key]["value"])
|
|
||||||
|
|
||||||
|
|
||||||
def setup_logging(log_file_path, log_name_prefix):
|
def setup_logging(log_file_path, log_name_prefix):
|
||||||
|
|||||||
@@ -219,3 +219,26 @@ def test_create_config_dict_invalid_type():
|
|||||||
|
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
conf_loader.create_config_dict()
|
conf_loader.create_config_dict()
|
||||||
|
|
||||||
|
|
||||||
|
def test_extend_paths():
|
||||||
|
# Test that only items with the path flag are extended
|
||||||
|
foo_data_dir = "foo/"
|
||||||
|
default_conf_copy = deepcopy(DEFAULT_CONF)
|
||||||
|
|
||||||
|
conf_loader = ConfigLoader(foo_data_dir, conf_file_name, default_conf_copy, {})
|
||||||
|
conf_loader.extend_paths()
|
||||||
|
|
||||||
|
for k, field in conf_loader.conf_fields.items():
|
||||||
|
if isinstance(field.get("value"), str):
|
||||||
|
if field.get("path") is True:
|
||||||
|
assert conf_loader.data_dir in field.get("value")
|
||||||
|
else:
|
||||||
|
assert conf_loader.data_dir not in field.get("value")
|
||||||
|
|
||||||
|
# Check that absolute paths are not extended
|
||||||
|
absolute_path = "/foo/var"
|
||||||
|
conf_loader.conf_fields["ABSOLUTE_PATH"] = {"value": absolute_path, "type": str, "path": True}
|
||||||
|
conf_loader.extend_paths()
|
||||||
|
|
||||||
|
assert conf_loader.conf_fields["ABSOLUTE_PATH"]["value"] == absolute_path
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ from common.tools import (
|
|||||||
check_locator_format,
|
check_locator_format,
|
||||||
compute_locator,
|
compute_locator,
|
||||||
setup_data_folder,
|
setup_data_folder,
|
||||||
extend_paths,
|
|
||||||
setup_logging,
|
setup_logging,
|
||||||
)
|
)
|
||||||
from test.common.unit.conftest import get_random_value_hex
|
from test.common.unit.conftest import get_random_value_hex
|
||||||
@@ -66,23 +65,6 @@ def test_setup_data_folder():
|
|||||||
os.rmdir(test_folder)
|
os.rmdir(test_folder)
|
||||||
|
|
||||||
|
|
||||||
def test_extend_paths():
|
|
||||||
# Test that only items with the path flag are extended
|
|
||||||
config_fields = {
|
|
||||||
"foo": {"value": "foofoo"},
|
|
||||||
"var": {"value": "varvar", "path": True},
|
|
||||||
"foovar": {"value": "foovarfoovar"},
|
|
||||||
}
|
|
||||||
base_path = "base_path/"
|
|
||||||
extend_paths(base_path, config_fields)
|
|
||||||
|
|
||||||
for k, field in config_fields.items():
|
|
||||||
if field.get("path") is True:
|
|
||||||
assert base_path in field.get("value")
|
|
||||||
else:
|
|
||||||
assert base_path not in field.get("value")
|
|
||||||
|
|
||||||
|
|
||||||
def test_setup_logging():
|
def test_setup_logging():
|
||||||
# Check that setup_logging creates two new logs for every prefix
|
# Check that setup_logging creates two new logs for every prefix
|
||||||
prefix = "foo"
|
prefix = "foo"
|
||||||
|
|||||||
Reference in New Issue
Block a user