Config#

Parsers#

class heptools.config.ConfigParser[source]#

A customizable config parser.

Parameters:
  • nested (bool, optional, default=True) – Parse dot-separated keys into nested dicts.

  • tag_parsers (dict[str, Optional[TagParser]], optional) – Custom tags.

  • extend_methods (dict[str, ExtendMethod], optional) – Custom <extend> methods.

  • patch_actions (dict[str, PatchAction], optional) – Custom patch actions.

io#

A config file loader with a shared cache.

Type:

FileLoader

__call__(*path_or_dict, result=None)[source]#

Load configs from multiple sources.

Parameters:
  • *path_or_dict (ConfigSource) – Dictionaries or paths to config files.

  • result (dict, optional) – If provided, the configs will be loaded into this dict.

Returns:

dict – The loaded configs.

Protocols#

heptools.config.ConfigSource[source]#

A path to the config file or a nested dict.

Type:

str, PathLike, dict

heptools.config.ExtendMethod[source]#

A method to merge two values into one.

Type:

Callable[[Any, Any], Any]

class heptools.config.TagParser[source]#

Tag parser protocol.

__call__(*, key, value, tag, tags, local, path)[source]#
Parameters:
  • key (str, optional) – The current key.

  • value (Any, optional) – The current value .

  • tag (str, optional) – The value of the current tag.

  • tags (dict[str, Optional[str]], optional) – All parsed tags of the current key.

  • local (dict, optional) – All parsed items in the current dictionary.

  • path (str or None, optional) – The path to the current config file.

Returns:

tuple[str, Any] – The key and value after parsing.

Customization#

class heptools.config.FileLoader(cache=True)[source]#

A module to load and deserialize objects from URL.

Parameters:

cache (bool, optional, default=True) – Enable the URL based cache. When an extension is registered or unregistered, the associated cache will be cleared.

load(url, use_cache=True, use_buffer=True)[source]#

Load objects from URL.

Parameters:
  • url (str) – URL to an object.

  • use_cache (bool, optional, default=True) – If True, use the cache if exists.

  • use_buffer (bool, optional, default=True) – If True, the whole file will be read into memory before deserialization.

clear_cache()[source]#

Clear all cache.

register(handler, *extensions)[source]#

Register a handler for file extensions. This method can be used as a decorator.

Parameters:
  • handler (Callable, optional) – Handler function. If not provided, will return a decorator.

  • *extensions (str) – File extensions to register.

unregister(*extensions)[source]#

Unregister handlers for file extensions.

Parameters:

*extensions (str) – File extensions to unregister.

registered()[source]#

List all registered file extensions.