
    4Ysh*                     X   d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
mZmZmZmZmZ d dlmZ d dlmZ d dlZ	 G d de      Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d      Z G d d      Z y)    N)MappingMutableMapping)NamespaceManager)VariableBNodeGraphConjunctiveGraphURIRefLiteral)Node)	CompValuec                       e Zd ZddZy)SPARQLErrorNc                 0    t         j                  | |       y N)	Exception__init__selfmsgs     |/var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/sparql/sparql.pyr   zSPARQLError.__init__   s    4%    r   __name__
__module____qualname__r    r   r   r   r      s    &r   r   c                       e Zd ZddZy)NotBoundErrorNc                 0    t         j                  | |       y r   r   r   r   s     r   r   zNotBoundError.__init__       T3'r   r   r   r   r   r   r   r      s    (r   r   c                       e Zd ZdZd Zy)AlreadyBoundz<Raised when trying to bind a variable that is already bound!c                 .    t         j                  |        y r   r!   r   s    r   r   zAlreadyBound.__init__   s    T"r   Nr   r   r   __doc__r   r   r   r   r$   r$      s
    F#r   r$   c                       e Zd Zd Zy)SPARQLTypeErrorc                 0    t         j                  | |       y r   r!   r   s     r   r   zSPARQLTypeError.__init__#   r"   r   Nr   r   r   r   r*   r*   "   s    (r   r*   c                   R    e Zd ZdZdg fdZd Zd Zd Zd Zde	fd	Z
d
 Zd Zd Zy)Bindingsz

    A single level of a stack of variable-value bindings.
    Each dict keeps a reference to the dict below it,
    any failed lookup is propegated back

    In python 3.3 this could be a collections.ChainMap
    Nc                 2    t        |      | _        || _        y r   )dict_douter)r   r1   ds      r   r   zBindings.__init__2   s    q'
r   c                     || j                   v r| j                   |   S | j                  s
t               | j                  |   S r   )r0   r1   KeyErrorr   keys     r   __getitem__zBindings.__getitem__6   s8    $''>773<zz*zz#r   c                 .    	 | |    y# t         $ r Y yw xY w)NTFr4   r5   s     r   __contains__zBindings.__contains__>   s#    	I 		s    	c                 "    || j                   |<   y r   r0   r   r6   values      r   __setitem__zBindings.__setitem__E   s    r   c                     t        d      )NzDelItem is not implemented!)r   r5   s     r   __delitem__zBindings.__delitem__H   s    566r   returnc                 `    d}| }|'|t        |j                        z  }|j                  }|'|S Nr   )lenr0   r1   )r   ir2   s      r   __len__zBindings.__len__K   s9    mQTTNAA m r   c              #   `   K   | }|$|j                   E d {    |j                  }|#y y 7 wr   )r0   r1   )r   r2   s     r   __iter__zBindings.__iter__S   s.     mttOOA ms   .,..c                 D     ddj                   fd D              z   dz   S )Nz
Bindings({z, c              3   ,   K   | ]  }||   f  y wr   r   ).0kr   s     r   	<genexpr>z#Bindings.__str__.<locals>.<genexpr>Z   s     'CDG'Cs   z}))joinr&   s   `r   __str__zBindings.__str__Y   s"    dii'Cd'CCCdJJr   c                     t        |       S r   )strr&   s    r   __repr__zBindings.__repr__\   s    4yr   )r   r   r   r(   r   r7   r:   r?   rA   intrG   rI   rP   rS   r   r   r   r-   r-   '   sC     "R 7 Kr   r-   c                   R    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)
FrozenDictz^
    An immutable hashable dict

    Taken from http://stackoverflow.com/a/2704866/81121

    c                 2    t        |i || _        d | _        y r   )r/   r0   _hash)r   argskwargss      r   r   zFrozenDict.__init__h   s    ''
r   c                 ,    t        | j                        S r   )iterr0   r&   s    r   rI   zFrozenDict.__iter__l       DGG}r   c                 ,    t        | j                        S r   )rE   r0   r&   s    r   rG   zFrozenDict.__len__o       477|r   c                      | j                   |   S r   r<   r5   s     r   r7   zFrozenDict.__getitem__r   s    wws|r   c                     | j                   [d| _         | j                         D ]A  \  }}| xj                   t        |      z  c_         | xj                   t        |      z  c_         C | j                   S rD   )rX   itemshashr=   s      r   __hash__zFrozenDict.__hash__u   s^     ::DJ"jjl *
U

d3i'


d5k)
* zzr   c                 H    t        fd| j                         D              S )Nc              3   2   K   | ]  }|d    v s|  ywr   Nr   rL   xvarss     r   rN   z%FrozenDict.project.<locals>.<genexpr>   s     Caddl1C   )rV   rb   r   rj   s    `r   projectzFrozenDict.project   s    CdjjlCDDr   c                 J    t        t        |       j                  |             S r   )boolsetintersection)r   others     r   disjointDomainzFrozenDict.disjointDomain   s    D	..u5666r   c                 L    | D ]  }	 | |   ||   k7  r y y# t         $ r Y w xY w)NFTr9   )r   rr   rM   s      r   
compatiblezFrozenDict.compatible   sG     	A7eAh&  '	   s   	##c                 |    t        t        j                  | j                         |j                                     }|S r   )rV   	itertoolschainrb   r   rr   ress      r   mergezFrozenDict.merge   s(    u{{}EF
r   c                 ,    t        | j                        S r   )rR   r0   r&   s    r   rP   zFrozenDict.__str__   r_   r   c                 ,    t        | j                        S r   )reprr0   r&   s    r   rS   zFrozenDict.__repr__   r]   r   N)r   r   r   r(   r   rI   rG   r7   rd   rm   rs   ru   r{   rP   rS   r   r   r   rV   rV   `   s?    E7
r   rV   c                   b    e Zd Zd Zd Zd Zd Zed        Zed        Z	ed        Z
dd	Zd
 Zy)FrozenBindingsc                 D    t        j                  | g|i | || _        y r   )rV   r   ctx)r   r   rY   rZ   s       r   r   zFrozenBindings.__init__   s!    D24262r   c                     t        |t              st        |      }t        |t        t        f      s|S || j                  vr| j
                  j                  |   S | j                  |   S r   )
isinstancer   r   r   r0   r   initBindingsr5   s     r   r7   zFrozenBindings.__getitem__   sX    #t$3-C#x01Jdgg88((--773<r   c                 ^    t        | j                  fd| j                         D              S )Nc              3   2   K   | ]  }|d    v s|  ywrg   r   rh   s     r   rN   z)FrozenBindings.project.<locals>.<genexpr>   s     (QqAaDDL(Qrk   r   r   rb   rl   s    `r   rm   zFrozenBindings.project   s     dhh(QDJJL(QRRr   c                     t        | j                  t        j                  | j	                         |j	                                     }|S r   )r   r   rw   rx   rb   ry   s      r   r{   zFrozenBindings.merge   s.    TXXytzz|U[[]'ST
r   c                 .    | j                   j                  S r   )r   nowr&   s    r   r   zFrozenBindings.now   s    xx||r   c                 .    | j                   j                  S r   )r   bnodesr&   s    r   r   zFrozenBindings.bnodes   s    xxr   c                 .    | j                   j                  S r   )r   prologuer&   s    r   r   zFrozenBindings.prologue   s    xx   r   Nc                 n     sg t         j                   fd j                         D              S )zY
        return a frozen dict only of bindings made in self
        since before
        c              3   v   K   | ]0  }|d    v s#|d    j                   j                  v s|d       | 2 ywrg   )r   r   )rL   ri   _exceptbeforer   s     r   rN   z(FrozenBindings.forget.<locals>.<genexpr>   sH      aDGOttxx444ad|+ s   69r   )r   r   r   s   ```r   forgetzFrozenBindings.forget   s6    
 G HH
 	
r   c                 ^    t        | j                  fd| j                         D              S )z@
        return a frozen dict only of bindings in these
        c              3   2   K   | ]  }|d    v s|  ywrg   r   )rL   ri   theses     r   rN   z*FrozenBindings.remember.<locals>.<genexpr>   s     (RqAaDEM(Rrk   r   )r   r   s    `r   rememberzFrozenBindings.remember   s"     dhh(RDJJL(RSSr   r   )r   r   r   r   r7   rm   r{   propertyr   r   r   r   r   r   r   r   r   r      s^     S     ! !
,Tr   r   c                       e Zd ZdZddZedej                  fd       ZddZed        Z	ddZ
d	 Zdd
ZddZd Zd Zd Zd Zd Zy)QueryContextz@
    Query context - passed along when evaluating the query
    Nc                    || _         t        |xs g       | _        |r| j                  j                  |       t	        |t
              rY|| _        t        j                  j                  j                  r| j                  | _        n*| j                  j                  | _        nd | _        || _        d | _        d | _        t!        j"                  t$              | _        y )N)r2   )r   r-   bindingsupdater   r	   _datasetrdflibpluginssparqlSPARQL_DEFAULT_GRAPH_UNIONdatasetgraphdefault_contextr   _nowcollectionsdefaultdictr   r   )r   r   r   r   s       r   r   zQueryContext.__init__   s    ( 8>r2MM  .e-.!DM~~$$??!\\
!\\99
 DMDJ	!--e4r   rB   c                     | j                   <t        j                  j                  t        j                  j
                        | _         | j                   S r   )r   datetimer   isodatetzinfoUTCr&   s    r   r   zQueryContext.now   s8    99 ))--gnn.@.@ADIyyr   c                     t        | j                  | j                  n| j                  |xs | j                  | j                        }| j
                  |_        | j                  |_        | j                  |_        |S )N)r   )r   r   r   r   r   r   r   )r   r   rs      r   clonezQueryContext.clone   s_    !]]6DMMDJJ%**

 ]]
**;;r   c                 H    | j                   t        d      | j                   S )z "current datasetzwYou performed a query operation requiring a dataset (i.e. ConjunctiveGraph), but operating currently on a single graph.)r   r   r&   s    r   r   zQueryContext.dataset  s,     == ; 
 }}r   c                    fd}t         j                  j                  j                  s2|r/| xj                  | j
                  j                  |      z  c_        y y |r || j                  |       y  || j
                  |       y )Nc                 D   	  | j                   |fddiS # t        $ r Y nw xY w	  | j                   |fddiS # t        $ r Y nw xY w	  | j                   |fddiS # t        $ r Y nw xY w	  | j                   |fddiS # t        $ r t        d|z        w xY w)Nformatturtlexmln3ntz3Could not load %s as either RDF/XML, N3 or NTriples)parser   )r   sourcerZ   s     r   _loadz QueryContext.load.<locals>._load  s    "u{{6E(EfEE "u{{6B%B6BB "u{{6A$A&AA "u{{6A$A&AA IFR s9    	$$= 	A	A	A" "	A.-A.2B B)r   r   r   SPARQL_LOAD_GRAPHSr   r   get_context)r   r   defaultrZ   r   s      ` r   loadzQueryContext.load  se    	( ~~$$77 

dll66v>>
  djj&)dllF+r   c                 p    t        |t        t        f      s|S 	 | j                  |   S # t        $ r Y y w xY wr   )r   r   r   r   r4   r5   s     r   r7   zQueryContext.__getitem__7  s<    #x01J	==%% 		s   ) 	55c                 0    	 | |   S # t         $ r |cY S w xY wr   r9   )r   r6   r   s      r   getzQueryContext.get@  s%    	9 	N	s    c                     r-t        | fd| j                  j                         D              S t        | | j                  j                               S )zO
        Return a static copy of the current variable bindings as dict
        c              3   6   K   | ]  \  }}|v s||f  y wr   r   )rL   rM   vrj   s      r   rN   z(QueryContext.solution.<locals>.<genexpr>L  s     M$!Q191vMs   	)r   r   rb   rl   s    `r   solutionzQueryContext.solutionF  sI     !M$--*=*=*?M  "$(;(;(=>>r   c                 v    || j                   v r| j                   |   |k7  r
t               || j                   |<   y r   )r   r$   r=   s      r   r?   zQueryContext.__setitem__Q  s4    $--DMM#$6%$?. "cr   c                 4    | j                         }||_        |S r   )r   r   )r   r   r   s      r   	pushGraphzQueryContext.pushGraphW  s    JJLr   c                 N    | j                  t        | j                              }|S r   )r   r-   r   )r   r   s     r   pushzQueryContext.push\  s    JJx./r   c                 $    | j                  g       S r   r   r&   s    r   cleanzQueryContext.clean`  s    zz"~r   c                 (    | j                  |      }|S )zO
        Create a new read/write query context from the given solution
        r   )r   frozenbindingscs      r   thawzQueryContext.thawc  s     JJ~&r   )NNNr   )F)r   r   r   r(   r   r   r   r   r   r   r   r7   r   r   r?   r   r   r   r   r   r   r   r   r      st    5* X&&  
	  ,B	?#
r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)ProloguezI
    A class for holding prefixing bindings and base URI information
    c                 B    d | _         t        t                     | _        y r   )baser   r   namespace_managerr&   s    r   r   zPrologue.__init__q  s    	!1%'!:r   c                     | j                   j                  j                  |xs d      }|t        d|z        t	        ||xs dz         S )N zUnknown namespace prefix : %s)r   store	namespacer   r
   )r   prefix	localnamenss       r   resolvePNamezPrologue.resolvePNameu  sL    ##))33FLbA:;fDEEbIO,--r   c                 @    | j                   j                  ||d       y )NT)replace)r   bind)r   r   uris      r   r   zPrologue.bind{  s    ##FC#>r   c                 ~   t        |t              r|j                  dk(  r&| j                  |j                  |j
                        S |j                  dk(  r;t        |j                  |j                  | j                  |j                              S |S t        |t              rd|vrt        || j                        S |S )z
        Apply BASE / PREFIXes to URIs
        (and to datatypes in Literals)

        TODO: Move resolving URIs to pre-processing
        pnameliteral)langdatatype:)r   )r   r   namer   r   r   r   stringr   
absolutizer   r
   r   )r   iris     r   r   zPrologue.absolutize~  s     c9%xx7"((S]]CCxx9$JJSXX8U  
 V$SCZ#DII..
r   N)r   r   r   r(   r   r   r   r   r   r   r   r   r   l  s    ;.?r   r   c                       e Zd ZdZd Zy)Queryz'
    A parsed and translated query
    c                      || _         || _        y r   )r   algebra)r   r   r   s      r   r   zQuery.__init__  s     r   Nr'   r   r   r   r   r     s    r   r   )!r   rw   r   r   rdflib.compatr   r   rdflib.namespacer   r   r   r   r   r	   r
   r   rdflib.termr   !rdflib.plugins.sparql.parserutilsr   rdflib.plugins.sparqlr   r   r   r$   r*   r-   rV   r   objectr   r   r   r   r   r   <module>r      s        1 - L L  7 &) &
(K (
#; #(k (
6~ 6r; ;|?TZ ?TDI6 IX$ $N r   