-
Notifications
You must be signed in to change notification settings - Fork 0
/
gerfo.py
114 lines (98 loc) · 4.51 KB
/
gerfo.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2018, S. <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
__author__ = 'essepuntato'
import argparse
import os
import shutil
import logging
from rdflib import Graph
# Formats available
formats = {
"json-ld": "json",
"xml": "xml",
"turtle": "ttl",
"nt": "nt"
}
def load(rdf_file_path, tmp_dir=None):
if os.path.isfile(rdf_file_path):
try:
cur_graph = __load_graph(rdf_file_path)
return cur_graph
except IOError:
if tmp_dir is not None:
current_file_path = tmp_dir + os.sep + "tmp_rdf_file.rdf"
shutil.copyfile(rdf_file_path, current_file_path)
try:
cur_graph = __load_graph(current_file_path)
return cur_graph
except IOError as e:
log.error("It was impossible to handle the format used for "
"storing the file (stored in the temporary path) '%s'. "
"Additional details: %s" % (current_file_path, str(e)))
os.remove(current_file_path)
else:
log.error("It was impossible to try to load the file from the "
"temporary path '%s' since that has not been specified in "
"advance" % rdf_file_path)
else:
log.error("The file specified ('%s') doesn't exist." % rdf_file_path)
def __load_graph(file_path):
errors = ""
current_graph = Graph()
for cur_format in formats:
try:
current_graph.parse(file_path, format=cur_format)
return current_graph
except Exception as e:
errors = " | " + str(e) # Try another format
raise IOError("1", "It was impossible to handle the format used for storing the file '%s'%s" %
(file_path, errors))
def store(dest_dir, file_name, f):
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
dest_file = dest_dir + os.sep + file_name
g.serialize(dest_file, format=f)
log.info("RDF graph serialised in %s format and stored in %s" % (f, dest_file))
# Main
if __name__ == "__main__":
arg_parser = argparse.ArgumentParser("gerfo.py - generate RDF formats in Python")
arg_parser.add_argument("-s", "--src", dest="source_file", required=True,
help="The source RDF file.")
arg_parser.add_argument("-d", "--dst", dest="dest_dir", required=True,
help="The directory where to store all the converted data.")
arg_parser.add_argument("-v", "--ver", dest="version_dir",
help="The directory where to store the current version of the RDF file.")
arg_parser.add_argument("-t", "--tmp", dest="tmp_dir",
help="A temporary directory that will be used in case there will be "
"issues in loading RDF graphs from files which have a path "
"with special characters (which is a bug introduced by RDFLIB).")
args = arg_parser.parse_args()
# Set logger
log = logging.getLogger("GeRFo logger")
log_h = logging.StreamHandler()
log_f = logging.Formatter('%(levelname)s - %(message)s')
log_h.setFormatter(log_f)
log.addHandler(log_h)
log.setLevel(logging.INFO)
g = load(args.source_file, args.tmp_dir)
if g is not None:
cur_name = os.path.splitext(os.path.basename(args.source_file))[0]
for f in formats:
store(args.dest_dir, cur_name + "." + formats[f], f)
if args.version_dir is not None:
store(args.version_dir, cur_name + "." + formats[f], f)
# How to call it for a specific ontology:
# python gerfo.py -s ../cito/docs/current/cito.owl -d ../cito/docs/current -v ../cito/docs/2017-10-30