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
|
import json
from rdflib import Graph
from rdflib.compare import isomorphic
def test_wrap():
"""
Example of intercepting a JSON-LD structure and performing some
in-memory manipulation and then passing that structure to Graph.parse
lists in the shacl graph.
"""
_data = """
{
"@context" : {
"ngff" : "http://example.com/ns#"
},
"@graph": [{
"@type": "ngff:ItemList",
"ngff:collectionType": {"@type": "ngff:Image"},
"ngff:itemListElement": [
{
"@type": "ngff:Image",
"path": "image1",
"name": "Image 1"
},
{
"@type": "ngff:Image",
"path": "something-else",
"name": "bob"
}
]
}]
}
"""
# Current workaround
data = json.loads(_data)
data = walk(data)
data = json.dumps(data) # wasteful
g1 = Graph()
g1.parse(data=data, format="json-ld")
# Desired behavior
data = json.loads(_data)
data = walk(data)
g2 = Graph()
g2.parse(data=data, format="json-ld")
assert isomorphic(g1, g2)
def walk(data, path=None):
"""
Some arbitrary operation on a Python data structure.
"""
if path is None:
path = []
if isinstance(data, dict):
for k, v in data.items():
data[k] = walk(v, path + [k])
elif isinstance(data, list):
replacement = list()
for idx, item in enumerate(data):
if path[-1] == "@graph":
replacement.append(walk(item, path))
else:
wrapper = {"@type": "ListItemWrapper", "ngff:position": idx}
wrapper["ngff:item"] = walk(item, path + [idx])
replacement.append(wrapper)
data = replacement
return data
|