o
    il0                     @  s  d dl mZ dZd dlZd dlZd dlmZmZmZ d dl	m
Z
mZmZmZmZmZmZmZmZ d dlmZmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlm Z  d dlm Z! erpd dl"Z#d dl$m%Z%m&Z&m'Z'm(Z( g dZ)dZ*dZ+de* d Z,e-dZ.e-dZ/e-dZ0e-dZ1e-e*Z2e-dZ3e-e+e, Z4dZ5dZ6G dd dZ7e-dZ8e-dZ9e-dZ:d-d!d"Z;e-d#Z<d.d%d&Z=ee>ef Z?G d'd( d(Z@G d)d* d*ZAG d+d, d,eZBdS )/    )annotationszWN-Triples Parser
License: GPL 2, W3C, BSD, or MIT
Author: Sean B. Palmer, inamidst.com
N)BytesIOStringIO
TextIOBase)	IOTYPE_CHECKINGAnyMatchMutableMappingOptionalPatternTextIOUnion)_string_escape_mapdecodeUnicodeEscape)ParserError)InputSourceParser)BNode)Literal)URIRef)Graph_ObjectType_PredicateType_SubjectType)unquoteuriquoteW3CNTriplesParserNTGraphSinkNTParser	DummySinkz<([^:]+:[^\s"<>]*)>z"([^"\\]*(?:\\.[^"\\]*)*)"z&(?:@([a-zA-Z]+(?:-[a-zA-Z0-9]+)*)|\^\^z)?z([^\r\n]*)(?:\r\n|\r|\n)z[ \t]*z[ \t]+z[ \t]*\.[ \t]*(#.*)?z3_:([A-Za-z0-9_:]([-A-Za-z0-9_:\.]*[-A-Za-z0-9_:])?)i   Fc                   @  s   e Zd Zdd Zdd ZdS )r    c                 C  s
   d| _ d S )Nr   )length)self r#   Z/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/rdflib/plugins/parsers/ntriples.py__init__@      
zDummySink.__init__c                 C  s   |  j d7  _ t||| d S )N   )r!   printr"   spor#   r#   r$   tripleC   s   zDummySink.tripleN)__name__
__module____qualname__r%   r-   r#   r#   r#   r$   r    ?   s    r    z([\x20\x21\x23-\x5B\x5D-\x7E]+)z\\([tbnrf"'\\])z'\\u([0-9A-Fa-f]{4})|\\U([0-9A-Fa-f]{8})r*   strreturnc                 C  s(  t st| trt| } | S | d} | S g }| rt| }|r0| | d } ||	d qt
| }|rH| dd } |t|	d  qt| }|rv| | d } | \}}t|pa|d}|dkrntd| |t| n| drtd	| dd
  td| d  | sd|S )zUnquote an N-Triples string.zunicode-escapeNr'         i zDisallowed codepoint: %08X\zIllegal escape at: %s...
   zIllegal literal character: %rr    )validate
isinstancer1   r   decoder_safematchendappendgroupr_quotr   	r_uniquotgroupsint
ParseErrorchr
startswithjoin)r*   resultmuU	codepointr#   r#   r$   r   M   s>   






r   z([\x80-\xFF])uric                 C  s   t s| S tdd | S )Nc                 S  s   dt | d S )Nz%%%02Xr'   )ordr?   )rI   r#   r#   r$   <lambda>{   s    zuriquote.<locals>.<lambda>)r8   r_hibytesub)rM   r#   r#   r$   r   w   s   r   c                   @  s   e Zd ZdZdZ		d6d7dd	Z	d8d9ddZd:ddZd;ddZd8d<ddZ	d=ddZ
d>d"d#Zd8d?d%d&Zd@d(d)Z	d8dAd+d,ZdBd.d/Z	d8dCd1d2ZdDd4d5ZdS )Er   a  An N-Triples Parser.
    This is a legacy-style Triples parser for NTriples provided by W3C
    Usage::

          p = W3CNTriplesParser(sink=MySink())
          sink = p.parse(f) # file; use parsestring for a string

    To define a context in which blank node identifiers refer to the same blank node
    across instances of NTriplesParser, pass the same dict as ``bnode_context`` to each
    instance. By default, a new blank node context is created for each instance of
    `W3CNTriplesParser`.
    )
_bnode_idssinkbufferfilelineNrS   )Optional[Union[DummySink, 'NTGraphSink']]bnode_contextOptional[_BNodeContextType]c                 C  sH   |d ur|| _ ni | _ |  |d ur|| _nt | _d | _d | _d| _d S )Nr7   )rR   rS   r    rT   rU   rV   )r"   rS   rX   r#   r#   r$   r%      s   
zW3CNTriplesParser.__init__f-Union[TextIO, IO[bytes], codecs.StreamReader]r2   Union[DummySink, 'NTGraphSink']c                 C  s   t |ds	tdt |dst |dstd|}|| _d| _	 |  | _| jdu r/	 | j
S z| j|d	 W n tyF   td
	| jw q!)a  
        Parse f as an N-Triples file.

        :type f: :term:`file object`
        :param f: the N-Triples source
        :type bnode_context: `dict`, optional
        :param bnode_context: a dict mapping blank node identifiers (e.g., ``a`` in ``_:a``)
                              to `~rdflib.term.BNode` instances. An empty dict can be
                              passed in to define a distinct context for a given call to
                              `parse`.
        readz)Item to parse must be a file-like object.encoding
charbufferutf-8r7   TN)rX   zInvalid line: {})hasattrrD   codecs	getreaderrU   rT   readlinerV   	parselineformatrS   )r"   rZ   rX   r#   r#   r$   parse   s"   


zW3CNTriplesParser.parser*   Union[bytes, bytearray, str]Nonec                 K  sX   t |tttfstdt |ttfrtdt|}nt|}| j	|fi | dS )zParse s as an N-Triples string.z(Item to parse must be a string instance.r`   N)
r9   r1   bytes	bytearrayrD   rb   rc   r   r   rg   )r"   r*   kwargsrZ   r#   r#   r$   parsestring   s   zW3CNTriplesParser.parsestringOptional[str]c                 C  s   | j s| jt}|sdS || _ 	 t| j }|r(| j | d | _ |dS | jt}|s:| j  s:|d7 }n|s>dS |  j |7  _ q)z+Read an N-Triples line from buffered input.NTr'   
)	rT   rU   r]   bufsizr_liner<   r=   r?   isspace)r"   rT   rI   r#   r#   r$   rd      s"   

zW3CNTriplesParser.readlinec                 C  s   |  t | jr| jdrd S | |}|  t |  }|  t | |}|  t | jr8t	d
| j| j||| d S )N#zTrailing garbage: {})eatr_wspacerV   rF   subject	r_wspaces	predicateobjectr_tailrD   rf   rS   r-   )r"   rX   rv   rx   object_r#   r#   r$   re      s   





zW3CNTriplesParser.parselinetokenr1   boolc                 C  s   | j |S N)rV   rF   )r"   r|   r#   r#   r$   peek   s   zW3CNTriplesParser.peekpatternPattern[str]
Match[str]c                 C  s<   | | j}|std|j| jf | j| d  | _|S )NzFailed to eat %s at %s)r<   rV   rD   r   r=   )r"   r   rI   r#   r#   r$   rt     s
   zW3CNTriplesParser.eatUnion[bNode, URIRef]c                 C  s"   |   p| |}|std|S )Nz Subject must be uriref or nodeID)urirefnodeidrD   )r"   rX   subjr#   r#   r$   rv     s   zW3CNTriplesParser.subjectr   c                 C  s   |   }|s
td|S )NzPredicate must be uriref)r   rD   )r"   predr#   r#   r$   rx     s   zW3CNTriplesParser.predicateUnion[URI, bNode, Literal]c                 C  s.   |   p| |p|  }|du rtd|S )NFzUnrecognised object type)r   r   literalrD   )r"   rX   objtr#   r#   r$   ry     s   zW3CNTriplesParser.objectUnion['te.Literal[False]', URI]c                 C  s6   |  dr| td}t|}t|}t|S dS )N<r'   F)r   rt   r_urirefr?   r   r   URI)r"   rM   r#   r#   r$   r   !  s   
zW3CNTriplesParser.uriref!Union['te.Literal[False]', bNode]c                 C  sZ   |  dr+|d u r| j}| td}||d }|d ur"t|S t }|||< |S dS )N_r'   F)r   rR   rt   r_nodeidr?   getbNode)r"   rX   bnode_idnew_idbnoder#   r#   r$   r   )  s   
zW3CNTriplesParser.nodeid#Union['te.Literal[False]', Literal]c                 C  sv   |  dr9| t \}}}|r|}nd }|r%t|}t|}t|}nd }|r/|r/tdt|}t|||S dS )N"z)Can't have both a language and a datatypeF)	r   rt   	r_literalrB   r   r   r   rD   r   )r"   litlangdtyper#   r#   r$   r   =  s   

zW3CNTriplesParser.literal)NN)rS   rW   rX   rY   r~   )rZ   r[   rX   rY   r2   r\   )r*   rh   r2   ri   )r2   rn   )rX   rY   r2   ri   )r|   r1   r2   r}   )r   r   r2   r   )r2   r   )r2   r   )rX   rY   r2   r   )r2   r   )rX   rY   r2   r   )r2   r   )r.   r/   r0   __doc__	__slots__r%   rg   rm   rd   re   r   rt   rv   rx   ry   r   r   r   r#   r#   r#   r$   r      s*    
$


	

	r   c                   @  s$   e Zd ZdZdddZdddZdS )r   ggraph'Graph'c                 C  s
   || _ d S r~   r   )r"   r   r#   r#   r$   r%   T  r&   zNTGraphSink.__init__r*   '_SubjectType'r+   '_PredicateType'r,   '_ObjectType'r2   ri   c                 C  s   | j |||f d S r~   )r   addr)   r#   r#   r$   r-   W  s   zNTGraphSink.tripleN)r   r   )r*   r   r+   r   r,   r   r2   ri   )r.   r/   r0   r   r%   r-   r#   r#   r#   r$   r   Q  s    
r   c                   @  s"   e Zd ZdZdZedddZdS )r   zyparser for the ntriples format, often stored with the .nt extension

    See http://www.w3.org/TR/rdf-testcases/#ntriplesr#   sourcer   rS   r   rl   r   r2   ri   c                 K  s\   |  }|s| }t|tr|}ntd|}tt|}|j|fi | |	  dS )a<  
        Parse the NT format

        :type source: `rdflib.parser.InputSource`
        :param source: the source of NT-formatted data
        :type sink: `rdflib.graph.Graph`
        :param sink: where to send parsed triples
        :param kwargs: Additional arguments to pass to `.W3CNTriplesParser.parse`
        r`   N)
getCharacterStreamgetByteStreamr9   r   rb   rc   r   r   rg   close)clsr   rS   rl   rZ   bparserr#   r#   r$   rg   b  s   
zNTParser.parseN)r   r   rS   r   rl   r   r2   ri   )r.   r/   r0   r   r   classmethodrg   r#   r#   r#   r$   r   [  s
    r   )r*   r1   r2   r1   )rM   r1   r2   r1   )C
__future__r   r   rb   reior   r   r   typingr   r   r   r	   r
   r   r   r   r   rdflib.compatr   r   rdflib.exceptionsr   rD   rdflib.parserr   r   rdflib.termr   r   r   r   r   typing_extensionsterdflib.graphr   r   r   r   __all__r   r   litinfocompilerq   ru   rw   rz   r   r   r   rp   r8   r    r;   r@   rA   r   rP   r   r1   _BNodeContextTyper   r   r   r#   r#   r#   r$   <module>   sR   ,	






	



'
 Q
