
    4Yshz%                     p    d 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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, 2021
    )BNodeLiteralURIRef)Error   )RecursiveSerializer)RDFLongTurtleSerializer   Fc                        e Zd ZdZdZ fdZ fdZ fdZ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dZd Zd ZddZddZd Z xZS )r
   
longturtlez    c                     i | _         t        t        |   |       t        j
                  di| _        | j                          d | _        t        | _
        y )Na)_ns_rewritesuperr
   __init__r	   typekeywordsresetstream_SPACIOUS_OUTPUT	_spacious)selfstore	__class__s     /var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/serializers/longturtle.pyr   zLongTurtleSerializer.__init__'   sA    "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                 T    t         t        |           i | _        d| _        i | _        y )NF)r   r
   r   _shortNames_startedr   )r   r   s    r   r   zLongTurtleSerializer.resetH   s(    "D/1r   c                    | j                          || _        ||| _        n1| j                  j                  | j                  j                  | _        ||| _        | j                          | j                         }| j                          d}|D ]>  }| j                  |      r|rd}| j                  |      s+|r.| j                  d       @ | j                          | j                  d       d | _        y )NTF
)r   r   baser   r   
preprocessorderSubjectsstartDocumentisDone	statementwriteendDocument)	r   r   r,   encodingspaciousargssubjects_list	firstTimesubjects	            r   	serializezLongTurtleSerializer.serializeN   s    

DIZZ__(

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

4 	! 	

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   r=   z%LongTurtleSerializer.preprocessTriplem   s    "D:6B ( 	KGAtt}}$MM$AIM7$(T]]dmm8IJ	K 1IaQ1$  r   c                 .   t        |t              sy 	 | j                  j                  ||      }|\  }}}|j                  d      ry | j                  ||      }|d|S #  | j                  j                  j	                  |      }|||df}nY y Y dxY w)N)generater   .:)rA   r   r   compute_qnamer%   endswithr$   )r   urir<   partspfxr%   r&   locals           r   r?   zLongTurtleSerializer.getQNamez   s    #v&
	JJ,,S:,FE $) 	5 >>#""695 %((%	**""))#.Cc2  s   A! !/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>
r+   )r)   sortedr"   itemsr,   r2   indentr   )r   ns_listr%   rN   s       r   r/   z"LongTurtleSerializer.startDocument   s    ..0199JJt{{}}tyy'@@A" 	LKFCJJt{{}fc'JJK	Lt~~JJt &7r   c                 @    | j                   r| j                  d       y y )Nr+   )r   r2   )r   s    r   r3   z LongTurtleSerializer.endDocument   s    >>JJt r   c                 l    | j                  |       | j                  |      xs | j                  |      S N)subjectDone	s_squared	s_defaultr   r9   s     r   r1   zLongTurtleSerializer.statement   s-    !~~g&A$..*AAr   c                     | j                  d| j                         z          | j                  |t               | j                  d| j                         z          | j	                  |       | j                  d       y)Nr+    ;
.T)r2   rU   pathSUBJECTpredicateListr]   s     r   r\   zLongTurtleSerializer.s_default   s[    

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

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

7r   c                     | j                   |   dkD  st        |t              sy| j                  d| j	                         z   dz          | j                  |       | j                  d       y)Nr   Fr+   z[]r_   T)rD   rA   r   r2   rU   rb   r]   s     r   r[   zLongTurtleSerializer.s_squared   sZ    W%)*We2L

4$++-'$./7#

7r   c                 p    | j                  |||      s#| j                  |||      st        d|d      y y )NzCannot serialize node '')	p_squared	p_defaultr   r   rG   positionnewlines       r   r`   zLongTurtleSerializer.path   s:    NN473~~dHg6>?? 7 4r   c                 ~    |t         k7  r|s| j                  d       | j                  | j                  ||             y)N T)ra   r2   labelrh   s       r   rg   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 rY   )r?   rC   )dtr   s    r   <lambda>z,LongTurtleSerializer.label.<locals>.<lambda>   s    $--<M*N r   )	use_plainqname_callback)
r	   nilr@   r   rA   r   _literal_n3
relativizer?   n3)r   rG   ri   s   `  r   rm   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|s| j                  d       | j                  |      r| xj                  dz  c_        | j                  d       | xj                  dz  c_        | j                  |       | xj                  dz  c_        | j                  d| j                  d      z   dz          y| j                  |       | xj                  dz  c_        | j                  d       | xj                  dz  c_        | j                  |d	       | xj                  dz  c_        | j                  d| j                  d      z   d
z          y)Nr   Frl   r   z(
r+   )z[
rj   ]T)rA   r   _serializedrD   ra   r2   isValidListdepthdoListrU   rZ   rb   rh   s       r   rf   zLongTurtleSerializer.p_squared   s-   4't'''%)7"JJsOD!JJ!OJJJuJJ!OJKKJJ!OJJJtdkk!n,s23  T"JJ!OJJJuJJ!OJtU3JJ!OJJJtdkk!n,s23r   c                 R   	 | j                   j                  |t        j                        y	 |rq|t        j                  k7  r1t        t        | j                   j                  |                  dk7  ry| j                   j                  |t        j                        }|rqy#  Y yxY w)zW
        Checks if l is a valid RDF list, i.e. no nodes have other properties.
        Fr   T)	r   valuer	   firstrt   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&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   r+   Trz   )
r   r   r	   r   r2   rU   r`   OBJECTrZ   r   )r   r   rF   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 y )Nr   r   Trz   z ;
)buildPredicateHashsortPropertiesr   r2   rU   verb
objectList)r   r9   rj   
propertiespropList	predicates         r   rb   z"LongTurtleSerializer.predicateList  s    ,,W5
&&z2x=A

4;;q>"		(1+t	,
8A;/0!!" 	3IJJvA./IIiI.OOJy12	3r   c                 2    | j                  |t        |       y rY   )r`   r@   )r   rG   rj   s      r   r   zLongTurtleSerializer.verb  s    		$g&r   c                    t        |      }|dk(  ry |dk(  xr dxs d}| xj                  |z  c_        d}|dkD  r%| j                  d| j                  d      z          d}| j	                  |d   t
        |       |dd  D ]K  }| j                  d       | j                  | j                  d             | j	                  |t
        d       M | xj                  |z  c_        y )Nr   r   Fr+   Trz   z ,
)r   r~   r2   rU   r`   r   )r   objectscountdepthmodfirst_nlobjs         r   r   zLongTurtleSerializer.objectList  s    GA:QJ%A*

h
19JJtdkk!n,-H		'!*fh	712; 	1CJJvJJt{{1~&IIc64I0	1 	

h
r   )NNN)T)F)__name__
__module____qualname__
short_nameindentStringr   r$   r   r:   r=   r?   r/   r3   r1   r\   r[   r`   rg   rm   rf   r}   r   rb   r   r   __classcell__)r   s   @r   r
   r
   "   st    JL*2>%)4	B@F>3'r   N)__doc__rdflib.termr   r   r   rdflib.exceptionsr   turtler   rdflib.namespacer	   __all__ra   r@   r   rC   r   r
    r   r   <module>r      sM   $ / . # '  !
"
	
  L. Lr   