
    4Ysh                         d dl Z d dl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Zd dlmZ d dlmZ d dlmZ  e j(                  e      Zerd dlZ G d	 d
e      ZddddddZ G d de      Zy)    N)OptionalTYPE_CHECKINGTuple)urlopenRequest)	urlencode)	HTTPErrorURLError)BytesIO)Result)BNodec                       e Zd Zy)SPARQLConnectorExceptionN)__name__
__module____qualname__     /var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/stores/sparqlconnector.pyr   r      s    r   r   z3application/sparql-results+xml, application/rdf+xmlzapplication/sparql-results+jsonztext/csvztext/tab-separated-valuesapplication/rdf+xml)xmljsoncsvtsvr   c                       e Zd ZdZ	 	 	 	 	 ddee   dee   dedddeeeef      f
d	Zed
        Z	e	j                  d        Z	ddedefdZ	 	 ddee   dee   fdZy)SPARQLConnectorzR
    this class deals with nitty gritty details of talking to a SPARQL server
    Nquery_endpointupdate_endpointreturnFormatmethodz&te.Literal['GET', 'POST', 'POST_FORM']authc                    || _         || _        || _        || _        || _        |t        |      t        k7  rt        d      t        |      dk7  rt        d      t        j                  t        d|z  d            }| j                  j                  di        | j                  d   j                  dd	|j                  d
      z  i       yy)z
        auth, if present, must be a tuple of (username, password) used for Basic Authentication

        Any additional keyword arguments will be passed to to the request, and can be used to setup timesouts etc.
        Nzauth must be a tuple   z%auth must be a tuple (user, password)z%s:%sasciiheadersAuthorizationzBasic %szutf-8)r   r   r   kwargsr    typetupler   lenbase64	b64encodebytes
setdefaultupdatedecode)selfr   r   r   r    r!   r'   base64strings           r   __init__zSPARQLConnector.__init__&   s     ),.DzU"./EFF4yA~./VWW!++E'D.',JKLKK""9b1KK	")) *|/B/B7/K"KL r   c                     | j                   S )N)_method)r1   s    r   r    zSPARQLConnector.methodE   s    ||r   c                 0    |dvrt        d      || _        y )N)GETPOST	POST_FORMz,Method must be "GET", "POST", or "POST_FORM")r   r5   )r1   r    s     r   r    zSPARQLConnector.methodI   s$    55*>  r   default_graphnamed_graphc           	      J   | j                   st        d      i }|t        |      t        k7  r||d<   dt        | j
                     i}t        | j                        }|j                  di        |j                  di        |d   j                  |       | j                  dk(  rR||d<   |d   j                  |       dt        |d         z   }	 t        t        | j                   |z   |d   	            }n| j                  dk(  r[|d   j                  ddi       dt        |      z   }	 t        t        | j                   |z   |j!                         |d               }n| j                  dk(  rY||d<   |d   j                  |       	 t        t        | j                   t        |d         j!                         |d               }nt        d| j                  z        t)        j*                  t-        |j/                               |j0                  d   j3                  d      d         S # t        $ r}	t        d
      d }	~	ww xY w# t"        $ r"}	|	j$                  t'        |	      d fcY d }	~	S d }	~	ww xY w# t"        $ r"}	|	j$                  t'        |	      d fcY d }	~	S d }	~	ww xY w)NQuery endpoint not set!zdefault-graph-uriAcceptparamsr%   r7   query?)r%   zGYou did something wrong formulating either the URI or your SPARQL queryr8   Content-Typezapplication/sparql-querydatar%   r9   zUnknown method %s;r   )content_type)r   r   r(   r   _response_mime_typesr   dictr'   r.   r/   r    r   r   r   	Exception
ValueErrorencoder	   codestrr   parser   readr%   split)
r1   r@   r:   r;   r?   r%   argsqsareses
             r   r@   zSPARQLConnector.queryR   s   ""*+DEE$m)<)E*7F&'1$2C2CDEDKK  	"%	2&Yw';;%#F7ON!!&)	$x.11CD//#5tIO [[F"O""N4N#OP	&))C	,++c1"\\^ $Y [[K'#F7ON!!&)	,++&tH~6==? $Y ++>+LMM||CHHJckk..I.O.OPS.TUV.W
 	
A   ]   ,vvs1vt++,  ,vvs1vt++,sT   &H- *5I	 	>I7 -	I6II		I4I/)I4/I47	J" JJ"J"c                    | j                   st        d      i }|||d<   |||d<   t        | j                     dd}t	        | j
                        }|j                  di        |d   j                  |       |j                  di        |d   j                  |       dt        |d         z   }t        t        | j                   |z   |j                         |d   	            }y )
Nr=   zusing-graph-urizusing-named-graph-urizapplication/sparql-update)r>   rB   r?   r%   rA   rC   )r   r   rG   r   rH   r'   r.   r/   r   r   r   rK   )	r1   r@   r:   r;   r?   r%   rQ   rR   rS   s	            r   r/   zSPARQLConnector.update   s     ##*+DEE$(5F$%".9F*+ +4+<+<=7

 DKK "%Xf%	2&Yw'Id8n--$$s*i
r   )NNr   r7   N)NN)r   r   r   __doc__r   rM   r   r3   propertyr    setterr@   r/   r   r   r   r   r   !   s     )-)-!;@*.  "# 	
 9 uS#X'>   ]] =
# =
3 =
D (,%)	"
  }"
 c]	"
r   r   )loggingtypingr   r   r   urllib.requestr   r   urllib.parser   urllib.errorr	   r
   r+   ior   rdflib.queryr   rdflibr   	getLoggerr   logtyping_extensionsterI   r   rG   objectr   r   r   r   <module>rf      so     1 1 + " ,    g!"	y 	 A-&0 R
f R
r   