From dfbb0af1602707a04d4667a7f4c8d385ff2ba1aa Mon Sep 17 00:00:00 2001 From: Brett Date: Fri, 20 Sep 2024 17:02:41 -0400 Subject: [PATCH] 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}")