Skip to content

Commit

Permalink
allow roman_datamodels to open associations via romancal
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Sep 20, 2024
1 parent 011f123 commit dfbb0af
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/roman_datamodels/datamodels/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}")

0 comments on commit dfbb0af

Please sign in to comment.