config

This module provides utilities for managing configuration settings in Anemoi. It includes functions to load, merge, and access configurations.

By default configuration files are loaded from ~/.config/anemoi/settings.toml.

An override path can be specified using the environment variable ANEMOI_CONFIG_OVERRIDE_PATH.

class anemoi.utils.config.DotDict(*args, **kwargs)

Bases: dict

A dictionary that allows access to its keys as attributes.

>>> d = DotDict({"a": 1, "b": {"c": 2}})
>>> d.a
1
>>> d.b.c
2
>>> d.b = 3
>>> d.b
3

The class is recursive, so nested dictionaries are also DotDicts.

The DotDict class has the same constructor as the dict class.

>>> d = DotDict(a=1, b=2)
static convert_to_nested_dot_dict(value: Any) Any

Convert nested dicts to DotDict recursively.

Parameters:

value (Any) – The value to convert

Returns:

Converted value with nested dicts as DotDict

Return type:

Any

classmethod from_file(path: str) DotDict

Create a DotDict from a file.

Parameters:

path (str) – The path to the file.

Returns:

The created DotDict.

Return type:

DotDict

classmethod from_yaml_file(path: str) DotDict

Create a DotDict from a YAML file.

Parameters:

path (str) – The path to the YAML file.

Returns:

The created DotDict.

Return type:

DotDict

classmethod from_json_file(path: str) DotDict

Create a DotDict from a JSON file.

Parameters:

path (str) – The path to the JSON file.

Returns:

The created DotDict.

Return type:

DotDict

classmethod from_toml_file(path: str) DotDict

Create a DotDict from a TOML file.

Parameters:

path (str) – The path to the TOML file.

Returns:

The created DotDict.

Return type:

DotDict

anemoi.utils.config.is_omegaconf_dict(value: Any) bool

Check if a value is an OmegaConf DictConfig.

Parameters:

value (Any) – The value to check.

Returns:

True if the value is a DictConfig, False otherwise.

Return type:

bool

anemoi.utils.config.is_omegaconf_list(value: Any) bool

Check if a value is an OmegaConf ListConfig.

Parameters:

value (Any) – The value to check.

Returns:

True if the value is a ListConfig, False otherwise.

Return type:

bool

anemoi.utils.config.config_path(name: str = 'settings.toml') str

Get the path to a configuration file.

Parameters:

name (str, optional) – The name of the configuration file, by default “settings.toml”.

Returns:

The path to the configuration file.

Return type:

str

anemoi.utils.config.load_any_dict_format(path: str) dict

Load a configuration file in any supported format: JSON, YAML and TOML.

Parameters:

path (str) – The path to the configuration file.

Returns:

The decoded configuration file.

Return type:

dict

anemoi.utils.config.save_config(name: str, data: Any) None

Save a configuration file.

Parameters:
  • name (str) – The name of the configuration file to save.

  • data (Any) – The data to save.

anemoi.utils.config.load_config(name: str = 'settings.toml', secrets: str | list[str] | None = None, defaults: str | dict | None = None) DotDict | str

Read a configuration file.

Parameters:
  • name (str, optional) – The name of the config file to read, by default “settings.toml”

  • secrets (str or list, optional) – The name of the secrets file, by default None

  • defaults (str or dict, optional) – The name of the defaults file, by default None

Returns:

Return DotDict if it is a dictionary, otherwise the raw data

Return type:

DotDict or str

anemoi.utils.config.load_raw_config(name: str, default: Any = None) DotDict | str

Load a raw configuration file.

Parameters:
  • name (str) – The name of the configuration file.

  • default (Any, optional) – The default value if the file does not exist, by default None.

Returns:

The loaded configuration or the default value.

Return type:

DotDict or str

anemoi.utils.config.check_config_mode(name: str = 'settings.toml', secrets_name: str = None, secrets: list[str] = None) None

Check that a configuration file is secure.

Parameters:
  • name (str, optional) – The name of the configuration file, by default “settings.toml”

  • secrets_name (str, optional) – The name of the secrets file, by default None

  • secrets (list, optional) – The list of secrets to check, by default None

Raises:

SystemError – If the configuration file is not secure.

anemoi.utils.config.find(metadata: dict | list, what: str, result: list = None, *, select: callable = None) list

Find all occurrences of a key in a nested dictionary or list with an optional selector.

Parameters:
  • metadata (dict or list) – The metadata to search.

  • what (str) – The key to search for.

  • result (list, optional) – The list to store results, by default None.

  • select (callable, optional) – A function to filter the results, by default None.

Returns:

The list of found values.

Return type:

list

anemoi.utils.config.merge_configs(*configs: dict) dict

Merge multiple configuration dictionaries.

Parameters:

*configs (dict) – The configuration dictionaries to merge.

Returns:

The merged configuration dictionary.

Return type:

dict