view service/reasoning/rdflibtrig.py @ 919:6ee2a90fc816

bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4 Ignore-this: 81dc30256f5d2658e53fce60abea66db darcs-hash:20130910073852-312f9-f863b729e4c75f160d0740ae9038b5fab084b86b
author drewp <drewp@bigasterisk.com>
date Tue, 10 Sep 2013 00:38:52 -0700
parents
children 7179284779fd
line wrap: on
line source

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