Mercurial > code > home > repos > homeauto
diff service/mqtt_graph_bridge/rdf_over_http.py @ 1495:690226a7dddb
extract rdfStatementsFromRequest for sharing with other tools
Ignore-this: 74eba63f3a29f48b48c6fb1ae74780d3
darcs-hash:9250a3eb40d95ef6522f3bb573f8fa093c84c48e
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Fri, 31 Jan 2020 23:54:20 -0800 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/service/mqtt_graph_bridge/rdf_over_http.py Fri Jan 31 23:54:20 2020 -0800 @@ -0,0 +1,34 @@ +from rdflib import Graph, URIRef, Literal, Namespace +from rdflib.parser import StringInputSource + +ROOM = Namespace('http://projects.bigasterisk.com/room/') + + +def rdfGraphBody(body, headers): + g = Graph() + g.parse(StringInputSource(body), format='nt') + return g + + +def expandQueryParamUri(txt) -> URIRef: + if txt.startswith(':'): + return ROOM[txt.lstrip(':')] + # etc + return URIRef(txt) + + +def rdfStatementsFromRequest(arg, body, headers): + if arg.get('s') and arg.get('p'): + subj = expandQueryParamUri(arg['s'][-1]) + pred = expandQueryParamUri(arg['p'][-1]) + turtleLiteral = body + try: + obj = Literal(float(turtleLiteral)) + except ValueError: + obj = Literal(turtleLiteral) + yield (subj, pred, obj) + else: + g = rdfGraphBody(body, headers) + assert len(g) == 1, len(g) + yield g.triples((None, None, None)).next() + # could support multiple stmts