uwtools.api.config
API access to uwtools configuration management tools.
- class uwtools.api.config.Config(config=None)
-
A base class specifying (and partially implementing) methods to read, manipulate, and write several configuration-file formats.
- Parameters:
config (dict | str | Config | Path | None) – Config file to load (None => read from stdin), or initial dict.
- compare_config(dict1, dict2=None, header=True)
Compare two config dictionaries.
Assumes a section/key/value structure.
- property config_file: Path | None
Return the path to the config file from which this object was instantiated, if applicable.
- dereference(context=None)
Render as much Jinja2 syntax as possible.
- abstract dump(path)
Dump the config to stdout or a file.
- Parameters:
path (Path | None) – Path to dump config to (default: stdout).
- Return type:
None
- class uwtools.api.config.FieldTableConfig(config=None)
Bases:
YAMLConfigWork with configs in field_table format.
- Parameters:
config (dict | str | Config | Path | None) – Config file to load (None => read from stdin), or initial dict.
- dump(path=None)
Dump the config in Field Table format.
- Parameters:
path (Path | None) – Path to dump config to (default: stdout).
- Return type:
None
- classmethod dump_dict(cfg, path=None)
Dump a provided config dictionary in Field Table format.
FMS field and tracer managers must be registered in an ASCII table called
field_table. This table lists field type, target model and methods the querying model will ask for. See UFS documentation for more information:https://ufs-weather-model.readthedocs.io/en/ufs-v1.0.0/InputsOutputs.html#field-table-file
The example format for generating a field file is:
sphum: longname: specific humidity units: kg/kg profile_type: name: fixed surface_value: 1.e30
- Parameters:
cfg (dict) – The in-memory config object to dump.
path (Path | None) – Path to dump config to (default: stdout).
- Return type:
None
- class uwtools.api.config.INIConfig(config=None)
Bases:
ConfigWork with INI configs.
- Parameters:
config (dict | Path | None) – Config file to load (None => read from stdin), or initial dict.
- dump(path=None)
Dump the config in INI format.
- Parameters:
path (Path | None) – Path to dump config to (default: stdout).
- Return type:
None
- class uwtools.api.config.NMLConfig(config=None)
Bases:
ConfigWork with Fortran namelist configs.
Construct an NMLConfig object.
- Parameters:
config (dict | Path | None) – Config file to load (None => read from stdin), or initial dict.
- dump(path)
Dump the config in Fortran namelist format.
- Parameters:
path (Path | None) – Path to dump config to (default: stdout).
- Return type:
None
- class uwtools.api.config.SHConfig(config=None)
Bases:
ConfigWork with key=value shell configs.
- Parameters:
config (dict | Path | None) – Config file to load (None => read from stdin), or initial dict.
- dump(path)
Dump the config as key=value lines.
- Parameters:
path (Path | None) – Path to dump config to (default: stdout).
- Return type:
None
- class uwtools.api.config.YAMLConfig(config=None)
Bases:
ConfigWork with YAML configs.
- Parameters:
config (dict | str | Config | Path | None) – Config file to load (None => read from stdin), or initial dict.
- dump(path=None)
Dump the config in YAML format.
- Parameters:
path (Path | None) – Path to dump config to (default: stdout).
- Return type:
None
- uwtools.api.config.compare(path1, path2, format1=None, format2=None)
Compare two config files.
Recognized file extensions are:
ini,nml,sh,yaml- Parameters:
- Returns:
Falseif config files had differences, otherwiseTrue.- Return type:
- uwtools.api.config.compose(configs, realize=False, output_file=None, input_format=None, output_format=None)
Compose config files.
Specify explicit input or output formats to override default treatment based on file extension. Recognized file extensions are:
ini,nml,sh,yaml.- Parameters:
realize (bool) – Render template expressions where possible.
output_file (Path | str | None) – Output config destination (default: write to
stdout).input_format (str | None) – Format of configs to compose (choices:
ini,nml,sh,yaml, default:yaml)output_format (str | None) – Format of output config (choices:
ini,nml,sh,yaml, default:yaml)
- Returns:
Trueif no errors were encountered.- Return type:
- uwtools.api.config.get_fieldtable_config(config=None, stdin_ok=False)
Get a
FieldTableConfigobject.- Parameters:
- Returns:
An initialized
FieldTableConfigobject.- Return type:
- uwtools.api.config.get_ini_config(config=None, stdin_ok=False)
Get an
INIConfigobject.
- uwtools.api.config.get_nml_config(config=None, stdin_ok=False)
Get an
NMLConfigobject.
- uwtools.api.config.get_sh_config(config=None, stdin_ok=False)
Get an
SHConfigobject.
- uwtools.api.config.get_yaml_config(config=None, stdin_ok=False)
Get a
YAMLConfigobject.- Parameters:
- Returns:
An initialized
YAMLConfigobject.- Return type:
- uwtools.api.config.realize(input_config=None, input_format=None, update_config=None, update_format=None, output_file=None, output_format=None, key_path=None, values_needed=False, total=False, dry_run=False, stdin_ok=False)
Realize a config based on a base input config and an optional update config.
The input config may be specified as a filesystem path, a
dict, or aConfigobject. When it is not, it will be read fromstdin.If an update config is specified, it is merged onto the input config, augmenting or overriding base values. It may be specified as a filesystem path, a
dict, or aConfigobject. When it is not, it will be read fromstdin.At most one of the input config or the update config may be left unspecified, in which case the other will be read from
stdin. If neither filename or format is specified for the update config, no update will be performed.The output destination may be specified as a filesystem path. When it is not, it will be written to
stdout.If
values_neededisTrue, a report of values needed to realize the config is logged. Indry_runmode, output is written tostderr.If
totalisTrue, an exception will be raised if any Jinja2 variables/expressions cannot be rendered. Otherwise, such variables/expressions will be passed through unchanged in the output.Recognized file extensions are:
ini,nml,sh,yaml- Parameters:
input_config (Config | Path | dict | str | None) – Input config file (
None=> readstdin).input_format (str | None) – Input config format (default: deduced from filename extension;
yamlif that fails).update_config (Config | Path | dict | str | None) – Update config file (
None=> readstdin).update_format (str | None) – Update config format (default: deduced from filename extension;
yamlif that fails).output_file (Path | str | None) – Output config file (
None=> write tostdout).output_format (str | None) – Output config format (default: deduced from filename extension;
yamlif that fails).key_path (list[YAMLKey] | None) – Path of keys to the desired output block.
values_needed (bool) – Report complete, missing, and template values.
total (bool) – Require rendering of all Jinja2 variables/expressions.
dry_run (bool) – Log output instead of writing to output.
stdin_ok (bool) – OK to read from
stdin?
- Returns:
The
dictrepresentation of the realized config.- Raises:
UWConfigRealizeErroriftotalisTrueand any Jinja2 syntax was not rendered.- Return type:
- uwtools.api.config.realize_to_dict(input_config=None, input_format=None, update_config=None, update_format=None, key_path=None, values_needed=False, total=False, dry_run=False, stdin_ok=False)
Realize a config to a
dict, based on a base input config and an optional update config.See
realize()for details on arguments, etc.
- uwtools.api.config.validate(schema_file, config_data=None, config_path=None, stdin_ok=False)
Check whether the specified config conforms to the specified JSON Schema spec.
Specify at most one of config_data or config_path. If no config is specified,
stdinis read and will be parsed as YAML and then validated.- Parameters:
- Raises:
TypeError if both config_* arguments specified.
- Returns:
Trueif the YAML file conforms to the schema,Falseotherwise.- Return type: