Mercurial > code > home > repos > homeauto
annotate service/rdf_to_mqtt/rdf_over_http.py @ 1615:bcfa368e5498
change a Graph.__sub__ to Set.difference in verify() for a big speedup
author | drewp@bigasterisk.com |
---|---|
date | Mon, 06 Sep 2021 23:20:23 -0700 |
parents | fdddbdaf07b5 |
children | 3f4b447d65f5 |
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'): |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff
changeset
|
22 subj = expandQueryParamUri(arg['s'][-1]) |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff
changeset
|
23 pred = expandQueryParamUri(arg['p'][-1]) |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
diff
changeset
|
24 turtleLiteral = body |
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 |