-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
178 lines (106 loc) · 3.86 KB
/
server.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from flask import Flask, send_file, jsonify
from flask_swagger import swagger
from flask_swagger_ui import get_swaggerui_blueprint
from flask_restful import Resource, Api, reqparse
from werkzeug import datastructures
import tempfile
from flask_cors import CORS
import morph_kgc
import zipfile
import traceback
import rdflib
import io
import os
parser = reqparse.RequestParser()
parser.add_argument('mapping',type=datastructures.FileStorage, location='files')
parser.add_argument('data',type=datastructures.FileStorage, location='files')
app = Flask(__name__)
api = Api(app)
CORS(app)
SWAGGER_URL = '/docs'
API_URL = '/'
old = []
swaggerui_blueprint = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={
'app_name': "Morph-KGC API"
},
)
app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)
def purge_mapping(mapping_path, data_path):
try:
g = rdflib.Graph().parse(mapping_path, format="turtle")
for t in g:
if t[1] == rdflib.term.URIRef('http://semweb.mmlab.be/ns/rml#source'):
new = (t[0], t[1], rdflib.term.Literal(data_path+str(t[2]).replace("/data/", "")))
g.remove(t)
g.add(new)
#print("Removed:", t)
#print("Added:", new)
g.serialize(destination=mapping_path, format='nt', encoding="utf-8")
except:
print("Unable to parse mapping!")
def run_morph_kgc(mapping_path, output_path):
#data_path =
config = '''
[CONFIGURATION]
# INPUT
na_values=,#N/A,N/A,#N/A N/A,n/a,NA,,#NA,NULL,null,NaN,nan
[KNOWLEDGEGRAPH]
mappings:'''+mapping_path
graph = morph_kgc.materialize(config)
graph.serialize(destination=output_path, format='nt', encoding="utf-8")
class Server(Resource):
def get(self):
swag = swagger(app)
swag['info']['version'] = "1.5.0"
swag['info']['title'] = "Morph-KGC API"
return jsonify(swag)
def post(self):
with tempfile.TemporaryDirectory(prefix="morph-kgc") as run_dir:
data_dir = run_dir+"/data/"
#mapping_file = tempfile.NamedTemporaryFile(prefix="morph-kgc_mapping", suffix=".ttl", dir=run_dir).name
#data_file = tempfile.NamedTemporaryFile(prefix="morph-kgc_data", suffix=".zip", dir=run_dir).name
mapping_file = run_dir+"/mapping.ttl"
data_file_zip = run_dir+"/data.zip"
output_file = run_dir+"/result.nt"
output_file_compressed = run_dir+"/result.zip" #io.BytesIO()
data = parser.parse_args()
if data['mapping'] != None and data['data'] != None:
os.mkdir(data_dir)
data['mapping'].save(mapping_file)
print(data['data'])
if data['data'].mimetype == 'application/zip':
data['data'].save(data_file_zip)
with zipfile.ZipFile(data_file_zip, 'r') as zip_data:
print(zip_data.infolist())
zip_data.extractall(path=data_dir)
elif data['data'].mimetype == "text/csv" or data['data'].mimetype == "application/json" or data['data'].mimetype == "text/xml":
data['data'].save(data_dir+data['data'].filename)
else:
return ("Not supported data file, check contents or extension!", 400)
try:
purge_mapping(mapping_file, data_dir)
except:
return ("There is an error with your mapping!", 400)
try:
run_morph_kgc(mapping_file, output_file)
except:
return ("Materialization failed!", 400)
zipObj = zipfile.ZipFile(output_file_compressed, 'w')
zipObj.write(output_file, "result.nt")
zipObj.close()
#output_file_compressed.seek(0)
return send_file(output_file_compressed, mimetype='application/x-zip')
else:
print("Not valid request")
print(data)
abort(400)
api.add_resource(Server, API_URL)
def run(host="0.0.0.0", port=5000):
print("API URL: "+API_URL)
print("SWAGGER URL: "+SWAGGER_URL)
app.run(debug=False, host=host, port=port)
if __name__ == '__main__':
run()