o
    i]                     @   s   d Z ddlZddlZddlmZmZmZmZ ddl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 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.
    N)IOOptionalTypeUnion)ConjunctiveGraphGraph)RDFXSD)
Serializer)BNodeLiteralNodeURIRefHextuplesSerializerc                   @   sf   e Zd ZdZdeeef fddZ		ddee	 de
e d	e
e fd
dZdd Zdd Zdd ZdS )r   z3
    Serializes RDF graphs to NTriples format.
    storec                 C   sp   |  |  t |tr&t| _t| | _|jr"|j| _| j|j nd | _n
t| _|g| _d | _t	| | d S N)

isinstancer   
graph_typelistcontextsdefault_contextappendr   r
   __init__)selfr    r   Z/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/rdflib/plugins/serializers/hext.pyr      s   
zHextuplesSerializer.__init__Nutf-8streambaseencodingc                 K   s|   |d ur	t d |dvrt d| d | jjdu r td| jD ]}|D ]}| ||}|d ur:||  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   	_hex_linewriteencode)r   r   r   r   kwargscontexttriplehlr   r   r   	serialize(   s,   
zHextuplesSerializer.serializec              
   C   s  t |d ttfrt |d tr|d n| |d }t |d tr%d}n0t |d tr/d}n&t |d trS|d jd urD|d j }n|d jd urOtj}nt	j
}nd S t |d trm|d jd urj|d j }nd}nd}t| |d |d |||| |gd S d S )Nr      globalIdlocalId    
)r   r   r   r   
_iri_or_bndatatypelanguager   
langStringr	   stringjsondumps_context)r   r*   r)   valuer4   r5   r   r   r   r%   G   sH   

zHextuplesSerializer._hex_linec                 C   s(   t |tr| S t |tr|  S d S r   )r   r   r   n3)r   i_r   r   r   r3   }   s
   


zHextuplesSerializer._iri_or_bnc                 C   sF   | j tkrdS |jdkrdS |d ur | jd ur |j| jjkr dS |jS )Nr0   zurn:x-rdflib:default)r   r   
identifierr   )r   r)   r   r   r   r:      s   

zHextuplesSerializer._contextr    )__name__
__module____qualname____doc__r   r   r   r   r   bytesr   strr,   r%   r3   r:   r   r   r   r   r      s    
6)rB   r8   r!   typingr   r   r   r   rdflib.graphr   r   rdflib.namespacer   r	   rdflib.serializerr
   rdflib.termr   r   r   r   __all__r   r   r   r   r   <module>   s    