coma.config.io¶
Utilities for serializing configs to file.
- class Extension(value)[source]¶
Supported config serialization file extensions:
Value
Meaning
YAML".yaml"YML".yml"JSON".json"
- maybe_add_ext(file_path: str, ext: Extension) str[source]¶
If
file_pathlacks a file extension, appendsext.
- is_yaml_ext(file_path: str, *, strict: bool = False) bool[source]¶
Returns whether
file_pathhas a YAML-like file extension.
- is_yml_ext(file_path: str, *, strict: bool = False) bool[source]¶
Returns whether
file_pathhas a YAML-like file extension.
- is_ext(file_path: str, which: Extension, *alts: Extension, strict: bool = False) bool[source]¶
Returns whether
file_pathhas a file extension from a specific set.- Parameters:
- Returns:
Whether
file_pathhas a file extension from a specific set.- Return type:
- initialize(config: Config, file_path: str | None = None, base_instance_key: str = 'BASE', file_instance_key: str = 'FILE') None[source]¶
Initializes a config object and possibly updates its attributes from file.
Initializes a base config object from
configusingomegaconf. Iffile_pathis notNone, attempts to also load a config object from file. If that succeeds, then attempts to update the base config object’s attributes with attributes of the config object loaded from file.If either step has an already cached instance (based on the given instance keys), the cached value is used instead.
- Parameters:
config (
Config) – The config to initialize.file_path (
str, optional) – If not None, file path from which the base instance’s attributes can be updated.base_instance_key (
InstanceKey) – The instance key of the base config instance.file_instance_key (
InstanceKey) – The instance key of the config instance updated from file data.
- Returns:
The
configobject is updated in-place.- Return type:
None
- Raises:
ValueError – If
file_pathhas an unsupported file extension.IOError – If there are issues relating to reading from
file_path.Others – As may be raised by the underlying
omegaconfhandler.
- write(config: Config, file_path: str, *, key: str | None = None, resolve: bool = False) None[source]¶
Serializes a config to file.
- Parameters:
config (
Config) – The config to serialize.file_path (str) – A file path for serializing
config.key (
InstanceKey, optional) – The specificconfiginstance to serialize. IfNone, the latest instance is used.resolve (bool) – Whether the underlying
omegaconfhandler should resolve variable interpolation before serializing.
- Raises:
ValueError – If
file_pathhas an unsupported file extension.IOError – If there are issues relating to writing to
file_path.Others – As may be raised by the underlying
omegaconfhandler.
- class PersistenceManager(default_extension: Extension = Extension.YAML)[source]¶
- register(config_id: str, extension: Extension | None = None, *names_or_flags: str, **kwargs: Any) PersistenceManager[source]¶
Registers parameters defining a file path argument for
config_idthat can later be added to anargparse.ArgumentParserviaadd_path_argument().- Parameters:
config_id (
ConfigID) – The config identifier for which to register file path argument parameters.extension (
Extension, optional) – The extension to use if the provided file path lacks one. IfNone, defaults todefault_extension.*names_or_flags (str) – Passed to add_argument(). A sensible default is used if empty.
**kwargs (Any) – Passed to add_argument(). A sensible default is used for many parameters if missing. For details, see
add_path_argument()
Note
Registering a particular
config_iddoes not guarantee/force that the config will be serialized, but rather only explicitly determines the parameters that get passed to add_argument() (instead of relying on the sensible defaults that are otherwise provided).- Returns:
This persistence manager (to enable fluent interfacing).
- Return type:
See also
- add_path_argument(parser: ArgumentParser, config_id: str) None[source]¶
Adds a file path argument for
config_idtoparser.Specifically, calls add_argument() on
parserwith the parametersregister()ed forconfig_id. Ifconfig_idis unregistered, or if any of the following parameters are missing from the registration, sensible defaults are used instead when calling add_argument():names_or_flags: based on 'config_id' type: str metavar: "FILE" dest: 'config_id' default: based on 'config_id' + 'extension' help: based on 'config_id'
Additional parameters beyond these can also be registered.
Note
These parameters enable the later retrieval of any user-specified file path using
get_file_path()or a default file path if the user fails to explicitly give a one.- Parameters:
parser (argparse.ArgumentParser) – The parser for which to add a file path arguments for the given
config_id.config_id (
ConfigID) – The config identifier for which to add file path arguments toparser.
See also
- get_file_path(config_id: str, known_args: Any) str[source]¶
Retrieves the file path for
config_idfromknown_args.Assumes that
known_argsis thenamespacereturn object (the first return value) of parse_known_args() from theparseron which a file path forconfig_idwas added usingadd_path_argument(). However, a sensible default is returned even if no such prior call occurred.- Parameters:
config_id (
ConfigID) – The config identifier for which to retrieve a file path fromknown_args.known_args (Any) – Typically, the
namespacereturn object (the first return value) of parse_known_args().
- Returns:
The retrieved file path for
config_id.- Return type:
See also