o
    mil                     @  s  d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	m
Z
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mZ ddlmZ ddlmZmZ ddlmZ ddl Z!dd	l!m"Z" dd
l#m$Z$ ddl%m&Z& ddl'm(Z( erddl)m*Z* ddlm+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 g dZ2G dd dZ3G dd deZ4G dd dej5Z5G dd de5Z6G dd de5Z7dde" iZ8G dd  d e5Z9G d!d" d"e5Z:						d6d7d.d/Z;d8d4d5Z<dS )9a;  Parser plugin interface.

This module defines the parser plugin interface and contains other
related parser support code.

The module is mainly useful for those wanting to write a parser that
can plugin to rdflib. If you are wanting to invoke a parser you likely
want to do so through the Graph class parse method.
    )annotationsN)BufferedIOBaseBytesIO	RawIOBaseStringIO
TextIOBaseTextIOWrapper)
IOTYPE_CHECKINGAnyBinaryIOListOptionalTextIOTupleUnioncast)urljoin)Requesturl2pathname)	xmlreader)__version__)_urlopen)	Namespace)URIRef)Message)BufferedReader)
addinfourl)Buffer)Graph)ParserInputSourceStringInputSourceURLInputSourceFileInputSourcePythonInputSourcec                   @  s"   e Zd ZdZdd Zdd
dZdS )r     c                 C     d S Nr&   selfr&   r&   G/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/parser.py__init__?      zParser.__init__sourcer!   sinkr   returnNonec                 C  r'   r(   r&   )r*   r.   r/   r&   r&   r+   parseB   r-   zParser.parseN)r.   r!   r/   r   r0   r1   )__name__
__module____qualname__	__slots__r,   r2   r&   r&   r&   r+   r    <   s    r    c                      s   e Zd ZdZd?d@ fddZdd Zd	d
 Zdd ZedAddZ	edBddZ
dBddZdBddZdCdDddZdBddZdEd d!Zd"d# Zd$d% ZdFdGd(d)ZdFdGd*d+ZdFdGd,d-ZdHd0d1ZdHd2d3ZdIdJd7d8Zd9d: ZdEd;d<Zd=d> Z  ZS )KBytesIOWrapper)wrappedenc_strtext_strencodingencoder	has_read1has_seek_name_fileno_isatty	_leftover_bytes_per_char_text_bytes_offsetutf-8r8    Union[str, StringIO, TextIOBase]c                   s   t t|   || _|| _t| j| _d | _d | _	d | _
d | _d | _d | _d | _d| _d| _| dd}|dv r@d| _d S |dv rId| _d S |dsS|d	rXd
| _d S |dsb|drgd| _d S d
| _d S )N    r   _-)rE   utf8u8cp65001   )latin1zlatin-1z
iso-8859-1z	iso8859-1asciizus-asciizutf-16utf16   zutf-32utf32   )superr7   r,   r8   r;   codecs
getencoderr<   r9   r:   r=   r>   r?   r@   rA   rB   rD   lowerreplacerC   
startswith)r*   r8   r;   norm_encoding	__class__r&   r+   r,   W   s.   

	


zBytesIOWrapper.__init__c                 C  s  d }t | jtr| | j\}}t|| _d}n&t | jtr2| jj}t |tr+t	dt
t|| _nt | jttfr>| j| _| jd urG| j}n| jd urP| j}nt	d|d u riz|j}W n tyh   d}Y nw t|d| _z| | _W n ty   t|d| _Y nw || _d S )NstringzYBytesIOWrapper cannot be wrapped in TextIOWrapper, then wrapped in another BytesIOWrapperzNo stream to read fromstreamread1seek)
isinstancer8   strr<   r   r9   r   bufferr7   	Exceptionr   r   r   r   r:   nameAttributeErrorhasattrr=   seekabler>   r?   )r*   re   bbleninner
use_streamr&   r&   r+   _init}   s@   





zBytesIOWrapper._initc              
   C     | j d u r| jd u r|   | j d ur| j }n| jd ur| j}z| | _W d S  ty= } z
|| _W Y d }~d S d }~w tyI   d| _Y d S w N)r9   r:   rm   filenor@   OSErrorrf   r*   rl   er&   r&   r+   _check_fileno      

zBytesIOWrapper._check_filenoc              
   C  rn   NF)r9   r:   rm   isattyrA   rr   rf   rs   r&   r&   r+   _check_isatty   rv   zBytesIOWrapper._check_isattyr0   r   c                 C  s   | j d u r	|   | j S r(   )r?   rm   r)   r&   r&   r+   re      s   
zBytesIOWrapper.nameboolc                 C  s   | j d u r| jd u rdS d }| j d ur%z| j j}W n" ty$   d }Y nw | jd ur<z| jj}W n ty;   d }Y nw |d u rBdS |S rw   )r9   r:   closedrf   )r*   r{   r&   r&   r+   r{      s    

zBytesIOWrapper.closedc                 C     dS )NTr&   r)   r&   r&   r+   readable   r-   zBytesIOWrapper.readablec                 C  r|   rw   r&   r)   r&   r&   r+   writable   r-   zBytesIOWrapper.writableNsizeOptional[int]intc                 C     t d)Nz!Cannot truncate on BytesIOWrapperNotImplementedErrorr*   r   r&   r&   r+   truncate      zBytesIOWrapper.truncatec                 C  s.   | j d u r	|   t| j tr| j t| j S r(   )rA   ry   ra   BaseExceptionrz   r)   r&   r&   r+   rx      s
   

zBytesIOWrapper.isattyc                 C  s8   | j d u r	|   t| j tr| j | j d u rdS | j S ro   )r@   ru   ra   r   r)   r&   r&   r+   rq      s
   
zBytesIOWrapper.filenoc                 C  s|   | j d u r| jd u rd S | j d ur$z| j   W d S  ty#   Y d S w | jd ur<z| j  W d S  ty;   Y d S w d S r(   )r9   r:   closerf   r)   r&   r&   r+   r      s   

zBytesIOWrapper.closec                 C  r'   r(   r&   r)   r&   r&   r+   flush   r-   zBytesIOWrapper.flushrp   bytesc                C  s  t r	| jd us	J |d u s|dk r:z| j }W n ty#   d}Y nw | |\}}| jr7| j| }d| _n|}n|t| jkrH| j}d| _nz|t| jk r_| jd | }| j|d  | _nct|| j\}}t	||dkrpdnd }| j}	t|	|k rz| j|}
W n ty   d}
Y nw t|
dk rn| |
\}}|	|7 }	t|	|k s|t|	|krd| _|	}n|	d | }|	|d  | _~	|  j
t|7  _
|S )Nr    rG   rM   )r
   r:   readEOFErrorr<   rB   lendivmodrC   r   rD   )r*   r   Zret_strZret_encodedZenc_len	ret_bytesdmZget_per_loopZ	got_bytesZgot_strr&   r&   r+   _read_bytes_from_text_stream  sR   
	z+BytesIOWrapper._read_bytes_from_text_streamc                C  sX   |dur
|dkr
dS | j du r| jdu r|   | j dur%| j |}|S | |}|S )z
        Read at most size bytes, returned as a bytes object.

        If the size argument is negative or omitted read until EOF is reached.
        Return an empty bytes object if already at EOF.
        Nr   rG   )r9   r:   rm   r   r   )r*   r   r   r&   r&   r+   r   ,  s   

zBytesIOWrapper.readc                C  sj   | j du r
| jdu s| jdu r|   | jst | j dur1|du s&|dk r+| j  S | j |S td)u   
        Read at most size bytes, with at most one call to the underlying raw stream’s
        read() or readinto() method. Returned as a bytes object.

        If the size argument is negative or omitted, read until EOF is reached.
        Return an empty bytes object at EOF.
        Nr   z2read1() not supported for TextIO in BytesIOWrapper)r9   r:   r=   rm   r   r_   r   r&   r&   r+   r_   =  s   

zBytesIOWrapper.read1ri   r   c                C  s   t rt|ttfsJ t|dkrdS | jdu r!| jdu r!|   | jdur,| j|S t|}| 	|}t|}|dkr?dS ||d|< |S )z
        Read len(b) bytes into buffer b.

        Returns number of bytes read (0 for EOF), or error if the object
        is set not to block and has no data to read.
        r   N)
r
   ra   
memoryview	bytearrayr   r9   r:   rm   readintor   )r*   ri   r   	read_dataread_lenr&   r&   r+   r   O  s   

zBytesIOWrapper.readintoc                C  sf   t rt|ttfsJ | jdu r| jdu s| jdu r|   | js$t | jdur/| j	|S td)a  
        Read len(b) bytes into buffer b, with at most one call to the underlying raw
        stream's read() or readinto() method.

        Returns number of bytes read (0 for EOF), or error if the object
        is set not to block and has no data to read.
        Nz6readinto1() not supported for TextIO in BytesIOWrapper)
r
   ra   r   r   r9   r:   r=   rm   r   	readinto1r*   ri   r&   r&   r+   r   g  s   
zBytesIOWrapper.readinto1r   offsetwhencec                C  s   | j d ur| j st | jd u r| jd u s| j d u r|   |dks&td|dkr.td| jd ur;| j|| n| jd urG| j|| d| _d| _dS )Nr   z,Only SEEK_SET is supported on BytesIOWrapperz3Only seeking to zero is supported on BytesIOWrapperrG   )r>   r   r9   r:   rm   r`   rD   rB   )r*   r   r   r&   r&   r+   r`   {  s"   

zBytesIOWrapper.seekc                 C  s,   | j d u r
| jd u s| jd u r|   | jS r(   )r9   r:   r>   rm   r)   r&   r&   r+   rh     s   zBytesIOWrapper.seekablec                 C  sT   | j d ur| j std| jd ur'z
| j | _W | jS  ty&   Y | jS w | jS )Nz/Cannot tell() pos because file is not seekable.)r>   r   r9   tellrD   rf   r)   r&   r&   r+   r     s   
zBytesIOWrapper.tellc                C  r   )Nz Cannot write to a BytesIOWrapperr   r   r&   r&   r+   write  r   zBytesIOWrapper.write)rE   )r8   rF   )r0   r   )r0   rz   r(   )r   r   r0   r   )r0   r   )rp   )r   r   r0   r   )ri   r   r0   r   )r   )r   r   r   r   r0   r   )r3   r4   r5   r6   r,   rm   ru   ry   propertyre   r{   r}   r~   r   rx   rq   r   r   r   r   r_   r   r   r`   rh   r   r   __classcell__r&   r&   r[   r+   r7   F   s4    &%



+



r7   c                   @  s&   e Zd ZdZddddZdd	d
ZdS )r!   z
    TODO:
    N	system_idOptional[str]c                 C  s    t jj| |d d | _d| _d S )Nr   F)r   r!   r,   content_type
auto_closer*   r   r&   r&   r+   r,     s   
zInputSource.__init__r0   r1   c                 C  sx   |   }|rt|drz|  W n	 ty   Y nw |  }|r8t|dr:z|  W d S  ty7   Y d S w d S d S )Nr   )ZgetCharacterStreamrg   r   rd   ZgetByteStream)r*   cfr&   r&   r+   r     s   zInputSource.closer(   )r   r   r0   r1   )r3   r4   r5   __doc__r,   r   r&   r&   r&   r+   r!     s    r!   c                   @  sN   e Zd ZdZddddZdd
dZdddZdddZdddZdddZ	dS )r%   a  
    Constructs an RDFLib Parser InputSource from a Python data structure,
    for example, loaded from JSON with json.load or json.loads:

    >>> import json
    >>> as_string = """{
    ...   "@context" : {"ex" : "http://example.com/ns#"},
    ...   "@graph": [{"@type": "ex:item", "@id": "#example"}]
    ... }"""
    >>> as_python = json.loads(as_string)
    >>> source = create_input_source(data=as_python)
    >>> isinstance(source, PythonInputSource)
    True
    Ndatar   r   r   c                 C  s"   d | _ d| _d | _|| _|| _d S rw   )r   r   	public_idr   r   )r*   r   r   r&   r&   r+   r,     s
   
zPythonInputSource.__init__r0   c                 C     | j S r(   r   r)   r&   r&   r+   getPublicId     zPythonInputSource.getPublicIdr   r1   c                 C  
   || _ d S r(   r   )r*   r   r&   r&   r+   setPublicId     
zPythonInputSource.setPublicIdc                 C  r   r(   r   r)   r&   r&   r+   getSystemId  r   zPythonInputSource.getSystemIdc                 C  r   r(   r   r   r&   r&   r+   setSystemId  r   zPythonInputSource.setSystemIdc                 C  s
   d | _ d S r(   )r   r)   r&   r&   r+   r     r   zPythonInputSource.closer(   )r   r   r   r   )r0   r   )r   r   r0   r1   )r   r   r0   r1   r   )
r3   r4   r5   r   r,   r   r   r   r   r   r&   r&   r&   r+   r%     s    



r%   c                      s(   e Zd ZdZ		dd fd
dZ  ZS )r"   zO
    Constructs an RDFLib Parser InputSource from a Python String or Bytes
    rE   NvalueUnion[str, bytes]r;   rb   r   r   c                   s   t t| | t|tr't|}| | | | t||}| 	| d S t
|}| 	| t||}| | | |j d S r(   )rT   r"   r,   ra   rb   r   setCharacterStreamsetEncodingr7   setByteStreamr   r   r;   )r*   r   r;   r   r^   Zb_streamZc_streamr[   r&   r+   r,     s   






zStringInputSource.__init__)rE   N)r   r   r;   rb   r   r   )r3   r4   r5   r   r,   r   r&   r&   r[   r+   r"     s
    r"   z
User-agentz8rdflib-%s (https://rdflib.github.io/; eikeon@eikeon.com)c                      s`   e Zd ZU dZded< edddZedddZddddZdd fddZ	dddZ
  ZS ) r#   zU
    Constructs an RDFLib Parser InputSource from a URL to read it from the Web.
    	List[str]linksmessager   r0   c                   s        fdd| D S )Nc                   s    g | ]\}}|   kr|qS r&   )rW   ).0keyvalre   r&   r+   
<listcomp>  s     z8URLInputSource.getallmatchingheaders.<locals>.<listcomp>)rW   items)clsr   re   r&   r   r+   getallmatchingheaders
  s   z$URLInputSource.getallmatchingheadersresponser   c                 C  sH   |  |jd}g }|D ]}dd |dD }|D ]}|| qq|S )NLinkc                 S     g | ]}|  qS r&   strip)r   Zlinkstrr&   r&   r+   r         z,URLInputSource.get_links.<locals>.<listcomp>,)r   headerssplitappend)r   r   Z
linkslinesZretarrayZ	linksliner   linkr&   r&   r+   	get_links  s   zURLInputSource.get_linksNtype_r   c                 C  s   |rd| dnd }d}g }| j D ],}dd |dD }||vr"q|r3||v r2||d d q||d d q|S )	Nztype=""zrel="alternate"c                 S  r   r&   r   )r   pr&   r&   r+   r      r   z1URLInputSource.get_alternates.<locals>.<listcomp>;r   z<>)r   r   r   r   )r*   r   ZtypestrZrelstraltsr   partsr&   r&   r+   get_alternates  s   
zURLInputSource.get_alternatesr   formatc                   s  t t| | || _tt}|dkrd|d< n^|dkr!d|d< nU|dv r*d|d< nL|dkr3d	|d< nC|d
kr<d|d< n:|dkrEd|d< n1|dkrNd|d< n(ddlm} ddlm	} g }||dD ]}d|j
v rn||j
 qad||d< t|d |}t|}	|	 | _| |	| _|dv r| jdd}
|
D ]}t| j|}|| jkr||krtt|}	|	 | _ nq| | j | |	jd}|r|d nd | _| jd ur| jddd | _| |	 |	 | _d S )Nxmlzapplication/rdf+xml, */*;q=0.1AcceptZn3ztext/n3, */*;q=0.1)turtlettlz,text/turtle, application/x-turtle, */*;q=0.1ntztext/plain, */*;q=0.1Ztrigzapplication/trig, */*;q=0.1Ztrixzapplication/trix, */*;q=0.1json-ldz6application/ld+json, application/json;q=0.9, */*;q=0.1r   )r    )plugins)kind/z, )r   application/ld+jsonr   )r   zcontent-typer   rM   )rT   r#   r,   urldictr   Zrdflib.parserr    Zrdflib.pluginr   re   r   joinr   r   geturlr   r   r   r   r   r   r   r   r   infoZresponse_info)r*   r   r   Z	myheadersr    r   accr   reqr   r   r   Z	full_linkZcontent_typesr[   r&   r+   r,   *  s\   










zURLInputSource.__init__rb   c                 C  r   r(   )r   r)   r&   r&   r+   __repr__d  s   zURLInputSource.__repr__)r   r   r0   r   )r   r   r0   r   r(   )r   r   r0   r   )NN)r   r   r   r   r0   rb   )r3   r4   r5   r   __annotations__classmethodr   r   r   r,   r   r   r&   r&   r[   r+   r#     s   
 	:r#   c                      s,   e Zd Z	dd fddZdd
dZ  ZS )r$   Nr;   r   file>Union[BinaryIO, TextIO, TextIOBase, RawIOBase, BufferedIOBase]c             	     s   t j  }tt |j  |d}tt| 	| || _
t|trR| | | |j z|j}| | W d S  ttfyQ   | t||jd Y d S w tr[t|ts[J | | |d urt| | | t||d d S 	 d S )Nbase)r;   )pathlibPathcwdas_urir   re   absoluterT   r$   r,   r   ra   r   r   r   r;   rc   r   rf   LookupErrorr7   r
   r   r   )r*   r   r;   r   r   ri   r[   r&   r+   r,   j  s(   



zFileInputSource.__init__r0   rb   c                 C  s
   t | jS r(   )reprr   r)   r&   r&   r+   r     r   zFileInputSource.__repr__r(   )r;   r   r   r   r   )r3   r4   r5   r,   r   r   r&   r&   r[   r+   r$   i  s    r$   r.   MOptional[Union[IO[bytes], TextIO, InputSource, str, bytes, pathlib.PurePath]]publicIDr   locationr   !Optional[Union[BinaryIO, TextIO]]r   !Optional[Union[str, bytes, dict]]r   r0   c              	   C  s  t tdd | |||g}t|dkrtdd}| durtr1|du s%J |du s+J |du s1J t| tr9| }n{t| trA| }nst| tj	rLt| }nht| t
rT| }n`t| drt| ts| }t }t| dr||  || j z
| j}	||	 W n ttfy   ||  Y nw || |tju r|d nt|d	r||j n
td
t| | f d}
d}|durtr|du sJ |du sJ | du sJ t||||d\}
}}}|durtr|du sJ |du sJ | du sJ t|}|dur;tr|du sJ |du sJ | du sJ t|tr"t|}d}nt|tt
tfr2t|}d}n	t dt| |du rDtd| j!|O  _!|durW|"| |S |# du rf|"|
pdd |S )zR
    Return an appropriate InputSource instance for the given
    parameters.
    c                 S  s   | d uS r(   r&   )vr&   r&   r+   <lambda>  s    z%create_input_source.<locals>.<lambda>rM   z;exactly one of source, location, file or data must be givenNr   r;   zfile:///dev/stdinre   z$Unexpected type '%s' for source '%s'F)r   r   input_sourcer   Tz(parse data can only str, or bytes. not: zcould not create InputSourcer   )$listfilterr   
ValueErrorr
   ra   r!   rb   r   PurePathr   rg   r   r   r   r;   rc   r   rf   r   sysstdinr   re   rd   type"_create_input_source_from_locationr$   r   r%   r   r"   RuntimeErrorr   r   r   )r.   r   r   r   r   r   Znon_empty_argumentsr  r   ri   absolute_locationr   r&   r&   r+   create_input_source  s   













r  r  Optional[InputSource]rb   MTuple[URIRef, bool, Optional[Union[BinaryIO, TextIO]], Optional[InputSource]]c                 C  s   t j|rt|  }tj  }tt	j
||d}|dr3t|dd}t|d} nt||}d}||| |fS )Nr   zfile:///r   rbT)ospathexistsr   r   r   r   r   r   rdflibutilZ_iri2urirY   r   rX   openr#   )r   r   r  r   r   r  filenamer   r&   r&   r+   r
    s   

r
  )NNNNNN)r.   r   r   r   r   r   r   r   r   r   r   r   r0   r!   )
r   r   r   r   r  r  r   rb   r0   r  )=r   
__future__r   rU   r  r   r  ior   r   r   r   r   r   typingr	   r
   r   r   r   r   r   r   r   r   urllib.parser   urllib.requestr   r   Zxml.saxr   Zrdflib.utilr  r   Zrdflib._networkingr   Zrdflib.namespacer   Zrdflib.termr   email.messager   r   urllib.responser   Ztyping_extensionsr   Zrdflib.graphr   __all__r    r7   r!   r%   r"   r   r#   r$   r  r
  r&   r&   r&   r+   <module>   sX    
 0

  ^'f&x