o
    i%                     @   sj   d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 dgZdZdZdZd	Zd	ZG d
d de
ZdS )ao  
LongTurtle RDF graph serializer for RDFLib.
See <http://www.w3.org/TeamSubmission/turtle/> for syntax specification.

This variant, longturtle as opposed to just turtle, makes some small format changes
to turtle - the original turtle serializer. It:

* uses PREFIX instead of @prefix
* uses BASE instead of @base
* adds a new line at RDF.type, or 'a'
* adds a newline and an indent for all triples with more than one object (object list)
* adds a new line and ';' for the last triple in a set with '.'
    on the start of the next line
* uses default encoding (encode()) is used instead of "latin-1"

- Nicholas Car, 2023
    )Error)RDF)BNodeLiteralURIRef   )RecursiveSerializerLongTurtleSerializer   Fc                       s   e Zd ZdZdZ fddZ fddZ fddZd.d
dZ fddZ	d/ddZ
dd Zdd Zdd Zdd Zdd Zd0ddZd0ddZd d! Zd"d# Zd$d% Zd&d' Zd0d(d)Zd0d*d+Zd,d- Z  ZS )1r	   
longturtlez    c                    s:   i | _ tt| | tjdi| _|   d | _t	| _
d S )Na)_ns_rewritesuperr	   __init__r   typekeywordsresetstream_SPACIOUS_OUTPUT	_spacious)selfstore	__class__ `/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/rdflib/plugins/serializers/longturtle.pyr   '   s   
zLongTurtleSerializer.__init__c                    s   |dkr
|d dks| j |||kr6|| jvr/d| }|| j v r*d| }|| j v s!|| j|< | j||}tt| || |S )N r   _p)
namespacesgetr   r   r	   addNamespace)r   prefix	namespacer   r   r   r   r!   /   s   




z!LongTurtleSerializer.addNamespacec                    s$   t t|   i | _d| _i | _d S )NF)r   r	   r   _shortNames_startedr   r   r   r   r   r   G   s   
zLongTurtleSerializer.resetNc           	      K   s   |    || _|d ur|| _n| jjd ur| jj| _|d ur!|| _|   |  }|   d}|D ]}| |r9q1|r=d}| 	|rI|sI| 
d q1|   d | _d S )NTF
)r   r   baser   r   
preprocessorderSubjectsstartDocumentisDone	statementwriteendDocument)	r   r   r(   encodingspaciousargssubjects_list	firstTimesubjectr   r   r   	serializeM   s,   



zLongTurtleSerializer.serializec                    s   t t| | t|D ]#\}}|| jv rq| j||tkd t|tr/|j	r/| j|j	t
d q|d }t|trD| j|  d7  < d S d S )N)
gen_prefixr   )r   r	   preprocessTriple	enumerater   getQNameVERB
isinstancer   datatype_GEN_QNAME_FOR_DTr   _references)r   tripleinoder   r   r   r   r8   k   s   

z%LongTurtleSerializer.preprocessTripleTc                 C   s   t |tsd S z
| jj||d}W n ty.   | jj|}|d ur)||df}nY d S Y nw |\}}}|dr;d S | ||}d||f S )N)generater   .z%s:%s)r<   r   r   compute_qname	Exceptionr"   endswithr!   )r   urir7   partspfxr"   r#   localr   r   r   r:   x   s    



zLongTurtleSerializer.getQNamec                 C   sz   d| _ t| j }| jr| |  d| j   |D ]\}}| |  d||f   q|r9| jr;| d d S d S d S )NTz
BASE <%s>
zPREFIX %s: <%s>
r'   )r%   sortedr   itemsr(   r.   indentr   )r   ns_listr"   rH   r   r   r   r+      s   
z"LongTurtleSerializer.startDocumentc                 C   s   | j r
| d d S d S )Nr'   )r   r.   r&   r   r   r   r/      s   z LongTurtleSerializer.endDocumentc                 C   s   |  | | |p| |S N)subjectDone	s_squared	s_defaultr   r5   r   r   r   r-      s   
zLongTurtleSerializer.statementc                 C   sH   |  d|    | |t |  d|    | | |  d dS )Nr'   z
.T)r.   rN   pathSUBJECTpredicateListrT   r   r   r   rS      s   

zLongTurtleSerializer.s_defaultc                 C   sN   | j | dkst|tsdS | d|   d  | j|dd | d dS )Nr   Fr'   z[]newlinez ;
.T)r?   r<   r   r.   rN   rW   rT   r   r   r   rR      s   
zLongTurtleSerializer.s_squaredFc                 C   s0   |  ||s| |||std|f d S d S )NzCannot serialize node '%s')	p_squared	p_defaultr   r   rB   positionrY   r   r   r   rU      s   
zLongTurtleSerializer.pathc                 C   s,   |t kr|s| d | | || dS )N T)rV   r.   labelr\   r   r   r   r[      s   
zLongTurtleSerializer.p_defaultc                    sl   |t jkrdS |tu r| jv r j| S t|tr%|jd fdddS  |} ||tkp5|	 S )Nz()Tc                    s     | tS rP   )r:   r>   )dtr&   r   r   <lambda>   s    z,LongTurtleSerializer.label.<locals>.<lambda>)	use_plainqname_callback)
r   nilr;   r   r<   r   _literal_n3
relativizer:   n3r   rB   r]   r   r&   r   r_      s   




zLongTurtleSerializer.labelc                 C   s   t |tr|| jv s| j| dks|tkrdS | |rA|  jd7  _| d |  jd8  _| | | d| 	  d  d	S | 
| | d| 	d d  |  jd7  _| | |  jd8  _| d| 	d d  d	S )
Nr   Fr
   z (
r'   )z[
]T)r<   r   _serializedr?   rV   isValidListdepthr.   doListrN   rQ   rW   rh   r   r   r   rZ      s(   






zLongTurtleSerializer.p_squaredc                 C   sv   z| j |tjdu rW dS W n
 ty   Y dS w |r9|tjkr/tt| j |dkr/dS | j |tj	}|sdS )zW
        Checks if l is a valid RDF list, i.e. no nodes have other properties.
        NFr
   T)
r   valuer   firstrF   rd   lenlistpredicate_objectsrest)r   l_r   r   r   rl      s   "z LongTurtleSerializer.isValidListc                 C   s   d}|rD| j |tj}|d ur4|dkr| | d n
| d| d  | j|tdd | | | j |tj	}|d7 }|sd S d S )Nr   r   r'   TrX   )
r   ro   r   rp   r.   rN   rU   OBJECTrQ   rt   )r   ru   rA   itemr   r   r   rn      s   
zLongTurtleSerializer.doListc                 C   s   |  |}| |}t|dkrd S | | d | j|d dd | ||d   |dd  D ]}| d| d  | j|dd | ||  q2| d d S )Nr   r   TrX   z ;
z ;)buildPredicateHashsortPropertiesrq   r.   rN   verb
objectList)r   r5   rY   
propertiespropList	predicater   r   r   rW     s   

z"LongTurtleSerializer.predicateListc                 C   s   |  |t| d S rP   )rU   r;   )r   rB   rY   r   r   r   rz     s   zLongTurtleSerializer.verbc                 C   s   t |}|dkr
d S |dkrdpd}|  j|7  _d}|dkr2t|d ts0| d| d  d}| j|d t|d |dd  D ]}| d t|tsX| d| d  | j|tdd qB|  j|8  _d S )Nr   r   Fr'   TrX   z ,)rq   rm   r<   r   r.   rN   rU   rv   )r   objectscountdepthmodfirst_nlobjr   r   r   r{     s"   

zLongTurtleSerializer.objectList)NNN)T)F)__name__
__module____qualname__
short_nameindentStringr   r!   r   r6   r8   r:   r+   r/   r-   rS   rR   rU   r[   r_   rZ   rl   rn   rW   rz   r{   __classcell__r   r   r   r   r	   #   s.    





N)__doc__rdflib.exceptionsr   rdflib.namespacer   rdflib.termr   r   r   turtler   __all__rV   r;   rv   r>   r   r	   r   r   r   r   <module>   s    