Skip to content

Commit

Permalink
cleanup converters, retain read_tag
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Jan 6, 2025
1 parent 49fbbcd commit 53e6513
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/roman_datamodels/stnode/_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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)

Expand All @@ -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.
Expand Down

0 comments on commit 53e6513

Please sign in to comment.