[docs]classExtension(abc.ABC):""" Abstract base class defining an extension to ASDF. Implementing classes must provide the `extension_uri`. Other properties are optional. """@classmethoddef__subclasshook__(cls,class_):ifclsisExtension:returnhasattr(class_,"extension_uri")returnNotImplemented# pragma: no cover@property@abc.abstractmethoddefextension_uri(self):""" Get the URI of the extension to the ASDF Standard implemented by this class. Note that this may not uniquely identify the class itself. Returns ------- str """@propertydeflegacy_class_names(self):""" Get the set of fully-qualified class names used by older versions of this extension. This allows a new-style implementation of an extension to prevent warnings when a legacy extension is missing. Returns ------- iterable of str """returnset()@propertydefasdf_standard_requirement(self):""" Get the ASDF Standard version requirement for this extension. Returns ------- str or None If str, PEP 440 version specifier. If None, support all versions. """return@propertydefconverters(self):""" Get the `asdf.extension.Converter` instances for tags and Python types supported by this extension. Returns ------- iterable of asdf.extension.Converter """return[]@propertydeftags(self):""" Get the YAML tags supported by this extension. Returns ------- iterable of str or asdf.extension.TagDefinition """return[]@propertydefcompressors(self):""" Get the `asdf.extension.Compressor` instances for compression schemes supported by this extension. Returns ------- iterable of asdf.extension.Compressor """return[]@propertydefyaml_tag_handles(self):""" Get a dictionary of custom yaml TAG handles defined by the extension. The dictionary key indicates the TAG handles to be placed in the YAML header, the value defines the string for tag replacement. See https://yaml.org/spec/1.1/#tag%20shorthand/ Example: ``{"!foo!": "tag:nowhere.org:custom/"}`` Returns ------- dict """return{}@propertydefvalidators(self):""" Get the `asdf.extension.Validator` instances for additional schema properties supported by this extension. Returns ------- iterable of asdf.extension.Validator """return[]
[docs]classExtensionProxy(Extension):""" Proxy that wraps an extension, provides default implementations of optional methods, and carries additional information on the package that provided the extension. """
def__init__(self,delegate,package_name=None,package_version=None):ifnotisinstance(delegate,Extension):msg="Extension must implement the Extension interface"raiseTypeError(msg)self._delegate=delegateself._package_name=package_nameself._package_version=package_versionself._class_name=get_class_name(delegate)self._legacy=False# Sort these out up-front so that errors are raised when the extension is loaded# and not in the middle of the user's session. The extension will fail to load# and a warning will be emitted, but it won't crash the program.self._legacy_class_names=set()forclass_nameingetattr(self._delegate,"legacy_class_names",[]):ifisinstance(class_name,str):self._legacy_class_names.add(class_name)else:msg="Extension property 'legacy_class_names' must contain str values"raiseTypeError(msg)ifself._legacy:self._legacy_class_names.add(self._class_name)value=getattr(self._delegate,"asdf_standard_requirement",None)ifisinstance(value,str):self._asdf_standard_requirement=SpecifierSet(value)elifvalueisNone:self._asdf_standard_requirement=SpecifierSet()else:msg="Extension property 'asdf_standard_requirement' must be str or None"raiseTypeError(msg)self._tags=[]fortagingetattr(self._delegate,"tags",[]):ifisinstance(tag,str):self._tags.append(TagDefinition(tag))elifisinstance(tag,TagDefinition):self._tags.append(tag)else:msg="Extension property 'tags' must contain str or asdf.extension.TagDefinition values"raiseTypeError(msg)self._yaml_tag_handles=getattr(delegate,"yaml_tag_handles",{})# Process the converters last, since they expect ExtensionProxy# properties to already be available.self._converters=[ConverterProxy(c,self)forcingetattr(self._delegate,"converters",[])]self._compressors=[]ifhasattr(self._delegate,"compressors"):forcompressorinself._delegate.compressors:ifnotisinstance(compressor,Compressor):msg="Extension property 'compressors' must contain instances of asdf.extension.Compressor"raiseTypeError(msg)self._compressors.append(compressor)self._validators=[]ifhasattr(self._delegate,"validators"):forvalidatorinself._delegate.validators:ifnotisinstance(validator,Validator):msg="Extension property 'validators' must contain instances of asdf.extension.Validator"raiseTypeError(msg)self._validators.append(validator)@propertydefextension_uri(self):""" Get the URI of the extension to the ASDF Standard implemented by this class. Note that this may not uniquely identify the class itself. Returns ------- str or None """returngetattr(self._delegate,"extension_uri",None)@propertydeflegacy_class_names(self):""" Get the set of fully-qualified class names used by older versions of this extension. This allows a new-style implementation of an extension to prevent warnings when a legacy extension is missing. Returns ------- set of str """returnself._legacy_class_names@propertydefasdf_standard_requirement(self):""" Get the extension's ASDF Standard requirement. Returns ------- packaging.specifiers.SpecifierSet """returnself._asdf_standard_requirement@propertydefconverters(self):""" Get the extension's converters. Returns ------- list of asdf.extension.Converter """returnself._converters@propertydefcompressors(self):""" Get the extension's compressors. Returns ------- list of asdf.extension.Compressor """returnself._compressors@propertydeftags(self):""" Get the YAML tags supported by this extension. Returns ------- list of asdf.extension.TagDefinition """returnself._tags@propertydeftypes(self):""" Get the legacy extension's ExtensionType subclasses. Returns ------- iterable of asdf.type.ExtensionType """returngetattr(self._delegate,"types",[])@propertydeftag_mapping(self):""" Get the legacy extension's tag-to-schema-URI mapping. Returns ------- iterable of tuple or callable """returngetattr(self._delegate,"tag_mapping",[])@propertydefurl_mapping(self):""" Get the legacy extension's schema-URI-to-URL mapping. Returns ------- iterable of tuple or callable """returngetattr(self._delegate,"url_mapping",[])@propertydefdelegate(self):""" Get the wrapped extension instance. Returns ------- asdf.extension.Extension """returnself._delegate@propertydefpackage_name(self):""" Get the name of the Python package that provided this extension. Returns ------- str or None `None` if the extension was added at runtime. """returnself._package_name@propertydefpackage_version(self):""" Get the version of the Python package that provided the extension Returns ------- str or None `None` if the extension was added at runtime. """returnself._package_version@propertydefclass_name(self):""" Get the fully qualified class name of the extension. Returns ------- str """returnself._class_name@propertydeflegacy(self):""" False """returnself._legacy@propertydefyaml_tag_handles(self):""" Get a dictionary of custom yaml TAG handles defined by the extension. The dictionary key indicates the TAG handles to be placed in the YAML header, the value defines the string for tag replacement. See https://yaml.org/spec/1.1/#tag%20shorthand/ Example: ``{"!foo!": "tag:nowhere.org:custom/"}`` Returns ------- dict """returnself._yaml_tag_handles@propertydefvalidators(self):""" Get the `asdf.extension.Validator` instances for additional schema properties supported by this extension. Returns ------- list of asdf.extension.Validator """returnself._validatorsdef__eq__(self,other):ifisinstance(other,ExtensionProxy):returnother.delegateisself.delegatereturnFalsedef__hash__(self):returnhash(id(self.delegate))def__repr__(self):package_description="(none)"ifself.package_nameisNoneelsef"{self.package_name}=={self.package_version}"uri_description="(none)"ifself.extension_uriisNoneelseself.extension_urireturn(f"<ExtensionProxy URI: {uri_description} class: {self.class_name} "f"package: {package_description} legacy: {self.legacy}>")