Skip to content

Commit

Permalink
Add tree_mapper argument to asdf.open
Browse files Browse the repository at this point in the history
  • Loading branch information
eslavich committed Jan 8, 2023
1 parent c85b77f commit 802b759
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The ASDF Standard is at v1.6.0
- Discard cache of lazy-loaded block data when it is no longer referenced
by the tree. [#1280]

- Add ``tree_mapper`` argument to ``asdf.open`` to permit arbitrary
transformations of the tagged tree prior to conversion. [#1289]

2.14.3 (2022-12-15)
-------------------

Expand Down
14 changes: 14 additions & 0 deletions asdf/asdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ def _open_asdf(
_force_raw_types=False,
strict_extension_check=False,
ignore_missing_extensions=False,
tree_mapper=None,
**kwargs,
):
"""Attempt to populate AsdfFile data from file-like object"""
Expand Down Expand Up @@ -867,6 +868,9 @@ def _open_asdf(
self._blocks.read_internal_blocks(fd, past_magic=True, validate_checksums=validate_checksums)
self._blocks.read_block_index(fd, self)

if tree_mapper is not None:
tree = tree_mapper(tree)

tree = reference.find_references(tree, self)

if self.version <= versioning.FILL_DEFAULTS_MAX_VERSION and get_config().legacy_fill_schema_defaults:
Expand Down Expand Up @@ -902,6 +906,7 @@ def _open_impl(
_force_raw_types=False,
strict_extension_check=False,
ignore_missing_extensions=False,
tree_mapper=None,
**kwargs,
):
"""Attempt to open file-like object as either AsdfFile or AsdfInFits"""
Expand All @@ -918,6 +923,7 @@ def _open_impl(
_force_raw_types,
strict_extension_check,
ignore_missing_extensions,
tree_mapper=tree_mapper,
**kwargs,
)
except Exception as e:
Expand All @@ -937,6 +943,7 @@ def _open_generic_file(
_force_raw_types=False,
strict_extension_check=False,
ignore_missing_extensions=False,
tree_mapper=None,
**kwargs,
):
"""Attempt to open a generic_file instance as either AsdfFile or AsdfInFits"""
Expand Down Expand Up @@ -980,6 +987,7 @@ def _open_generic_file(
_force_raw_types=_force_raw_types,
strict_extension_check=strict_extension_check,
ignore_missing_extensions=ignore_missing_extensions,
tree_mapper=tree_mapper,
**kwargs,
)
else:
Expand Down Expand Up @@ -1733,6 +1741,7 @@ def open_asdf(
strict_extension_check=False,
ignore_missing_extensions=False,
_compat=False,
tree_mapper=None,
**kwargs,
):
"""
Expand Down Expand Up @@ -1807,6 +1816,10 @@ def open_asdf(
and custom schemas. Recommended unless the file is already known
to be valid.
tree_mapper : callable, optional
Callable that accepts the tagged ASDF tree before conversion
and returns a modified tree.
Returns
-------
asdffile : AsdfFile
Expand Down Expand Up @@ -1840,6 +1853,7 @@ def open_asdf(
_force_raw_types=_force_raw_types,
strict_extension_check=strict_extension_check,
ignore_missing_extensions=ignore_missing_extensions,
tree_mapper=tree_mapper,
**kwargs,
)

Expand Down
14 changes: 14 additions & 0 deletions asdf/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,17 @@ def test_none_values(tmp_path):
with asdf.open(path) as af:
assert "foo" in af
assert af["foo"] is None


def test_tree_mapper(tmp_path):
path = str(tmp_path / "test.asdf")

af = asdf.AsdfFile({"foo": "bar"})
af.write_to(path)

def tree_mapper(tree):
tree["foo"] = "baz"
return tree

with asdf.open(path, tree_mapper=tree_mapper) as af:
assert af["foo"] == "baz"

0 comments on commit 802b759

Please sign in to comment.