-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
109 lines (79 loc) · 3.71 KB
/
main.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
from flask import Flask, request, make_response
from flask_restful import Resource, Api, abort
from rdflib import Graph, URIRef, BNode, Literal, Namespace
from rdflib.plugins import sparql, parsers
from xml import sax
import os,tempfile
turtle_mediatype = 'text/turtle'
rdf_mediatype = 'application/rdf+xml'
supported_media = [ turtle_mediatype, rdf_mediatype ]
default_mediatype = turtle_mediatype
app = Flask(__name__)
api = Api(app)
local_file = "data.ttl"
companies_graph = Graph()
company_query = sparql.prepareQuery("""
CONSTRUCT {
?company <http://schema.org/name> ?name .
?company a <http://schema.org/Organization> .
?company <http://schema.org/subOrganization> ?subOrganization .
?company <http://schema.org/owner> ?owner .
?company <http://schema.org/founder> ?founder .
?company <http://schema.org/url> ?url .
} WHERE {
?company <http://schema.org/name> ?name .
OPTIONAL { ?company a <http://schema.org/Organization> . }
OPTIONAL { ?company <http://schema.org/subOrganization> ?subOrganization } .
OPTIONAL { ?company <http://schema.org/owner> ?owner } .
OPTIONAL { ?company <http://schema.org/founder> ?founder } .
OPTIONAL { ?company <http://schema.org/url> ?url } .
} """,
initNs = { "schema": Namespace("http://schema.org") })
class Company(Resource):
def put(self, company_id):
if request.headers['Content-type'].lower() not in supported_media:
abort(415, message="Unsupported medyatype, please provide" + " or ".join(supported_media))
dataFormat = request.headers['Content-type'].lower()
payload = request.data
temporal_graph = Graph()
try:
temporal_graph.parse(data=payload, format=dataFormat)
except (parsers.notation3.BadSyntax, sax._exceptions.SAXParseException) as e:
abort(400, message=str(e))
company = URIRef("http://example.org/" + company_id)
result = temporal_graph.query(company_query, initBindings={'company': company})
if len(result) == 0:
abort(400, message="No triples provided for " + company)
for s, p, o in result:
companies_graph.add((s, p, o))
companies_graph.serialize(destination=local_file, format="turtle")
response = make_response(result.serialize(format=dataFormat), 201)
response.headers['Content-type'] = turtle_mediatype
return response
def get(self, company_id):
responseFormat = default_mediatype
if request.headers['Accept'].lower() in supported_media:
responseFormat = request.headers['Accept'].lower()
print(responseFormat)
print(responseFormat)
company = URIRef("http://example.org/" + company_id)
result = companies_graph.query(company_query, initBindings={'company': company})
if len(result) == 0:
abort(404, message="Company {} doesn't exist".format(company))
else:
response = make_response(result.serialize(format=responseFormat), 200)
response.headers['Content-type'] = responseFormat
return response
api.add_resource(Company, '/company/<string:company_id>')
@app.route('/dataset', methods=['GET'])
def download():
responseFormat = default_mediatype
if request.headers['Accept'].lower() in supported_media:
responseFormat = request.headers['Accept'].lower()
print(responseFormat)
response = make_response(companies_graph.serialize(format=responseFormat), 200)
response.headers['Content-type'] = responseFormat
return response
if __name__ == '__main__':
companies_graph.parse(local_file, format="turtle")
app.run(debug=True)