
    i+                        d Z ddlm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 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e      Zy)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
    )annotations)IOAnyOptional)to_canonical_graph)Error)Graph)RDF)BNodeLiteralURIRef   )RecursiveSerializerLongTurtleSerializer   Fc                       e Zd ZdZdZdZ fdZ fdZd Z fdZ		 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ
 fd	Zdd
Zd Zd Zd Zd Zd ZddZddZd Zd Zd Zd ZddZddZd Z xZS )r   a:  LongTurtle, a Turtle serialization format.

    When the optional parameter ``canon`` is set to :py:obj:`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.
    
longturtlez    c                    i | _         d| _        t        t        |   |       t
        j                  di| _        | j                          d | _	        t        | _        y )NFa)_ns_rewrite_canonsuperr   __init__r
   typekeywordsresetstream_SPACIOUS_OUTPUT	_spacious)selfstore	__class__s     TC:\Projects\mas-dev\.venv\Lib\site-packages\rdflib/plugins/serializers/longturtle.pyr   zLongTurtleSerializer.__init__4   sH    "D2593

/    c                H   |dkD  r|d   dk(  s| j                   j                  ||      |k7  r`|| j                  vr6d|z   }|| j                   v rd|z   }|| j                   v r|| j                  |<   | j                  j                  ||      }t        t        |   ||       |S )N r   _p)
namespacesgetr   r   r   addNamespace)r    prefix	namespacer(   r"   s       r#   r+   z!LongTurtleSerializer.addNamespace=   s     RKF1I,1D1DI2
2 T---&L4??*aA 4??*+,  (%%))&&9F"D6vyIr$   c                p   | j                   sy| j                  j                  }t        | j                        }|j	                  d      }|j                  d      }|j                          t               }|j                  dj                  |      dd       |j                         }||_        || _        y)zApply canonicalization to the store.

        This normalizes blank node identifiers and allows for deterministic
        serialization of the graph.
        Nzapplication/n-triples)format
T)datar/   	skolemize)r   r!   namespace_managerr   	serializesplitsortr	   parsejoinde_skolemize)r    r3   r!   contentlinesgraphs         r#   canonizezLongTurtleSerializer.canonizeU   s     {{ JJ88"4::.//)@/Ad#

5!*AT 	 	
 ""$"3
r$   c                t    t         t        |           i | _        d| _        i | _        | j                          y )NF)r   r   r   _shortNames_startedr   r=   )r    r"   s    r#   r   zLongTurtleSerializer.resetk   s1    "D/1r$   c                    |j                  dd      | _        | j                          || _        ||| _        n1| j
                  j                  | j
                  j                  | _        ||| _        | j                          | j                         }| j                          d}|D ]>  }| j                  |      r|rd}| j                  |      s+|r.| j                  d       @ | j                          d | _        y )NcanonFTr0   )r*   r   r   r   baser!   r   
preprocessorderSubjectsstartDocumentisDone	statementwriteendDocument)	r    r   rC   encodingspaciouskwargssubjects_list	firstTimesubjects	            r#   r4   zLongTurtleSerializer.serializer   s     jj%0

DIZZ__(

DI%DN**,	$G{{7#!	~~g&y

4  % 		r$   c                |   t         t        |   |       t        |      D ]m  \  }}|| j                  v r| j                  ||t        k(         t        |t              s@|j                  sM| j                  |j                  t               o |d   }t        |t              r| j                  |xx   dz  cc<   y y )N)
gen_prefixr   )r   r   preprocessTriple	enumerater   getQNameVERB
isinstancer   datatype_GEN_QNAME_FOR_DTr   _references)r    tripleinoder(   r"   s        r#   rS   z%LongTurtleSerializer.preprocessTriple   s    "D:6B (GAtt}}$MM$AIM7$(T]]dmm8IJ ) 1IaQ1$  r$   c                >   t        |t              sy 	 | j                  j                  ||      }|\  }}}|j                  d      ry | j                  ||      }|d|S # t        $ r2 | j                  j                  j                  |      }|||df}nY y Y kw xY w)N)generater&   .:)rW   r   r!   compute_qname	Exceptionr,   endswithr+   )r    urirR   partspfxr,   r-   locals           r#   rU   zLongTurtleSerializer.getQName   s    #v&
	JJ,,S:,FE $) 	5 >>#""695 %((%  	**""))#.Cc2  	s   A! !6BBc           	     v   d| _         t        | j                  j                               }| j                  r/| j                  | j                         d| j                  z  z          |D ].  \  }}| j                  | j                         d|d|dz          0 |r| j                  r| j                  d       y y y )NTz
BASE <%s>
zPREFIX z: <z>
r0   )r@   sortedr)   itemsrC   rI   indentr   )r    ns_listr,   re   s       r#   rF   z"LongTurtleSerializer.startDocument   s    ..0199JJt{{}}tyy'@@A"KFCJJt{{}fc'JJK #t~~JJt &7r$   c                @    | j                   r| j                  d       y y )Nr0   )r   rI   )r    s    r#   rJ   z LongTurtleSerializer.endDocument   s    >>JJt r$   c                l    | j                  |       | j                  |      xs | j                  |      S N)subjectDone	s_squared	s_defaultr    rP   s     r#   rH   zLongTurtleSerializer.statement   s-    !~~g&A$..*AAr$   c                    | j                  d| j                         z          | j                  |t               | j                  d| j                         z          | j	                  |       | j                  d       y)Nr0   
.T)rI   rl   pathSUBJECTpredicateListrt   s     r#   rs   zLongTurtleSerializer.s_default   s[    

4$++-'(		'7#

4$++-'(7#

5r$   c                    | j                   |   dkD  st        |t              sy| j                  d| j	                         z   dz          | j                  |d       | j                  d       y)Nr   Fr0   z[]newlinerv   T)rZ   rW   r   rI   rl   ry   rt   s     r#   rr   zLongTurtleSerializer.s_squared   s_    W%)*We2L

4$++-'$./7E2

5r$   c                n    | j                  ||      s#| j                  |||      st        d|d      y y )NzCannot serialize node '')	p_squared	p_defaultr   r    r]   positionr|   s       r#   rw   zLongTurtleSerializer.path   s6    NN4*dnnT8W.U>?? /V*r$   c                ~    |t         k7  r|s| j                  d       | j                  | j                  ||             y)N T)rx   rI   labelr   s       r#   r   zLongTurtleSerializer.p_default   s1    wwJJsO

4::dH-.r$   c                :    |t         j                  k(  ry|t        u r| j                  v r j                  |   S t	        |t
              r|j                  d fd      S  j                  |      } j                  ||t        k(        xs |j                         S )Nz()Tc                0    j                  | t              S rp   )rU   rY   )dtr    s    r#   <lambda>z,LongTurtleSerializer.label.<locals>.<lambda>   s    $--<M*Nr$   )	use_plainqname_callback)
r
   nilrV   r   rW   r   _literal_n3
relativizerU   n3r    r]   r   s   `  r#   r   zLongTurtleSerializer.label   s    377?t 5==&&dG$##N $  
 ??4(D==x4'78EDGGIEr$   c                   t        |t              r)|| j                  v s| j                  |   dkD  s	|t        k(  ry| j                  |      rr| xj                  dz  c_        | j                  d       | xj                  dz  c_        | j                  |       | j                  d| j                         z   dz          y	| j                  |       | j                  d| j                  d      z   dz          | xj                  dz  c_        | j                  |       | xj                  dz  c_        | j                  d| j                  d      z   dz          y	)
Nr   Fr   z (
r0   )z[
]T)rW   r   _serializedrZ   rx   isValidListdepthrI   doListrl   rq   ry   r   s      r#   r   zLongTurtleSerializer.p_squared   s    4't'''%)7"D!JJ!OJJJvJJ!OJKKJJtdkkm+c12  T"JJtdkk!n,u45JJ!OJt$JJ!OJJJtdkk!n,s23r$   c                b   	 | j                   j                  |t        j                        y	 |rq|t        j
                  k7  r1t        t        | j                   j                  |                  dk7  ry| j                   j                  |t        j                        }|rqy# t        $ r Y yw xY w)zW
        Checks if l is a valid RDF list, i.e. no nodes have other properties.
        Fr   T)
r!   valuer
   firstrc   r   lenlistpredicate_objectsrest)r    l_s     r#   r   z LongTurtleSerializer.isValidList  s    	zzCII.6 7 SWW}T$***F*Fr*J%K!LPQ!Q!!"chh/B    		s   *B" "	B.-B.c                   d}|r| j                   j                  |t        j                        }|r|dk(  r!| j	                  | j                  d             n#| j	                  d| j                  d      z          | j                  |t        d       | j                  |       | j                   j                  |t        j                        }|dz  }|ry y )Nr   r   r0   Tr{   )
r!   r   r
   r   rI   rl   rw   OBJECTrq   r   )r    r   r\   items       r#   r   zLongTurtleSerializer.doList+  s    ::##B		2D6JJt{{1~.JJtdkk!n45		$	5  $!!"chh/BFA r$   c                   | j                  |      }| j                  |      }t        |      dk(  ry | j                  | j	                  d             | j                  |d   d       | j                  ||d             |dd  D ]L  }| j                  d| j	                  d      z          | j                  |d       | j                  ||          N | j                  d       y )Nr   r   Tr{   z ;
z ;)buildPredicateHashsortPropertiesr   rI   rl   verb
objectList)r    rP   r|   
propertiespropList	predicates         r#   ry   z"LongTurtleSerializer.predicateList9  s    ,,W5
&&z2x=A

4;;q>"		(1+t	,
8A;/0!!"IJJvA./IIiI.OOJy12 & 	

4r$   c                2    | j                  |t        |       y rp   )rw   rV   )r    r]   r|   s      r#   r   zLongTurtleSerializer.verbG  s    		$g&r$   c                8   t        |      }|dk(  ry |dk(  xr dxs d}| xj                  |z  c_        d}|dkD  rJt        |d   t              s$| j	                  d| j                  d      z          n| j	                  d       d}| j                  |d   t        |       |dd  D ]^  }| j	                  d       t        |t              s#| j	                  d| j                  d      z          | j                  |t        d       ` | xj                  |z  c_        y )	Nr   r   Fr0   r   Tr{   z ,)r   r   rW   r   rI   rl   rw   r   )r    objectscountdepthmodfirst_nlobjs         r#   r   zLongTurtleSerializer.objectListJ  s    GA:QJ%A*

h
19gaj%0

4$++a.01

3H		'!*fh	712;CJJtc5)

4$++a.01IIc64I0	 
 	

h
r$   )NNN)r   z	IO[bytes]rC   Optional[str]rK   r   rL   zOptional[bool]rM   r   returnNone)T)F)__name__
__module____qualname____doc__
short_nameindentStringr   r+   r=   r   r4   rS   rU   rF   rJ   rH   rs   rr   rw   r   r   r   r   r   ry   r   r   __classcell__)r"   s   @r#   r   r   )   s     JL00, #"&#'$$ $  	$
 !$ $ 
$L%)4	B@F>'r$   N)r   
__future__r   typingr   r   r   rdflib.comparer   rdflib.exceptionsr   rdflib.graphr	   rdflib.namespacer
   rdflib.termr   r   r   turtler   __all__rx   rV   r   rY   r   r    r$   r#   <module>r      s\   $ # $ $ - #    . . '!
"
	
  t. tr$   