/usr/lib/python3/dist-packages/asdf/extension.py is in python3-asdf 1.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | # Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals, print_function
import abc
import six
from . import asdftypes
from . import resolver
@six.add_metaclass(abc.ABCMeta)
class AsdfExtension(object):
"""
Subclass to define an extension to ASDF.
"""
@classmethod
def __subclasshook__(cls, C):
if cls is AsdfExtension:
return (hasattr(C, 'types') and
hasattr(C, 'tag_mapping') and
hasattr(C, 'url_mapping'))
return NotImplemented
@abc.abstractproperty
def types(self):
"""
A list of AsdfType subclasses that describe how to store
custom objects to and from ASDF.
"""
pass
@abc.abstractproperty
def tag_mapping(self):
"""
A list of 2-tuples or callables mapping YAML tag prefixes to JSON Schema
URL prefixes.
For each entry:
- If a 2-tuple, the first part of the tuple is a YAML tag
prefix to match. The second part is a string, where case
the following are available as Python formatting tokens:
- ``{tag}``: the complete YAML tag.
- ``{tag_suffix}``: the part of the YAML tag after the
matched prefix.
- ``{tag_prefix}``: the matched YAML tag prefix.
- If a callable, it is passed the entire YAML tag must return
the entire JSON schema URL if it matches, otherwise, return `None`.
Note that while JSON Schema URLs uniquely define a JSON
Schema, they do not have to actually exist on an HTTP server
and be fetchable (much like XML namespaces).
For example, to match all YAML tags with the
``tag:nowhere.org:custom` prefix to the
``http://nowhere.org/schemas/custom/`` URL prefix::
return [('tag:nowhere.org:custom/',
'http://nowhere.org/schemas/custom/{tag_suffix}')]
"""
pass
@abc.abstractproperty
def url_mapping(self):
"""
A list of 2-tuples or callables mapping JSON Schema URLs to
other URLs. This is useful if the JSON Schemas are not
actually fetchable at their corresponding URLs but are on the
local filesystem, or, to save bandwidth, we have a copy of
fetchable schemas on the local filesystem. If neither is
desirable, it may simply be the empty list.
For each entry:
- If a 2-tuple, the first part is a URL prefix to match. The
second part is a string, where the following are available
as Python formatting tokens:
- ``{url}``: The entire JSON schema URL
- ``{url_prefix}``: The matched URL prefix
- ``{url_suffix}``: The part of the URL after the prefix.
- If a callable, it is passed the entire JSON Schema URL and
must return a resolvable URL pointing to the schema content.
If it doesn't match, should return `None`.
For example, to map a remote HTTP URL prefix to files installed
alongside as data alongside Python module::
return [('http://nowhere.org/schemas/custom/1.0.0/',
asdf.util.filepath_to_url(
os.path.join(SCHEMA_PATH, 'stsci.edu')) +
'/{url_suffix}.yaml'
)]
"""
pass
class AsdfExtensionList(object):
"""
Manage a set of extensions that are in effect.
"""
def __init__(self, extensions):
tag_mapping = []
url_mapping = []
validators = {}
self._type_index = asdftypes.AsdfTypeIndex()
for extension in extensions:
if not isinstance(extension, AsdfExtension):
raise TypeError(
"Extension must implement asdftypes.AsdfExtension "
"interface")
tag_mapping.extend(extension.tag_mapping)
url_mapping.extend(extension.url_mapping)
for typ in extension.types:
self._type_index.add_type(typ)
validators.update(typ.validators)
self._tag_mapping = resolver.Resolver(tag_mapping, 'tag')
self._url_mapping = resolver.Resolver(url_mapping, 'url')
self._validators = validators
@property
def tag_to_schema_resolver(self):
return self._tag_mapping
@property
def url_mapping(self):
return self._url_mapping
@property
def type_index(self):
return self._type_index
@property
def validators(self):
return self._validators
class BuiltinExtension(object):
"""
This is the "extension" to ASDF that includes all the built-in
tags. Even though it's not really an extension and it's always
available, it's built in the same way as an extension.
"""
@property
def types(self):
return asdftypes._all_asdftypes
@property
def tag_mapping(self):
return [
('tag:stsci.edu:asdf',
'http://stsci.edu/schemas/asdf{tag_suffix}')
]
@property
def url_mapping(self):
return resolver.DEFAULT_URL_MAPPING
# A special singleton for the common case of when no extensions are used.
_builtin_extension_list = AsdfExtensionList([BuiltinExtension()])
|