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m	Z	m
Z
mZmZmZ ddlmZ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mZmZ zddlZd	ZW n ey_   dZd
ZY nw dgZ G dd deZ!dS )z~
HextuplesSerializer RDF graph serializer for RDFLib.
See <https://github.com/ontola/hextuples> for details about the format.
    )annotationsN)IOAnyCallableListOptionalTypeUnioncast)DATASET_DEFAULT_GRAPH_IDConjunctiveGraphDatasetGraph)RDFXSD)
Serializer)BNodeIdentifiedNodeLiteralURIRefTFHextuplesSerializerc                      sj   e Zd ZU dZded< ded< d$ fdd	Zd$d
dZ		d%d&ddZd'ddZdd Z	d(d"d#Z
  ZS ))r   z3
    Serializes RDF graphs to NTriples format.
    z"List[Union[Graph, IdentifiedNode]]contextsr   dumpsstore'Union[Graph, Dataset, ConjunctiveGraph]c                   s   t r1td| _td| _td| _tdtjd d | _	tdt
jd d | _nd| _d| _d| _tj | _	t
j | _t| | | S )	Ns	   "localId"s
   "globalId"s   ""   "utf-8ZlocalIdZglobalId )_HAS_ORJSONorjsonFragmentstr_local_idstr_global_idemptyr   Z
langStringencodelang_strr   string
xsd_stringsuper__new__)clsr   	__class__ Y/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/plugins/serializers/hext.pyr)   $   s    

zHextuplesSerializer.__new__c                 C  s   |  |  t |ttfr/t |trtnt| _t| | _|jr+|j| _| j|j nd | _n
t| _|g| _d | _t	
| | d S N)
isinstancer   r   
graph_typelistr   default_contextappendr   r   __init__)selfr   r-   r-   r.   r5   7   s   zHextuplesSerializer.__init__Nr   stream	IO[bytes]baseOptional[str]encodingkwargsr   returnNonec           	   
   K  s   |d ur	t d |dvrt d| d | jjdu r td| jD ]>}|D ]9}tttt	f | j
tu r7| jntrEtd| | d n| |}| ||}|d ur`|tr[|n|  q'q#d S )NzIbase has no meaning for Hextuples serialization. I will ignore this valueNr   z8Hextuples files are always utf-8 encoded. I was passed: z*, but I'm still going to use utf-8 anyway!Tz>Hextuple serialization can't (yet) handle formula-aware stores")warningswarnr   Zformula_aware	Exceptionr   r
   r	   strbytesr1   r   r#   r   r   r    _context_str	_hex_linewriter$   )	r6   r7   r9   r;   r<   contexttriplecontext_strZhlr-   r-   r.   	serializeK   s<   


zHextuplesSerializer.serializerK   Union[bytes, str]c                 C  s2  t |d ttfrt |d tr|d n| |d }t |d tr&| j}n1t |d tr1| j}n&t |d trU|d jd urF|d j }n|d jd urQ| j	}n| j
}nd S t |d trp|d jd url|d j }n| j}n| j}| |d |d ||||g}trtj|tjd}|S t|d }|S d S )Nr         )option
)r0   r   r   r   
_iri_or_bnr"   r!   datatypelanguager%   r'   r#   r   r   r   ZOPT_APPEND_NEWLINEjson)r6   rJ   rK   valuerS   rT   Z	line_listoutliner-   r-   r.   rG   y   sH   
	zHextuplesSerializer._hex_linec                 C  s(   t |tr| S t |tr|  S d S r/   )r0   r   r   n3)r6   Zi_r-   r-   r.   rR      s
   


zHextuplesSerializer._iri_or_bnrI   Union[Graph, IdentifiedNode]rD   c                 C  s   t |tr|jn|}|tkrdS | jd ur0t | jtr"|| jkr"dS t | jtr0|| jjkr0dS | jtu r7dS t |tr?| S | S )Nr   )	r0   r   
identifierr   r3   r   r1   r   rX   )r6   rI   Zcontext_identifierr-   r-   r.   rF      s(   




z HextuplesSerializer._context_str)r   r   r?   )
r7   r8   r9   r:   r;   r:   r<   r   r=   r>   )rK   rM   )rI   rY   r=   rD   )__name__
__module____qualname____doc____annotations__r)   r5   rL   rG   rR   rF   __classcell__r-   r-   r+   r.   r      s   
 

.6)"r^   
__future__r   rU   rA   typingr   r   r   r   r   r   r	   r
   Zrdflib.graphr   r   r   r   Zrdflib.namespacer   r   Zrdflib.serializerr   Zrdflib.termr   r   r   r   r   r   ImportError__all__r   r-   r-   r-   r.   <module>   s$    (