annotate service/rdf_to_mqtt/rdf_over_http.py @ 1732:3f4b447d65f5

port to starlette/asyncio
author drewp@bigasterisk.com
date Mon, 10 Jul 2023 17:37:58 -0700
parents fdddbdaf07b5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
1 from rdflib import Graph, URIRef, Literal, Namespace
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
2 from rdflib.parser import StringInputSource
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
3
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
4 ROOM = Namespace('http://projects.bigasterisk.com/room/')
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
5
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
6
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
7 def rdfGraphBody(body, headers):
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
8 g = Graph()
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
9 g.parse(StringInputSource(body), format='nt')
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
10 return g
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
11
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
12
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
13 def expandQueryParamUri(txt) -> URIRef:
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
14 if txt.startswith(':'):
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
15 return ROOM[txt.lstrip(':')]
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
16 # etc
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
17 return URIRef(txt)
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
18
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
19
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
20 def rdfStatementsFromRequest(arg, body, headers):
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
21 if arg.get('s') and arg.get('p'):
1732
3f4b447d65f5 port to starlette/asyncio
drewp@bigasterisk.com
parents: 732
diff changeset
22 subj = expandQueryParamUri(arg['s'])
3f4b447d65f5 port to starlette/asyncio
drewp@bigasterisk.com
parents: 732
diff changeset
23 pred = expandQueryParamUri(arg['p'])
3f4b447d65f5 port to starlette/asyncio
drewp@bigasterisk.com
parents: 732
diff changeset
24 turtleLiteral = body.decode('utf8')
694
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
25 try:
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
26 obj = Literal(float(turtleLiteral))
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
27 except ValueError:
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
28 obj = Literal(turtleLiteral)
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
29 yield (subj, pred, obj)
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
30 else:
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
31 g = rdfGraphBody(body, headers)
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
32 assert len(g) == 1, len(g)
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
33 yield g.triples((None, None, None)).next()
925bc4137c93 extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff changeset
34 # could support multiple stmts