
    Pi<                       d dl mZ d dlZd dlZd dlZd dl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mZmZmZmZ d dlmZ d dlmZ g dZd dlZe	rd dlmZmZ d d	l m!Z!m"Z" d d
lm#Z#m$Z$  G d d          Z% G d d          Z& G d de'          Z( G d d          Z) G d deej*        j#        df                   Z+ G d d          Z, G d d          Z- G d d          Z.dS )    )annotationsN)BytesIO)IOTYPE_CHECKINGAnyBinaryIODictIteratorListMappingMutableSequenceOptionalTupleUnioncastoverload)urlparse)url2pathname)	ProcessorUpdateProcessorResult	ResultRowResultParserResultSerializerResultExceptionEncodeOnlyUnicode)Graph_TripleType)QueryUpdate)
IdentifierVariablec                  *    e Zd ZdZddZi i dfddZdS )r   z
    Query plugin interface.

    This module is useful for those wanting to write a query processor
    that can plugin to rdf. If you are wanting to execute a query you
    likely want to do so through the Graph class query method.
    graphr   c                    d S N selfr$   s     `/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/rdflib/query.py__init__zProcessor.__init__6           F
strOrQueryUnion[str, Query]initBindingsMapping[str, Identifier]initNsMapping[str, Any]DEBUGboolreturnc                    d S r&   r'   )r)   r.   r0   r2   r4   s        r*   queryzProcessor.query:   s	     	r-   Nr$   r   )
r.   r/   r0   r1   r2   r3   r4   r5   r6   r3   )__name__
__module____qualname____doc__r+   r8   r'   r-   r*   r   r   -   sV             24$&      r-   r   c                  (    e Zd ZdZddZi i fddZdS )r   a&  Update plugin interface.

    This module is useful for those wanting to write an update
    processor that can plugin to rdflib. If you are wanting to execute
    an update statement you likely want to do so through the Graph
    class update method.

    !!! example "New in version 4.0"
    r$   r   c                    d S r&   r'   r(   s     r*   r+   zUpdateProcessor.__init__O   r,   r-   r.   Union[str, Update]r0   r1   r2   r3   r6   Nonec                    d S r&   r'   )r)   r.   r0   r2   s       r*   updatezUpdateProcessor.updateR   s	     	r-   Nr9   )r.   r@   r0   r1   r2   r3   r6   rA   )r:   r;   r<   r=   r+   rC   r'   r-   r*   r   r   D   sS             24$&	      r-   r   c                      e Zd ZdS )r   N)r:   r;   r<   r'   r-   r*   r   r   [   s        Dr-   r   c                  (    e Zd ZdZddZd Zdd
ZdS )r   zBThis is a crappy work-around for http://bugs.python.org/issue11649streamr   c                    || _         d S r&   )_EncodeOnlyUnicode__stream)r)   rF   s     r*   r+   zEncodeOnlyUnicode.__init__b   s    r-   c                    t          |t                    r/| j                            |                    d                     d S | j                            |           d S )Nutf-8)
isinstancestrrH   writeencode)r)   args     r*   rM   zEncodeOnlyUnicode.writee   sX    c3 	%M

7 3 344444M$$$$$r-   namerL   r6   r   c                ,    t          | j        |          S r&   )getattrrH   r)   rP   s     r*   __getattr__zEncodeOnlyUnicode.__getattr__k   s    t}d+++r-   N)rF   r   rP   rL   r6   r   )r:   r;   r<   r=   r+   rM   rT   r'   r-   r*   r   r   _   sQ        LL   % % %, , , , , ,r-   r   c                       e Zd ZU dZded<   d fdZddZddZedd            Z	e	 ddd            Z		 dddZ	ddZ
 xZS ) r   a  A single result row allows accessing bindings as attributes or with []

    ```python
    >>> from rdflib import URIRef, Variable
    >>> rr=ResultRow({ Variable('a'): URIRef('urn:cake') }, [Variable('a')])

    >>> rr[0]
    rdflib.term.URIRef('urn:cake')
    >>> rr[1]
    Traceback (most recent call last):
        ...
    IndexError: tuple index out of range

    >>> rr.a
    rdflib.term.URIRef('urn:cake')
    >>> rr.b
    Traceback (most recent call last):
        ...
    AttributeError: b

    >>> rr['a']
    rdflib.term.URIRef('urn:cake')
    >>> rr['b']
    Traceback (most recent call last):
        ...
    KeyError: 'b'

    >>> rr[Variable('a')]
    rdflib.term.URIRef('urn:cake')

    ```

    !!! example "New in version 4.0"
    zMapping[str, int]labelsvaluesMapping[Variable, Identifier]List[Variable]c                    t          t          |                               | fd|D                       }t          d t	          |          D                       |_        |S )Nc              3  B   K   | ]}                     |          V  d S r&   )get).0vrX   s     r*   	<genexpr>z$ResultRow.__new__.<locals>.<genexpr>   s-      6U6Uvzz!}}6U6U6U6U6U6Ur-   c              3  P   K   | ]!}t          |d                    |d         fV  "dS )   r   N)rL   )r^   xs     r*   r`   z$ResultRow.__new__.<locals>.<genexpr>   s5      LLQAaD		1Q40LLLLLLr-   )superr   __new__dict	enumeraterW   )clsrX   rW   instance	__class__s    `  r*   re   zResultRow.__new__   se     C((006U6U6U6Uf6U6U6UVVLL)F:K:KLLLLLr-   rP   rL   r6   r!   c                ~    || j         vrt          |          t                              | | j         |                   S r&   )rW   AttributeErrortuple__getitem__rS   s     r*   rT   zResultRow.__getattr__   s:    t{"" &&&  t{4'8999r-   Union[str, int, Any]c                l   	 t                               | |          S # t          $ r || j        v r(t                               | | j        |                   cY S t	          |          | j        v r5t                               | | j        t	          |                             cY S t          |          w xY wr&   )rm   rn   	TypeErrorrW   rL   KeyErrorrS   s     r*   rn   zResultRow.__getitem__   s    		!$$T4000 	! 	! 	!t{""((t{4/@AAAAA4yyDK''((t{3t99/EFFFFF4.. 	!s    9B3A	B3#B3defaultc                    d S r&   r'   r)   rP   rs   s      r*   r]   zResultRow.get   s    ADr-   .Optional[Identifier]c                    d S r&   r'   ru   s      r*   r]   zResultRow.get   s	      #sr-   Nc                8    	 | |         S # t           $ r |cY S w xY wr&   )rr   ru   s      r*   r]   zResultRow.get   s5    	: 	 	 	NNN	s   
 Dict[str, Identifier]c                D     t           fd j        D                       S )Nc              3  <   K   | ]}|         
||         fV  d S r&   r'   )r^   r_   r)   s     r*   r`   z#ResultRow.asdict.<locals>.<genexpr>   s4      MMQa9LQQL9L9L9L9LMMr-   )rf   rW   r)   s   `r*   asdictzResultRow.asdict   s(    MMMM$+MMMMMMr-   )rX   rY   rW   rZ   )rP   rL   r6   r!   )rP   ro   r6   r!   )rP   rL   rs   r!   r6   r!   ).)rP   rL   rs   rv   r6   rv   r&   )r6   ry   )r:   r;   r<   r=   __annotations__re   rT   rn   r   r]   r}   __classcell__)rj   s   @r*   r   r   o   s         ! !F      : : : :
! 
! 
! 
! DDD XD9<# # # # X#
 :>    N N N N N N N Nr-   r   .c                      e Zd ZdZd'dZed(d            Zej        d)d            Ze	 	 	 d*d+d            Z		 	 	 d,d-dZ
d.dZd/d Zd0d"Zd1d$Zd2d&ZdS )3r   a  
    A common class for representing query result.

    There is a bit of magic here that makes this appear like different
    Python objects, depending on the type of result.

    If the type is "SELECT", iterating will yield lists of ResultRow objects

    If the type is "ASK", iterating will yield a single bool (or
    bool(result) will return the same bool)

    If the type is "CONSTRUCT" or "DESCRIBE" iterating will yield the
    triples.

    `len(result)` also works.
    type_rL   c                    |dvrt          d|z            || _        d | _        	 d | _        d | _        d | _        d | _        d S )N)	CONSTRUCTDESCRIBESELECTASKzUnknown Result type: %s)r   typevars	_bindings_genbindings	askAnswerr$   )r)   r   s     r*   r+   zResult.__init__   sT    BBB!";e"CDDD	.2	9IMOS)-&*


r-   r6   .MutableSequence[Mapping[Variable, Identifier]]c                p    | j         r)| xj        t          | j                   z  c_        d| _         | j        S )z6
        a list of variable bindings as dicts
        N)r   r   listr|   s    r*   bindingszResult.bindings   s:    
  	%NNd4#4555NN $D~r-   b^Union[MutableSequence[Mapping[Variable, Identifier]], Iterator[Mapping[Variable, Identifier]]]rA   c                    t          |t          j        t          j        f          r|| _        g | _        d S || _        d S r&   )rK   typesGeneratorType	itertoolsislicer   r   )r)   r   s     r*   r   zResult.bindings   sA     a%-y/?@AA 	 !DDNNN DNNNr-   NsourceOptional[IO]formatOptional[str]content_typekwargsr   c                    ddl m} |r|}n!|r|                    dd          d         }nd} |                    |t                                } |j        | fd|i|S )z#Parse a query result from a source.r   plugin;rb   xmlr   )rdflibr   splitr]   r   parse)r   r   r   r   r   
plugin_keyparsers          r*   r   zResult.parse   s     	"!!!!! 	JJ 	%++C33A6JJJ5J5577 v|
 
!-
17
 
 	
r-   rJ   r   destinationOptional[Union[str, IO]]encodingargsOptional[bytes]c                   | j         dv r | j        j        |f||d|S 	 ddlm}  |                    |t                    |           }|At                      }t          |          } |j        |fd|i| |	                                S t          |d          r1t          t          t                   |          }	 |j        |	fd|i| nt          t          |          }
t          |
          \  }}}}}}|dk    r)|d	k    rt!          d
|
d          t#          |          }n|
}t%          |d          5 }	 |j        |	fd|i| ddd           n# 1 swxY w Y   dS )a  
        Serialize the query result.


        Args:
            destination: Path of file output or BufferedIOBase object
                to write the output to. If `None` this function
                will return the output as  `bytes`
            encoding: Encoding of output.
            format: The format used for serialization.
                See [sparql.results module][rdflib.plugins.sparql.results]
                for all builtin SPARQL result serialization.
                Further serializer can be loaded [as plugin][rdflib.plugin].
                Some example formats are
                [csv][rdflib.plugins.sparql.results.csvresults.CSVResultSerializer],
                [json][rdflib.plugins.sparql.results.jsonresults.JSONResultSerializer],
                [txt][rdflib.plugins.sparql.results.txtresults.TXTResultSerializer]
                or
                [xml][rdflib.plugins.sparql.results.xmlresults.XMLResultSerializer]

        Returns:
            Serialized result, when destination is not given.
        r   r   )r   r   r   r   Nr   rM   file zthe file URI z2 has an authority component which is not supportedwb)r   r$   	serializer   r   r]   r   r   r   getvaluehasattrr   r   bytesrL   r   
ValueErrorr   open)r)   r   r   r   r   r   
serializerstreambstream2rF   locationschemenetlocpathparamsr8   fragmentos_paths                     r*   r   zResult.serialize  s   < 9111 (4:'&.v AE   	4!!!!!!9VZZ(899$??
&yyG'00G !J DD8DtDDD##%%%;(( 	H"U)[11F J CC(CdCCCCC--H<DX<N<N9FFD&%R<<$ffff   't,,"gt$$ H$
$VGGhG$GGGH H H H H H H H H H H H H H Hts   ;EEEintc                    | j         dk    rdS | j         dk    rt          | j                  S t          | j                  S )Nr   rb   r   )r   lenr   r$   r|   s    r*   __len__zResult.__len__V  sA    91Y(""t}%%% tz??"r-   r5   c                L    | j         dk    r| j        S t          |           dk    S )Nr   r   )r   r   r   r|   s    r*   __bool__zResult.__bool___  s'    9>!t99q= r-   -Iterator[Union[_TripleType, bool, ResultRow]]c              #  b  K   | j         dv r| j        D ]}|V  d S | j         dk    r| j        V  d S | j         dk    rp| j        rF| j        D ]5}|r1| j                            |           t          || j                  V  6d | _        d S | j        D ]}|rt          || j                  V  d S d S )Nr   r   r   )r   r$   r   r   r   appendr   r   )r)   tr   s      r*   __iter__zResult.__iter__f  s      9111Z   Y%.     Y(""   6* 6 6A 6--a000'4955555$(!!! 6 6A 6'4955555 #"6 6r-   rP   c                     j         dv r" j         j                             |          S  j         dk    r5|dk    r/t          j        dt
          d            fd j        D             S t          d d	|d          )
Nr   r   resultzPaccessing the 'result' attribute is deprecated. Iterate over the object instead.   )
stacklevelc                R    g | ]"t          fd j        D                       #S )c              3  (   K   | ]}|         V  d S r&   r'   )r^   r_   r   s     r*   r`   z0Result.__getattr__.<locals>.<listcomp>.<genexpr>  s'      33A1Q4333333r-   )rm   r   )r^   r   r)   s    @r*   
<listcomp>z&Result.__getattr__.<locals>.<listcomp>  s9    LLLU333333333LLLr-   'z' object has no attribute ')r   r$   rT   warningswarnDeprecationWarningr   rl   rS   s   ` r*   rT   zResult.__getattr__  s    9111dj6L:))$555Y(""tx'7'7M4"	    MLLLdmLLLL .ddd!STTTr-   otherc                    	 | j         |j         k    rdS | j         dk    r| j        |j        k    S | j         dk    r | j        |j        k    o| j        |j        k    S | j        |j        k    S # t
          $ r Y dS w xY w)NFr   r   )r   r   r   r   r$   	Exception)r)   r   s     r*   __eq__zResult.__eq__  s    
	yEJ&&uyE!!~88h&&yEJ.R4=EN3RRzU[00 	 	 	55	s!   A* A* *A* A* *
A87A8)r   rL   )r6   r   )r   r   r6   rA   )NNN)
r   r   r   r   r   r   r   r   r6   r   )NrJ   r   )
r   r   r   rL   r   rL   r   r   r6   r   )r6   r   )r6   r5   )r6   r   rU   )r   r   r6   r5   )r:   r;   r<   r=   r+   propertyr   setterstaticmethodr   r   r   r   r   rT   r   r'   r-   r*   r   r      s4        "+ + + +    X _   _ # $&*
 
 
 
 \
2 15	@ @ @ @ @D# # # #! ! ! !6 6 6 64U U U U"     r-   r   c                      e Zd Zd Zd
dZd	S )r   c                    d S r&   r'   r|   s    r*   r+   zResultParser.__init__  r,   r-   r   r   r   r   r6   r   c                    dS )zreturn a Result objectNr'   )r)   r   r   s      r*   r   zResultParser.parse      r-   N)r   r   r   r   r6   r   )r:   r;   r<   r+   r   r'   r-   r*   r   r     s7               r-   r   c                       e Zd ZddZdddZdS )r   r   r   c                    || _         d S r&   )r   )r)   r   s     r*   r+   zResultSerializer.__init__  s    r-   rJ   rF   r   r   rL   r   r   r6   rA   c                    dS )z#return a string properly serializedNr'   )r)   rF   r   r   s       r*   r   zResultSerializer.serialize  r   r-   N)r   r   )rJ   )rF   r   r   rL   r   r   r6   rA   )r:   r;   r<   r+   r   r'   r-   r*   r   r     sA                 r-   r   )/
__future__r   r   r   r   ior   typingr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   urllib.parser   urllib.requestr   __all__rdflib.termr   rdflib.graphr   r   rdflib.plugins.sparql.sparqlr   r    r!   r"   r   r   r   r   r   termr   r   r   r   r'   r-   r*   <module>r      s   " " " " " "                                             " ! ! ! ! ! ' ' ' ' ' '	 	 	     1////////::::::::00000000       .       .	 	 	 	 	i 	 	 	, , , , , , , , QN QN QN QN QNfk,c12 QN QN QNhY Y Y Y Y Y Y Yx                r-   