home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-rdflib / rdflib / syntax / parsers / N3Parser.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  1.6 KB  |  51 lines

  1. from rdflib import URIRef, BNode, Literal, RDF, Variable
  2.  
  3. from rdflib.util import from_n3
  4.  
  5. from rdflib.syntax.parsers import Parser
  6. from rdflib.syntax.parsers.n3p.n3proc import N3Processor
  7.  
  8. from rdflib.Graph import Graph, QuotedGraph, ConjunctiveGraph
  9.  
  10.  
  11. class N3Parser(Parser):
  12.  
  13.     def __init__(self):
  14.         pass
  15.  
  16.     def parse(self, source, graph):
  17.         # we're currently being handed a Graph, not a ConjunctiveGraph
  18.         assert graph.store.context_aware # is this implied by formula_aware
  19.         assert graph.store.formula_aware
  20.  
  21.         conj_graph = ConjunctiveGraph(store=graph.store)
  22.         conj_graph.default_context = graph # TODO: CG __init__ should have a default_context arg
  23.         # TODO: update N3Processor so that it can use conj_graph as the sink
  24.         sink = Sink(conj_graph)
  25.         if False:
  26.             sink.quantify = lambda *args: True
  27.             sink.flatten = lambda *args: True
  28.         baseURI = graph.absolutize(source.getPublicId() or source.getSystemId() or "")
  29.         p = N3Processor("nowhere", sink, baseURI=baseURI) # pass in "nowhere" so we can set data instead
  30.         p.userkeys = True # bah
  31.         p.data = source.getByteStream().read() # TODO getCharacterStream?
  32.         p.parse()
  33.         for prefix, namespace in p.bindings.items():
  34.             conj_graph.bind(prefix, namespace)
  35.  
  36.  
  37. class Sink(object):
  38.     def __init__(self, graph):
  39.         self.graph = graph
  40.  
  41.     def start(self, root):
  42.         pass
  43.  
  44.     def statement(self, s, p, o, f):
  45.         f.add((s, p, o))
  46.  
  47.     def quantify(self, formula, var):
  48.         #print "quantify(%s, %s)" % (formula, var)
  49.         pass
  50.  
  51.