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

CLN: Drop support for settings via environment #522

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 8 additions & 28 deletions src/fmu/dataio/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@


GLOBAL_ENVNAME: Final = "FMU_GLOBAL_CONFIG"
SETTINGS_ENVNAME: Final = "FMU_DATAIO_CONFIG" # input settings from a spesific file!
SETTINGS_ENVNAME: Final = (
"FMU_DATAIO_CONFIG" # Feature deprecated, still used for user warning.
)

logger: Final = null_logger(__name__)

Expand Down Expand Up @@ -375,21 +377,6 @@ class ExportData:

The output files will be on the form: somename--t1_t0.ext

.. note:: Using config from file

Optionally, the keys can be stored in a yaml file as argument, and you can let
the environment variable FMU_DATAIO_CONFIG point to that file. This can e.g.
make it possible for ERT jobs to point to external input config's. For example::

export FMU_DATAIO_CONFIG="/path/to/mysettings.yml"
export FMU_GLOBAL_CONFIG="/path/to/global_variables.yml"

In python:

eda = ExportData()
eda.export(obj)


"""

# ----------------------------------------------------------------------------------
Expand Down Expand Up @@ -485,18 +472,11 @@ def __post_init__(self) -> None:

# if input is provided as an ENV variable pointing to a YAML file; will override
if SETTINGS_ENVNAME in os.environ:
external_input = some_config_from_env(SETTINGS_ENVNAME)

if external_input:
# derive legal input from dataclass signature
annots = getattr(self, "__annotations__", {})
legals = {
key: val for key, val in annots.items() if not key.startswith("_")
}

for key, value in external_input.items():
if _validate_variable(key, value, legals):
setattr(self, key, value)
warnings.warn(
"Providing input settings through environment variables is deprecated, "
"use ExportData(**yaml_load(<your_file>)) instead. To "
"disable this warning, remove the 'FMU_DATAIO_CONFIG' env.",
)

# global config which may be given as env variable -> a file; will override
if GLOBAL_ENVNAME in os.environ:
Expand Down
70 changes: 0 additions & 70 deletions tests/test_units/test_dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,76 +332,6 @@ def test_global_config_from_env(monkeypatch, global_config2_path):
assert "smda" in edata.config["masterdata"]


def test_settings_config_from_env(tmp_path, rmsglobalconfig, regsurf, monkeypatch):
"""Testing getting user settings config from a file via env variable."""

settings = {}
settings["name"] = "MyFancyName"
settings["tagname"] = "MyFancyTag"
settings["workflow"] = "Some work flow"
settings["config"] = rmsglobalconfig

with open(tmp_path / "mysettings.yml", "w") as stream:
yaml.dump(settings, stream)

monkeypatch.setenv("FMU_DATAIO_CONFIG", str(tmp_path / "mysettings.yml"))
edata = ExportData(content="depth") # the env variable will override this
assert edata.name == "MyFancyName"

meta = edata.generate_metadata(regsurf)
assert "myfancyname--myfancytag" in meta["file"]["relative_path"]


def test_settings_and_global_config_from_env(
tmp_path,
rmsglobalconfig,
regsurf,
monkeypatch,
):
"""Testing getting user settings config ands global from a env -> file."""

settings = {}
settings["name"] = "MyFancyName"
settings["tagname"] = "MyFancyTag"
settings["workflow"] = "Some work flow"
settings["config"] = rmsglobalconfig

with open(tmp_path / "mysettings.yml", "w") as stream:
yaml.dump(settings, stream)

with open(tmp_path / "global_variables.yml", "w") as stream:
yaml.dump(rmsglobalconfig, stream)

monkeypatch.setenv("FMU_GLOBAL_CONFIG", str(tmp_path / "global_variables.yml"))
monkeypatch.setenv("FMU_DATAIO_CONFIG", str(tmp_path / "mysettings.yml"))

edata = ExportData(content="depth") # the env variable will override this
assert edata.name == "MyFancyName"

meta = edata.generate_metadata(regsurf)
assert "myfancyname--myfancytag" in meta["file"]["relative_path"]


def test_settings_config_from_env_invalid(
tmp_path,
rmsglobalconfig,
monkeypatch,
):
"""Testing getting user settings config from a file but some invalid stuff."""

settings = {}
settings["invalid"] = "MyFancyName"
settings["workflow"] = "Some work flow"
settings["config"] = rmsglobalconfig

with open(tmp_path / "mysettings.yml", "w") as stream:
yaml.dump(settings, stream)

monkeypatch.setenv("FMU_DATAIO_CONFIG", str(tmp_path / "mysettings.yml"))
with pytest.raises(ValidationError):
_ = ExportData(content="depth")


def test_norwegian_letters_globalconfig(
globalvars_norwegian_letters,
regsurf,
Expand Down
Loading