coma.hooks.config_hook
Config hook utilities, factories, and defaults.
- single_load_and_write_factory(config_id: str, *, parser_attr_name: str | None = None, default_file_path: str | None = None, default_ext: Extension = Extension.YAML, raise_on_fnf: bool = False, write_on_fnf: bool = True, resolve: bool = False) Callable[[...], Dict[str, Any]][source]
Factory for creating a config hook that initializes a config object.
The created config hook has the following behaviour:
First, an attempt is made to load the config object corresponding to
config_idfrom file.Note
If a file path is provided as a command line argument (assuming the presence of
multi_config()or equivalent functionality), that path is used. Otherwise,default_file_pathis used as a default. Ifdefault_file_pathisNone, a sensible default is derived fromconfig_idinstead.In any case, if the provided or derived file path has no file extension,
default_extis used as a default extension.If loading the file fails due to a
FileNotFoundError, then:The created config hook raises:
Example
Fail fast when encountering a
FileNotFoundError:coma.initiate(..., config_hook=single_factory(..., raise_on_fnf=True))
- Parameters:
config_id (str) – A config identifier
parser_attr_name (str) – The
known_argsattribute representing this config’s file path parser argument. IfNone, a sensible default is derived fromdefault_dest().default_file_path (str) – An optional default value for the config file path. If
None, a sensible default is derived fromdefault_default().default_ext (coma.config.io.Extension) – The extension to use when the provided file path lacks one
raise_on_fnf (bool) – If
True, raises aFileNotFoundErrorif the config file was not found. IfFalse, a config object with default values is initialized instead of failing outright.write_on_fnf (bool) – If the config file was not found and
raise_on_fnfisFalse, thenwrite_on_fnfindicates whether to write the config object to the provided fileresolve (bool) – If about to write a config object to file, then
resolveindicates whether the underlyingomegaconfhandler attempts to resolve variable interpolation beforehand
- Returns:
A config hook
- multi_load_and_write_factory(*, default_ext: Extension = Extension.YAML, raise_on_fnf: bool = False, write_on_fnf: bool = True, resolve: bool = False) Callable[[...], Dict[str, Any]][source]
Factory for creating a config hook that is a sequence of single factory calls.
Equivalent to calling
single_load_and_write_factory()for each config with the other arguments passed along. Seesingle_load_and_write_factory()for details.- Returns:
A config hook