Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update loaded configuration with command line options #119

Closed
ns-rse opened this issue Dec 13, 2024 · 1 comment
Closed

Update loaded configuration with command line options #119

ns-rse opened this issue Dec 13, 2024 · 1 comment

Comments

@ns-rse
Copy link
Contributor

ns-rse commented Dec 13, 2024

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
@ns-rse ns-rse closed this as completed Dec 13, 2024
@ns-rse
Copy link
Contributor Author

ns-rse commented Dec 13, 2024

Close by #120

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant