-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrowser.py
106 lines (77 loc) · 2.54 KB
/
browser.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
from rdflib import Graph, Literal, URIRef
from flask import request, render_template
# create Flask app
from flask import Flask
app = Flask(__name__)
# configure Flask app
DEBUG = True
app.config.from_object(__name__)
# list of disallowed RDF predicates
DISALLOWED_PREDICATE_NAMES = [
"isDefinedBy",
"comment",
"description",
]
def is_allowed(triple):
"""
Return True if the triple can be printed in the graph. Disallowed
triples are ones that relate to themselves, or contain predicates
that are in DISALLOWED_PREDICATE_NAMES.
"""
# reject triples with predicates that aren't allowed
for predicate in DISALLOWED_PREDICATE_NAMES:
if predicate in triple[1]:
return False
# reject self-referring triples, regardless of the relationship
if triple[0] == triple[2]:
return False
return True
def make_readable(element):
"""
Return a readable version of the passed element.
For literals, newlines are removed. For URIs with
fragments, everything before the fragment is removed,
and all whitespace is removed. For URIs without
fragments, whitespace is removed, and everything after
the last slash is removed (if there is no slash, just
the URI is returned).
For all other types of elements, no processing is done;
the element is just returned verbatim.
Empty results are returned as the string "<None>".
"""
if isinstance(element, URIRef):
uri = element
uri = uri.replace(" ", "")
uri = uri.replace("\n", "")
if "#" in uri:
words = uri.split("#")[-1]
elif "/" in uri:
words = uri.split("/")[-1]
else:
words = uri
elif isinstance(element, Literal):
words = element.replace("\n", "")
else:
words = element
if len(words) == 0:
return "<None>"
return words
# views
@app.route('/', methods=['GET'])
def browse():
triples = []
uri = request.args.get("uri", "")
if uri:
graph = Graph()
# populate the graph
try:
graph.parse(uri, format="n3")
# if the input is not in N3, try to get it in XML
except SyntaxError:
graph.parse(uri, format="xml")
# make triples readable, and filter out undesired triples
triples = [(subj, make_readable(pred), obj) for (subj, pred, obj) in graph]
triples = filter(is_allowed, triples)
return render_template('graph.html', uri=uri, triples=triples)
if __name__ == '__main__':
app.run()