This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
/
app.py
701 lines (622 loc) · 24.5 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
import sys
import io
import json
import requests
from rdflib import Graph
from flask import (
Flask,
Response,
request,
render_template,
Markup,
g,
redirect,
url_for,
)
from vocprez.model import *
from vocprez import _config as config
import vocprez.utils as u
from pyldapi import Renderer, ContainerRenderer
from vocprez.model import VocPrezRenderer, VocabulariesRenderer
import logging
import vocprez.source as source
import markdown
from flask_compress import Compress
from flaskext.markdown import Markdown
logging.basicConfig(
filename=config.LOGFILE,
level=logging.DEBUG,
datefmt="%Y-%m-%d %H:%M:%S",
format="%(asctime)s %(levelname)s %(filename)s:%(lineno)s %(message)s",
)
app = Flask(
__name__, template_folder=config.TEMPLATES_DIR, static_folder=config.STATIC_DIR
)
app.config["COMPRESS_MIMETYPES"] = [
'text/html',
'text/css',
'text/xml',
'application/json',
'application/geo+json',
'application/javascript',
] + Renderer.RDF_MEDIA_TYPES
Compress(app)
Markdown(app)
# FUNCTION before_request
@app.before_request
def before_request():
"""
Runs before every request and populates vocab index either from disk (VOCABS.p) or from a complete reload by
calling collect() for each of the vocab sources defined in config/__init__.py -> VOCAB_SOURCES
:return: nothing
"""
logging.debug("before_request()")
# always rebuild if DEBUG True
if config.DEBUG:
u.cache_reload()
elif hasattr(g, "VOCABS"):
# if g.VOCABS exists, if so, do nothing
pass
else:
u.cache_load()
# END FUNCTION before_request
# FUNCTION context_processor
@app.context_processor
def context_processor():
"""
A set of variables available globally for all Jinja templates.
:return: A dictionary of variables
:rtype: dict
"""
return dict(
utils=u, # gives access to all functions defined in utils.py
)
# END FUNCTION context_processor
# ROUTE index
@app.route("/")
def index():
return VocPrezRenderer(
request,
config.SYSTEM_BASE_URI,
config.VOCS_URI,
config.VOCS_TITLE,
config.VOCS_DESC,
g.VOCABS
).render()
# END ROUTE index
# ROUTE vocabs
@app.route("/vocab/")
def vocabularies():
return VocabulariesRenderer(
request,
g.VOCABS,
config.SYSTEM_BASE_URI,
config.VOCS_URI,
config.VOCS_TITLE,
config.VOCS_DESC
).render()
# END ROUTE vocabs
# ROUTE one vocab
@app.route("/vocab/<string:vocab_id>/")
def vocabulary(vocab_id):
if vocab_id not in [x.id for x in g.VOCABS.values()]:
return return_vocprez_error(
"vocab_id not valid",
400,
markdown.markdown(
"The 'vocab_id' you supplied, {}, is not known. Valid vocab_ids are:\n\n{}".format(
vocab_id,
"\n".join(
["* [{}]({}): {}".format(x.id, u.get_content_uri(x.uri), x.title) for x in g.VOCABS.values()])
)
)
)
for v in g.VOCABS.values():
if v.id == vocab_id:
return redirect(url_for("object") + "?uri=" + v.uri)
# END ROUTE one vocab
# ROUTE concepts
@app.route("/vocab/<vocab_id>/concept/")
def concepts(vocab_id):
if vocab_id not in [x.id for x in g.VOCABS.values()]:
return return_vocprez_error(
"vocab_id not valid",
400,
markdown.markdown(
"The 'vocab_id' you supplied, {}, is not known. Valid vocab_ids are:\n\n{}".format(
vocab_id,
"\n".join(["* [{}]({}): {}".format(
x.id, u.get_content_uri(x.uri), x.title) for x in g.VOCABS.values()])
)
)
)
try:
vocab_source = getattr(source, g.VOCABS[vocab_id].source)(vocab_id, request)
cpts = vocab_source.list_concepts()
# concepts.sort(key=lambda x: x["prefLabel"]) -- sort not needed when receiving pre-sorted tuples
total = len(cpts)
page = (
int(request.values.get("page")) if request.values.get("page") is not None else 1
)
per_page = (
int(request.values.get("per_page"))
if request.values.get("per_page") is not None
else 20
)
start = (page - 1) * per_page
end = start + per_page
members = cpts[start:end]
except Exception as e:
return Response(
str(e),
status=500,
mimetype="text/plain"
)
return ContainerRenderer(
request,
url_for("concepts", vocab_id=vocab_id),
"All Concepts",
'All Concepts within Vocab {}'.format(vocab_id),
None,
None,
members,
total
).render()
# END ROUTE concepts
# ROUTE object
@app.route("/object")
def object():
"""
This is the general RESTful endpoint and corresponding Python function to handle requests for individual objects,
be they a Vocabulary (ConceptScheme), a Collection or Concept. Only those 3 classes of object are supported for the
moment.
An HTTP URI query string argument parameter 'uri' must be supplied, indicating the URI of the object being requested
:return: A Flask Response object
:rtype: :class:`flask.Response`
"""
uri = request.values.get("uri")
# must have a URI or Vocab URI supplied
if uri is None:
return return_vocprez_error(
"Input Error",
400,
"A Query String Argument of 'uri' must be supplied for this endpoint"
)
if uri == config.SYSTEM_BASE_URI or uri == config.SYSTEM_BASE_URI + "/":
return index()
if uri == config.VOCS_URI or uri == config.VOCS_URI + "/":
return vocabularies()
# get the class of the object
q = """
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?c ?cs
WHERE {
GRAPH ?g {
<xxx> a ?c .
OPTIONAL {
VALUES ?memberof { skos:inScheme skos:topConceptOf }
<xxx> ?memberof ?cs .
}
}
}
""".replace("xxx", uri)
cs = None
for r in u.sparql_query(q):
if r["c"]["value"] == "http://www.w3.org/2004/02/skos/core#ConceptScheme":
if uri in g.VOCABS.keys():
# get vocab details using appropriate source handler
vocab = source.SPARQL(request).get_vocabulary(uri)
return VocabularyRenderer(request, vocab).render()
else:
return None
elif r["c"]["value"] == "http://www.w3.org/2004/02/skos/core#Collection":
try:
c = source.SPARQL(request).get_collection(uri)
return CollectionRenderer(request, c).render()
except:
return None
elif r["c"]["value"] == "http://www.w3.org/2004/02/skos/core#Concept":
try:
if r.get("cs"):
cs = r["cs"]["value"]
c = source.SPARQL(request).get_concept(cs, uri)
return ConceptRenderer(request, c).render()
except:
return None
return return_vocprez_error(
"Input Error",
400,
"The object with URI {} is not of type skos:ConceptScheme, skos:Collection or skos:Concept "
"and only these classes of object are understood by VocPrez".format(uri)
)
# END ROUTE object
# ROUTE about
@app.route("/about")
def about():
import os
# using basic Markdown method from http://flask.pocoo.org/snippets/19/
with open(os.path.join(config.APP_DIR, "..", "README.md")) as f:
content = f.read()
# make images come from web dir
content = content.replace(
"vocprez/view/style/", request.url_root + "style/"
)
content = Markup(markdown.markdown(content, extensions=['tables']))
return render_template(
"about.html",
title="About",
navs={},
content=content
)
# END ROUTE about
# ROUTE sparql
@app.route("/sparql", methods=["GET", "POST"])
@app.route("/sparql/", methods=["GET", "POST"])
def sparql():
# queries to /sparql with an accept header set to a SPARQL return type or an RDF type
# are forwarded to /endpoint for a response
# all others (i.e. with no Accept header, an Accept header HTML or for an unsupported Accept header
# result in the SPARQL page HTML respose where the query is placed into the YasGUI UI for interactive querying
SPARQL_RESPONSE_MEDIA_TYPES = [
"application/sparql-results+json",
"text/csv",
"text/tab-separated-values",
]
QUERY_RESPONSE_MEDIA_TYPES = ["text/html"] + SPARQL_RESPONSE_MEDIA_TYPES + Renderer.RDF_MEDIA_TYPES
accept_type = request.accept_mimetypes.best_match(QUERY_RESPONSE_MEDIA_TYPES, "text/html")
logging.debug("accept_type: " + accept_type)
if accept_type in SPARQL_RESPONSE_MEDIA_TYPES or accept_type in Renderer.RDF_MEDIA_TYPES:
# return data
logging.debug("returning endpoint()")
return endpoint()
else:
# return HTML UI
logging.debug("returning sparql()")
return render_template(
"sparql.html",
)
# END ROUTE sparql
# ROUTE search
@app.route("/search")
def search():
if request.values.get("search"):
last_search = request.values.get("search")
if request.values.get("from") and request.values.get("from") != "all":
grf = '<' + request.values.get("from") + '>'
bind = f"BIND ({grf} AS ?g)"
else:
grf = "?g"
bind = ""
q = """
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?g ?uri ?pl (SUM(?w) AS ?weight)
WHERE {{
GRAPH {grf} {{
{{ # exact match on a prefLabel always wins
?uri a skos:Concept ;
skos:prefLabel ?pl .
BIND (50 AS ?w)
FILTER REGEX(?pl, "^{input}$", "i")
}}
UNION
{{
?uri a skos:Concept ;
skos:prefLabel ?pl .
BIND (10 AS ?w)
FILTER REGEX(?pl, "{input}", "i")
}}
UNION
{{
?uri a skos:Concept ;
skos:altLabel ?al ;
skos:prefLabel ?pl .
BIND (5 AS ?w)
FILTER REGEX(?al, "{input}", "i")
}}
UNION
{{
?uri a skos:Concept ;
skos:hiddenLabel ?hl ;
skos:prefLabel ?pl .
BIND (5 AS ?w)
FILTER REGEX(?hl, "{input}", "i")
}}
UNION
{{
?uri a skos:Concept ;
skos:definition ?d ;
skos:prefLabel ?pl .
BIND (1 AS ?w)
FILTER REGEX(?d, "{input}", "i")
}}
}}
{bind}
}}
GROUP BY ?g ?uri ?pl
ORDER BY DESC(?weight)
""".format(**{"grf": grf, "input": request.values.get("search"), "bind": bind})
results = {}
for r in u.sparql_query(q):
if r.get("uri") is None:
break # must do this check as r["weight"] will appear at least once with value 0 for no results
if r["g"]["value"] not in results.keys():
results[r["g"]["value"]] = []
results[r["g"]["value"]].append((r["uri"]["value"], r["pl"]["value"]))
return render_template(
"search.html",
vocabs=[(v.uri, v.title) for k, v in g.VOCABS.items()],
last_search=last_search,
selected_vocab=request.values.get("from"),
results=results
)
else:
return render_template(
"search.html",
vocabs=[(v.uri, v.title) for k, v in g.VOCABS.items()]
)
# END ROUTE search
# ROUTE endpoint
# the SPARQL endpoint under-the-hood
@app.route("/endpoint", methods=["GET", "POST"])
def endpoint():
"""
TESTS
Form POST:
curl -X POST -d query="PREFIX%20skos%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2004%2F02%2Fskos%2Fcore%23%3E%0ASELECT%20* \
%20WHERE%20%7B%3Fs%20a%20skos%3AConceptScheme%20.%7D" http://localhost:5000/endpoint
Raw POST:
curl -X POST -H 'Content-Type: application/sparql-query' --data-binary @query.sparql http://localhost:5000/endpoint
using query.sparql:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT * WHERE {?s a skos:ConceptScheme .}
GET:
curl http://localhost:5000/endpoint?query=PREFIX%20skos%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2004%2F02%2Fskos%2Fcore \
%23%3E%0ASELECT%20*%20WHERE%20%7B%3Fs%20a%20skos%3AConceptScheme%20.%7D
GET CONSTRUCT:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
CONSTRUCT {?s a rdf:Resource}
WHERE {?s a skos:ConceptScheme}
curl -H 'Accept: application/ld+json' http://localhost:5000/endpoint?query=PREFIX%20rdf%3A%20%3Chttp%3A%2F%2F \
www.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0APREFIX%20skos%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2004%2F02%2F \
skos%2Fco23%3E%0ACONSTRUCT%20%7B%3Fs%20a%20rdf%3AResource%7D%0AWHERE%20%7B%3Fs%20a%20skos%3AConceptScheme%7D
"""
logging.debug("request: {}".format(request.__dict__))
def get_sparql_service_description(rdf_fmt="turtle"):
"""Return an RDF description of PROMS' read only SPARQL endpoint in a requested format
:param rdf_fmt: 'turtle', 'n3', 'xml', 'json-ld'
:return: string of RDF in the requested format
"""
sd_ttl = """
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sd: <http://www.w3.org/ns/sparql-service-description#> .
@prefix sdf: <http://www.w3.org/ns/formats/> .
@prefix void: <http://rdfs.org/ns/void#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<{0}>
a sd:Service ;
sd:endpoint <{0}> ;
sd:supportedLanguage sd:SPARQL11Query ; # yes, read only, sorry!
sd:resultFormat sdf:SPARQL_Results_JSON ; # yes, we only deliver JSON results, sorry!
sd:feature sd:DereferencesURIs ;
sd:defaultDataset [
a sd:Dataset ;
sd:defaultGraph [
a sd:Graph ;
void:triples "100"^^xsd:integer
]
]
.
""".format(config.SYSTEM_BASE_URI + url_for("sparql"))
grf = Graph().parse(io.StringIO(sd_ttl), format="turtle")
rdf_formats = list(set([x for x in Renderer.RDF_SERIALIZER_TYPES_MAP]))
if rdf_fmt in rdf_formats:
return grf.serialize(format=rdf_fmt)
else:
raise ValueError(
"Input parameter rdf_format must be one of: " + ", ".join(rdf_formats)
)
def sparql_query2(q, mimetype="application/json"):
""" Make a SPARQL query"""
logging.debug("sparql_query2: {}".format(q))
data = q
headers = {
"Content-Type": "application/sparql-query",
"Accept": mimetype,
"Accept-Encoding": "UTF-8",
}
if config.SPARQL_USERNAME is not None and config.SPARQL_PASSWORD is not None:
auth = (config.SPARQL_USERNAME, config.SPARQL_PASSWORD)
else:
auth = None
try:
logging.debug(
"endpoint={}\ndata={}\nheaders={}".format(
config.SPARQL_ENDPOINT, data, headers
)
)
if auth is not None:
r = requests.post(
config.SPARQL_ENDPOINT, auth=auth, data=data, headers=headers, timeout=60
)
else:
r = requests.post(
config.SPARQL_ENDPOINT, data=data, headers=headers, timeout=60
)
logging.debug("response: {}".format(r.__dict__))
return r.content.decode("utf-8")
except Exception as ex:
raise ex
format_mimetype = request.headers["ACCEPT"]
# Query submitted
if request.method == "POST":
"""Pass on the SPARQL query to the underlying endpoint defined in config
"""
if "application/x-www-form-urlencoded" in request.content_type:
"""
https://www.w3.org/TR/2013/REC-sparql11-protocol-20130321/#query-via-post-urlencoded
2.1.2 query via POST with URL-encoded parameters
Protocol clients may send protocol requests via the HTTP POST method by URL encoding the parameters. When
using this method, clients must URL percent encode all parameters and include them as parameters within the
request body via the application/x-www-form-urlencoded media type with the name given above. Parameters must
be separated with the ampersand (&) character. Clients may include the parameters in any order. The content
type header of the HTTP request must be set to application/x-www-form-urlencoded.
"""
if (
request.values.get("query") is None
or len(request.values.get("query")) < 5
):
return Response(
"Your POST request to the SPARQL endpoint must contain a 'query' parameter if form posting "
"is used.",
status=400,
mimetype="text/plain",
)
else:
query = request.values.get("query")
elif "application/sparql-query" in request.content_type:
"""
https://www.w3.org/TR/2013/REC-sparql11-protocol-20130321/#query-via-post-direct
2.1.3 query via POST directly
Protocol clients may send protocol requests via the HTTP POST method by including the query directly and
unencoded as the HTTP request message body. When using this approach, clients must include the SPARQL query
string, unencoded, and nothing else as the message body of the request. Clients must set the content type
header of the HTTP request to application/sparql-query. Clients may include the optional default-graph-uri
and named-graph-uri parameters as HTTP query string parameters in the request URI. Note that UTF-8 is the
only valid charset here.
"""
query = request.data.decode("utf-8") # get the raw request
if query is None:
return Response(
"Your POST request to this SPARQL endpoint must contain the query in plain text in the "
"POST body if the Content-Type 'application/sparql-query' is used.",
status=400,
)
else:
return Response(
"Your POST request to this SPARQL endpoint must either the 'application/x-www-form-urlencoded' or"
"'application/sparql-query' ContentType.",
status=400,
)
try:
if "CONSTRUCT" in query:
format_mimetype = "text/turtle"
return Response(
sparql_query2(
query, mimetype=format_mimetype
),
status=200,
mimetype=format_mimetype,
)
else:
return Response(
sparql_query2(query, format_mimetype),
status=200,
)
except ValueError as e:
return Response(
"Input error for query {}.\n\nError message: {}".format(query, str(e)),
status=400,
mimetype="text/plain",
)
except ConnectionError as e:
return Response(str(e), status=500)
else: # GET
if request.args.get("query") is not None:
# SPARQL GET request
"""
https://www.w3.org/TR/2013/REC-sparql11-protocol-20130321/#query-via-get
2.1.1 query via GET
Protocol clients may send protocol requests via the HTTP GET method. When using the GET method, clients must
URL percent encode all parameters and include them as query parameter strings with the names given above.
HTTP query string parameters must be separated with the ampersand (&) character. Clients may include the
query string parameters in any order.
The HTTP request MUST NOT include a message body.
"""
query = request.args.get("query")
if "CONSTRUCT" in query:
acceptable_mimes = [x for x in Renderer.RDF_MEDIA_TYPES]
best = request.accept_mimetypes.best_match(acceptable_mimes)
query_result = sparql_query2(
query, mimetype=best
)
file_ext = {
"text/turtle": "ttl",
"application/rdf+xml": "rdf",
"application/ld+json": "json",
"text/n3": "n3",
"application/n-triples": "nt",
}
return Response(
query_result,
status=200,
mimetype=best,
headers={
"Content-Disposition": "attachment; filename=query_result.{}".format(
file_ext[best]
)
},
)
else:
query_result = sparql_query2(query)
return Response(
query_result, status=200, mimetype="application/sparql-results+json"
)
else:
# SPARQL Service Description
"""
https://www.w3.org/TR/sparql11-service-description/#accessing
SPARQL services made available via the SPARQL Protocol should return a service description document at the
service endpoint when dereferenced using the HTTP GET operation without any query parameter strings
provided. This service description must be made available in an RDF serialization, may be embedded in
(X)HTML by way of RDFa, and should use content negotiation if available in other RDF representations.
"""
acceptable_mimes = [x for x in Renderer.RDF_MEDIA_TYPES] + ["text/html"]
best = request.accept_mimetypes.best_match(acceptable_mimes)
if best == "text/html":
# show the SPARQL query form
return redirect(url_for("sparql"))
elif best is not None:
for item in Renderer.RDF_MEDIA_TYPES:
if item == best:
rdf_format = best
return Response(
get_sparql_service_description(
rdf_fmt=rdf_format
),
status=200,
mimetype=best,
)
return Response(
"Accept header must be one of " + ", ".join(acceptable_mimes) + ".",
status=400,
)
else:
return Response(
"Accept header must be one of " + ", ".join(acceptable_mimes) + ".",
status=400,
)
# END ROUTE endpoint
# FUNCTION return_vocrez_error
# TODO: allow conneg - at least text v. HTML
def return_vocprez_error(title, status, message):
return render_template(
"error.html",
title=title,
status=status,
msg=message
), status
# END FUNCTION return_vocrez_error
# ROUTE cache_reload
@app.route("/cache-reload")
def cache_reload():
u.cache_reload()
return Response(
"Cache reloaded",
status=200,
mimetype="text/plain"
)
# END ROUTE cache_reload
# run the Flask app
if __name__ == "__main__":
app.run(debug=config.DEBUG, threaded=True, port=config.PORT)