o
    mi+                     @  s   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r7ddlmZmZmZ 	d,d-ddZ			d.d/ddZ	d0d1ddZd2d d!Zd3d%d&Zd4d(d)Zd5d*d+ZdS )6zRDF4J utility functions.    )annotationsN)DATASET_DEFAULT_GRAPH_IDDatasetGraph)prepareQuery)BNodeIdentifiedNodeURIRef)
ObjectTypePredicateTypeSubjectTypeparamsdict[str, str]
graph_name8IdentifiedNode | t.Iterable[IdentifiedNode] | str | NonereturnNonec                 C  s   |durt |tr|tkrd| d< dS | | d< dS |dur.t |tr.t| | d< dS |durHt |tjrJddd |D }|| d< dS dS dS )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,c                 S  s   g | ]}|  qS  n3.0xr   r   S/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/contrib/rdf4j/util.py
<listcomp>)   s    z'build_context_param.<locals>.<listcomp>)	
isinstancer   r   r   strr	   tIterablejoin)r   r   Zgraph_namesr   r   r   build_context_param   s   r#   subjr   predr   objr
   c                 C  sD   |dur
|  | d< |dur|  | d< |dur |  | d< dS dS )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.
    Nr$   r%   r&   r   )r   r$   r%   r&   r   r   r   build_spo_param-   s   r'   Tinferboolc                 C  s   |sd| d< dS dS )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.
    falser(   Nr   )r   r(   r   r   r   build_infer_paramF   s   r+   data*str | bytes | t.BinaryIO | Graph | Datasettuple[t.BinaryIO, bool]c              	   C  s   t | trBd| vr4t| dk r4zt| d}d}W ||fS  ttfy3   t| d}d}Y ||fS w t| d}d}||fS t | t	rRt| }d}||fS t | t
tfrw| jrgt| jddd}n
t| jd	dd}d}||fS | }d}||fS )
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   Zcontext_aware	serialize)r,   streamZshould_closer   r   r   rdf_payload_to_streamW   s>   



r>   queryr   headersc                 C  sJ   t | }|jjdv rd|d< dS |jjdv rd|d< dS td|jj )zBuild the SPARQL query accept header.

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

    Parameters:
        query: The SPARQL query.
    )ZSelectQueryZAskQueryzapplication/sparql-results+jsonAccept)ZConstructQueryZDescribeQueryr3   zUnsupported query type: N)r   Zalgebraname
ValueError)r?   r@   Zprepared_queryr   r   r    build_sparql_query_accept_header   s   	rD   (URIRef | t.Iterable[URIRef] | str | Nonec                 C  s8   t | tst | tjrtdd | D rtdd S d S )Nc                 s  s    | ]}t |tV  qd S N)r   r   r   r   r   r   	<genexpr>   s    z&validate_graph_name.<locals>.<genexpr>zGraph name must not be a BNode.)r   r   r    r!   anyrC   )r   r   r   r   validate_graph_name   s   
rI   c                 C  sN   t | tst |tst |tst |tr!td|  d| d| t| dS )z@Validate that the subject, predicate, and object are not BNodes.z4Subject, predicate, and object must not be a BNode: z, N)r   r   rC   rI   )r$   r%   r&   r   r   r   r   validate_no_bnodes   s$   rJ   rF   )r   r   r   r   r   r   )NNN)
r   r   r$   r   r%   r   r&   r
   r   r   )T)r   r   r(   r)   r   r   )r,   r-   r   r.   )r?   r   r@   r   )r   rE   )
r$   r   r%   r   r&   r
   r   rE   r   r   )__doc__
__future__r   r8   typingr    Zrdflib.graphr   r   r   Zrdflib.plugins.sparql.processorr   Zrdflib.termr   r   r	   TYPE_CHECKINGZrdflib.contrib.rdf4j.clientr
   r   r   r#   r'   r+   r>   rD   rI   rJ   r   r   r   r   <module>   s*    

3
	