rdflib.plugins.parsers package

Submodules

rdflib.plugins.parsers.notation3 module

notation3.py - Standalone Notation3 Parser Derived from CWM, the Closed World Machine

Authors of the original suite:

http://www.w3.org/2000/10/swap/notation3.py

Copyright 2000-2007, World Wide Web Consortium. Copyright 2001, MIT. Copyright 2001, Zolera Systems Inc.

License: W3C Software License http://www.w3.org/Consortium/Legal/copyright-software

Modified by Sean B. Palmer Copyright 2007, Sean B. Palmer.

Modified to work with rdflib by Gunnar Aastrand Grimnes Copyright 2010, Gunnar A. Grimnes

exception rdflib.plugins.parsers.notation3.BadSyntax(uri, lines, argstr, i, why)[source]

Bases: SyntaxError

__init__(uri, lines, argstr, i, why)[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.notation3'
__str__()[source]

Return str(self).

__weakref__

list of weak references to the object (if defined)

property message
class rdflib.plugins.parsers.notation3.N3Parser[source]

Bases: rdflib.plugins.parsers.notation3.TurtleParser

An RDFLib parser for Notation3

See http://www.w3.org/DesignIssues/Notation3.html

__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.notation3'
parse(source, graph, encoding='utf-8')[source]
class rdflib.plugins.parsers.notation3.TurtleParser[source]

Bases: rdflib.parser.Parser

An RDFLib parser for Turtle

See http://www.w3.org/TR/turtle/

__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.notation3'
parse(source, graph, encoding='utf-8', turtle=True)[source]
rdflib.plugins.parsers.notation3.splitFragP(uriref, punct=0)[source]

split a URI reference before the fragment

Punctuation is kept.

e.g.

>>> splitFragP("abc#def")
('abc', '#def')
>>> splitFragP("abcdef")
('abcdef', '')
rdflib.plugins.parsers.notation3.join(here, there)[source]

join an absolute URI and URI reference (non-ascii characters are supported/doctested; haven’t checked the details of the IRI spec though)

here is assumed to be absolute. there is URI reference.

>>> join('http://example/x/y/z', '../abc')
'http://example/x/abc'

Raise ValueError if there uses relative path syntax but here has no hierarchical path.

>>> join('mid:foo@example', '../foo') 
Traceback (most recent call last):
    raise ValueError(here)
ValueError: Base <mid:foo@example> has no slash
after colon - with relative '../foo'.
>>> join('http://example/x/y/z', '')
'http://example/x/y/z'
>>> join('mid:foo@example', '#foo')
'mid:foo@example#foo'

We grok IRIs

>>> len(u'Andr\xe9')
5
>>> join('http://example.org/', u'#Andr\xe9')
u'http://example.org/#Andr\xe9'
rdflib.plugins.parsers.notation3.base()[source]

The base URI for this process - the Web equiv of cwd

Relative or abolute unix-standard filenames parsed relative to this yeild the URI of the file. If we had a reliable way of getting a computer name, we should put it in the hostname just to prevent ambiguity

rdflib.plugins.parsers.notation3.runNamespace()[source]

Return a URI suitable as a namespace for run-local objects

rdflib.plugins.parsers.notation3.uniqueURI()[source]

A unique URI

rdflib.plugins.parsers.notation3.hexify(ustr)[source]

Use URL encoding to return an ASCII string corresponding to the given UTF8 string

>>> hexify("http://example/a b")
%(b)s'http://example/a%%20b'

rdflib.plugins.parsers.nquads module

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") 
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> assert len(g.store) == 449
>>> # There should be 16 separate contexts
>>> assert len([x for x in g.store.contexts()]) == 16
>>> # is the name of entity E10009 "Arco Publications"?
>>> #   (in graph http://bibliographica.org/entity/E10009)
>>> # Looking for:
>>> # <http://bibliographica.org/entity/E10009>
>>> #   <http://xmlns.com/foaf/0.1/name>
>>> #   "Arco Publications"
>>> #   <http://bibliographica.org/entity/E10009>
>>> s = URIRef("http://bibliographica.org/entity/E10009")
>>> FOAF = Namespace("http://xmlns.com/foaf/0.1/")
>>> assert(g.value(s, FOAF.name).eq("Arco Publications"))
class rdflib.plugins.parsers.nquads.NQuadsParser(sink=None)[source]

Bases: rdflib.plugins.parsers.ntriples.NTriplesParser

__module__ = 'rdflib.plugins.parsers.nquads'
parse(inputsource, sink, **kwargs)[source]

Parse f as an N-Triples file.

parseline()[source]

rdflib.plugins.parsers.nt module

class rdflib.plugins.parsers.nt.NTSink(graph)[source]

Bases: object

__dict__ = mappingproxy({'__module__': 'rdflib.plugins.parsers.nt', '__init__': <function NTSink.__init__>, 'triple': <function NTSink.triple>, '__dict__': <attribute '__dict__' of 'NTSink' objects>, '__weakref__': <attribute '__weakref__' of 'NTSink' objects>, '__doc__': None})
__init__(graph)[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.nt'
__weakref__

list of weak references to the object (if defined)

triple(s, p, o)[source]
class rdflib.plugins.parsers.nt.NTParser[source]

Bases: rdflib.parser.Parser

parser for the ntriples format, often stored with the .nt extension

See http://www.w3.org/TR/rdf-testcases/#ntriples

__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.nt'
parse(source, sink, baseURI=None)[source]

rdflib.plugins.parsers.ntriples module

N-Triples Parser License: GPL 2, W3C, BSD, or MIT Author: Sean B. Palmer, inamidst.com

rdflib.plugins.parsers.ntriples.unquote(s)[source]

Unquote an N-Triples string.

rdflib.plugins.parsers.ntriples.uriquote(uri)[source]
class rdflib.plugins.parsers.ntriples.Sink[source]

Bases: object

__dict__ = mappingproxy({'__module__': 'rdflib.plugins.parsers.ntriples', '__init__': <function Sink.__init__>, 'triple': <function Sink.triple>, '__dict__': <attribute '__dict__' of 'Sink' objects>, '__weakref__': <attribute '__weakref__' of 'Sink' objects>, '__doc__': None})
__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.ntriples'
__weakref__

list of weak references to the object (if defined)

triple(s, p, o)[source]
class rdflib.plugins.parsers.ntriples.NTriplesParser(sink=None)[source]

Bases: object

An N-Triples Parser.

Usage:

p = NTriplesParser(sink=MySink())
sink = p.parse(f) # file; use parsestring for a string
__dict__ = mappingproxy({'__module__': 'rdflib.plugins.parsers.ntriples', '__doc__': 'An N-Triples Parser.\n\n Usage::\n\n p = NTriplesParser(sink=MySink())\n sink = p.parse(f) # file; use parsestring for a string\n ', '_bnode_ids': {}, '__init__': <function NTriplesParser.__init__>, 'parse': <function NTriplesParser.parse>, 'parsestring': <function NTriplesParser.parsestring>, 'readline': <function NTriplesParser.readline>, 'parseline': <function NTriplesParser.parseline>, 'peek': <function NTriplesParser.peek>, 'eat': <function NTriplesParser.eat>, 'subject': <function NTriplesParser.subject>, 'predicate': <function NTriplesParser.predicate>, 'object': <function NTriplesParser.object>, 'uriref': <function NTriplesParser.uriref>, 'nodeid': <function NTriplesParser.nodeid>, 'literal': <function NTriplesParser.literal>, '__dict__': <attribute '__dict__' of 'NTriplesParser' objects>, '__weakref__': <attribute '__weakref__' of 'NTriplesParser' objects>})
__init__(sink=None)[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.ntriples'
__weakref__

list of weak references to the object (if defined)

eat(pattern)[source]
literal()[source]
nodeid()[source]
object()[source]
parse(f)[source]

Parse f as an N-Triples file.

parseline()[source]
parsestring(s)[source]

Parse s as an N-Triples string.

peek(token)[source]
predicate()[source]
readline()[source]

Read an N-Triples line from buffered input.

subject()[source]
uriref()[source]

rdflib.plugins.parsers.rdfxml module

An RDF/XML parser for RDFLib

rdflib.plugins.parsers.rdfxml.create_parser(target, store)[source]
class rdflib.plugins.parsers.rdfxml.BagID(val)[source]

Bases: rdflib.term.URIRef

__init__(val)[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.rdfxml'
__slots__ = ['li']
li
next_li()[source]
class rdflib.plugins.parsers.rdfxml.ElementHandler[source]

Bases: object

__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.rdfxml'
__slots__ = ['start', 'char', 'end', 'li', 'id', 'base', 'subject', 'predicate', 'object', 'list', 'language', 'datatype', 'declared', 'data']
base
char
data
datatype
declared
end
id
language
li
list
next_li()[source]
object
predicate
start
subject
class rdflib.plugins.parsers.rdfxml.RDFXMLHandler(store)[source]

Bases: xml.sax.handler.ContentHandler

__init__(store)[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.rdfxml'
absolutize(uri)[source]
add_reified(sid, spo)[source]
characters(content)[source]

Receive notification of character data.

The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.

convert(name, qname, attrs)[source]
property current
document_element_start(name, qname, attrs)[source]
endElementNS(name, qname)[source]

Signals the end of an element in namespace mode.

The name parameter contains the name of the element type, just as with the startElementNS event.

endPrefixMapping(prefix)[source]

End the scope of a prefix-URI mapping.

See startPrefixMapping for details. This event will always occur after the corresponding endElement event, but the order of endPrefixMapping events is not otherwise guaranteed.

error(message)[source]
get_current()[source]
get_next()[source]
get_parent()[source]
ignorableWhitespace(content)[source]

Receive notification of ignorable whitespace in element content.

Validating Parsers must use this method to report each chunk of ignorable whitespace (see the W3C XML 1.0 recommendation, section 2.10): non-validating parsers may also use this method if they are capable of parsing and using content models.

SAX parsers may return all contiguous whitespace in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity, so that the Locator provides useful information.

list_node_element_end(name, qname)[source]
literal_element_char(data)[source]
literal_element_end(name, qname)[source]
literal_element_start(name, qname, attrs)[source]
property next
node_element_end(name, qname)[source]
node_element_start(name, qname, attrs)[source]
property parent
processingInstruction(target, data)[source]

Receive notification of a processing instruction.

The Parser will invoke this method once for each processing instruction found: note that processing instructions may occur before or after the main document element.

A SAX parser should never report an XML declaration (XML 1.0, section 2.8) or a text declaration (XML 1.0, section 4.3.1) using this method.

property_element_char(data)[source]
property_element_end(name, qname)[source]
property_element_start(name, qname, attrs)[source]
reset()[source]
setDocumentLocator(locator)[source]

Called by the parser to give the application a locator for locating the origin of document events.

SAX parsers are strongly encouraged (though not absolutely required) to supply a locator: if it does so, it must supply the locator to the application by invoking this method before invoking any of the other methods in the DocumentHandler interface.

The locator allows the application to determine the end position of any document-related event, even if the parser is not reporting an error. Typically, the application will use this information for reporting its own errors (such as character content that does not match an application’s business rules). The information returned by the locator is probably not sufficient for use with a search engine.

Note that the locator will return correct information only during the invocation of the events in this interface. The application should not attempt to use it at any other time.

startDocument()[source]

Receive notification of the beginning of a document.

The SAX parser will invoke this method only once, before any other methods in this interface or in DTDHandler (except for setDocumentLocator).

startElementNS(name, qname, attrs)[source]

Signals the start of an element in namespace mode.

The name parameter contains the name of the element type as a (uri, localname) tuple, the qname parameter the raw XML 1.0 name used in the source document, and the attrs parameter holds an instance of the Attributes class containing the attributes of the element.

The uri part of the name tuple is None for elements which have no namespace.

startPrefixMapping(prefix, namespace)[source]

Begin the scope of a prefix-URI Namespace mapping.

The information from this event is not necessary for normal Namespace processing: the SAX XML reader will automatically replace prefixes for element and attribute names when the http://xml.org/sax/features/namespaces feature is true (the default).

There are cases, however, when applications need to use prefixes in character data or in attribute values, where they cannot safely be expanded automatically; the start/endPrefixMapping event supplies the information to the application to expand prefixes in those contexts itself, if necessary.

Note that start/endPrefixMapping events are not guaranteed to be properly nested relative to each-other: all startPrefixMapping events will occur before the corresponding startElement event, and all endPrefixMapping events will occur after the corresponding endElement event, but their order is not guaranteed.

class rdflib.plugins.parsers.rdfxml.RDFXMLParser[source]

Bases: rdflib.parser.Parser

__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.rdfxml'
parse(source, sink, **args)[source]

rdflib.plugins.parsers.trig module

class rdflib.plugins.parsers.trig.TrigParser[source]

Bases: rdflib.parser.Parser

An RDFLib parser for TriG

__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.trig'
parse(source, graph, encoding='utf-8')[source]
class rdflib.plugins.parsers.trig.TrigSinkParser(store, openFormula=None, thisDoc='', baseURI=None, genPrefix='', why=None, turtle=False)[source]

Bases: rdflib.plugins.parsers.notation3.SinkParser

__module__ = 'rdflib.plugins.parsers.trig'
directiveOrStatement(argstr, h)[source]
graph(argstr, i)[source]

Parse trig graph, i.e.

<urn:graphname> = { .. triples .. }

return -1 if it doesn’t look like a graph-decl raise Exception if it looks like a graph, but isn’t.

labelOrSubject(argstr, i, res)[source]
rdflib.plugins.parsers.trig.becauseSubGraph(*args, **kwargs)[source]

rdflib.plugins.parsers.trix module

A TriX parser for RDFLib

rdflib.plugins.parsers.trix.create_parser(store)[source]
class rdflib.plugins.parsers.trix.TriXHandler(store)[source]

Bases: xml.sax.handler.ContentHandler

An Sax Handler for TriX. See http://sw.nokia.com/trix/

__init__(store)[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.trix'
characters(content)[source]

Receive notification of character data.

The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.

endElementNS(name, qname)[source]

Signals the end of an element in namespace mode.

The name parameter contains the name of the element type, just as with the startElementNS event.

endPrefixMapping(prefix)[source]

End the scope of a prefix-URI mapping.

See startPrefixMapping for details. This event will always occur after the corresponding endElement event, but the order of endPrefixMapping events is not otherwise guaranteed.

error(message)[source]
get_bnode(label)[source]
ignorableWhitespace(content)[source]

Receive notification of ignorable whitespace in element content.

Validating Parsers must use this method to report each chunk of ignorable whitespace (see the W3C XML 1.0 recommendation, section 2.10): non-validating parsers may also use this method if they are capable of parsing and using content models.

SAX parsers may return all contiguous whitespace in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity, so that the Locator provides useful information.

processingInstruction(target, data)[source]

Receive notification of a processing instruction.

The Parser will invoke this method once for each processing instruction found: note that processing instructions may occur before or after the main document element.

A SAX parser should never report an XML declaration (XML 1.0, section 2.8) or a text declaration (XML 1.0, section 4.3.1) using this method.

reset()[source]
setDocumentLocator(locator)[source]

Called by the parser to give the application a locator for locating the origin of document events.

SAX parsers are strongly encouraged (though not absolutely required) to supply a locator: if it does so, it must supply the locator to the application by invoking this method before invoking any of the other methods in the DocumentHandler interface.

The locator allows the application to determine the end position of any document-related event, even if the parser is not reporting an error. Typically, the application will use this information for reporting its own errors (such as character content that does not match an application’s business rules). The information returned by the locator is probably not sufficient for use with a search engine.

Note that the locator will return correct information only during the invocation of the events in this interface. The application should not attempt to use it at any other time.

startDocument()[source]

Receive notification of the beginning of a document.

The SAX parser will invoke this method only once, before any other methods in this interface or in DTDHandler (except for setDocumentLocator).

startElementNS(name, qname, attrs)[source]

Signals the start of an element in namespace mode.

The name parameter contains the name of the element type as a (uri, localname) tuple, the qname parameter the raw XML 1.0 name used in the source document, and the attrs parameter holds an instance of the Attributes class containing the attributes of the element.

The uri part of the name tuple is None for elements which have no namespace.

startPrefixMapping(prefix, namespace)[source]

Begin the scope of a prefix-URI Namespace mapping.

The information from this event is not necessary for normal Namespace processing: the SAX XML reader will automatically replace prefixes for element and attribute names when the http://xml.org/sax/features/namespaces feature is true (the default).

There are cases, however, when applications need to use prefixes in character data or in attribute values, where they cannot safely be expanded automatically; the start/endPrefixMapping event supplies the information to the application to expand prefixes in those contexts itself, if necessary.

Note that start/endPrefixMapping events are not guaranteed to be properly nested relative to each-other: all startPrefixMapping events will occur before the corresponding startElement event, and all endPrefixMapping events will occur after the corresponding endElement event, but their order is not guaranteed.

class rdflib.plugins.parsers.trix.TriXParser[source]

Bases: rdflib.parser.Parser

A parser for TriX. See http://sw.nokia.com/trix/

__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'rdflib.plugins.parsers.trix'
parse(source, sink, **args)[source]

Module contents