from pathlib import Path
import logging
import yaml


def params_from_yaml(args):
    """Extract the parameters for preparation from a yaml file and return a dict"""
    # Check the path exists
    try:
        config_file_path = Path(args.config)
        assert config_file_path.exists()
    except Exception:
        logging.error(f"Could not find config file at {args.config}")
        raise

    # Load the data from the config file
    try:
        with open(config_file_path, "r") as f:
            params = yaml.safe_load(f)
    except Exception:
        logging.error(
            f"Could not extract parameters from yaml file at {config_file_path}"
        )
        raise

    if "verbose" not in params.keys():
        params["verbose"] = True

    return params