Source code for rdflib.plugins.parsers.nquads

This is a rdflib plugin for parsing NQuad files into Conjunctive
graphs that can be used and queried. The store that backs the graph
*must* be able to handle contexts.

>>> from rdflib import ConjunctiveGraph, URIRef, Namespace
>>> g = ConjunctiveGraph()
>>> data = open("test/nquads.rdflib/example.nquads", "rb")
>>> g.parse(data, format="nquads") # doctest:+ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> assert len( == 449
>>> # There should be 16 separate contexts
>>> assert len([x for x in]) == 16
>>> # is the name of entity E10009 "Arco Publications"?
>>> #   (in graph
>>> # Looking for:
>>> # <>
>>> #   <>
>>> #   "Arco Publications"
>>> #   <>
>>> s = URIRef("")
>>> FOAF = Namespace("")
>>> assert(g.value(s,"Arco Publications"))

from codecs import getreader

from rdflib import ConjunctiveGraph

# Build up from the NTriples parser:
from rdflib.plugins.parsers.ntriples import W3CNTriplesParser
from rdflib.plugins.parsers.ntriples import ParseError
from rdflib.plugins.parsers.ntriples import r_tail
from rdflib.plugins.parsers.ntriples import r_wspace

__all__ = ["NQuadsParser"]

[docs]class NQuadsParser(W3CNTriplesParser):
[docs] def parse(self, inputsource, sink, bnode_context=None, **kwargs): """ Parse inputsource as an N-Quads file. :type inputsource: `rdflib.parser.InputSource` :param inputsource: the source of N-Quads-formatted data :type sink: `rdflib.graph.Graph` :param sink: where to send parsed triples :type bnode_context: `dict`, optional :param bnode_context: a dict mapping blank node identifiers to `~rdflib.term.BNode` instances. See `.NTriplesParser.parse` """ assert, ( "NQuadsParser must be given" " a context aware store." ) self.sink = ConjunctiveGraph(, identifier=sink.identifier) source = inputsource.getCharacterStream() if not source: source = inputsource.getByteStream() source = getreader("utf-8")(source) if not hasattr(source, "read"): raise ParseError("Item to parse must be a file-like object.") self.file = source self.buffer = "" while True: self.line = __line = self.readline() if self.line is None: break try: self.parseline(bnode_context) except ParseError as msg: raise ParseError("Invalid line (%s):\n%r" % (msg, __line)) return self.sink
[docs] def parseline(self, bnode_context=None): if (not self.line) or self.line.startswith(("#")): return # The line is empty or a comment subject = self.subject(bnode_context) predicate = self.predicate() obj = self.object(bnode_context) context = self.uriref() or self.nodeid(bnode_context) or self.sink.identifier if self.line: raise ParseError("Trailing garbage") # Must have a context aware store - add on a normal Graph # discards anything where the ctx != graph.identifier self.sink.get_context(context).add((subject, predicate, obj))