3
d                 @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ dgZG dd deZdS )a  
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(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"))
    )absolute_import)division)print_function)	getreader)b)ConjunctiveGraph)NTriplesParser)
ParseError)r_tail)r_wspace)	r_wspacesNQuadsParserc               @   s   e Zd Zdd Zdd ZdS )r   c             K   s   |j jstdt|j |jd| _|j }t|ds<tdt	d|}|| _
d| _x^| j  | _}| jdkrpP y| j  W qV tk
r } ztd||f W Y dd}~X qVX qVW | jS )	zParse f as an N-Triples file.z1NQuadsParser must be given a context aware store.)store
identifierreadz)Item to parse must be a file-like object.zutf-8 NzInvalid line (%s):
%r)r   Zcontext_awareAssertionErrorr   r   sinkgetByteStreamhasattrr	   r   filebufferreadlineline	parseline)selfZinputsourcer   kwargssourceZ_NQuadsParser__linemsg r   ?/tmp/pip-build-7vycvbft/rdflib/rdflib/plugins/parsers/nquads.pyparse/   s"    

&zNQuadsParser.parsec             C   s   | j t | j s| jjdr"d S | j }| j t | j }| j t | j }| j t | j pn| j pn| j	j
}| j t | jrtd| j	j|j|||f d S )N#zTrailing garbage)Zeatr   r   
startswithsubject	predicateobjectZurirefZnodeidr   r   r
   r	   Zget_contextadd)r   r$   r%   objcontextr   r   r    r   I   s    




zNQuadsParser.parselineN)__name__
__module____qualname__r!   r   r   r   r   r    r   -   s   N)__doc__
__future__r   r   r   codecsr   sixr   Zrdflibr   Zrdflib.plugins.parsers.ntriplesr   r	   r
   r   r   __all__r   r   r   r   r    <module>   s   