
    #HJj+                       d Z ddlmZ ddlZddlZddlmZmZm	Z	 ddl
mZ ddlmZmZmZ ej                   r
ddlmZmZmZ 	 d	 	 	 	 	 ddZ	 	 	 d	 	 	 	 	 	 	 	 	 dd	Z	 d	 	 	 	 	 dd
Z	 	 	 	 ddZddZddZ	 	 	 	 	 	 	 	 	 	 ddZy)zRDF4J utility functions.    )annotationsN)DATASET_DEFAULT_GRAPH_IDDatasetGraph)prepareQuery)BNodeIdentifiedNodeURIRef)
ObjectTypePredicateTypeSubjectTypec                v   |3t        |t              r#|t        k(  rd| d<   y|j                         | d<   y|-t        |t              rt        |      j                         | d<   y|Nt        |t        j                        r3dj                  |D cg c]  }|j                          c}      }|| d<   yyyc c}w )aJ  Build the RDF4J http context query parameters dictionary.

    !!! Note
        This mutates the params dictionary key `context`.

    Parameters:
        params: The `httpx.Request` parameter dictionary.
        graph_name: The graph name or iterable of graph names.

            This is the `context` query parameter value.
    Nnullcontext,)	
isinstancer	   r   n3strr
   tIterablejoin)params
graph_namexgraph_namess       c/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/rdflib/contrib/rdf4j/util.pybuild_context_paramr      s     *Z"H11 &F9 *F9		Jz3$?":.113y		Jz1::$Fhh
;
1
;<'y %G	;s   B6c                    ||j                         | d<   ||j                         | d<   ||j                         | d<   yy)a  Build the RDF4J http subj, predicate, and object query parameters dictionary.

    !!! Note
        This mutates the params dictionary key `subj`, `pred`, and `obj`.

    Parameters:
        params: The `httpx.Request` parameter dictionary.
        subj: The `subj` query parameter value.
        pred: The `pred` query parameter value.
        obj: The `obj` query parameter value.
    Nsubjpredobj)r   )r   r   r    r!   s       r   build_spo_paramr"   -   sG    " vv
u     c                    |sd| d<   yy)zBuild the RDF4J http infer query parameters dictionary.

    !!! Note
        This mutates the params dictionary key `infer`.

    Parameters:
        params: The `httpx.Request` parameter dictionary.
        infer: The `infer` query parameter value.
    falseinferN )r   r&   s     r   build_infer_paramr(   F   s     !w r#   c                   t        | t              rOd| vr!t        |       dk  r	 t        | d      }d}||fS t        j                  | j                  d            }d}||fS t        | t              rt        j                  |       }d}||fS t        | t        t        f      r_| j                  r't        j                  | j                  dd            }n&t        j                  | j                  d	d            }d}||fS | }d}||fS # t        t
        f$ r, t        j                  | j                  d            }d}Y ||fS w xY w)
a  Convert an RDF payload into a file-like object.

    Parameters:
        data: The RDF payload.

            This can be a python `str`, `bytes`, `BinaryIO`, or a
            [`Graph`][rdflib.graph.Graph] or [`Dataset`][rdflib.graph.Dataset].

    Returns:
        A tuple containing the file-like object and a boolean indicating whether the
        immediate caller should close the stream.
    
i  rbTzutf-8Fzapplication/n-quads)formatencodingapplication/n-triples)r   r   lenopenFileNotFoundErrorOSErrorioBytesIOencodebytesr   r   context_aware	serialize)datastreamshould_closes      r   rdf_payload_to_streamr<   W   s^     $tD	C%dD)#6 <) ZZG 45F L& <% 
D%	 D!  < 
D5'*	+ZZ&;gNF ZZ&=PF  < <5 &w/ %DKK$89$. <5%s   D 5EEc                    t        |       }|j                  j                  dv rd|d<   y|j                  j                  dv rd|d<   yt        d|j                  j                         )zBuild the SPARQL query accept header.

    !!! Note
        This mutates the headers dictionary key `Accept`.

    Parameters:
        query: The SPARQL query.
    )SelectQueryAskQueryzapplication/sparql-results+jsonAccept)ConstructQueryDescribeQueryr.   zUnsupported query type: N)r   algebraname
ValueError)queryheadersprepared_querys      r    build_sparql_query_accept_headerrI      sl     "%(N""&AA=				$	$(K	K33N4J4J4O4O3PQRRr#   c                    t        | t              s,t        | t        j                        rt	        d | D              rt        d      y y )Nc              3  <   K   | ]  }t        |t                y wN)r   r   ).0r   s     r   	<genexpr>z&validate_graph_name.<locals>.<genexpr>   s     9j
1e$js   zGraph name must not be a BNode.)r   r   r   r   anyrE   )r   s    r   validate_graph_namerP      s>    :u%j!**-9j99:;; : .r#   c                    t        | t              s0t        |t              s t        |t              st        |t              rt        d|  d| d|       t        |       y)z@Validate that the subject, predicate, and object are not BNodes.z4Subject, predicate, and object must not be a BNode: z, N)r   r   rE   rP   )r   r    r!   r   s       r   validate_no_bnodesrR      s\     	4dE"c5!j%(BfBtfBse%
 	
 
#r#   rL   )r   dict[str, str]r   z8IdentifiedNode | t.Iterable[IdentifiedNode] | str | NonereturnNone)NNN)
r   rS   r   r   r    r   r!   r   rT   rU   )T)r   rS   r&   boolrT   rU   )r9   z*str | bytes | t.BinaryIO | Graph | DatasetrT   ztuple[t.BinaryIO, bool])rF   r   rG   rS   )r   (URIRef | t.Iterable[URIRef] | str | None)
r   r   r    r   r!   r   r   rW   rT   rU   )__doc__
__future__r   r3   typingr   rdflib.graphr   r   r   rdflib.plugins.sparql.processorr   rdflib.termr   r	   r
   TYPE_CHECKINGrdflib.contrib.rdf4j.clientr   r   r   r   r"   r(   r<   rI   rP   rR   r'   r#   r   <module>r`      s	    " 	  A A 8 5 5??RR
 LP((H( 
(> 	!!
! ! 
	!
 
!6 """ 
""0 
40 0 fS$<$
$
$ 
$ 9	$
 
$r#   