From bd2c8e5dc198d90138592b66959148766e5dab0a Mon Sep 17 00:00:00 2001 From: Brett Date: Fri, 20 Sep 2024 17:02:41 -0400 Subject: [PATCH 1/6] allow roman_datamodels to open associations via romancal --- src/roman_datamodels/datamodels/_utils.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) 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}") From 53bda4008696af1db3cb3dd2c7c1f10f9c4597b9 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 23 Sep 2024 12:17:44 -0400 Subject: [PATCH 2/6] add romancal to ci --- .github/workflows/ci.yml | 5 +++++ tests/test_open.py | 21 +++++++++++++++++++++ tox.ini | 12 ++++++++++++ 3 files changed, 38 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 653a52e1..638c68d0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,3 +33,8 @@ jobs: with: envs: | - linux: rad + test_with_romancal: + uses: OpenAstronomy/github-actions-workflows/.github/workflows/tox.yml@v1 + with: + envs: | + - linux: with_romancal diff --git a/tests/test_open.py b/tests/test_open.py index c3d44717..55197197 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.asn" + asn = { + "product": [ + { + "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..32b01f6e 100644 --- a/tox.ini +++ b/tox.ini @@ -69,3 +69,15 @@ deps = build commands = python -m build . + +[testenv:with_romancal] +allowlist_externals = + git + bash +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 From 2159b63048c2c9df485469976f126f2d5220a016 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 23 Sep 2024 13:35:38 -0400 Subject: [PATCH 3/6] fix asn file suffix --- tests/test_open.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_open.py b/tests/test_open.py index 55197197..f89269f0 100644 --- a/tests/test_open.py +++ b/tests/test_open.py @@ -242,7 +242,7 @@ def test_rdm_open_non_datamodel(): def test_open_asn(tmp_path): romancal = pytest.importorskip("romancal") - fn = tmp_path / "test.asn" + fn = tmp_path / "test.json" asn = { "product": [ { From 51ed84308e8f000f2f18d76c94d555b7a2ec2c6d Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 23 Sep 2024 13:43:28 -0400 Subject: [PATCH 4/6] fix test asn --- tests/test_open.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_open.py b/tests/test_open.py index f89269f0..99511bd2 100644 --- a/tests/test_open.py +++ b/tests/test_open.py @@ -244,7 +244,7 @@ def test_open_asn(tmp_path): fn = tmp_path / "test.json" asn = { - "product": [ + "products": [ { "members": [], "name": "foo", From bb4d5da9a18cb7a37dc3c24280374598ece2fd88 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 23 Sep 2024 14:26:47 -0400 Subject: [PATCH 5/6] add changelog fragment --- changes/389.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/389.feature.rst 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. From 93574c6c21ca39c61ab009158bc41a8c7c6f2955 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 23 Sep 2024 14:59:38 -0400 Subject: [PATCH 6/6] add coverage to with_romancal env --- .github/workflows/ci.yml | 3 ++- tox.ini | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 638c68d0..f1269ee8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,4 +37,5 @@ jobs: uses: OpenAstronomy/github-actions-workflows/.github/workflows/tox.yml@v1 with: envs: | - - linux: with_romancal + - linux: withromancal + coverage: codecov diff --git a/tox.ini b/tox.ini index 32b01f6e..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: @@ -70,14 +71,18 @@ deps = commands = python -m build . -[testenv:with_romancal] +[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 + pytest tests/test_open.py::test_open_asn \ + --cov=tests --cov-config pyproject.toml --cov-report term-missing --cov-report xml \ + {posargs}