|
20 | 20 | Set, |
21 | 21 | Tuple, |
22 | 22 | Union, |
| 23 | + NamedTuple, |
23 | 24 | ) |
24 | 25 | from urllib.parse import urljoin, urlsplit |
25 | 26 |
|
@@ -74,27 +75,47 @@ class Defined(int): |
74 | 75 |
|
75 | 76 |
|
76 | 77 | class Context: |
| 78 | + """ |
| 79 | + A JSON-LD context, which contains term definitions |
| 80 | + """ |
| 81 | + |
| 82 | + _base: str | None |
| 83 | + #: _alias maps NODE_KEY to list of aliases |
| 84 | + _alias: dict[str, list[str]] |
| 85 | + _lookup: dict[tuple[str, Any, Defined | str, bool], Term] |
| 86 | + _prefixes: dict[str, Any] |
| 87 | + _context_cache: dict[str, Any] |
| 88 | + |
| 89 | + version: float |
| 90 | + language: str | None |
| 91 | + doc_base: str | None |
| 92 | + vocab: str | None |
| 93 | + active: bool |
| 94 | + propagate: bool |
| 95 | + terms: dict[str, Any] |
| 96 | + parent: Context | None |
| 97 | + |
77 | 98 | def __init__( |
78 | 99 | self, |
79 | 100 | source: _ContextSourceType = None, |
80 | 101 | base: Optional[str] = None, |
81 | 102 | version: Optional[float] = 1.1, |
82 | 103 | ): |
83 | | - self.version: float = version or 1.1 |
| 104 | + self._alias = {} |
| 105 | + self._lookup = {} |
| 106 | + self._prefixes = {} |
| 107 | + self._context_cache = {} |
| 108 | + |
| 109 | + self.version = version or 1.1 |
84 | 110 | self.language = None |
85 | | - self.vocab: Optional[str] = None |
86 | | - self._base: Optional[str] |
87 | | - self.base = base |
88 | 111 | self.doc_base = base |
89 | | - self.terms: Dict[str, Any] = {} |
90 | | - # _alias maps NODE_KEY to list of aliases |
91 | | - self._alias: Dict[str, List[str]] = {} |
92 | | - self._lookup: Dict[Tuple[str, Any, Union[Defined, str], bool], Term] = {} |
93 | | - self._prefixes: Dict[str, Any] = {} |
94 | 112 | self.active = False |
95 | | - self.parent: Optional[Context] = None |
96 | 113 | self.propagate = True |
97 | | - self._context_cache: Dict[str, Any] = {} |
| 114 | + self.vocab = None |
| 115 | + self.base = base |
| 116 | + self.terms = {} |
| 117 | + self.parent = None |
| 118 | + |
98 | 119 | if source: |
99 | 120 | self.load(source) |
100 | 121 |
|
@@ -668,9 +689,40 @@ def to_dict(self) -> Dict[str, Any]: |
668 | 689 | return r |
669 | 690 |
|
670 | 691 |
|
671 | | -Term = namedtuple( |
672 | | - "Term", |
673 | | - "id, name, type, container, index, language, reverse, context," "prefix, protected", |
674 | | -) |
675 | | - |
676 | | -Term.__new__.__defaults__ = (UNDEF, UNDEF, UNDEF, UNDEF, False, UNDEF, False, False) |
| 692 | +class Term(NamedTuple): |
| 693 | + """ |
| 694 | + Describes how a JSON key should be interpreted when parsed as RDF |
| 695 | + """ |
| 696 | + |
| 697 | + #: The IRI or CURIE of the term. |
| 698 | + id: str |
| 699 | + #: The name of the term, ie an alias for the id. |
| 700 | + name: str |
| 701 | + #: The type of the term, such as @id, @json, @none or @vocab |
| 702 | + type: Defined | str = UNDEF |
| 703 | + #: The container type, such as @graph, @id, @index, @language, @list, @set or @type, |
| 704 | + container: Collection[Any] | str | Defined = UNDEF |
| 705 | + #: A predicate IRI that should be used to interpret keys of this object, |
| 706 | + #: when used alongside `@container: @index`. |
| 707 | + #: See https://www.w3.org/TR/json-ld11/#property-based-data-indexing |
| 708 | + #: Ideally this wouldn't be called 'index' as it overrides the tuple's builtin index() method |
| 709 | + #: Hence the pyright ignore comment |
| 710 | + index: str | Defined | None = None # pyright: ignore[reportIncompatibleMethodOverride] |
| 711 | + #: The language to be used for values of this term |
| 712 | + language: str | Defined | None = UNDEF |
| 713 | + #: Indicates that this term is a reverse property, so subject and object are swapped. |
| 714 | + #: https://www.w3.org/TR/json-ld11/#reverse-properties |
| 715 | + reverse: bool = False |
| 716 | + #: A scoped context used inside values that use this term. |
| 717 | + #: See https://www.w3.org/TR/json-ld11/#scoped-contexts |
| 718 | + context: Any = UNDEF |
| 719 | + #: If true, indicates that this should be used during compaction. |
| 720 | + #: If false, indicates that this term cannot be used in compaction. |
| 721 | + #: See https://www.w3.org/TR/json-ld11/#compact-iris |
| 722 | + prefix: bool | None = None |
| 723 | + #: If true, marks the term as protected, meaning it cannot be overridden by a subcontext. |
| 724 | + #: See https://www.w3.org/TR/json-ld11/#protected-term-definitions |
| 725 | + protected: bool = False |
| 726 | + |
| 727 | + |
| 728 | +Context.terms |
0 commit comments