-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdfconv.py
61 lines (51 loc) · 1.87 KB
/
rdfconv.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
import os
import sys
from rdflib import Graph
from tabulate import tabulate
formats = {
".jsonld": "json-ld",
".n3": "n3",
".nt": "nt",
".rdf": "xml",
".owl": "xml",
".xml": "xml",
".trig": "trig",
".ttl": "turtle"
}
########################################################################
# ERROR MESSAGES
########################################################################
n = len(sys.argv)
if n < 3:
print("\nUsage: rdfconv <input file or URL> <output file>. The allowed input and output file formats are: \n")
print(tabulate(
[['JSON-LD', '.jsonld'], ['N3', '.n3'], ['N-Triples', '.nt'], ['RDF/XML', '.rdf'],
['RDF/XML', '.owl'], ['RDF/XML', '.xml'], ['TriG', '.trig'], ['Turtle', '.ttl']],
headers=['Syntax', 'File Extension']))
print("")
exit(1)
########################################################################
# EXECUTION
########################################################################
g = Graph()
try:
g.parse(sys.argv[1])
except OSError:
print(f"\nCould not READ file {sys.argv[1]}. Please provide a valid input file. Exiting program.\n")
exit(1)
target_extension = os.path.splitext(sys.argv[2])[1]
if target_extension in formats:
try:
with open(sys.argv[2], "w", encoding="utf-8") as f:
f.write(g.serialize(format=formats[target_extension]))
except OSError:
print(f"\nCould not WRITE file {sys.argv[2]}. Exiting program.\n")
exit(1)
else:
print("\nPlease provide a valid output file name. The allowed input and output file formats are: \n")
print(tabulate(
[['JSON-LD', '.jsonld'], ['N3', '.n3'], ['N-Triples', '.nt'], ['RDF/XML', '.rdf'],
['RDF/XML', '.owl'], ['RDF/XML', '.xml'], ['TriG', '.trig'], ['Turtle', '.ttl']],
headers=['Syntax', 'File Extension']))
print("\nExiting program.\n")
exit(1)