From e18730799772235a6b0d667f60d82f14c9626cd3 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 6 Jan 2025 13:57:08 -0500 Subject: [PATCH] cleanup converters, retain read_tag --- src/roman_datamodels/stnode/_converters.py | 25 ++++++++-------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/roman_datamodels/stnode/_converters.py b/src/roman_datamodels/stnode/_converters.py index 7144f0e5..12db6cd4 100644 --- a/src/roman_datamodels/stnode/_converters.py +++ b/src/roman_datamodels/stnode/_converters.py @@ -41,6 +41,14 @@ def __init_subclass__(cls, **kwargs) -> None: NODE_CONVERTERS[cls.__name__] = cls() + def select_tag(self, obj, tags, ctx): + return obj.tag + + def from_yaml_tree(self, node, tag, ctx): + obj = NODE_CLASSES_BY_TAG[tag](node) + obj._read_tag = tag + return obj + class TaggedObjectNodeConverter(_RomanConverter): """ @@ -55,15 +63,9 @@ def tags(self): def types(self): return list(OBJECT_NODE_CLASSES_BY_PATTERN.values()) - def select_tag(self, obj, tags, ctx): - return obj.tag - def to_yaml_tree(self, obj, tag, ctx): return dict(obj._data) - def from_yaml_tree(self, node, tag, ctx): - return NODE_CLASSES_BY_TAG[tag](node) - class TaggedListNodeConverter(_RomanConverter): """ @@ -78,15 +80,9 @@ def tags(self): def types(self): return list(LIST_NODE_CLASSES_BY_PATTERN.values()) - def select_tag(self, obj, tags, ctx): - return obj.tag - def to_yaml_tree(self, obj, tag, ctx): return list(obj) - def from_yaml_tree(self, node, tag, ctx): - return NODE_CLASSES_BY_TAG[tag](node) - class TaggedScalarNodeConverter(_RomanConverter): """ @@ -101,9 +97,6 @@ def tags(self): def types(self): return list(SCALAR_NODE_CLASSES_BY_PATTERN.values()) - def select_tag(self, obj, tags, ctx): - return obj.tag - def to_yaml_tree(self, obj, tag, ctx): node = obj.__class__.__bases__[0](obj) @@ -117,7 +110,7 @@ def from_yaml_tree(self, node, tag, ctx): if "file_date" in tag: converter = ctx.extension_manager.get_converter_for_type(Time) node = converter.from_yaml_tree(node, tag, ctx) - return NODE_CLASSES_BY_TAG[tag](node) + return super().from_yaml_tree(node, tag, ctx) # Create the ASDF extension for the STNode classes.