
    4Ysh1                     B   d Z ddl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mZ ddlmZmZ dd	lmZmZ dd
lmZ ddlmZmZmZmZmZmZmZ ddgZej<                  ej>                  ej@                  ejB                  hZ" G d de      Z#	 	 	 	 	 	 	 ddZ$ G d de%      Z&y)a  
This serialiser will output an RDF Graph as a JSON-LD formatted document. See:

    http://json-ld.org/

Example usage::

    >>> from rdflib import Graph
    >>> testrdf = '''
    ... @prefix dc: <http://purl.org/dc/terms/> .
    ... <http://example.org/about>
    ...     dc:title "Someone's Homepage"@en .
    ... '''

    >>> g = Graph().parse(data=testrdf, format='n3')

    >>> print(g.serialize(format='json-ld', indent=4))
    [
        {
            "@id": "http://example.org/about",
            "http://purl.org/dc/terms/title": [
                {
                    "@language": "en",
                    "@value": "Someone's Homepage"
                }
            ]
        }
    ]

    N)
Serializer)Graph)URIRefLiteralBNode)RDFXSD)IOOptional   )ContextUNDEF)json)CONTEXTGRAPHIDVOCABLISTSETLANGJsonLDSerializerfrom_rdfc                   P     e Zd Zdef fdZ	 	 ddee   dee   dee   fdZ	 xZ
S )r   storec                 ,    t         t        |   |       y N)superr   __init__)selfr   	__class__s     /var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/serializers/jsonld.pyr   zJsonLDSerializer.__init__9   s    .u5    streambaseencodingc                    |xs d}|dvrt        j                  d|        |j                  d      }|j                  dd      f}|j                  dd      }|j                  dd      }|j                  d	d
      }	|j                  dd      }
|j                  dd      }|j                  dd      }t        | j                  |||||      }t        j                  ||	|
||      }|j                  |j                  |d             y )Nutf-8)r'   zutf-16z7JSON should be encoded as unicode. Given encoding was: contextuse_native_typesFuse_rdf_typeauto_compactindentr   
separators),z: 	sort_keysTensure_ascii)r+   )r,   r-   r/   r0   replace)	warningswarngetr   r   r   dumpswriteencode)r   r#   r$   r%   kwargscontext_datar)   r*   r+   r,   r-   r/   r0   objdatas                  r!   	serializezJsonLDSerializer.serialize<   s
    &w..MMLXJW zz),"JJ'95ACzz.%8zz.%8Ha(ZZk:
JJ{D1	zz.%8JJ%
 zz!%
 	T[[956r"   NN)__name__
__module____qualname__r   r   r
   bytesr   strr<   __classcell__)r    s   @r!   r   r   8   sD    6e 6 #"&	)75	)7 sm)7 3-	)7r"   c                 v   |s"|r t        d | j                         D              }t        |t              r|}|j	                         }nt        ||      }t        |||      }	|	j                  |       }
|	j                  j                  r0t        |
t              r|j                  t              |
i}
||
t        <   |
S )Nc              3   \   K   | ]$  \  }}|rt        |      d k7  r|t        |      f & yw)z$http://www.w3.org/XML/1998/namespaceN)rB   ).0pfxnss      r!   	<genexpr>zfrom_rdf.<locals>.<genexpr>v   s3      
bs2w"HH #b'N
s   *,)r$   )dict
namespaces
isinstancer   to_dict	Converterconvertr(   activelistget_keyr   r   )graphr9   r$   r)   r*   r+   	startnodeindexr(   	converterresults              r!   r   r   h   s     L 
"--/
 
 ,((,T2'#3\BIu%Ffd#ooe,f5F&wMr"   c                   <    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
y	)
rN   c                 J    || _         |j                  xs || _        || _        y r   )r(   rP   r)   r*   )r   r(   r)   r*   s       r!   r   zConverter.__init__   s#     ' B2B(r"   c                    |j                   rTt               }|g}|j                         D ]3  }t        |j                  t
              r|j                  |       /||z  }5 n|g}| j                  }g }|D ]  }i }d }t        |j                  t
              r*|j                  |j                        }|||j                  <   | j                  |      }	|s#t        |	      dk(  r|j                  |	d          n|	s|	||j                  <   |r?|d   j                  |j                  t                     |k(  r|d   j                  |       |j                  |        t        |      dk(  rXt        |      dk(  rJ| j                  j"                  s4|d   }
|
j                  |j                        }t        |
      dk(  r|r|}|S t        |      dk(  r| j                  j"                  r|d   }|S )N   r   )context_awarer   contextsrL   
identifierr   appendr(   
shrink_iriid_key
from_graphlenupdate	graph_keyr4   rR   r   rP   )r   rS   default_graphgraphsgr(   objsr:   	graphnamenodesdefaultitemss               r!   rO   zConverter.convert   s    !GM#_F^^% 'allF3MM!$!Q&M	' WF,, 	!ACI!,,/#..q||<	&/GNN#OOA&EUq

58$).G%%&QGOOB$78IEQs#C )	!, v;!D	Qt||7J7J1gGKK 1 12E7|q U  Y!^ 3 37Dr"   c                    i }t        |j                               D ]R  }t        |t              s-t        |t              s$t        |j                  d |            r@| j                  |||       T t        |j                               S r   )	setsubjectsrL   r   r   anyprocess_subjectrQ   values)r   rS   nodemapss       r!   rb   zConverter.from_graph   sn    U^^%& 	8A!V$1e$Sa1H-I$$UAw7	8 GNN$%%r"   c           	      L   t        |t              r| j                  j                  |      }n#t        |t              r|j                         }nd }||v ry i }||| j                  j                  <   |||<   |j                  |      D ]  \  }}| j                  ||||||        |S r   )	rL   r   r(   r`   r   n3ra   predicate_objectsadd_to_node)r   rS   ru   rt   node_idnodepos           r!   rr   zConverter.process_subject   s    a ll--a0G5!ddfGG g$+T\\  !++A. 	<DAqUAq!T7;	< r"   c           	         | j                   }t        |t              rM|j                  rt	        |j                        nd }|j
                  }	|j                  t	        |      ||	      }
nl|j                  |t        j                        rt        d gnd g}|D ]<  }t        t        t        fD ]"  }|j                  t	        |      ||      }
|
s" n 
s< n d }|j                   }
r|
j                  }|
j                   r| j#                  ||
j                         }n`|
j
                  r%|j
                  |
j
                  k(  rt	        |      }n/|j
                  r#|
j
                  |j
                  t	        |      }t        |
j$                  v rQ| j'                  ||      D cg c]4  }| j#                  ||
j                         xs | j)                  ||||      6 }}n1t*        |
j$                  v rt	rr|j-                  |i       }|j/                  |	      }t	        |      }|st0        |
j$                  v r*t        |t2              s|gx||	<   }|j5                  |       y |||	<   y t0        |
j$                  v rd}n|j7                  |      }|j8                  j/                  |      }|r|j                   s|j$                  r|}|
sL|t        j                   k(  r9| j:                  s-t        |t<              r|j7                  |      }|j>                  }|| j)                  ||||      }|j/                  |      }|r%t        |t2              s|g}|j5                  |       n|r|g}n|}|||<   y c c}w )N)languageT) r(   rL   r   datatyperB   r   	find_termvaluer   firstr   r   r   r   rP   nametypetype_coerce	containerto_collectionto_raw_valuer   
setdefaultr4   r   rQ   r_   	to_symboltermsr*   r   type_key)r   rS   ru   r|   r}   s_nodert   r(   r   r   term
containersr   coercionr{   use_setp_keyvr   rs   key_terms                        r!   ry   zConverter.add_to_node   s   ,,a!*+**s1::$HzzH$$SVX$ID).Q		)B$J' 	!#UE 2 H",,SVXyID  nn$IIEyy''49951::#>1v!!t}}'<AS1vt~~% "//q9  $$Q		2 ?((1g>? 
 'H))%48,1vSDNN2%fd34:8;h&MM$'  '+E(O& %%a(E}}((/HX]]h.@.@AM$2C2Ca(",,Q/D((<$$UAq':D

5!eT*LLFEEuUs   #9M-c                 l   |t         k(  rMt        |t              r| j                  j	                  |      S t        |t
              r|j                         S |S |t        k(  r+t        |t              r| j                  j                  |      S t        |t              rt        |j                        |k(  r|S y r   )r   rL   r   r(   r`   r   rw   r   r   r   rB   r   )r   r}   coerce_types      r!   r   zConverter.type_coerce0  s    "!V$||..q11Au%ttvE!jF&;<<))!,,7#AJJ;(FHr"   c           	         | j                   }| j                  ||      }|?| j                  ||      D cg c]  }| j                  |||       }}|j                  |iS t	        |t
              rmd}| j                  |||      }	|	r:|r&t        fd|j                  d |      D              s|	S |	||	|j                     <   |j                  |j                         iS t	        |t              r|j                  |j                  |      iS t	        |t              r"| j                  xr |j                  t         v }
|
r|j#                         }nt%        |      }|j                  r[|
r&| j                   j&                  r|S |j(                  |iS |j*                  |j-                  |j                        |j(                  |iS |j.                  r=|j.                  |j.                  k7  r$|j0                  |j.                  |j(                  |iS |j&                  r|j.                  r|j.                  s|j(                  |iS |S y c c}w )NFc              3   .   K   | ]  }|k7  s	|  y wr    )rF   s2ru   s     r!   rI   z)Converter.to_raw_value.<locals>.<genexpr>N  s     $UBRSTWR$Us   
)r(   r   r   list_keyrL   r   rr   rq   rp   ra   rw   r   r`   r   r)   r   PLAIN_LITERAL_TYPEStoPythonrB   rP   	value_keyr   r   r   lang_key)r   rS   ru   r}   rt   r(   collloembedonodenativer   s     `         r!   r   zConverter.to_raw_value?  s   ,,!!%+ ,,UA6 !!%B8D  $$d++5!  ((7;E$U%..q2I$U!U L5:GE'..12NNADDF++6"NNG$6$6q$9::7#**Pqzz=P/PFJJLFzz||**  ' 1 1155$$g&7&7

&C%%q  

g.>.> >((!**g6G6GKK^^w'7'7

))1--- $'s   H?c                    |t         j                  k7  r!|j                  |t         j                        sy g }t	        |g      }|r|t         j                  k(  r|S t        |t              ry d\  }}|j                  |      D ]]  \  }}|s|t         j                  k(  r|}|s|t         j                  k(  r|}6|t         j                  k7  s|t         j                  k7  s] y  |j                  |       |}||v ry |j                  |       |ry y )Nr=   )r   nilr   r   ro   rL   r   rx   restr   Listr_   add)	r   rS   l_
list_nodeschainr   r   r|   r}   s	            r!   r   zConverter.to_collectionn  s    =R!;
RD	SWW}!!"f%$KE4//3  1ciiE!sxx-D#((]a388m  e$BU{IIbM# r"   N)r>   r?   r@   r   rO   rb   rr   ry   r   r   r   r   r"   r!   rN   rN      s.    )
/b
&*IV-^r"   rN   )NNFFFNF)'__doc__r2   rdflib.serializerr   rdflib.graphr   rdflib.termr   r   r   rdflib.namespacer   r	   typingr
   r   shared.jsonld.contextr   r   shared.jsonld.utilr   shared.jsonld.keysr   r   r   r   r   r   r   __all__booleanintegerdoublestringr   r   r   objectrN   r   r"   r!   <module>r      s   H  (  . . %  2 % K K Kz
* {{CKKSZZH -7z -7d 	
"Jw wr"   