Skip to content

save_config

anomaly_match.data_io.save_config

Configuration serialization utilities.

save_config_toml(config, file_path)

Save configuration to TOML file.

Parameters:

Name Type Description Default
config Union[DotMap, Dict[str, Any]]

Configuration object (DotMap) or dictionary to save

required
file_path Union[str, Path]

Path where to save the TOML file

required

Raises:

Type Description
IOError

If file cannot be written.

Source code in anomaly_match/data_io/save_config.py
def save_config_toml(config: Union[DotMap, Dict[str, Any]], file_path: Union[str, Path]) -> None:
    """Save configuration to TOML file.

    Args:
        config: Configuration object (DotMap) or dictionary to save
        file_path: Path where to save the TOML file

    Raises:
        IOError: If file cannot be written.
    """
    file_path = Path(file_path)

    # Convert DotMap to regular dict if needed
    if isinstance(config, DotMap):
        config_dict = config.toDict()
    else:
        config_dict = dict(config)

    # Ensure critical optional fields are present even when None
    # Use string "null" to represent None in TOML since TOML doesn't support None
    for field in _critical_optional_fields():
        if field not in config_dict:
            config_dict[field] = "null"  # Use string representation for None
        elif config_dict[field] is None:
            config_dict[field] = "null"

    # Convert enums to strings for TOML serialization
    config_dict = _convert_enum_to_string(config_dict)

    try:
        with open(file_path, "w") as f:
            toml.dump(config_dict, f)
        logger.debug(f"Saved configuration to TOML file: {file_path}")
    except Exception as e:
        logger.error(f"Failed to save config to TOML: {e}")
        raise IOError(f"Could not save config to {file_path}: {e}")