"""
Provides helper functions for working with TOML configurations.
This module includes utilities for loading TOML files using the built-in
`tomllib` and for converting them into OmegaConf DictConfig objects.
"""
# =============================================================================
# METADATA
# =============================================================================
__author__ = "Yeremia Gunawan Adhisantoso"
__email__ = "adhisant@tnt.uni-hannover.de"
__license__ = "Clear BSD"
__version__ = "1.0.0"
# =============================================================================
# STANDARD LIBRARY IMPORTS
# =============================================================================
import typing as t
import tomllib
from pathlib import Path
# =============================================================================
# THIRD-PARTY IMPORTS
# =============================================================================
from omegaconf import DictConfig, OmegaConf
# =============================================================================
# HELPER FUNCTIONS
# =============================================================================
[docs]
def load_toml(path: t.Union[str, Path]) -> dict[str, t.Any]:
"""
Loads a TOML configuration file into a standard Python dictionary.
Parameters
----------
path : t.Union[str, Path]
The path to the TOML file.
Returns
-------
dict
The configuration loaded from the TOML file.
Raises
------
FileNotFoundError
If the file does not exist.
tomllib.TOMLDecodeError
If the file is not a valid TOML file.
"""
with open(path, "rb") as f:
return tomllib.load(f)
[docs]
def load_toml_as_dictconfig(path: t.Union[str, Path]) -> DictConfig:
"""
Loads a TOML configuration file and converts it to an OmegaConf DictConfig.
This is useful for integrating TOML-based configs with existing
Hydra/OmegaConf-based codebases.
Parameters
----------
path : t.Union[str, Path]
The path to the TOML file.
Returns
-------
DictConfig
The configuration as a DictConfig object.
"""
data = load_toml(path)
return OmegaConf.create(data)