-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
258 lines (201 loc) · 7.43 KB
/
app.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import json
import re
import sys
import os
from flask import Flask, render_template, request, make_response, send_from_directory
from waitress import serve
import argparse
import mimeparse
import traceback
from rdflib import URIRef
from datetime import datetime
from app import CypherBackend, SparqlProxy, ApiError, Config
# TODO: move to library file
def file_info(path, name):
stat = os.stat(os.path.join(path, name))
return {
"name": name,
"time": datetime.fromtimestamp(stat.st_mtime),
"size": stat.st_size
}
def jsonify(data, status=200, indent=3, sort_keys=False):
response = make_response(json.dumps(
data, indent=indent, sort_keys=sort_keys))
response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.headers['mimetype'] = 'application/json'
response.headers['Access-Control-Allow-Origin'] = '*'
response.status_code = status
return response
app = Flask(__name__)
def render(template, **vars):
# TODO: better title?
title = template.split(".")[0]
return render_template(template, title=title, githash=app.config["githash"], **vars)
@app.errorhandler(ApiError)
def handle_api_error(error):
response = jsonify(error.to_dict())
response.status_code = error.status
return response
@app.errorhandler(Exception)
def handle_exception(error):
if app.config["debug"]:
print(traceback.format_exc())
if hasattr(error, 'message'):
message = error.message
else:
message = str(error)
return handle_api_error(ApiError(message))
@app.route('/')
def index():
return render('index.html')
@app.route('/license')
def license():
return render('license.html')
@app.context_processor
def utility_processor():
return dict(URIRef=URIRef)
rdf_formats = {
'application/x-turtle': 'turtle',
'text/turtle': 'turtle',
'application/rdf+xml': 'xml',
'application/trix': 'trix',
'application/n-quads': 'nquads',
'application/n-triples': 'nt',
'text/n-triples': 'nt',
'text/rdf+nt': 'nt',
'application/n3': 'n3',
'text/n3': 'n3',
'text/rdf+n3': 'n3'
}
@app.route('/terminology')
@app.route('/terminology/')
def terminology():
# TODO: server RDF as well
return render('terminologies.html')
@app.route('/repository')
@app.route('/repository/')
def repository():
return render('repositories.html')
@app.route('/collection', defaults={'id': None, 'path': None})
@app.route('/collection/', defaults={'id': None, 'path': None})
@app.route('/collection/<int:id>', defaults={'path': None})
@app.route('/collection/<int:id>/', defaults={'path': ""})
@app.route('/collection/<int:id>/<path:path>')
def collection(id, path):
if not id:
# TODO: server RDF as well
return render('collections.html')
format = request.args.get("format")
html_wanted = "html" in request.headers["Accept"] or format == "html"
stage_base = app.config.get("stage")
stage_path = os.path.join(stage_base, str(id)) if stage_base else None
if path is not None:
if stage_base:
if path == "":
files = map(lambda f: file_info(stage_path, f),
os.listdir(stage_path))
return render('import.html', files=files, id=id)
else:
return send_from_directory(stage_path, path)
# TODO: more beautiful message
return "Not found!"
uri = "https://graph.nfdi4objects.net/collection/" + str(id)
graph = app.config["sparql-proxy"].request(
"DESCRIBE <" + uri + ">",
{"named-graph-uri": "https://graph.nfdi4objects.net/collection/"})
if html_wanted:
if len(graph) > 0:
stage = "./" + \
str(id) + "/" if stage_path and os.path.isdir(stage_path) else None
return render('collection.html', uri=uri, graph=graph, stage=stage)
else:
return render('collection.html', uri=uri, graph=None), 404
else:
mimetype = "text/plain"
if format in set(rdf_formats.values()):
mimetype = [
type for type in rdf_formats if rdf_formats[type] == format][0]
else:
accept = request.headers.get("Accept")
types = list(rdf_formats.keys())
mimetype = mimeparse.best_match(types, accept)
if mimetype in rdf_formats:
format = rdf_formats[mimetype]
else:
format = "turtle"
mimetype = "text/turtle"
print("Format, mimetype")
print(format, mimetype)
response = make_response("Not found", 404)
response.mimetype = "text/plain"
if len(graph) > 0:
# TODO: add known namespaces for pretty Turtle
response = make_response(graph.serialize(format=format), 200)
response.mimetype = mimetype
return response
# Detect write queries the simple way. This also block some valid read-queries.
def isAllowedCypherQuery(cmd: str) -> bool:
return re.search('merge|create|delete|set', cmd, re.IGNORECASE) is None
@app.route('/api/cypher', methods=('GET', 'POST'))
def cypher_api():
query = ''
if 'query' in request.args: # GET
query = request.args.get('query')
elif request.data: # POST
query = request.data.decode('UTF-8')
if query:
if isAllowedCypherQuery(query):
answer = app.config["cypher-backend"].execute(query)
else:
raise ApiError("Cypher query is not allowed!", 403)
else:
raise ApiError('missing or empty "query" parameter', 400)
return jsonify(answer)
@app.route('/api/sparql', methods=('GET', 'POST'))
def sparql_api():
return app.config["sparql-proxy"].proxyRequest(request)
@app.route('/cypher')
def cypher_form():
return render('cypher.html')
@app.route('/sparql')
def sparql_form():
return render('sparql.html', **config["sparql"])
@app.route('/tools')
def tools():
return render('tools.html')
def quit(msg):
print(msg, file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port', type=int,
default=8000, help="Server port")
parser.add_argument(
'-w', '--wsgi', action=argparse.BooleanOptionalAction, help="Use WSGI server")
parser.add_argument('-c', '--config', type=str,
default="config.yaml", help="Config file")
parser.add_argument('-d', '--debug', action=argparse.BooleanOptionalAction)
args = parser.parse_args()
try:
config = Config(args.config, args.debug)
except Exception as err:
quit(str(err))
for key in config.keys():
app.config[key] = config[key]
endpoint = config["sparql"]["endpoint"]
app.config["sparql-proxy"] = SparqlProxy(endpoint, config["debug"])
try:
app.config["sparql-proxy"].alive()
except Exception:
quit(f"SPARQL endpoint {endpoint} is not available!")
if config["stage"] and not os.path.isdir(config["stage"]):
quit(f"N4o import directory {config['stage']} is not available!")
if "cypher" in config:
# TODO: check if backend is alive, else exit
print(f"Using Cypher backend {config['cypher']['uri']}")
app.config["cypher-backend"] = CypherBackend(config['cypher'])
opts = {"port": args.port, "debug": config["debug"]}
if args.wsgi:
serve(app, host="0.0.0.0", **opts)
else:
app.run(host="0.0.0.0", **opts)