nat.utils.io.yaml_tools#

Attributes#

Functions#

_interpolate_variables(→ str | int | float | bool | None)

Interpolate variables in a string with the format ${VAR:-default_value}.

deep_merge(→ dict)

Recursively merge override dictionary into base dictionary.

yaml_load(→ dict)

Load a YAML file and interpolate variables in the format

yaml_loads(→ dict)

Load a YAML string and interpolate variables in the format

yaml_dump(→ None)

Dump a configuration dictionary to a YAML file.

yaml_dumps(→ str)

Dump a configuration dictionary to a YAML string.

Module Contents#

logger#
_interpolate_variables(
value: str | int | float | bool | None,
) str | int | float | bool | None#

Interpolate variables in a string with the format ${VAR:-default_value}. If the variable is not set, the default value will be used. If no default value is provided, an empty string will be used.

Args:

value (str | int | float | bool | None): The value to interpolate variables in.

Returns:

str | int | float | bool | None: The value with variables interpolated.

deep_merge(base: dict, override: dict) dict#

Recursively merge override dictionary into base dictionary.

Args:

base (dict): The base configuration dictionary. override (dict): The override configuration dictionary.

Returns:

dict: The merged configuration dictionary.

yaml_load(
config_path: nat.utils.type_utils.StrPath,
_visited: set[pathlib.Path] | None = None,
) dict#

Load a YAML file and interpolate variables in the format ${VAR:-default_value}.

If the YAML file contains a “base” key, the file at that path will be loaded first, and the current config will be merged on top of it. This enables config inheritance to reduce duplication across similar configuration files.

Args:

config_path (StrPath): The path to the YAML file to load. _visited (set[Path] | None): Internal parameter for circular dependency detection.

Returns:

dict: The processed configuration dictionary.

Raises:

TypeError: If the “base” key is not a string. FileNotFoundError: If the base configuration file does not exist. ValueError: If a circular dependency is detected in configuration inheritance.

yaml_loads(config: str) dict#

Load a YAML string and interpolate variables in the format ${VAR:-default_value}.

Args:

config (str): The YAML string to load.

Returns:

dict: The processed configuration dictionary.

yaml_dump(config: dict, fp: TextIO) None#

Dump a configuration dictionary to a YAML file.

Args:

config (dict): The configuration dictionary to dump. fp (typing.TextIO): The file pointer to write the YAML to.

yaml_dumps(config: dict) str#

Dump a configuration dictionary to a YAML string.

Args:

config (dict): The configuration dictionary to dump.

Returns:

str: The YAML string.