Mercurial > code > home > repos > homeauto
diff service/reasoning/rdflibtrig.py @ 114:4cd065b97fa1
bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
Ignore-this: 81dc30256f5d2658e53fce60abea66db
author | drewp@bigasterisk.com |
---|---|
date | Tue, 10 Sep 2013 00:38:52 -0700 |
parents | |
children | 7179284779fd |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/service/reasoning/rdflibtrig.py Tue Sep 10 00:38:52 2013 -0700 @@ -0,0 +1,32 @@ +import re, time +import restkit +from rdflib import URIRef +try: + from rdflib import StringInputSource + from rdflib.Graph import Graph +except ImportError: + from rdflib.parser import StringInputSource + from rdflib import Graph + +def parseTrig(trig): + """ + yields quads + """ + m = re.match(r"<([^>]+)> \{(.*)\}\s*$", trig, re.DOTALL) + if m is None: + raise NotImplementedError("trig format was too tricky: %r..." % trig[:200]) + + ctx = URIRef(m.group(1)) + n3 = m.group(2) + g = Graph() + g.parse(StringInputSource(n3), format="n3") + for stmt in g: + yield stmt + (ctx,) + + +def addTrig(graph, url): + t1 = time.time() + trig = restkit.request(url).body_string() + fetchTime = time.time() - t1 + graph.addN(parseTrig(trig)) + return fetchTime