From bcd7d9f22c8d9bd66c8836e857b02b834e2504bd Mon Sep 17 00:00:00 2001 From: William Jamieson Date: Mon, 10 Jul 2023 15:40:45 -0400 Subject: [PATCH] Move object node based init away from file based init --- src/roman_datamodels/datamodels/_core.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/roman_datamodels/datamodels/_core.py b/src/roman_datamodels/datamodels/_core.py index 709a3c165..02d0f9de4 100644 --- a/src/roman_datamodels/datamodels/_core.py +++ b/src/roman_datamodels/datamodels/_core.py @@ -77,6 +77,18 @@ def __init__(self, init=None, **kwargs): self._instance = None self._asdf = None + if isinstance(init, stnode.TaggedObjectNode): + if not isinstance(self, MODEL_REGISTRY.get(init.__class__)): + expected = {mdl: node for node, mdl in MODEL_REGISTRY.items()}[self.__class__].__name__ + raise ValidationError( + f"TaggedObjectNode: {init.__class__.__name__} is not of the type expected. Expected {expected}" + ) + with validate.nuke_validation(): + self._instance = init + self._asdf = asdf.AsdfFile({"roman": init}) + + return + if init is None: self._asdf = self.open_asdf(init=None, **kwargs) elif isinstance(init, (str, bytes, PurePath)): @@ -91,16 +103,6 @@ def __init__(self, init=None, **kwargs): elif isinstance(init, asdf.AsdfFile): self._asdf = init self._instance = self._asdf.tree["roman"] - elif isinstance(init, stnode.TaggedObjectNode): - if not isinstance(self, MODEL_REGISTRY.get(init.__class__)): - expected = {mdl: node for node, mdl in MODEL_REGISTRY.items()}[self.__class__].__name__ - raise ValidationError( - f"TaggedObjectNode: {init.__class__.__name__} is not of the type expected. Expected {expected}" - ) - with validate.nuke_validation(): - self._instance = init - self._asdf = asdf.AsdfFile() - self._asdf.tree = {"roman": init} else: raise OSError("Argument does not appear to be an ASDF file or TaggedObjectNode.")