o
    miX1                     @  s  d Z ddlm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m Z  ddlm Z! erld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 )/zX
N-Triples Parser
License: GPL 2, W3C, BSD, or MIT
Author: Sean B. Palmer, inamidst.com
    )annotationsN)BytesIOStringIO
TextIOBase)	IOTYPE_CHECKINGAnyMatchMutableMappingOptionalPatternTextIOUnion)_string_escape_mapdecodeUnicodeEscape)ParserError)InputSourceParser)BNode)LiteralURIRef)r   )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#   Y/home/kim/smarthome/.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$   tripleA   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   K   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>y   s    zuriquote.<locals>.<lambda>)r8   r_hibytesub)rM   r#   r#   r$   r   u   s   r   c                   @  s   e Zd ZdZdZ		d7d8dd	Z		
d9d:ddZd;ddZd<ddZd=d>ddZ	d?dd Z
d@d$d%Zd=dAd'd(ZdAd)d*Z	d=dBd,d-ZdCd/d0Z	d=dDd2d3ZdEd5d6ZdS )Fr   a  An N-Triples Parser.

    This is a legacy-style Triples parser for NTriples provided by W3C

    Example:
        ```python
        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line	skolemizeNrS   'Optional[Union[DummySink, NTGraphSink]]bnode_contextOptional[_BNodeContextType]c                 C  sN   d| _ |d ur|| _ni | _|  |d ur|| _nt | _d | _d | _d| _d S )NFr7   )rW   rR   rS   r    rT   rU   rV   )r"   rS   rY   r#   r#   r$   r%      s   
zW3CNTriplesParser.__init__Ff-Union[TextIO, IO[bytes], codecs.StreamReader]rW   boolr2   Union[DummySink, NTGraphSink]c                 C  s   t |ds	tdt |dst |dstd|}|| _|| _d| _	 |  | _| jdu r2	 | jS z| j	|d	 W n tyI   td

| jw q$)a  Parse f as an N-Triples file.

        Args:
            f: The N-Triples source
            bnode_context: A dict mapping blank node identifiers (e.g., `a` in `_:a`)
                to [`BNode`][rdflib.term.BNode] instances. An empty dict can be
                passed in to define a distinct context for a given call to
                `parse`.
            skolemize: Whether to skolemize blank nodes

        Returns:
            The sink containing the parsed triples
        readz)Item to parse must be a file-like object.encoding
charbufferutf-8r7   TN)rY   zInvalid line: {})hasattrrD   codecs	getreaderrW   rU   rT   readlinerV   	parselineformatrS   )r"   r[   rY   rW   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.rb   N)
r9   r1   bytes	bytearrayrD   rd   re   r   r   ri   )r"   r*   kwargsr[   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$   rf      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   rh   rS   r-   )r"   rY   rx   rz   Zobject_r#   r#   r$   rg      s   





zW3CNTriplesParser.parselinetokenr1   c                 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$   rv   
  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"   rY   Zsubjr#   r#   r$   rx     s   zW3CNTriplesParser.subjectc                 C  s   |   }|s
td|S )NzPredicate must be uriref)r   rD   )r"   predr#   r#   r$   rz     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"   rY   Zobjtr#   r#   r$   r{      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   rv   r_urirefr?   r   r   URI)r"   rM   r#   r#   r$   r   (  s   
zW3CNTriplesParser.uriref$Union[te.Literal[False], bNode, URI]c                 C  s|   |  dr<| jr| td}t| S |d u r| j}| td}||d }|d ur3t|S t }|||< |S dS )N_r'   F)r   rW   rv   r_nodeidr?   bNoderR   get)r"   rY   Zbnode_idZnew_idZbnoder#   r#   r$   r   0  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   rv   	r_literalrB   r   r   r   rD   r   )r"   ZlitlangZdtyper#   r#   r$   r   I  s   

zW3CNTriplesParser.literal)NN)rS   rX   rY   rZ   )NF)r[   r\   rY   rZ   rW   r]   r2   r^   )r*   rj   r2   rk   )r2   rp   r~   )rY   rZ   r2   rk   )r}   r1   r2   r]   )r   r   r2   r   )r2   r   )rY   rZ   r2   r   )r2   r   )rY   rZ   r2   r   )r2   r   )r.   r/   r0   __doc__	__slots__r%   ri   ro   rf   rg   r   rv   rx   rz   r{   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graphr   c                 C  s
   || _ d S r~   r   )r"   r   r#   r#   r$   r%   `  r&   zNTGraphSink.__init__r*   r   r+   r   r,   r   r2   rk   c                 C  s   | j |||f d S r~   )r   addr)   r#   r#   r$   r-   c  s   zNTGraphSink.tripleN)r   r   )r*   r   r+   r   r,   r   r2   rk   )r.   r/   r0   r   r%   r-   r#   r#   r#   r$   r   ]  s    
r   c                   @  s"   e Zd ZdZdZedddZdS )r   zParser for the N-Triples format, often stored with the .nt extension.

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

        Args:
            source: The source of NT-formatted data
            sink: Where to send parsed triples
            **kwargs: Additional arguments to pass to `W3CNTriplesParser.parse`
        rb   N)
ZgetCharacterStreamZgetByteStreamr9   r   rd   re   r   r   ri   close)clsr   rS   rn   r[   bparserr#   r#   r$   ri   o  s   

zNTParser.parseN)r   r   rS   r   rn   r   r2   rk   )r.   r/   r0   r   r   classmethodri   r#   r#   r#   r$   r   g  s
    r   )r*   r1   r2   r1   )rM   r1   r2   r1   )Cr   
__future__r   rd   reior   r   r   typingr   r   r   r	   r
   r   r   r   r   Zrdflib.compatr   r   Zrdflib.exceptionsr   rD   Zrdflib.parserr   r   Zrdflib.termr   r   r   r   r   Ztyping_extensionsteZrdflib.graphr   r   r   r   __all__r   r   Zlitinfocompilers   rw   ry   r|   r   r   r   rr   r8   r    r;   r@   rA   r   rP   r   r1   Z_BNodeContextTyper   r   r   r#   r#   r#   r$   <module>   sP    ,	






	



'
 _
