o
    ÒmÆiW0  ã                   @  s®   d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZ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 )am  
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
é    )ÚannotationsN)ÚIOÚAnyÚOptional)Úto_canonical_graph)ÚError)ÚGraphÚ_TripleType)ÚRDF)ÚBNodeÚLiteralÚURIRefé   )ÚRecursiveSerializerÚLongTurtleSerializeré   Fc                      sô   e Zd ZdZdZdZ‡ fdd„Z‡ fdd„Zdd	„ Z‡ fd
d„Z				d@dAdd„Z
dB‡ fdd„ZdCdd „ZdCd!d"„Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ ZdDd.d/„ZdDd0d1„Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ ZdDd:d;„ZdDd<d=„Zd>d?„ Z‡  ZS )Er   a0  LongTurtle, a Turtle serialization format.

    When the optional parameter `canon` is set to `True`, the graph is canonicalized
    before serialization. This normalizes blank node identifiers and allows for
    deterministic serialization of the graph. Useful when consistent outputs are required.
    Z
longturtlez    c                   s@   i | _ d| _tt| ƒ |¡ tjdi| _|  ¡  d | _	t
| _d S )NFÚa)Ú_ns_rewriteÚ_canonÚsuperr   Ú__init__r
   ÚtypeÚkeywordsÚresetÚstreamÚ_SPACIOUS_OUTPUTÚ	_spacious)ÚselfÚstore©Ú	__class__© ú_/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/plugins/serializers/longturtle.pyr   5   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                 C  sn   | j sdS | jj}t| jƒ}|jdd}| d¡}| ¡  tƒ }|jd 	|¡ddd | 
¡ }||_|| _dS )zžApply canonicalization to the store.

        This normalizes blank node identifiers and allows for deterministic
        serialization of the graph.
        Nzapplication/n-triples)ÚformatÚ
T)Údatar+   Z	skolemize)r   r   Únamespace_managerr   Ú	serializeÚsplitÚsortr   ÚparseÚjoinZde_skolemize)r   r.   r   ÚcontentÚlinesÚgraphr!   r!   r"   ÚcanonizeV   s   

ÿ
zLongTurtleSerializer.canonizec                   s,   t t| ƒ ¡  i | _d| _i | _|  ¡  d S )NF)r   r   r   Z_shortNamesÚ_startedr   r7   ©r   r   r!   r"   r   l   s
   zLongTurtleSerializer.resetNr   ú	IO[bytes]ÚbaseúOptional[str]ÚencodingÚspaciousúOptional[bool]Úkwargsr   ÚreturnÚNonec           	      K  s´   |  dd¡| _|  ¡  || _|d ur|| _n| jjd ur!| jj| _|d ur(|| _|  ¡  |  ¡ }|  	¡  d}|D ]}|  
|¡r@q8|rDd}|  |¡rP|sP|  d¡ q8|  ¡  d | _d S )NZcanonFTr,   )r'   r   r   r   r;   r   r   Ú
preprocessZorderSubjectsÚstartDocumentZisDoneÚ	statementÚwriteÚendDocument)	r   r   r;   r=   r>   r@   Zsubjects_listZ	firstTimeÚsubjectr!   r!   r"   r/   s   s.   


€
zLongTurtleSerializer.serializeÚtripler	   c                   sÚ   t t| ƒ |¡ t|ƒD ]J\}}|tkr=|| jv rq| jd ur=t|tƒr=| 	| j¡r=d| 
| jd¡vr=d| 
| jd¡vr=q| j||tkd t|tƒrV|jrV| j|jtd q|d }t|tƒrk| j|  d7  < d S d S )Nú#r#   ú/)Ú
gen_prefixr   )r   r   ÚpreprocessTripleÚ	enumerateÚVERBr   r;   Ú
isinstancer   Ú
startswithÚreplaceÚ	get_pnamer   ÚdatatypeÚ_GEN_QNAME_FOR_DTr   Ú_references)r   rI   ÚiÚnoder%   r   r!   r"   rM   ™   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d¡ dd¡}| j d|¡}| 	d¡rLd S |  
||¡}d	||f S )
N)Úgenerater#   ú(z\(ú)z\)z\%Ú.z%s:%s)rP   r   r   Zcompute_qnameÚ	Exceptionr)   rR   Z2LOCALNAME_PECRENT_CHARACTER_REQUIRING_ESCAPE_REGEXÚsubÚendswithr(   )r   ÚurirL   ÚpartsÚpfxr)   r*   Úlocalr!   r!   r"   rS   ±   s(   
ýû

ÿ
zLongTurtleSerializer.get_pnamec                 C  s   t jdtdd |  ||¡S )NzXLongTurtleSerializer.getQName is deprecated, use LongTurtleSerializer.get_pname instead.r   )Ú
stacklevel)ÚwarningsÚwarnÚDeprecationWarningrS   )r   r`   rL   r!   r!   r"   ÚgetQNameÓ   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,   )r8   Úsortedr&   Úitemsr;   rF   Úindentr   )r   Zns_listr)   r`   r!   r!   r"   rD   Û   s   
ÿz"LongTurtleSerializer.startDocumentc                 C  s   | j r
|  d¡ d S d S )Nr,   )r   rF   r9   r!   r!   r"   rG   æ   s   ÿz LongTurtleSerializer.endDocumentc                 C  s   |   |¡ |  |¡p|  |¡S ©N)ÚsubjectDoneÚ	s_squaredÚ	s_default©r   rH   r!   r!   r"   rE   ê   s   
zLongTurtleSerializer.statementc                 C  sH   |   d|  ¡  ¡ |  |t¡ |   d|  ¡  ¡ |  |¡ |   d¡ dS )Nr,   ú
.T)rF   rk   ÚpathÚSUBJECTÚpredicateListrp   r!   r!   r"   ro   î   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[]©Únewlinerq   T)rV   rP   r   rF   rk   rt   rp   r!   r!   r"   rn   ö   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   rX   Úpositionrv   r!   r!   r"   rr   þ   s   
ÿÿýzLongTurtleSerializer.pathc                 C  s,   |t kr|s|  d¡ |  |  ||¡¡ dS )Nú T)rs   rF   Úlabelry   r!   r!   r"   rx     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 rl   )rS   rU   )Údtr9   r!   r"   Ú<lambda>  s    z,LongTurtleSerializer.label.<locals>.<lambda>)Z	use_plainZqname_callback)
r
   ÚnilrO   r   rP   r   Z_literal_n3Z
relativizerS   Zn3©r   rX   rz   r!   r9   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,   r[   z[
ú]T)rP   r   Z_serializedrV   rs   ÚisValidListÚdepthrF   ÚdoListrk   rm   rt   r€   r!   r!   r"   rw     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
   Úfirstr]   r   ÚlenÚlistZpredicate_objectsÚrest)r   Úl_r!   r!   r"   r‚   8  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,   Tru   )
r   r…   r
   r†   rF   rk   rr   ÚOBJECTrm   r‰   )r   rŠ   rW   Úitemr!   r!   r"   r„   G  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   Tru   z ;
z ;)ZbuildPredicateHashZsortPropertiesr‡   rF   rk   ÚverbÚ
objectList)r   rH   rv   Ú
propertiesZpropListÚ	predicater!   r!   r"   rt   U  s   

z"LongTurtleSerializer.predicateListc                 C  s   |   |t|¡ d S rl   )rr   rO   )r   rX   rv   r!   r!   r"   r   c  s   zLongTurtleSerializer.verbc                 C  sà   t |ƒ}|dkr
d S |dkrdpd}|  j|7  _d}|dkr8t|d tƒs1|  d|  d¡ ¡ n|  d¡ d}| j|d t|d |dd … D ]}|  d¡ t|tƒs^|  d|  d¡ ¡ | j|tdd qH|  j|8  _d S )	Nr   r   Fr,   r{   Tru   z ,)r‡   rƒ   rP   r   rF   rk   rr   r‹   )r   ÚobjectsÚcountZdepthmodZfirst_nlÚobjr!   r!   r"   rŽ   f  s$   


zLongTurtleSerializer.objectList)NNN)r   r:   r;   r<   r=   r<   r>   r?   r@   r   rA   rB   )rI   r	   rA   rB   )T)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z
short_nameZindentStringr   r(   r7   r   r/   rM   rS   rh   rD   rG   rE   ro   rn   rr   rx   r|   rw   r‚   r„   rt   r   rŽ   Ú__classcell__r!   r!   r   r"   r   *   s:    	
û&

"



)r—   Ú
__future__r   re   Útypingr   r   r   Zrdflib.comparer   Zrdflib.exceptionsr   Zrdflib.graphr   r	   Zrdflib.namespacer
   Zrdflib.termr   r   r   Úturtler   Ú__all__rs   rO   r‹   rU   r   r   r!   r!   r!   r"   Ú<module>   s"    