o
    
i}                     @  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-8localIdglobalId )_HAS_ORJSONorjsonFragmentstr_local_idstr_global_idemptyr   
langStringencodelang_strr   string
xsd_stringsuper__new__)clsr   	__class__ S/home/ubuntu/.local/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   r0   r0   r1   r8   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   formula_aware	Exceptionr   r
   r	   strbytesr4   r   r%   r    r!   r"   _context_str	_hex_linewriter'   )	r9   r:   r<   r>   r?   contexttriplecontext_strhlr0   r0   r1   	serializeK   s<   


zHextuplesSerializer.serializerO   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
)r3   r   r   r   
_iri_or_bnr$   r#   datatypelanguager(   r*   r%   r    r!   r   OPT_APPEND_NEWLINEjson)r9   rN   rO   valuerX   rY   	line_listoutliner0   r0   r1   rK   y   sH   
	zHextuplesSerializer._hex_linec                 C  s(   t |tr| S t |tr|  S d S r2   )r3   r   r   n3)r9   i_r0   r0   r1   rW      s
   


zHextuplesSerializer._iri_or_bnrM   Union[Graph, IdentifiedNode]rH   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   )	r3   r   
identifierr   r6   r   r4   r   r_   )r9   rM   context_identifierr0   r0   r1   rJ      s(   




z HextuplesSerializer._context_str)r   r   rB   )
r:   r;   r<   r=   r>   r=   r?   r   r@   rA   )rO   rR   )rM   ra   r@   rH   )__name__
__module____qualname____doc____annotations__r,   r8   rQ   rK   rW   rJ   __classcell__r0   r0   r.   r1   r      s   
 

.6)"rg   
__future__r   r[   rD   typingr   r   r   r   r   r   r	   r
   rdflib.graphr   r   r   r   rdflib.namespacer   r   rdflib.serializerr   rdflib.termr   r   r   r   r!   r    ImportError__all__r   r0   r0   r0   r1   <module>   s$    (