Mercurial > code > home > repos > homeauto
comparison service/rdf_to_mqtt/rdf_over_http.py @ 1532:7cc7700302c2
more service renaming; start a lot more serv.n3 job files
Ignore-this: 635aaefc7bd2fa5558eefb8b3fc9ec75
darcs-hash:2c8b587cbefa4db427f9a82676abdb47e651187e
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Thu, 06 Feb 2020 16:36:35 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1531:a3dc6a31590f | 1532:7cc7700302c2 |
---|---|
1 from rdflib import Graph, URIRef, Literal, Namespace | |
2 from rdflib.parser import StringInputSource | |
3 | |
4 ROOM = Namespace('http://projects.bigasterisk.com/room/') | |
5 | |
6 | |
7 def rdfGraphBody(body, headers): | |
8 g = Graph() | |
9 g.parse(StringInputSource(body), format='nt') | |
10 return g | |
11 | |
12 | |
13 def expandQueryParamUri(txt) -> URIRef: | |
14 if txt.startswith(':'): | |
15 return ROOM[txt.lstrip(':')] | |
16 # etc | |
17 return URIRef(txt) | |
18 | |
19 | |
20 def rdfStatementsFromRequest(arg, body, headers): | |
21 if arg.get('s') and arg.get('p'): | |
22 subj = expandQueryParamUri(arg['s'][-1]) | |
23 pred = expandQueryParamUri(arg['p'][-1]) | |
24 turtleLiteral = body | |
25 try: | |
26 obj = Literal(float(turtleLiteral)) | |
27 except ValueError: | |
28 obj = Literal(turtleLiteral) | |
29 yield (subj, pred, obj) | |
30 else: | |
31 g = rdfGraphBody(body, headers) | |
32 assert len(g) == 1, len(g) | |
33 yield g.triples((None, None, None)).next() | |
34 # could support multiple stmts |