Mercurial > code > home > repos > homeauto
view service/rdf_to_mqtt/rdf_over_http.py @ 1745:d90cb7c06f15
try to crash if mqtt doesn't connect
author | drewp@bigasterisk.com |
---|---|
date | Thu, 09 Nov 2023 17:21:59 -0800 |
parents | 3f4b447d65f5 |
children |
line wrap: on
line source
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']) pred = expandQueryParamUri(arg['p']) turtleLiteral = body.decode('utf8') 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