clapper.config

Functionality to implement python-based config file parsing and loading.

Functions

load(paths[, context, entry_point_group, ...])

Load a set of configuration files, in sequence.

mod_to_context(mod)

Convert the loaded module of load() to a dictionary context.

resource_keys(entry_point_group[, ...])

Read and returns all resources that are registered on a entry-point group.

clapper.config.load(paths, context=None, entry_point_group=None, attribute_name=None)[source]

Load a set of configuration files, in sequence.

This method will load one or more configuration files. Every time a configuration file is loaded, the context (variables) loaded from the previous file is made available, so the new configuration file can override or modify this context.

Parameters:
  • paths (list[str | Path]) – A list or iterable containing paths (relative or absolute) of configuration files that need to be loaded in sequence. Each configuration file is loaded by creating/modifying the context generated after each file readout.

  • context (dict[str, Any] | None) – If provided, start the readout of the first configuration file with the given context. Otherwise, create a new internal context.

  • entry_point_group (str | None) – If provided, it will treat non-existing file paths as entry point names under the entry_point_group name.

  • attribute_name (str | None) – If provided, will look for the attribute_name variable inside the loaded files. Paths ending with some_path:variable_name can override the attribute_name. The entry_point_group must provided as well attribute_name is not None.

Return type:

ModuleType | Any

Returns:

A module representing the resolved context, after loading the provided modules and resolving all variables. If attribute_name is given, the object with the given attribute_name name (or the name provided by user) is returned instead of the module.

Raises:
  • ImportError – If attribute_name is given but the object does not exist in the paths.

  • ValueError – If attribute_name is given but entry_point_group is not given.

clapper.config.mod_to_context(mod)[source]

Convert the loaded module of load() to a dictionary context.

This function removes all the variables that start and end with __.

Parameters:

mod (ModuleType) – a Python module, e.g., as returned by load().

Return type:

dict[str, Any]

Returns:

The context that was in mod, as a dictionary mapping strings to objects.

clapper.config.resource_keys(entry_point_group, exclude_packages=(), strip=('dummy',))[source]

Read and returns all resources that are registered on a entry-point group.

Entry points from the given exclude_packages list are ignored. Notice we are using importlib.metadata to load entry-points, and that that entry point distribution (.dist attribute) was only added to Python in version 3.10. We therefore currently only verify if the named resource does not start with any of the strings provided in exclude_package`.

Parameters:
  • entry_point_group (str) – The entry point group name.

  • exclude_packages (tuple[str, ...]) – List of packages to exclude when finding resources.

  • strip (tuple[str, ...]) – Entrypoint names that start with any value in strip will be ignored.

Return type:

list[str]

Returns:

Alphabetically sorted list of resources matching your query