o
    iG=                     @  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 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- g dZ.G dd dZ/G dd deZ0G dd dej1Z1G dd de1Z2G dd de1Z3dde! iZ4G dd de1Z5G dd  d e1Z6						d4d5d,d-Z7d6d2d3Z8dS )7a=  
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)urljoin)Requesturl2pathname)	xmlreader)__version__)_urlopen)	Namespace)URIRef)Message)
addinfourl)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#   H/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/rdflib/parser.py__init__;      zParser.__init__source'InputSource'sink'Graph'returnNonec                 C  r$   r%   r#   )r'   r+   r-   r#   r#   r(   parse>   r*   zParser.parseN)r+   r,   r-   r.   r/   r0   )__name__
__module____qualname__	__slots__r)   r1   r#   r#   r#   r(   r   8   s    r   c                      sL   e Zd ZdZdd fddZdd Zd	d
 Zdd Zdd Zdd Z	  Z
S )BytesIOWrapper)wrappedencodedencodingutf-8r7   strc                   s$   t t|   || _|| _d | _d S r%   )superr6   r)   r7   r9   r8   )r'   r7   r9   	__class__r#   r(   r)   E   s   
zBytesIOWrapper.__init__c                 O  s<   | j d u rt| j| j\}}t|| _ | j j|i |S r%   )r8   codecs
getencoderr9   r7   r   read)r'   argskwargsbblenr#   r#   r(   rA   K   s   

zBytesIOWrapper.readc                 O  s8   | j d u rt| j| j}t|| _ | j j|i |S r%   )r8   r?   r@   r9   r7   r   read1)r'   rB   rC   rD   r#   r#   r(   rF   Q   s   

zBytesIOWrapper.read1c                 O     t  r%   NotImplementedErrorr'   rB   rC   r#   r#   r(   readintoW      zBytesIOWrapper.readintoc                 O  rG   r%   rH   rJ   r#   r#   r(   	readinto1Z   rL   zBytesIOWrapper.readinto1c                 O  rG   r%   rH   rJ   r#   r#   r(   write]   rL   zBytesIOWrapper.write)r:   )r7   r;   )r2   r3   r4   r5   r)   rA   rF   rK   rM   rN   __classcell__r#   r#   r=   r(   r6   B   s    r6   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rP   F)r   r   r)   content_type
auto_closer'   rP   r#   r#   r(   r)   f   s   
zInputSource.__init__r/   r0   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 )Nclose)getCharacterStreamhasattrrV   	ExceptiongetByteStream)r'   cfr#   r#   r(   rV   k   s   zInputSource.closer%   )rP   rQ   r/   r0   )r2   r3   r4   __doc__r)   rV   r#   r#   r#   r(   r   a   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   rP   rQ   c                 C  s"   d | _ d| _d | _|| _|| _d S )NF)rS   rT   	public_idrP   r_   )r'   r_   rP   r#   r#   r(   r)      s
   
zPythonInputSource.__init__r/   c                 C     | j S r%   r`   r&   r#   r#   r(   getPublicId   rL   zPythonInputSource.getPublicIdr`   r0   c                 C  
   || _ d S r%   rb   )r'   r`   r#   r#   r(   setPublicId      
zPythonInputSource.setPublicIdc                 C  ra   r%   rR   r&   r#   r#   r(   getSystemId   rL   zPythonInputSource.getSystemIdc                 C  rd   r%   rR   rU   r#   r#   r(   setSystemId   rf   zPythonInputSource.setSystemIdc                 C  s
   d | _ d S r%   )r_   r&   r#   r#   r(   rV      rf   zPythonInputSource.closer%   )r_   r   rP   rQ   )r/   rQ   )r`   rQ   r/   r0   )rP   rQ   r/   r0   r]   )
r2   r3   r4   r^   r)   rc   re   rg   rh   rV   r#   r#   r#   r(   r"   z   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
    r:   NvalueUnion[str, bytes]r9   r;   rP   rQ   c                   s   t t| | t|tr't|}| | | | t||}| 	| d S t
|}| 	| t||}| | | |j d S r%   )r<   r   r)   
isinstancer;   r   setCharacterStreamsetEncodingr6   setByteStreamr   r   r9   )r'   ri   r9   rP   streamb_streamc_streamr=   r#   r(   r)      s   






zStringInputSource.__init__)r:   N)ri   rj   r9   r;   rP   rQ   )r2   r3   r4   r^   r)   rO   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message	'Message'r/   c                   s        fdd| D S )Nc                   s    g | ]\}}|   kr|qS r#   )lower).0keyvalnamer#   r(   
<listcomp>   s     z8URLInputSource.getallmatchingheaders.<locals>.<listcomp>)rv   items)clsrt   r{   r#   rz   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)rw   linkstrr#   r#   r(   r|          z,URLInputSource.get_links.<locals>.<listcomp>,)r   headerssplitappend)r~   r   
linkslinesretarray	linksliners   linkr#   r#   r(   	get_links   s   zURLInputSource.get_linksNtype_rQ   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   )rw   pr#   r#   r(   r|      r   z1URLInputSource.get_alternates.<locals>.<listcomp>;r   z<>)rs   r   r   r   )r'   r   typestrrelstraltsr   partsr#   r#   r(   get_alternates   s   
zURLInputSource.get_alternatesrP   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Acceptn3ztext/n3, */*;q=0.1)turtlettlz,text/turtle, application/x-turtle, */*;q=0.1ntztext/plain, */*;q=0.1trigzapplication/trig, */*;q=0.1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      )r<   r    r)   urldictr   rdflib.parserr   rdflib.pluginr   r{   r   joinr   r   geturlr   rs   r   r   re   r   rS   r   rn   inforesponse_info)r'   rP   r   	myheadersr   r   accr   reqr   r   r   	full_linkcontent_typesr=   r#   r(   r)      s`   










zURLInputSource.__init__r;   c                 C  ra   r%   )r   r&   r#   r#   r(   __repr__#  s   zURLInputSource.__repr__)rt   ru   r/   rr   )r   r   r/   rr   r%   )r   rQ   r/   rr   )NN)rP   rQ   r   rQ   r/   r;   )r2   r3   r4   r^   __annotations__classmethodr   r   r   r)   r   rO   r#   r#   r=   r(   r       s   
 	:r    c                      s(   e Zd Zd	 fddZd
ddZ  ZS )r!   file>Union[BinaryIO, TextIO, TextIOBase, RawIOBase, BufferedIOBase]c              	     s   t j  }tt |j  |d}tt| 	| || _
t|trM| | | |j z|j}| | W d S  ttfyL   | | Y d S w | | d S )Nbase)pathlibPathcwdas_urir   r{   absoluter<   r!   r)   r   rk   r   rl   rm   r9   bufferrn   AttributeErrorLookupError)r'   r   r   rP   rD   r=   r#   r(   r)   )  s   

zFileInputSource.__init__r/   r;   c                 C  s
   t | jS r%   )reprr   r&   r#   r#   r(   r   =  rf   zFileInputSource.__repr__)r   r   r   )r2   r3   r4   r)   r   rO   r#   r#   r=   r(   r!   (  s    r!   r+   MOptional[Union[IO[bytes], TextIO, InputSource, str, bytes, pathlib.PurePath]]publicIDrQ   locationr   !Optional[Union[BinaryIO, TextIO]]r_   !Optional[Union[str, bytes, dict]]r   r/   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  s    z%create_input_source.<locals>.<lambda>r   z;exactly one of source, location, file or data must be givenNrA   r9   zfile:///dev/stdinr{   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 InputSource )$listfilterlen
ValueErrorr
   rk   r   r;   r   PurePathbytesrX   r   rl   rm   r9   r   rn   r   r   sysstdinrh   r{   rY   type"_create_input_source_from_locationr!   r   r"   	bytearrayr   RuntimeErrorrT   re   rc   )r+   r   r   r   r_   r   non_empty_argumentsr   r\   rD   absolute_locationrT   r#   r#   r(   create_input_sourceA  s   













r   r   Optional[InputSource]r;   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util_iri2uri
startswithr   replaceopenr    )r   r   r   r   r   r   filenamerT   r#   r#   r(   r     s   

r   )NNNNNN)r+   r   r   rQ   r   rQ   r   r   r_   r   r   rQ   r/   r   )
r   r   r   rQ   r   r   r   r;   r/   r   )9r^   
__future__r   r?   r   r   r   ior   r   r   r   r   r   typingr	   r
   r   r   r   r   r   r   r   urllib.parser   urllib.requestr   r   xml.saxr   rdflib.utilr   r   rdflib._networkingr   rdflib.namespacer   rdflib.termr   email.messager   urllib.responser   rdflib.graphr   __all__r   r6   r   r"   r   r   r    r!   r   r   r#   r#   r#   r(   <module>   sP     ,

'fx