Skip to content

Commit

Permalink
Add Extension and Converter ABCs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Slavich committed Aug 12, 2020
1 parent 535f958 commit 752d96e
Show file tree
Hide file tree
Showing 18 changed files with 1,517 additions and 65 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- Drop support for Python 3.5. [#856]

- Add new extension API to support versioned extensions.
[#850, #851]
[#850, #851, #853]

- Permit wildcard in tag validator URIs. [#858]

Expand Down
56 changes: 43 additions & 13 deletions asdf/asdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
AsdfExtension,
ExtensionProxy,
get_cached_asdf_extension_list,
get_cached_extension_manager,
)
from .util import NotSet
from .search import AsdfSearchResult
Expand Down Expand Up @@ -71,9 +72,10 @@ def __init__(self, tree=None, uri=None, extensions=None, version=None,
extensions : object, optional
Additional extensions to use when reading and writing the file.
May be any of the following: `asdf.extension.AsdfExtension`, `str`
extension URI, `asdf.extension.AsdfExtensionList` or a `list`
of URIs and/or extensions.
May be any of the following: `asdf.extension.AsdfExtension`,
`asdf.extension.Extension`, `str` extension URI,
`asdf.extension.AsdfExtensionList` or a `list` of URIs and/or
extensions.
version : str, optional
The ASDF Standard version. If not provided, defaults to the
Expand Down Expand Up @@ -184,7 +186,7 @@ def version(self):

@version.setter
def version(self, value):
""""
"""
Set this AsdfFile's ASDF Standard version.
Parameters
Expand Down Expand Up @@ -219,7 +221,7 @@ def extensions(self):
Returns
-------
list of asdf.extension.AsdfExtension
list of asdf.extension.AsdfExtension or asdf.extension.Extension
"""
return self._extensions

Expand All @@ -231,15 +233,30 @@ def extensions(self, value):
Parameters
----------
value : list of asdf.extension.AsdfExtension
value : list of asdf.extension.AsdfExtension or asdf.extension.Extension
"""
self._extensions = self._process_extensions(value)
self._extension_manager = None
self._extension_list = None

@property
def extension_manager(self):
"""
Get the ExtensionManager for this AsdfFile.
Returns
-------
asdf.extension.ExtensionManager
"""
if self._extension_manager is None:
self._extension_manager = get_cached_extension_manager(self.extensions)
return self._extension_manager

@property
def extension_list(self):
"""
Get the AsdfExtensionList for this AsdfFile.
Returns
-------
asdf.extension.AsdfExtensionList
Expand Down Expand Up @@ -1549,7 +1566,7 @@ def _warn_tag_mismatch(self, tag, best_tag):
# This function is called from within yamlutil methods to create
# a context when one isn't explicitly passed in.
def _create_serialization_context(self):
return SerializationContext(self.version_string)
return SerializationContext(self.version_string, self.extension_manager)


# Inherit docstring from dictionary
Expand Down Expand Up @@ -1605,9 +1622,10 @@ def open_asdf(fd, uri=None, mode=None, validate_checksums=False,
extensions : object, optional
Additional extensions to use when reading and writing the file.
May be any of the following: `asdf.extension.AsdfExtension`, `str`
extension URI, `asdf.extension.AsdfExtensionList` or a `list`
of URIs and/or extensions.
May be any of the following: `asdf.extension.AsdfExtension`,
`asdf.extension.Extension`, `str` extension URI,
`asdf.extension.AsdfExtensionList` or a `list` of URIs and/or
extensions.
do_not_fill_defaults : bool, optional
When `True`, do not fill in missing default values.
Expand Down Expand Up @@ -1729,8 +1747,9 @@ class SerializationContext:
"""
Container for parameters of the current (de)serialization.
"""
def __init__(self, version):
def __init__(self, version, extension_manager):
self._version = validate_version(version)
self._extension_manager = extension_manager

self._extensions_used = set()

Expand All @@ -1745,13 +1764,24 @@ def version(self):
"""
return self._version

@property
def extension_manager(self):
"""
Get the ExtensionManager for enabled extensions.
Returns
-------
asdf.extension.ExtensionManager
"""
return self._extension_manager

def mark_extension_used(self, extension):
"""
Note that an extension was used when reading or writing the file.
Parameters
----------
extension : asdf.extension.AsdfExtension
extension : asdf.extension.AsdfExtension or asdf.extension.Extension
"""
self._extensions_used.add(ExtensionProxy.maybe_wrap(extension))

Expand All @@ -1762,6 +1792,6 @@ def extensions_used(self):
Returns
-------
set of asdf.extension.AsdfExtension
set of asdf.extension.AsdfExtension or asdf.extension.Extension
"""
return self._extensions_used
10 changes: 5 additions & 5 deletions asdf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def extensions(self):
Returns
-------
list of asdf.extension.AsdfExtension
list of asdf.extension.AsdfExtension or asdf.extension.Extension
"""
if self._extensions is None:
with self._lock:
Expand All @@ -154,7 +154,7 @@ def add_extension(self, extension):
Parameters
----------
extension : asdf.extension.AsdfExtension
extension : asdf.extension.AsdfExtension or asdf.extension.Extension
"""
with self._lock:
extension = ExtensionProxy.maybe_wrap(extension)
Expand All @@ -166,8 +166,8 @@ def remove_extension(self, extension=None, *, package=None):
Parameters
----------
extension : asdf.extension.AsdfExtension or str, optional
An extension instance or URI or URI pattern to remove.
extension : asdf.extension.AsdfExtension or asdf.extension.Extension or str, optional
An extension instance or URI pattern to remove.
package : str, optional
Remove only extensions provided by this package. If the `extension`
argument is omitted, then all extensions from this package will
Expand Down Expand Up @@ -206,7 +206,7 @@ def get_extension(self, extension_uri):
Returns
-------
asdf.extension.AsdfExtension
asdf.extension.AsdfExtension or asdf.extension.Extension
Raises
------
Expand Down
4 changes: 3 additions & 1 deletion asdf/entry_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


RESOURCE_MAPPINGS_GROUP = "asdf.resource_mappings"
EXTENSIONS_GROUP = "asdf.extensions"
LEGACY_EXTENSIONS_GROUP = "asdf_extensions"


Expand All @@ -15,9 +16,10 @@ def get_resource_mappings():


def get_extensions():
extensions = _list_entry_points(EXTENSIONS_GROUP, ExtensionProxy)
legacy_extensions = _list_entry_points(LEGACY_EXTENSIONS_GROUP, ExtensionProxy)

return legacy_extensions
return extensions + legacy_extensions


def _list_entry_points(group, proxy_class):
Expand Down
11 changes: 10 additions & 1 deletion asdf/extension/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
Support for plugins that extend asdf to serialize
additional custom types.
"""
from ._extension import ExtensionProxy
from ._extension import Extension, ExtensionProxy
from ._manager import ExtensionManager, get_cached_extension_manager
from ._tag import TagDefinition
from ._converter import Converter, ConverterProxy
from ._legacy import (
AsdfExtension,
AsdfExtensionList,
Expand All @@ -15,7 +18,13 @@

__all__ = [
# New API
"Extension",
"ExtensionProxy",
"ExtensionManager",
"get_cached_extension_manager",
"TagDefinition",
"Converter",
"ConverterProxy",
# Legacy API
"AsdfExtension",
"AsdfExtensionList",
Expand Down
Loading

0 comments on commit 752d96e

Please sign in to comment.