o
    mi;                     @  s   d Z ddlm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mZmZmZ ddlmZmZmZ ddlmZmZ ddlmZmZmZ zddlZd	ZW n eyY   dZd
ZY nw erbddlmZ dgZG dd deZdS )z
This is a rdflib plugin for parsing Hextuple files, which are Newline-Delimited JSON
(ndjson) files, into Conjunctive. The store that backs the graph *must* be able to
handle contexts, i.e. multiple graphs.
    )annotationsN)TextIOWrapper)TYPE_CHECKINGAnyBinaryIOListOptionalTextIOUnion)ConjunctiveGraphDatasetGraph)InputSourceParser)BNodeLiteralURIRefTF)BufferedReaderHextuplesParserc                      s6   e Zd ZdZ fddZdd
dZddddZ  ZS )r   z(
    An RDFLib parser for Hextuples
    c                   s   t t|   d | _d| _d S )NF)superr   __init__default_context	skolemize)self	__class__ U/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/plugins/parsers/hext.pyr   %   s   
zHextuplesParser.__init__ds Union[Dataset, ConjunctiveGraph]tupList[Union[str, None]]returnNonec                 C  s  |d d u s|d d u s|d d u s|d d u rt d| |d dr9t|d ddd	}| jr8| }nt|d }t|d }|d d
krRt|d }n6|d dkrkt|d ddd	}| jrj| }n|d d u r~t|d t|d d}n
t|d |d d}|d d ur|d drt|d ddnt|d }t|tr| jr| }||	|||f d S | j
d ur| j
	|||f d S td)Nr            z;subject, predicate, value, datatype cannot be None. Given: _z_: )valueZglobalIdZlocalId   )datatype)lang   zNo context to parse into!)
ValueError
startswithr   replacer   r   r   
isinstanceget_contextaddr   	Exception)r   r   r    spocr   r   r   _parse_hextuple*   s@   0

zHextuplesParser._parse_hextupleFsourcer   graphr   r   boolkwargsr   c              	   K  s  | ddvrtd| d d |jjsJ d|| _t|jdd}|j}t|tt	fr4|j| _n|j
d urK|j
|j
krC|| _n||j
| _n|| _| j|ur\| j|_|| z| }W n ttfyo   d }Y nw z| }W n ttfy   d }Y nw |d u r|d u rtdt| tr|d us|d usJ tr|d ur|}	ntrt|tsJ |}	tj}
n|d ur|}	ntrt|tsJ t|d	d
}	tj}
|	D ](}t|dks| rq|
|}dd |D }|d dkrd|d< | || qd S )Nencoding)Nutf-8z8Hextuples files are always utf-8 encoded, I was passed: z", but I'm still going to use utf-8z-Hextuples Parser needs a context-aware store!T)storeZdefault_unionzLSource does not have a character stream or a byte stream and cannot be used r?   )r>   r   c                 S  s   g | ]
}|d kr
|ndqS )r(   Nr   ).0xr   r   r   
<listcomp>   s    z)HextuplesParser.parse.<locals>.<listcomp>r%   r(   )getwarningswarnr@   Zcontext_awarer   r   r   r1   r   
identifierr2   Zremove_graphZgetCharacterStreamAttributeErrorLookupErrorZgetByteStreamr.   typer   _HAS_ORJSONr   orjsonloadsr   jsonlenisspacer9   )r   r:   r;   r   r=   r   Z
ds_defaultZtext_streamZbinary_streamZ
use_streamrM   lineZraw_lineZhex_tuple_liner   r   r   parse`   sz   



zHextuplesParser.parse)r   r   r    r!   r"   r#   )F)
r:   r   r;   r   r   r<   r=   r   r"   r#   )__name__
__module____qualname____doc__r   r9   rR   __classcell__r   r   r   r   r       s
    
6) rV   
__future__r   rN   rE   ior   typingr   r   r   r   r   r	   r
   Zrdflib.graphr   r   r   Zrdflib.parserr   r   Zrdflib.termr   r   r   rL   rK   ImportErrorr   __all__r   r   r   r   r   <module>   s(    $