+130
-2
lines changedFilter options
+130
-2
lines changed Original file line number Diff line number Diff line change
@@ -85,7 +85,7 @@ def __init__(
85
85
self.terms: Dict[str, Any] = {}
86
86
# _alias maps NODE_KEY to list of aliases
87
87
self._alias: Dict[str, List[str]] = {}
88
-
self._lookup: Dict[Tuple[str, Any, Union[Defined, str], bool], Any] = {}
88
+
self._lookup: Dict[Tuple[str, Any, Union[Defined, str], bool], Term] = {}
89
89
self._prefixes: Dict[str, Any] = {}
90
90
self.active = False
91
91
self.parent: Optional[Context] = None
@@ -243,8 +243,10 @@ def add_term(
243
243
244
244
if isinstance(container, (list, set, tuple)):
245
245
container = set(container)
246
-
else:
246
+
elif container is not UNDEF:
247
247
container = set([container])
248
+
else:
249
+
container = set()
248
250
249
251
term = Term(
250
252
idref,
@@ -617,6 +619,37 @@ def _get_source_id(self, source: Dict[str, Any], key: str) -> Optional[str]:
617
619
term = term.get(ID)
618
620
return term
619
621
622
+
def _term_dict(self, term: Term) -> Union[Dict[str, Any], str]:
623
+
tdict: Dict[str, Any] = {}
624
+
if term.type != UNDEF:
625
+
tdict[TYPE] = self.shrink_iri(term.type)
626
+
if term.container:
627
+
tdict[CONTAINER] = list(term.container)
628
+
if term.language != UNDEF:
629
+
tdict[LANG] = term.language
630
+
if term.reverse:
631
+
tdict[REV] = term.id
632
+
else:
633
+
tdict[ID] = term.id
634
+
if tdict.keys() == {ID}:
635
+
return tdict[ID]
636
+
return tdict
637
+
638
+
def to_dict(self) -> Dict[str, Any]:
639
+
"""
640
+
Returns a dictionary representation of the context that can be
641
+
serialized to JSON.
642
+
643
+
:return: a dictionary representation of the context.
644
+
"""
645
+
r = {v: k for (k, v) in self._prefixes.items()}
646
+
r.update({term.name: self._term_dict(term) for term in self._lookup.values()})
647
+
if self.base:
648
+
r[BASE] = self.base
649
+
if self.language:
650
+
r[LANG] = self.language
651
+
return r
652
+
620
653
621
654
Term = namedtuple(
622
655
"Term",
Original file line number Diff line number Diff line change
@@ -2,10 +2,12 @@
2
2
JSON-LD Context Spec
3
3
"""
4
4
5
+
import json
5
6
from functools import wraps
6
7
from pathlib import Path
7
8
from typing import Any, Dict
8
9
10
+
from rdflib.namespace import PROV, XSD, Namespace
9
11
from rdflib.plugins.shared.jsonld import context, errors
10
12
from rdflib.plugins.shared.jsonld.context import Context
11
13
@@ -234,3 +236,52 @@ def test_dict_source(tmp_path: Path) -> None:
234
236
file.write_text(r"""{ "@context": { "ex": "http://example.com/" } }""")
235
237
ctx = Context(source=[{"@context": file.as_uri()}])
236
238
assert "http://example.com/" == ctx.terms["ex"].id
239
+
240
+
241
+
EG = Namespace("https://example.com/")
242
+
243
+
DIVERSE_CONTEXT = json.loads(
244
+
"""
245
+
{
246
+
"@context": {
247
+
"ex": "https://example.com/",
248
+
"generatedAt": { "@id": "http://www.w3.org/ns/prov#generatedAtTime", "@type": "http://www.w3.org/2001/XMLSchema#dateTime" },
249
+
"graphMap": { "@id": "https://example.com/graphMap", "@container": ["@graph", "@id"] },
250
+
"occupation_en": { "@id": "https://example.com/occupation", "@language": "en" },
251
+
"children": { "@reverse": "https://example.com/parent" }
252
+
}
253
+
}
254
+
"""
255
+
)
256
+
257
+
258
+
def test_parsing() -> None:
259
+
"""
260
+
A `Context` can be parsed from a dict.
261
+
"""
262
+
ctx = Context(DIVERSE_CONTEXT)
263
+
assert f"{EG}" == ctx.terms["ex"].id
264
+
assert f"{PROV.generatedAtTime}" == ctx.terms["generatedAt"].id
265
+
assert f"{XSD.dateTime}" == ctx.terms["generatedAt"].type
266
+
assert f"{EG.graphMap}" == ctx.terms["graphMap"].id
267
+
assert {"@graph", "@id"} == ctx.terms["graphMap"].container
268
+
assert f"{EG.occupation}" == ctx.terms["occupation_en"].id
269
+
assert "en" == ctx.terms["occupation_en"].language
270
+
assert False is ctx.terms["occupation_en"].reverse
271
+
assert True is ctx.terms["children"].reverse
272
+
assert f"{EG.parent}" == ctx.terms["children"].id
273
+
274
+
275
+
def test_to_dict() -> None:
276
+
"""
277
+
A `Context` can be converted to a dictionary.
278
+
"""
279
+
ctx = Context()
280
+
ctx.add_term("ex", f"{EG}")
281
+
ctx.add_term("generatedAt", f"{PROV.generatedAtTime}", coercion=f"{XSD.dateTime}")
282
+
ctx.add_term("graphMap", f"{EG.graphMap}", container=["@graph", "@id"])
283
+
ctx.add_term("occupation_en", f"{EG.occupation}", language="en")
284
+
ctx.add_term("children", f"{EG.parent}", reverse=True)
285
+
result = ctx.to_dict()
286
+
result["graphMap"]["@container"] = sorted(result["graphMap"]["@container"])
287
+
assert DIVERSE_CONTEXT["@context"] == result
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
1
+
import json
2
+
import logging
3
+
import pprint
4
+
from typing import Any, Dict, Union
5
+
6
+
import pytest
7
+
8
+
from rdflib import Graph
9
+
from rdflib.namespace import Namespace
10
+
from rdflib.plugins.shared.jsonld.context import Context
11
+
12
+
EG = Namespace("http://example.org/")
13
+
14
+
15
+
@pytest.mark.parametrize(
16
+
["input"],
17
+
[
18
+
(
19
+
Context(
20
+
{
21
+
"eg": f"{EG}",
22
+
}
23
+
),
24
+
),
25
+
({"eg": f"{EG}"},),
26
+
],
27
+
)
28
+
def test_serialize_context(input: Union[Dict[str, Any], Context]) -> None:
29
+
"""
30
+
The JSON-LD serializer accepts and correctly serializes the context argument to the output.
31
+
"""
32
+
graph = Graph()
33
+
graph.add((EG.subject, EG.predicate, EG.object0))
34
+
graph.add((EG.subject, EG.predicate, EG.object1))
35
+
context = Context(
36
+
{
37
+
"eg": f"{EG}",
38
+
}
39
+
)
40
+
logging.debug("context = %s", pprint.pformat(vars(context)))
41
+
data = graph.serialize(format="json-ld", context=context)
42
+
logging.debug("data = %s", data)
43
+
obj = json.loads(data)
44
+
assert obj["@context"] == {"eg": f"{EG}"}
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4