coma.hooks.config_hook
Config hook utilities, factories, and defaults.
- single_load_and_write_factory(config_id: str, *, parser_attr_name: Optional[str] = None, default_file_path: Optional[str] = None, default_ext: coma.config.io.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_id
from 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_path
is used as a default. Ifdefault_file_path
isNone
, a sensible default is derived fromconfig_id
instead.In any case, if the provided or derived file path has no file extension,
default_ext
is 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_args
attribute 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 aFileNotFoundError
if 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_fnf
isFalse
, thenwrite_on_fnf
indicates whether to write the config object to the provided fileresolve (bool) – If about to write a config object to file, then
resolve
indicates whether the underlyingomegaconf
handler attempts to resolve variable interpolation beforehand
- Returns
A config hook
- multi_load_and_write_factory(*, default_ext: coma.config.io.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