forked from catalyst-fp7/ontology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_jsonld.py
executable file
·37 lines (32 loc) · 1.32 KB
/
save_jsonld.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
#!/usr/bin/python
import sys
from argparse import ArgumentParser, FileType
import simplejson as json
from rdflib import ConjunctiveGraph
context_url = 'http://purl.org/conversence/jsonld'
def convert(context, input, output, input_format):
context = json.load(context)
g = ConjunctiveGraph()
g.parse(data=input.read(), format=input_format)
# It should be as simple as
# f.write(g.serialize(format='json-ld', indent=2, context=context))
# Bug in rdflib: Above loses the TextPositionSelector.
jgraph = json.loads(g.serialize(format='json-ld', context=context))
jgraph['@context'] = context_url
json.dump(jgraph, output, indent=" ")
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--context', '-c', type=FileType('r'),
help="The context file")
parser.add_argument('--format', '-f', default='trig',
help="The input format (as defined in rdflib)")
parser.add_argument('--output', '-o', type=FileType('w'),
default=sys.stdout, help="the output file")
parser.add_argument('input_fname', help="the input file",
type=FileType('r'))
args = parser.parse_args()
context = args.context
if not context:
import requests
context = requests.get(context_url)
convert(context, args.input_fname, args.output, args.format)