forked from zxenia/jsonld-to-rdf-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.py
84 lines (67 loc) · 2.4 KB
/
converter.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import logging
import getopt
from sys import argv
from rdflib import Graph
import json
from rdflib.plugin import register, Serializer
register('json-ld', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')
logger = logging.getLogger(__name__)
def main(argv):
FORMAT = '%(message)s'
logging.basicConfig(format=FORMAT)
logging.getLogger().setLevel(logging.INFO)
opts, args = getopt.getopt(argv, "", ["file=", "format="])
jsonld_filename = ''
output_format = None
for opt, arg in opts:
if opt == '--file':
jsonld_filename = arg
elif opt == '--format':
output_format = arg
if jsonld_filename == '':
help()
exit()
with open(jsonld_filename) as json_file:
jsonld_obj = json.load(json_file)
if output_format:
try:
jsonld_to_rdf(jsonld_obj, jsonld_filename.split('.jsonld')[0], output_format)
except Exception:
available_serializers()
exit()
else:
jsonld_to_rdf(jsonld_obj, jsonld_filename.split('.jsonld')[0])
def jsonld_to_rdf(jsonld_obj, filename, output_format=None):
"""
:param jsonld_obj: json object with LD context
:param output_format: rdf format to serialize to
:return: data serialized in rdf triples
"""
# Initiate a graph
g = Graph().parse(data=json.dumps(jsonld_obj), format='json-ld')
g.bind('dcat', 'http://www.w3.org/ns/dcat#')
g.bind('dct', 'http://purl.org/dc/terms/')
g.bind('sdm', 'https://smartdatamodels.org/')
# Available formats for serialization and corresponding file extensions
formats = {
'pretty-xml': 'rdf',
'n3': 'n3',
'nt': 'nt',
'trig': 'trig',
'turtle': 'ttl',
'xml': 'rdf',
'json-ld': 'json-ld'
}
if output_format:
rdf_data = g.serialize(destination='{}.{}'.format(filename, formats[output_format]),
format=output_format)
else:
# By default serialize to rdf/xml
rdf_data = g.serialize(destination='{}.rdf'.format(filename), format='pretty-xml')
return rdf_data
def help():
return logger.info('Usage: python converter.py --file=doc.jsonld [--format=turtle]')
def available_serializers():
return logger.info('Available serializer formats are pretty-xml, n3, nt, trig, turtle, xml')
if __name__ == "__main__":
main(argv[1:])