Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UW-269 Extend set_config.py tool to "export" variables #280

Merged
merged 21 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/uwtools/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import yaml

from uwtools import exceptions, logger
from uwtools.exceptions import UWConfigError
from uwtools.j2template import J2Template
from uwtools.logger import Logger
from uwtools.utils import cli_helpers
Expand Down Expand Up @@ -752,3 +753,17 @@ def create_config_obj(
raise ValueError(err_msg)
# Dump to file:
dump_method(path=outfile, cfg=config_obj)


def export_variables(config_dict: dict, section_path: list, log: Logger) -> None:
elcarpenterNOAA marked this conversation as resolved.
Show resolved Hide resolved
"""
Allows users to export sections of the config file as environment variables.
elcarpenterNOAA marked this conversation as resolved.
Show resolved Hide resolved
"""
for key in section_path:
config_dict = config_dict[key]
elcarpenterNOAA marked this conversation as resolved.
Show resolved Hide resolved
for key, value in config_dict.items():
if type(value) not in (bool, str, int, float):
elcarpenterNOAA marked this conversation as resolved.
Show resolved Hide resolved
log.error(f"Non-scalar variable {key} was provided")
elcarpenterNOAA marked this conversation as resolved.
Show resolved Hide resolved
raise UWConfigError("Section values provided must be scalar values")
for key, value in config_dict.items():
print(f"{key}={value}")
37 changes: 37 additions & 0 deletions src/uwtools/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from uwtools import config, exceptions
from uwtools.exceptions import UWConfigError
from uwtools.logger import Logger
from uwtools.tests.support import compare_files, fixture_path, line_in_lines, msg_in_caplog
from uwtools.utils import cli_helpers

Expand Down Expand Up @@ -810,3 +811,39 @@ def test_YAMLConfig__load_unexpected_error(tmp_path):
with raises(UWConfigError) as e:
config.YAMLConfig(config_path=cfgfile)
assert msg in str(e.value)


def test_export_variables_yaml(capsys):
config_obj = config.YAMLConfig(fixture_path("FV3_GFS_v16.yaml"))
section = ["sgs_tke", "profile_type"]
config.export_variables(config_obj.data, section, log=Logger())
actual = capsys.readouterr().out
expected = """name=fixed
surface_value=0.0\n"""
assert actual == expected


def test_export_variables_yaml_for_nonscalar():
config_obj = config.YAMLConfig(fixture_path("FV3_GFS_v16.yaml"))
section = ["o3mr"]
with raises(UWConfigError):
config.export_variables(config_obj.data, section, log=Logger())


def test_export_variables_ini(capsys):
config_obj = config.INIConfig(fixture_path("simple3.ini"))
section = ["dessert"]
config.export_variables(config_obj.data, section, log=Logger())
actual = capsys.readouterr().out
expected = """type=pie
flavor={{flavor}}
side=False
servings=0\n"""
assert actual == expected


def test_export_variables_ini_missing_section():
config_obj = config.INIConfig(fixture_path("simple3.ini"))
section = ["sandwich"]
with raises(KeyError):
config.export_variables(config_obj.data, section, log=Logger())