Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Robs tweaks - a few graph fixes and safe heirarchy recursion #57

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f14ef74
add imports as an aoption
rob-metalinkage May 21, 2021
eb2f73d
Merge branch 'robs_tweaks' of https://github.com/rob-metalinkage/VocP…
rob-metalinkage May 21, 2021
efcac25
fix another graph issue and try to raise exception - still not reporting
rob-metalinkage May 26, 2021
fbb06dc
Merge remote-tracking branch 'upstream/master' into robs_tweaks
rob-metalinkage May 26, 2021
318060b
safe heirachy recursions
rob-metalinkage Jul 9, 2021
ae348bf
Merge remote-tracking branch 'upstream/master' into robs_tweaks
rob-metalinkage Jul 30, 2021
99f0fea
no message
rob-metalinkage Jul 30, 2021
3ac6580
fix to allow multi-valued skos:relations
rob-metalinkage Aug 12, 2021
56786e0
fixed SKOS rendering bug with multi-values relationships
rob-metalinkage Sep 1, 2021
385159c
tweak template for formatting styles
rob-metalinkage Sep 1, 2021
c8bb4d6
another formatting tweak
rob-metalinkage Sep 1, 2021
82d8fd2
fix graph assumptions in search
rob-metalinkage Sep 10, 2021
fc07945
more fixes for graphs in search
rob-metalinkage Sep 10, 2021
6470530
Fix failure on missing label for collection member
rob-metalinkage Oct 12, 2021
6d8c01f
Handle vocabulary details including nested objects
rob-metalinkage Oct 27, 2021
673c3a6
improved ordering, labelling and suppression of rubbish
rob-metalinkage Oct 28, 2021
0070b6a
Support for dcat:landingPage as preferred view
rob-metalinkage Oct 29, 2021
c210b0d
Nested objects in concept properties
rob-metalinkage Nov 4, 2021
0fdadd3
inline images
rob-metalinkage Nov 4, 2021
aeec972
fixed missing labels on object links - use URI if no label. Suppress …
rob-metalinkage Nov 7, 2021
a65a0db
metadata and alt links for framed content
rob-metalinkage Nov 10, 2021
b9c2d6b
workaround for weird QSA assumptions
rob-metalinkage Dec 7, 2021
e455b0a
fix pyldapi to avoid unmet requirements on rdflib 6
rob-metalinkage Dec 7, 2021
ba3b205
smarter detection of images
rob-metalinkage Dec 8, 2021
8e3b82a
support inline images in schemes/vocabs
rob-metalinkage Dec 8, 2021
059cd55
debug metadata link and template error.
rob-metalinkage Jan 20, 2022
bd30491
Support exposing describe,dd and skos RDF renderings with all relevan…
rob-metalinkage Jan 20, 2022
575b15c
fix bug in profile qsa - cannot use empty dics as a default as it per…
rob-metalinkage Jan 24, 2022
563f1f5
prioritise concept scheme over concept if polymorphic (until better a…
rob-metalinkage Feb 18, 2022
ba9b8a1
regression on concept rendering
rob-metalinkage Feb 18, 2022
e3da1df
bug in concept scheme typing
rob-metalinkage Mar 4, 2022
b552584
better fix for polymorphism detection
rob-metalinkage Mar 4, 2022
7e64be6
concept heirarchy in concept view if polymorphic
rob-metalinkage Apr 27, 2022
b250b83
fix external url logic for whitelisted annotation properties.
rob-metalinkage Sep 19, 2022
576f277
fix error if no nested objects found in vocab.
rob-metalinkage Oct 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/master' into robs_tweaks
# Conflicts:
#	vocprez/app.py
rob-metalinkage committed Jul 30, 2021
commit ae348bf6c85951d667a5e81c56859b752ad1b1d7
1,491 changes: 745 additions & 746 deletions vocprez/app.py
Original file line number Diff line number Diff line change
@@ -1,746 +1,745 @@
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

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)


# 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_URI_BASE,
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_URI_BASE,
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_URI_BASE or uri == config.SYSTEM_URI_BASE + "/":
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 {
<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"]
if cs:
c = source.SPARQL(request).get_concept(cs, uri)
else:
raise Exception ("Cannot find Concept Scheme")
return ConceptRenderer(request, c).render()
except Exception as e:
raise e

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":
q = """
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?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")
}}
}}
}}
GROUP BY ?uri ?pl
ORDER BY DESC(?weight)
""".format(**{"grf": request.values.get("from"), "input": request.values.get("search")})
results = []
else:
q = """
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?g ?uri ?pl (SUM(?w) AS ?weight)
WHERE {{
GRAPH ?g {{
{{ # 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")
}}
}}
}}
GROUP BY ?g ?uri ?pl
ORDER BY DESC(?weight)
""".format(**{"input": request.values.get("search")})
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 request.values.get("from") and request.values.get("from") != "all":
results.append((r["uri"]["value"], r["pl"]["value"]))
else:
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_URI_BASE + 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)
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":
q = """
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?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")
}}
}}
}}
GROUP BY ?uri ?pl
ORDER BY DESC(?weight)
""".format(**{"grf": request.values.get("from"), "input": request.values.get("search")})
results = []
else:
q = """
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?g ?uri ?pl (SUM(?w) AS ?weight)
WHERE {{
GRAPH ?g {{
{{ # 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")
}}
}}
}}
GROUP BY ?g ?uri ?pl
ORDER BY DESC(?weight)
""".format(**{"input": request.values.get("search")})
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 request.values.get("from") and request.values.get("from") != "all":
results.append((r["uri"]["value"], r["pl"]["value"]))
else:
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)
4 changes: 2 additions & 2 deletions vocprez/source/_source.py
Original file line number Diff line number Diff line change
@@ -513,9 +513,9 @@ def get_object_class(self):
q = """
SELECT DISTINCT *
WHERE {{
GRAPH ?g {{
<{uri}> a ?c .
}}
}}
""".format(
uri=url_decode(self.request.values.get("uri"))
You are viewing a condensed version of this merge commit. You can view the full changes here.