
    4Ysh	                     |    d 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	 ddl
mZmZmZ  G d de      Z G d d	e      Zy)
z

This module implements a parser and serializer for the CSV SPARQL result
formats

http://www.w3.org/TR/sparql11-results-csv-tsv/

    N)IO)VariableBNodeURIRefLiteral)ResultResultSerializerResultParserc                   &    e Zd Zd ZddZd Zd Zy)CSVResultParserc                     d| _         y )N,)delim)selfs    /var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/sparql/results/csvresults.py__init__zCSVResultParser.__init__   s	    
    Nc                    t        d      }t        |j                  d      t              r t	        j
                  d      |      }t        j                  || j                        }t        |      D cg c]  }t        |       c}|_        g |_        |D ]7  }|j                  j                  | j                  ||j                               9 |S c c}w )NSELECTr   utf-8	delimiter)r   
isinstancereadbytescodecs	getreadercsvreaderr   nextr   varsbindingsappendparseRow)r   sourcecontent_typerr   xrows          r   parsezCSVResultParser.parse   s    8fkk!ne,.V%%g.v6FFdjj9'+F|4!(1+4
 	:CJJdmmC89	:  5s   4Cc                 ~    t        d t        ||D cg c]  }| j                  |       c}      D              S c c}w )Nc              3   .   K   | ]  \  }}|||f  y wN ).0varvals      r   	<genexpr>z+CSVResultParser.parseRow.<locals>.<genexpr>)   s%      
S #J
s   )dictzipconvertTerm)r   r)   vts       r   r$   zCSVResultParser.parseRow(   s<     
#EAD$4$4Q$7#EF
 
 	
#Es   :c                     |dk(  ry |j                  d      rt        |      S |j                  d      s|j                  d      rt        |      S t        |      S )N z_:zhttp://zhttps://)
startswithr   r   r   )r   r7   s     r   r5   zCSVResultParser.convertTerm/   sL    7<<8O<<	"all:&>!9qzr   r-   )__name__
__module____qualname__r   r*   r$   r5   r.   r   r   r   r      s    "
r   r   c                   *    e Zd Zd ZddedefdZd Zy)CSVResultSerializerc                 r    t        j                  | |       d| _        |j                  dk7  rt	        d      y )Nr   r   z5CSVSerializer can only serialize select query results)r	   r   r   type	Exception)r   results     r   r   zCSVResultSerializer.__init__:   s6    !!$/
;;("STT #r   streamencodingc                    dd l }  |j                  |      |      }t        j                  || j                        }| j
                  j                  D cg c]  }| j                  ||       }}|j                  |       | j
                  j                  D ]T  }|j                  | j
                  j                  D cg c]#  }| j                  |j                  |      |      % c}       V y c c}w c c}w )Nr   r   )r   	getwriterr   writerr   rC   r!   serializeTermwriterowr"   get)	r   rD   rE   kwargsr   outr6   vsr)   s	            r   	serializezCSVResultSerializer.serializeA   s     	+!!!(+F3jj4::67;{{7G7GH!d  H-HHR;;'' 	CLLCG;;CSCSTa##CGGAJ9T	 I Us   C0>(C5
c                     |y|S )Nr9   r.   )r   termrE   s      r   rI   z!CSVResultSerializer.serializeTermT   s    <Kr   N)r   )r;   r<   r=   r   r   strrO   rI   r.   r   r   r?   r?   9   s     U c &r   r?   )__doc__r   r   typingr   rdflibr   r   r   r   rdflib.queryr   r	   r
   r   r?   r.   r   r   <module>rW      s;     
  3 3 ? ?#l #L* r   