-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
103 lines (89 loc) · 3.34 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
import json
import io
import sys
from collections import Counter
from flask import Flask, render_template
from flask import request, jsonify
import rdflib
from rdflib.util import from_n3
from rdflib.plugins.sparql.results.jsonresults import JSONResultSerializer
graph = rdflib.Graph()
graph.parse("opt/" + sys.argv[1], format="turtle")
graph.parse("brick-classes.ttl", format="turtle")
app = Flask(__name__)
def to_uri(abbr):
if abbr:
return rdflib.URIRef(abbr)
def wrap_counter(func):
def returned_func():
res = func()
res = set(filter_bnodes(res)) # make unique and remove bnodes
return jsonify(list(sorted(res))) # sort and convert to json
return returned_func
def filter_bnodes(seq):
res = []
for item in seq:
if isinstance(item, (tuple, list)):
if any(isinstance(x, rdflib.BNode) for x in item):
continue
elif isinstance(item, rdflib.BNode):
continue
res.append(item)
return res
@app.route("/")
def home():
return render_template("index.html")
@app.route('/query', methods=['GET', 'POST'], endpoint='query_graph')
def query_graph():
if request.method == "GET":
query = request.args.get("query")
elif (
request.method == "POST"
and request.content_type == "application/x-www-form-urlencoded"
):
query = request.form.get("query")
elif (
request.method == "POST"
and request.content_type == "application/sparql-query"
):
print("SPARQL", request.form.keys())
query = request.get_data()
results = graph.query(query)
json_results = io.StringIO()
JSONResultSerializer(results).serialize(json_results)
return jsonify(json.loads(json_results.getvalue()))
@app.route("/subjects", methods=['POST'], endpoint='get_subjects')
@wrap_counter
def get_subjects():
pred = to_uri(request.json.get("predicate", None))
obj = to_uri(request.json.get("object", None))
return graph.subjects(predicate=pred, object=obj)
@app.route("/predicates", methods=['POST'], endpoint='get_predicates')
@wrap_counter
def get_predicates():
sub = to_uri(request.json.get("subject", None))
obj = to_uri(request.json.get("object", None))
return graph.predicates(subject=sub, object=obj)
@app.route("/objects", methods=['POST'], endpoint='get_objects')
@wrap_counter
def get_objects():
sub = to_uri(request.json.get("subject", None))
pred = to_uri(request.json.get("predicate", None))
return graph.objects(subject=sub, predicate=pred)
@app.route("/subject_objects", methods=['POST'], endpoint='get_subject_objects')
@wrap_counter
def get_subject_objects():
pred = to_uri(request.json.get("predicate", None))
return graph.subject_objects(predicate=pred)
@app.route("/subject_predicates", methods=['POST'], endpoint='get_subject_predicates')
@wrap_counter
def get_subject_predicates():
obj = to_uri(request.json.get("object", None))
return graph.subject_predicates(object=obj)
@app.route("/predicate_objects", methods=['POST'], endpoint='get_predicate_objects')
@wrap_counter
def get_predicate_objects():
sub = to_uri(request.json.get("subject", None))
return graph.predicate_objects(subject=sub)
if __name__ == "__main__":
app.run("0.0.0.0")