Mercurial > code > home > repos > homeauto
diff service/reasoning/rdflibtrig.py @ 235:5ad229334a88
rdflib can parse trig now
Ignore-this: ad1feb1d88249f509d2906b6cdbe62c6
author | drewp@bigasterisk.com |
---|---|
date | Mon, 01 Feb 2016 02:15:23 -0800 |
parents | 745eff67ad40 |
children | 0c306e76d8c5 |
line wrap: on
line diff
--- a/service/reasoning/rdflibtrig.py Sat Jan 30 06:45:55 2016 -0800 +++ b/service/reasoning/rdflibtrig.py Mon Feb 01 02:15:23 2016 -0800 @@ -1,28 +1,7 @@ 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,) - +from rdflib.parser import StringInputSource +from rdflib import ConjunctiveGraph def addTrig(graph, url, timeout=2): t1 = time.time() @@ -31,5 +10,7 @@ raise ValueError("status %s from %s" % (response.status, url)) trig = response.body_string() fetchTime = time.time() - t1 - graph.addN(parseTrig(trig)) + g = ConjunctiveGraph() + g.parse(StringInputSource(trig), format='trig') + graph.addN(g.quads()) return fetchTime