From ce9eb297e54bd8718304c843fa5373e87992fdb3 Mon Sep 17 00:00:00 2001 From: Brett Graham Date: Thu, 26 Sep 2024 16:03:11 -0400 Subject: [PATCH] Open `json` files as `ModelLibrary` if `romancal` is installed (#389) --- .github/workflows/ci.yml | 6 ++++++ changes/389.feature.rst | 1 + src/roman_datamodels/datamodels/_utils.py | 17 ++++++++--------- tests/test_open.py | 21 +++++++++++++++++++++ tox.ini | 17 +++++++++++++++++ 5 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 changes/389.feature.rst diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 653a52e1..f1269ee8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,3 +33,9 @@ jobs: with: envs: | - linux: rad + test_with_romancal: + uses: OpenAstronomy/github-actions-workflows/.github/workflows/tox.yml@v1 + with: + envs: | + - linux: withromancal + coverage: codecov diff --git a/changes/389.feature.rst b/changes/389.feature.rst new file mode 100644 index 00000000..f2220abc --- /dev/null +++ b/changes/389.feature.rst @@ -0,0 +1 @@ +Open ``.json`` files as ``ModelLibrary`` if ``romancal`` is installed. diff --git a/src/roman_datamodels/datamodels/_utils.py b/src/roman_datamodels/datamodels/_utils.py index 781c17ae..34f68f8b 100644 --- a/src/roman_datamodels/datamodels/_utils.py +++ b/src/roman_datamodels/datamodels/_utils.py @@ -63,6 +63,14 @@ def rdm_open(init, memmap=False, **kwargs): ------- `DataModel` """ + if isinstance(init, str | Path): + if Path(init).suffix.lower() == ".json": + try: + from romancal.datamodels.library import ModelLibrary + + return ModelLibrary(init) + except ImportError: + raise ImportError("Please install romancal to allow opening associations with roman_datamodels") with validate.nuke_validation(): if isinstance(init, DataModel): # Copy the object so it knows not to close here @@ -76,14 +84,5 @@ def rdm_open(init, memmap=False, **kwargs): if (model_type := type(asdf_file.tree["roman"])) in MODEL_REGISTRY: return MODEL_REGISTRY[model_type](asdf_file, **kwargs) - if isinstance(init, str): - exts = Path(init).suffixes - if not exts: - raise ValueError(f"Input file path does not have an extension: {init}") - - # Assume json files are asn and return them - if exts[0] == "json": - return init - asdf_file.close() raise TypeError(f"Unknown datamodel type: {model_type}") diff --git a/tests/test_open.py b/tests/test_open.py index c3d44717..99511bd2 100644 --- a/tests/test_open.py +++ b/tests/test_open.py @@ -1,3 +1,4 @@ +import json from pathlib import Path import asdf @@ -236,3 +237,23 @@ def test_rdm_open_non_datamodel(): with pytest.raises(TypeError, match=r"Unknown datamodel type: .*"): rdm_open(Path(__file__).parent / "data" / "not_a_datamodel.asdf") + + +def test_open_asn(tmp_path): + romancal = pytest.importorskip("romancal") + + fn = tmp_path / "test.json" + asn = { + "products": [ + { + "members": [], + "name": "foo", + } + ], + } + with open(fn, "w") as f: + json.dump(asn, f) + + lib = datamodels.open(fn) + + assert isinstance(lib, romancal.datamodels.ModelLibrary) diff --git a/tox.ini b/tox.ini index 8d784b31..5e0cb04a 100644 --- a/tox.ini +++ b/tox.ini @@ -4,6 +4,7 @@ env_list = test{,-devdeps}{,-pyargs,-cov}-xdist test-numpy{120,121,122}-xdist build-{docs,dist} + withromancal # tox environments are constructed with so-called 'factors' (or terms) # separated by hyphens, e.g. test-devdeps-cov. Lines below starting with factor: @@ -69,3 +70,19 @@ deps = build commands = python -m build . + +[testenv:withromancal] +allowlist_externals = + git + bash +deps = + pytest-cov +commands_pre = + bash -c "pip freeze -q | grep 'roman_datamodels @' > {env_tmp_dir}/requirements.txt" + pip install git+https://github.com/spacetelescope/romancal.git + pip install -r {env_tmp_dir}/requirements.txt + pip freeze +commands = + pytest tests/test_open.py::test_open_asn \ + --cov=tests --cov-config pyproject.toml --cov-report term-missing --cov-report xml \ + {posargs}