-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathconfig_manager.py
More file actions
75 lines (63 loc) · 2.51 KB
/
Copy pathconfig_manager.py
File metadata and controls
75 lines (63 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import traceback
from typing import Union
import yaml
from pydantic import ValidationError
from GANDLF.configuration.parameters_config import Parameters
from GANDLF.configuration.exclude_parameters import exclude_parameters
from GANDLF.configuration.utils import handle_configuration_errors
def _parseConfig(
config_file_path: Union[str, dict], version_check_flag: bool = True
) -> None:
"""
This function parses the configuration file and returns a dictionary of parameters.
Args:
config_file_path (Union[str, dict]): The filename of the configuration file.
version_check_flag (bool, optional): Whether to check the version in configuration file. Defaults to True.
Returns:
dict: The parameter dictionary.
"""
params = config_file_path
try:
if not isinstance(config_file_path, dict):
params = yaml.safe_load(open(config_file_path, "r"))
except yaml.YAMLError as e:
# this is a special case for config files with panoptica parameters
from panoptica.utils.config import _load_yaml
params = _load_yaml(config_file_path)
return params
def ConfigManager(
config_file_path: Union[str, dict], version_check_flag: bool = True
) -> dict:
"""
This function parses the configuration file and returns a dictionary of parameters.
Args:
config_file_path (Union[str, dict]): The filename of the configuration file.
version_check_flag (bool, optional): Whether to check the version in configuration file. Defaults to True.
Returns:
dict: The parameter dictionary.
"""
try:
parameters_config = Parameters(
**_parseConfig(config_file_path, version_check_flag)
)
parameters = parameters_config.model_dump(
exclude={
field
for field in exclude_parameters
if getattr(parameters_config, field) is None
}
)
return parameters
except Exception as e:
if isinstance(e, ValidationError):
handle_configuration_errors(e)
raise
## todo: ensure logging captures assertion errors
else:
assert (
False
), f"Config parsing failed: {config_file_path=}, {version_check_flag=}, Exception: {str(e)}, {traceback.format_exc()}"
# logging.error(
# f"gandlf config parsing failed: {config_file_path=}, {version_check_flag=}, Exception: {str(e)}, {traceback.format_exc()}"
# )
# raise