/
context_switcher.py
57 lines (45 loc) · 1.37 KB
/
context_switcher.py
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
from pyld import jsonld
from pyld.jsonld import compact, expand, frame
import json
# Stop code from looking up the contexts online EVERY TIME
def load_document_local(url):
doc = {
'contextUrl': None,
'documentUrl': None,
'document': ''
}
if url == "http://iiif.io/api/presentation/2/context.json":
fn = "contexts/context_20.json"
elif url in ["http://www.w3.org/ns/oa.jsonld","http://www.w3.org/ns/oa-context-20130208.json"]:
fn = "contexts/context_oa.json"
else:
fn = "contexts/context_10.json"
fh = file(fn)
data = fh.read()
fh.close()
doc['document'] = data;
return doc
jsonld.set_document_loader(load_document_local)
# Load our (very simple) frame
fh = file('contexts/annotation_frame.json')
data = fh.read()
fh.close()
annoframe = json.loads(data)
# Output Context URI
contextURI = "http://www.w3.org/ns/oa-context-20130208.json"
def convert(anno):
# check we're already parsed
if not type(anno) == dict:
anno = json.loads(anno)
# rdf = expand(anno)
reframed = frame(anno, annoframe)
outjs = compact(reframed, contextURI)
return outjs
if __name__ == "__main__":
# Read example from the spec
fh = file('example_iiif_anno.json')
data = fh.read()
fh.close()
# And convert to OA
out = convert(data)
print json.dumps(out, sort_keys=True, indent=2)