view service/rdf_to_mqtt/rdf_over_http.py @ 1562:c2ed44ed1e3c dependabot/pip/service/collector/twisted-19.7.0

Bump twisted from 19.2.0 to 19.7.0 in /service/collector Bumps [twisted](https://github.com/twisted/twisted) from 19.2.0 to 19.7.0. - [Release notes](https://github.com/twisted/twisted/releases) - [Changelog](https://github.com/twisted/twisted/blob/trunk/NEWS.rst) - [Commits](https://github.com/twisted/twisted/compare/twisted-19.2.0...twisted-19.7.0) Signed-off-by: dependabot[bot] <support@github.com>
author dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
date Fri, 14 Feb 2020 10:01:26 +0000
parents 7cc7700302c2
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'][-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