Skip to content

Commit

Permalink
Refactor the ASDF extension into stnode (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamJamieson authored Jun 24, 2023
1 parent 2dd4d7f commit 9e60328
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

- Remove the unused project deployment scripts and actions. [#222]

- Refactor the ASDF extension to be entirely part of the stnode sub-package. [#220]

0.15.0 (2023-05-15)
===================

Expand Down
4 changes: 0 additions & 4 deletions docs/roman_datamodels/datamodels/developer_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ Developer API

.. automodapi:: roman_datamodels.datamodels

.. automodapi:: roman_datamodels.extensions

.. automodapi:: roman_datamodels.integration

..
The converters are not documented because of https://github.com/asdf-format/asdf/issues/1565
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ docs = [
'repository' = 'https://github.com/spacetelescope/roman_datamodels'

[project.entry-points]
'asdf.extensions' = { roman_datamodels = 'roman_datamodels.integration:get_extensions' }
'asdf.extensions' = { roman_datamodels = 'roman_datamodels.stnode._integration:get_extensions' }

[build-system]
requires = ["setuptools >=61", "setuptools_scm[toml] >=3.4", "wheel"]
Expand Down
3 changes: 1 addition & 2 deletions src/roman_datamodels/datamodels/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from jsonschema import ValidationError

from roman_datamodels import stnode, validate
from roman_datamodels.extensions import DATAMODEL_EXTENSIONS

__all__ = ["DataModel", "MODEL_REGISTRY"]

Expand Down Expand Up @@ -105,7 +104,7 @@ def check_type(self, asdffile_instance):
@property
def schema_uri(self):
# Determine the schema corresponding to this model's tag
schema_uri = next(t for t in DATAMODEL_EXTENSIONS[0].tags if t.tag_uri == self._instance._tag).schema_uris[0]
schema_uri = next(t for t in stnode.NODE_EXTENSIONS[0].tags if t.tag_uri == self._instance._tag).schema_uris[0]
return schema_uri

def close(self):
Expand Down
13 changes: 0 additions & 13 deletions src/roman_datamodels/extensions.py

This file was deleted.

35 changes: 30 additions & 5 deletions src/roman_datamodels/stnode/_converters.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
"""
The ASDF Converters to handle the serialization/deseialization of the STNode classes to ASDF.
"""
from asdf.extension import Converter
from asdf.extension import Converter, ManifestExtension
from astropy.time import Time

from ._registry import LIST_NODE_CLASSES_BY_TAG, OBJECT_NODE_CLASSES_BY_TAG, SCALAR_NODE_CLASSES_BY_TAG
from ._registry import LIST_NODE_CLASSES_BY_TAG, NODE_CONVERTERS, OBJECT_NODE_CLASSES_BY_TAG, SCALAR_NODE_CLASSES_BY_TAG

__all__ = [
"TaggedObjectNodeConverter",
"TaggedListNodeConverter",
"TaggedScalarNodeConverter",
"NODE_EXTENSIONS",
]


class TaggedObjectNodeConverter(Converter):
class _RomanConverter(Converter):
"""
Base class for the roman_datamodels converters.
"""

def __init_subclass__(cls, **kwargs) -> None:
"""
Automatically create the converter objects.
"""
super().__init_subclass__(**kwargs)

if not cls.__name__.startswith("_"):
if cls.__name__ in NODE_CONVERTERS:
raise ValueError(f"Duplicate converter for {cls.__name__}")

NODE_CONVERTERS[cls.__name__] = cls()


class TaggedObjectNodeConverter(_RomanConverter):
"""
Converter for all subclasses of TaggedObjectNode.
"""
Expand All @@ -36,7 +55,7 @@ def from_yaml_tree(self, node, tag, ctx):
return OBJECT_NODE_CLASSES_BY_TAG[tag](node)


class TaggedListNodeConverter(Converter):
class TaggedListNodeConverter(_RomanConverter):
"""
Converter for all subclasses of TaggedListNode.
"""
Expand All @@ -59,7 +78,7 @@ def from_yaml_tree(self, node, tag, ctx):
return LIST_NODE_CLASSES_BY_TAG[tag](node)


class TaggedScalarNodeConverter(Converter):
class TaggedScalarNodeConverter(_RomanConverter):
"""
Converter for all subclasses of TaggedScalarNode.
"""
Expand Down Expand Up @@ -94,3 +113,9 @@ def from_yaml_tree(self, node, tag, ctx):
node = converter.from_yaml_tree(node, tag, ctx)

return SCALAR_NODE_CLASSES_BY_TAG[tag](node)


# Create the ASDF extension for the STNode classes.
NODE_EXTENSIONS = [
ManifestExtension.from_uri("asdf://stsci.edu/datamodels/roman/manifests/datamodels-1.0", converters=NODE_CONVERTERS.values())
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def get_extensions():
-------
List[`asdf.extension.Extension`]
"""
from . import extensions
from ._converters import NODE_EXTENSIONS

return extensions.DATAMODEL_EXTENSIONS
return NODE_EXTENSIONS
1 change: 1 addition & 0 deletions src/roman_datamodels/stnode/_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
LIST_NODE_CLASSES_BY_TAG = {}
SCALAR_NODE_CLASSES_BY_TAG = {}
SCALAR_NODE_CLASSES_BY_KEY = {}
NODE_CONVERTERS = {}
3 changes: 1 addition & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from roman_datamodels import datamodels
from roman_datamodels import maker_utils as utils
from roman_datamodels import stnode
from roman_datamodels.extensions import DATAMODEL_EXTENSIONS
from roman_datamodels.testing import assert_node_equal

EXPECTED_COMMON_REFERENCE = {"$ref": "ref_common-1.0.0"}
Expand Down Expand Up @@ -238,7 +237,7 @@ def test_reference_file_model_base(tmp_path):
# Set temporary asdf file

# Get all reference file classes
tags = [t for t in DATAMODEL_EXTENSIONS[0].tags if "/reference_files/" in t.tag_uri]
tags = [t for t in stnode.NODE_EXTENSIONS[0].tags if "/reference_files/" in t.tag_uri]
for tag in tags:
schema = asdf.schema.load_schema(tag.schema_uris[0])
# Check that schema references common reference schema
Expand Down

0 comments on commit 9e60328

Please sign in to comment.