Closed
Description
We load either a default configuration (from isoslam/default_config.yaml
) or a user specified one (via isoslam --config <config_file.yaml>
) but there are command line options for each sub-processor that should over-ride the
defaults.
To which end we need a function that updates the dictionary loaded from the YAML configuration with the options
specified on the command line, otherwise the command line options are ignored.
The following is just such a function which uses recursion to update dictionaries and also converts base_dir
and
output_dir
to Path
using the associated helper function.
def convert_path(path: str | Path) -> Path:
"""
Ensure path is Path object.
Parameters
----------
path : str | Path
Path to be converted.
Returns
-------
Path
Pathlib object of path.
"""
return Path().cwd() if path == "./" else Path(path).expanduser()
def update_config(config: dict, args: dict | Namespace) -> dict:
"""
Update the configuration with any arguments.
Parameters
----------
config : dict
Dictionary of configuration (typically read from YAML file specified with '-c/--config <filename>').
args : Namespace
Command line arguments.
Returns
-------
dict
Dictionary updated with command arguments.
"""
args = vars(args) if isinstance(args, Namespace) else args
config_keys = config.keys()
for arg_key, arg_value in args.items():
if isinstance(arg_value, dict):
update_config(config, arg_value)
else:
if arg_key in config_keys and arg_value is not None:
original_value = config[arg_key]
config[arg_key] = arg_value
LOGGER.debug(f"Updated config config[{arg_key}] : {original_value} > {arg_value} ")
if "base_dir" in config.keys():
config["base_dir"] = convert_path(config["base_dir"])
if "output_dir" in config.keys():
config["output_dir"] = convert_path(config["output_dir"])
return config
Metadata
Metadata
Assignees
Labels
No labels