File: _tag.py

package info (click to toggle)
python-asdf 2.14.3-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,280 kB
  • sloc: python: 16,612; makefile: 124
file content (110 lines) | stat: -rw-r--r-- 2,563 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
import warnings

from asdf.exceptions import AsdfDeprecationWarning


class TagDefinition:
    """
    Container for properties of a custom YAML tag.

    Parameters
    ----------
    tag_uri : str
        Tag URI.
    schema_uri : str, optional
        URI of the schema that should be used to validate objects
        with this tag.
    title : str, optional
        Short description of the tag.
    description : str, optional
        Long description of the tag.
    """

    def __init__(self, tag_uri, *, schema_uris=None, title=None, description=None):
        if "*" in tag_uri:
            raise ValueError("URI patterns are not permitted in TagDefinition")

        self._tag_uri = tag_uri

        if schema_uris is None:
            self._schema_uris = []
        else:
            if isinstance(schema_uris, list):
                self._schema_uris = schema_uris
            else:
                self._schema_uris = [schema_uris]

        self._title = title
        self._description = description

    @property
    def tag_uri(self):
        """
        Get the tag URI.

        Returns
        -------
        str
        """
        return self._tag_uri

    @property
    def schema_uri(self):
        """
        DEPRECATED

        Get the URI of the schema that should be used to validate
        objects with this tag.

        Returns
        -------
        str or None
        """
        warnings.warn(
            "The TagDefinition.schema_uri property is deprecated.  Use TagDefinition.schema_uris instead.",
            AsdfDeprecationWarning,
        )

        if len(self._schema_uris) == 0:
            return None
        elif len(self._schema_uris) == 1:
            return self._schema_uris[0]
        else:
            raise RuntimeError("Cannot use TagDefinition.schema_uri when multiple schema URIs are present")

    @property
    def schema_uris(self):
        """
        Get the URIs of the schemas that should be used to validate
        objects with this tag.

        Returns
        -------
        list
        """
        return self._schema_uris

    @property
    def title(self):
        """
        Get the short description of the tag.

        Returns
        -------
        str or None
        """
        return self._title

    @property
    def description(self):
        """
        Get the long description of the tag.

        Returns
        -------
        str or None
        """
        return self._description

    def __repr__(self):
        return f"<TagDefinition URI: {self.tag_uri}>"