
    4Ysh5                         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dlmZ ddlmZmZ dd	gZd
 Z G d de      ZdZdZdZdZdZ G d d	e      Zy)zr
Turtle RDF graph serializer for RDFLib.
See <http://www.w3.org/TeamSubmission/turtle/> for syntax specification.
    )defaultdict)
cmp_to_key)BNodeLiteralURIRef)Error)
Serializer)RDFRDFSRecursiveSerializerTurtleSerializerc                 ~    	 | |kD  ry| |k  ryy# t         $ r$ t        |       } t        |      }| |kD  | |k  z
  cY S w xY w)a  
    for nice clean output we sort the objects of triples,
    some of them are literals,
    these are sorted according to the sort order of the underlying python objects
    in py3 not all things are comparable.
    This falls back on comparing string representations when not.
       r   )	TypeErrorstr)abs     /var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/serializers/turtle.py_object_comparatorr      sR    
!q5q5 !FFA!a%  !s     *<<c                        e Zd Zej                  gZej                  ej                  gZ	dZ
dZdZ fdZd Zd Zd Zd Zd	 Zd
 Zd Zd Zd Zd ZddZd Z xZS )r   
   z   c                 Z    t         t        |   |       d | _        | j	                          y N)superr   __init__streamresetselfstore	__class__s     r   r   zRecursiveSerializer.__init__/   s#    !41%8

    c           	          || j                   v r3| j                   |   |k7  r!t        d|d|d| j                   |         || j                   |<   y )Nz$Trying to override namespace prefix z => z, but it's already bound to )
namespaces	Exception)r!   prefixuris      r   addNamespacez RecursiveSerializer.addNamespace5   sP    T__$)@C)G3 79  #&r$   c                     | j                  |      sT|| j                  vsF|| j                  v r| j                  dkD  s)t	        |t
              r| j                  | j                  k\  ryy)z4Check to see if the subject should be serialized yetr   FT)isDone	_subjects
_topLevelsdepth
isinstancer   maxDepthr!   subjects     r   checkSubjectz RecursiveSerializer.checkSubject=   sQ     [[!t~~-DOO+$**q.7F+t}}1Lr$   c                     || j                   v S )z$Return true if subject is serialized_serializedr2   s     r   r,   zRecursiveSerializer.isDoneH   s    $****r$   c           
      
   i }g }| j                   D ]q  }t        | j                  j                  t        j
                  |            }|j                          |j                  |       |D ]  }d| j                  |<   d||<    s | j                  D cg c]&  }||vr t        |t              | j                  |   |f( }}|j                          |j                  |D 	cg c]  \  }}	}|
 c}}	}       |S c c}w c c}}	}w )NT)
topClasseslistr"   subjectsr
   typesortextendr.   r-   r0   r   _references)
r!   seenr;   classURImembersmemberr3   
recursableisbnoderefss
             r   orderSubjectsz!RecursiveSerializer.orderSubjectsL   s     	$H4::..sxxBCGLLNOOG$! $*.'#V$	$  >>
d" ')9)9')BGL

 
 	LL%=gtWLM
 Ms   +C9!C>c                 f    | j                   j                  d      D ]  }| j                  |        y )NNNN)r"   triplespreprocessTriple)r!   triples     r   
preprocesszRecursiveSerializer.preprocessd   s/    jj(();< 	*F!!&)	*r$   c                 \    |\  }}}| j                   |xx   dz  cc<   d| j                  |<   y )Nr   T)r?   r-   )r!   spospos        r   rK   z$RecursiveSerializer.preprocessTripleh   s1    1aq  qr$   c                    d| _         i | _        i | _        t        t              | _        i | _        i | _        i | _        | j                  rt        | j                  d      rD| j                  j                         D ]&  \  }}|| j                  v s| j                  ||       ( y | j                  j                         D ]  \  }}| j                  ||        y y )Nr   __iter__)r/   listsr&   r   intr?   r7   r-   r.   roundtrip_prefixeshasattrr"   r*   )r!   r(   nss      r   r   zRecursiveSerializer.resetm   s    

&s+""t..
;"&**"7"7"9 6JFB!8!88))&"56 #'**"7"7"9 2JFB%%fb12 #r$   c                     i }| j                   j                  |ddf      D ].  \  }}}|j                  |g       }|j                  |       |||<   0 |S )zb
        Build a hash key by predicate to a list of objects for the given
        subject
        N)r"   rJ   getappend)r!   r3   
propertiesrP   rQ   rR   oLists          r   buildPredicateHashz&RecursiveSerializer.buildPredicateHash   s`    
 
zz))7D$*?@ 	"GAq!NN1b)ELLO!JqM	" r$   c                 t   |j                         D ]$  \  }}|j                  t        t                     & g }i }| j                  D ]"  }||v s||vs|j                  |       d||<   $ t        |j                               }|j                          |D ]  }||vs|j                  |       d||<    |S )zzTake a hash from predicate uris to lists of values.
        Sort the lists of values.  Return a sorted list of properties.)keyT)itemsr=   r   r   predicateOrderr\   r:   keys)r!   r]   propobjectspropListr@   propss          r   sortPropertiesz"RecursiveSerializer.sortProperties   s     (--/ 	=MD'LLZ(:;L<	= '' 	"D
"T)9%!T
	" Z__&'

 	"D4%!T
	" r$   c                 "    d| j                   |<   y)zMark a subject as done.TNr6   r2   s     r   subjectDonezRecursiveSerializer.subjectDone   s    $(!r$   c                 :    | j                   |z   | j                  z  S )z-Returns indent string multiplied by the depth)r/   indentString)r!   modifiers     r   indentzRecursiveSerializer.indent   s    

X%):):::r$   c                 n    | j                   j                  |j                  | j                  d             y)zWrite text in given encoding.replaceN)r   writeencodeencoding)r!   texts     r   rr   zRecursiveSerializer.write   s#    $++dmmY?@r$   )r   )__name__
__module____qualname__r   Classr9   r
   r<   labelrc   r1   rm   rW   r   r*   r4   r,   rG   rM   rK   r   r_   ri   rk   ro   rr   __classcell__r#   s   @r   r   r   '   sr    **Jhh

+NHL&	+0*!
2$
,);Ar$   r      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   turtlez    c                     i | _         t        t        |   |       t        j
                  di| _        | j                          d | _        t        | _
        y )Nr   )_ns_rewriter   r   r   r
   r<   keywordsr   r   _SPACIOUS_OUTPUT	_spaciousr    s     r   r   zTurtleSerializer.__init__   sA    .u53

)r$   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   _rQ   )r&   r[   r   r   r   r*   )r!   r(   	namespacerQ   r#   s       r   r*   zTurtleSerializer.addNamespace   s     RKF1I,1D1DI2
2 T---&L4??*aA 4??*+,  (%%))&&9F269Er$   c                 T    t         t        |           i | _        d| _        i | _        y )NF)r   r   r   _shortNames_startedr   )r!   r#   s    r   r   zTurtleSerializer.reset   s(    +-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j                  d             d | _        y )NTF
zlatin-1)r   r   baser"   r   rM   rG   startDocumentr,   	statementrr   endDocumentrs   )	r!   r   r   rt   spaciousargssubjects_list	firstTimer3   s	            r   	serializezTurtleSerializer.serialize   s    

DIZZ__(

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

4 	! 	T[[+,	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   rK   	enumerater   getQNameVERBr0   r   datatype_GEN_QNAME_FOR_DTr   r?   )r!   rL   inoderQ   r#   s        r   rK   z!TurtleSerializer.preprocessTriple  s    6v> ( 	KGAtt}}$MM$AIM7$(T]]dmm8IJ	K 1IaQ1$  r$   c                 2   t        |t              sy d }	 | j                  j                  ||      }|\  }}}|j                  d      ry | j                  ||      }|d|S #  | j                  j                  j	                  |      }|||df}nY y Y dxY w)N)generater   .:)r0   r   r"   compute_qnamer(   endswithr*   )r!   r)   r   partspfxr(   r   locals           r   r   zTurtleSerializer.getQName  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&   rb   r   rr   ro   r   )r!   ns_listr(   r)   s       r   r   zTurtleSerializer.startDocument+  s    ..0199JJt{{}'7$))'CCD" 	OKFCJJt{{}'MMN	Ot~~JJt &7r$   c                 @    | j                   r| j                  d       y y )Nr   )r   rr   )r!   s    r   r   zTurtleSerializer.endDocument6  s    >>JJt r$   c                 l    | j                  |       | j                  |      xs | j                  |      S r   )rk   	s_squared	s_defaultr2   s     r   r   zTurtleSerializer.statement:  s-    !~~g&A$..*AAr$   c                     | j                  d| j                         z          | j                  |t               | j	                  |       | j                  d       y)Nr    .T)rr   ro   pathSUBJECTpredicateListr2   s     r   r   zTurtleSerializer.s_default>  sD    

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

4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)r?   r0   r   rr   ro   r   r2   s     r   r   zTurtleSerializer.s_squaredE  sZ    W%)*We2L

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

4r$   c                 p    | j                  |||      s#| j                  |||      st        d|d      y y )NzCannot serialize node '')	p_squared	p_defaultr   r!   r   positionnewlines       r   r   zTurtleSerializer.pathM  s:    NN473~~dHg6>?? 7 4r$   c                 ~    |t         k7  r|s| j                  d       | j                  | j                  ||             y)N T)r   rr   rz   r   s       r   r   zTurtleSerializer.p_defaultT  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 r   )r   r   )dtr!   s    r   <lambda>z(TurtleSerializer.label.<locals>.<lambda>b  s    $--<M*N r$   )	use_plainqname_callback)
r
   nilr   r   r0   r   _literal_n3
relativizer   n3)r!   r   r   s   `  r   rz   zTurtleSerializer.labelZ  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^| j                  d       | xj                  dz  c_        | j                  |       | xj                  dz  c_        | j                  d       y
| j                  |       | xj                  dz  c_        | j                  d       | xj                  dz  c_        | j                  |d       | j                  d	       | xj                  dz  c_        y
)Nr   Fr   (z )r}   [r   z ]T)r0   r   r7   r?   r   rr   isValidListr/   doListrk   r   r   s       r   r   zTurtleSerializer.p_squaredi  s    4't'''%)7"JJsOD!JJsOJJ!OJKKJJ!OJJJt  T"JJ!OJJJsOJJ!OJtU3JJtJJ!OJ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
   firstr   lenr:   predicate_objectsrest)r!   l_s     r   r   zTurtleSerializer.isValidList  s    	zzCII.6 7 SWW}T$***F*Fr*J%K!LPQ!Q!!"chh/B  	s   *B" "B&c                 
   |r| j                   j                  |t        j                        }|'| j	                  |t
               | j                  |       | j                   j                  |t        j                        }|ry y r   )r"   r   r
   r   r   OBJECTrk   r   )r!   r   items      r   r   zTurtleSerializer.doList  s`    ::##B		2D		$'  $!!"chh/B r$   c                 h   | j                  |      }| j                  |      }t        |      dk(  ry | j                  |d   |       | j	                  ||d             |dd  D ]L  }| j                  d| j                  d      z          | j                  |d       | j	                  ||          N y )Nr   r   r   z ;
T)r_   ri   r   verb
objectListrr   ro   )r!   r3   r   r]   rg   	predicates         r   r   zTurtleSerializer.predicateList  s    ,,W5
&&z2x=A		(1+w	/
8A;/0!!" 	3IJJvA./IIiI.OOJy12	3r$   c                 2    | j                  |t        |       y r   )r   r   )r!   r   r   s      r   r   zTurtleSerializer.verb  s    		$g&r$   c                 P   t        |      }|dk(  ry |dk(  xr dxs d}| xj                  |z  c_        | j                  |d   t               |dd  D ]=  }| j	                  d| j                  d      z          | j                  |t        d       ? | xj                  |z  c_        y )Nr   r   z,
Tr   )r   r/   r   r   rr   ro   )r!   rf   countdepthmodobjs        r   r   zTurtleSerializer.objectList  s    GA:QJ%A*

h
		'!*f%12; 	1CJJut{{1~-.IIc64I0	1 	

h
r$   rI   )T)F)rv   rw   rx   
short_namerm   r   r*   r   r   rK   r   r   r   r   r   r   r   r   rz   r   r   r   r   r   r   r{   r|   s   @r   r   r      su    JL*2>%):	B@FB0
3'
r$   N)__doc__collectionsr   	functoolsr   rdflib.termr   r   r   rdflib.exceptionsr   rdflib.serializerr	   rdflib.namespacer
   r   __all__r   r   r   r   r   r   r   r   r   r$   r   <module>r      sn   
 $   . . # ( & "4
5!,DA* DAN 	
  D* Dr$   