Module data_request_api.stable.utilities.config

Functions

def load_config() ‑> dict
Expand source code
def load_config() -> dict:
    """Load the configuration file, creating it if necessary.

    Returns:
        dict: The configuration data.

    Raises:
        FileNotFoundError: If the config file does not exist.
        yaml.YAMLError: If the config file is not in the correct format.
        KeyError: If the key is not in the DEFAULT_CONFIG.
        TypeError: If the value is not of the expected type for the key.
        ValueError: If the value is not within the valid values for the key.
    """
    global CONFIG
    if CONFIG == {}:
        try:
            with open(CONFIG_FILE) as f:
                CONFIG = yaml.safe_load(f)
        except FileNotFoundError:
            pass

        # Read configuration must be a dict - if no or an empty file is read,
        #  assign DEFAULT_CONFIG
        if CONFIG == "" or CONFIG is None or CONFIG == {}:
            with open(CONFIG_FILE, "w") as f:
                yaml.dump(DEFAULT_CONFIG, f)
            CONFIG = DEFAULT_CONFIG.copy()
        elif not isinstance(CONFIG, dict):
            raise TypeError(
                f"Config file ('{CONFIG_FILE}') must contain a dictionary"
            )

        # Sanity test for allowed types and values
        for key, value in CONFIG.items():
            _sanity_check(key, value)

        # Ensure all required keys are present and update config file if necessary
        missing_keys = {
            k: v for k, v in DEFAULT_CONFIG.items() if k not in CONFIG
        }
        for key, value in missing_keys.items():
            update_config(key, value)

    return CONFIG

Load the configuration file, creating it if necessary.

Returns

dict
The configuration data.

Raises

FileNotFoundError
If the config file does not exist.
yaml.YAMLError
If the config file is not in the correct format.
KeyError
If the key is not in the DEFAULT_CONFIG.
TypeError
If the value is not of the expected type for the key.
ValueError
If the value is not within the valid values for the key.
def update_config(key, value)
Expand source code
def update_config(key, value):
    """
    Update the configuration with the specified key-value pair.

    Args:
        key (str): The configuration key to update.
        value (Any): The new value for the configuration key. Boolean-like strings
                     ("true", "false") will be converted to actual booleans.

    Raises:
        KeyError: If the key is not in the DEFAULT_CONFIG.
        TypeError: If the value is not of the expected type for the key.
        ValueError: If the value is not within the valid values for the key.

    This function updates the global configuration dictionary with the given key-value
    pair and writes the updated configuration back to the configuration file.
    """
    global CONFIG
    if CONFIG == {}:
        CONFIG = load_config()

    # Convert boolean-like strings to actual booleans
    key = str(key)
    value = str(value)
    if value.lower() in {"true", "false"}:
        value = value.lower() == "true"
    _sanity_check(key, value)

    # Overwrite / set the value
    CONFIG[key] = value

    # Write the updated config back to the file
    with open(CONFIG_FILE, "w") as f:
        yaml.dump(CONFIG, f)

    print(f"Updated {key} to {value} in '{CONFIG_FILE}'.")

Update the configuration with the specified key-value pair.

Args

key : str
The configuration key to update.
value : Any
The new value for the configuration key. Boolean-like strings ("true", "false") will be converted to actual booleans.

Raises

KeyError
If the key is not in the DEFAULT_CONFIG.
TypeError
If the value is not of the expected type for the key.
ValueError
If the value is not within the valid values for the key.

This function updates the global configuration dictionary with the given key-value pair and writes the updated configuration back to the configuration file.