File: reference.py

package info (click to toggle)
python-asdf 2.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,248 kB
  • sloc: python: 13,104; makefile: 125
file content (188 lines) | stat: -rw-r--r-- 5,549 bytes parent folder | download
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-

"""
Manages external references in the YAML tree using the `JSON Reference
standard <http://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03>`__
and `JSON Pointer standard <http://tools.ietf.org/html/rfc6901>`__.
"""


from collections.abc import Sequence
import weakref

import numpy as np

from urllib import parse as urlparse

from .types import AsdfType
from . import generic_io
from . import treeutil
from . import util

__all__ = [
    'resolve_fragment', 'Reference', 'find_references', 'resolve_references',
    'make_reference']


def resolve_fragment(tree, pointer):
    """
    Resolve a JSON Pointer within the tree.
    """
    pointer = pointer.lstrip(u"/")
    parts = urlparse.unquote(pointer).split(u"/") if pointer else []

    for part in parts:
        part = part.replace(u"~1", u"/").replace(u"~0", u"~")

        if isinstance(tree, Sequence):
            # Array indexes should be turned into integers
            try:
                part = int(part)
            except ValueError:
                pass
        try:
            tree = tree[part]
        except (TypeError, LookupError):
            raise ValueError(
                "Unresolvable reference: '{0}'".format(pointer))

    return tree


class Reference(AsdfType):
    yaml_tag = 'tag:yaml.org,2002:map'

    def __init__(self, uri, base_uri=None, asdffile=None, target=None):
        self._uri = uri
        if asdffile is not None:
            self._asdffile = weakref.ref(asdffile)
        self._base_uri = base_uri
        self._target = target

    def _get_target(self, do_not_fill_defaults=False):
        if self._target is None:
            base_uri = self._base_uri
            if base_uri is None:
                base_uri = self._asdffile().uri
            uri = generic_io.resolve_uri(base_uri, self._uri)
            asdffile = self._asdffile().open_external(
                uri, do_not_fill_defaults=do_not_fill_defaults)
            parts = urlparse.urlparse(self._uri)
            fragment = parts.fragment
            self._target = resolve_fragment(asdffile.tree, fragment)
        return self._target

    def __repr__(self):
        # repr alone should not force loading of the reference
        if self._target is None:
            return "<Reference (unloaded) to '{0}'>".format(
                self._uri)
        else:
            return "<Reference to {0}>".format(repr(self._target))

    def __str__(self):
        # str alone should not force loading of the reference
        if self._target is None:
            return "<Reference (unloaded) to '{0}'>".format(
                self._uri)
        else:
            return str(self._target)

    def __len__(self):
        return len(self._get_target())

    def __getattr__(self, attr):
        if attr == '_tag':
            return None
        try:
            return getattr(self._get_target(), attr)
        except Exception:
            raise AttributeError("No attribute '{0}'".format(attr))

    def __getitem__(self, item):
        return self._get_target()[item]

    def __setitem__(self, item, val):
        self._get_target()[item] = val

    def __array__(self):
        return np.asarray(self._get_target())

    def __call__(self, do_not_fill_defaults=False):
        return self._get_target(do_not_fill_defaults=do_not_fill_defaults)

    def __contains__(self, item):
        return item in self._get_target()

    @classmethod
    def to_tree(self, data, ctx):
        if ctx.uri is not None:
            uri = generic_io.relative_uri(ctx.uri, data._uri)
        else:
            uri = data._uri
        return {'$ref': uri}

    @classmethod
    def validate(self, data):
        pass


def find_references(tree, ctx):
    """
    Find all of the JSON references in the tree, and convert them into
    `Reference` objects.
    """
    def do_find(tree, json_id):
        if isinstance(tree, dict) and '$ref' in tree:
            return Reference(tree['$ref'], json_id, asdffile=ctx)
        return tree

    return treeutil.walk_and_modify(
        tree, do_find, ignore_implicit_conversion=ctx._ignore_implicit_conversion)


def resolve_references(tree, ctx, do_not_fill_defaults=False):
    """
    Resolve all of the references in the tree, by loading the external
    data and inserting it directly into the tree.
    """
    def do_resolve(tree):
        if isinstance(tree, Reference):
            return tree(do_not_fill_defaults=do_not_fill_defaults)
        return tree

    tree = find_references(tree, ctx)

    return treeutil.walk_and_modify(
        tree, do_resolve, ignore_implicit_conversion=ctx._ignore_implicit_conversion)


def make_reference(asdffile, path):
    """
    Make a reference to a subtree of the given ASDF file.

    Parameters
    ----------
    asdffile : AsdfFile

    path : list of str and int, optional
        The parts of the path pointing to an item in this tree.
        If omitted, points to the root of the tree.

    Returns
    -------
    reference : reference.Reference
        A reference object.
    """
    path_str = '/'.join(
        x.replace(u"~", u"~0").replace(u"/", u"~1")
        for x in path)
    target = resolve_fragment(asdffile.tree, path_str)

    if asdffile.uri is None:
        raise ValueError(
            "Can not make a reference to a AsdfFile without an associated URI.")
    base_uri = util.get_base_uri(asdffile.uri)
    uri = base_uri + '#' + path_str
    return Reference(uri, target=target)