
    4YshD                        d Z ddlZddlmZ ddlmZ ddlZddlmZm	Z	 ddl
mZmZmZ ddlmZmZmZ dd	lmZmZmZmZ dd
lmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& ddgZ'	 ddl(m)Z) de)vrde)d<    e e+ejX                        e$e&      Z-dZ. G d dej^                  j`                        Z1	 	 	 	 	 ddZ2 G d de3      Z0y# e*$ r Y Ww xY w)aQ  
This parser will interpret a JSON-LD document as an RDF Graph. See:

    http://json-ld.org/

Example usage::

    >>> from rdflib import Graph, URIRef, Literal
    >>> test_json = '''
    ... {
    ...     "@context": {
    ...         "dc": "http://purl.org/dc/terms/",
    ...         "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    ...         "rdfs": "http://www.w3.org/2000/01/rdf-schema#"
    ...     },
    ...     "@id": "http://example.org/about",
    ...     "dc:title": {
    ...         "@language": "en",
    ...         "@value": "Someone's Homepage"
    ...     }
    ... }
    ... '''
    >>> g = Graph().parse(data=test_json, format='json-ld')
    >>> list(g) == [(URIRef('http://example.org/about'),
    ...     URIRef('http://purl.org/dc/terms/title'),
    ...     Literal("Someone's Homepage", lang='en'))]
    True

    N)ConjunctiveGraph)URLInputSource)RDFXSD)URIRefBNodeLiteral   )ContextTermUNDEF)jsonsource_to_jsonVOCAB_DELIMScontext_from_urlinputsource)CONTEXTGRAPHIDINCLUDEDINDEXJSONLANGLISTNESTNONEREVSETTYPEVALUEVOCABJsonLDParserto_rdf)SUFFIX_FORMAT_MAPjsonldzapplication/ld+jsonTc                   $     e Zd Z fdZd Z xZS )r!   c                 *    t         t        |           y N)superr!   __init__)self	__class__s    }/var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/parsers/jsonld.pyr)   zJsonLDParser.__init__X   s    lD*,    c                 f   |j                  d      xs d}|dvrt        j                  d|z         |j                  d      xs5 |j                  |j	                         xs |j                         xs d      }|j                  d      }|st        |t              rt        |      }	 t        |j                  dd	            }|j                  d
d      }t        |      }	|j                  s"t        |j                  |j                        }
n|}
t!        |	|
||||       y # t        $ r d }Y kw xY w)Nencodingutf-8)r0   zutf-16z9JSON should be encoded as unicode. Given encoding was: %sbase contextversionz1.0generalized_rdfF)store
identifier)getwarningswarn
absolutizegetPublicIdgetSystemId
isinstancer   r   float
ValueErrorr   context_awarer   r6   r7   r"   )r*   sourcesinkkwargsr/   r1   context_datar4   r5   data	conj_sinks              r,   parsezJsonLDParser.parse[   s   ::j)4W..MM)+34
 zz&! 
T__ >F$6$6$8>B&
 zz),
6> B6v>L	FJJy%89G !**%6>f%
 !!(tzzdooVIItYlG_M  	G	s   )D" "D0/D0)__name__
__module____qualname__r)   rH   __classcell__)r+   s   @r,   r!   r!   W   s    -"Nr-   c                     t        ||      }|r|j                  |       t        ||      }|j                  | ||      S )N)r1   r4   )r5   allow_lists_of_lists)r   loadParserrH   )	rF   datasetr1   rE   r4   r5   rN   r3   parsers	            r,   r"   r"      sD     41G\"'>RF <<gw//r-   c                   r    e Zd ZddZd ZddZd Z	 ddZd Ze	d        Z
dd	Zd
 Zd Zd Ze	d        Zy)rP   Nc                 <    || _         ||| _        y t        | _        y r'   )r5   ALLOW_LISTS_OF_LISTSrN   )r*   r5   rN   s      r,   r)   zParser.__init__   s,    . $/ ! 	! & 	!r-   c                 v   d}t        |t              r|}nZt        |t              rJ|j                  t              }|r|j                  ||j                         d}|}t        |t              s|g}|j                  r|j                  d |j                         |j                  j                         D ]N  \  }}|j                  s|j                  j                  t              s3|j                  ||j                         P |j                  r|j                  n|}	D ]  }
| j!                  ||	||
|        |	S )NFT)r>   listdictr8   r   rO   r1   vocabbindtermsitemsidendswithr   rA   default_context_add_to_graph)r*   rF   r3   rQ   
topcontext	resourceslocal_contextnametermgraphnodes              r,   rH   zParser.parse   s   
dD!Id# HHW-M]GLL9!
Ii.&K	==LLw}}-!----/ 	,JD$ww477++L9T477+	, ,3+@+@''g 	JDwwjI	J r-   c                    t        |t              r|j                  |      ry t        |v r5|s3|t           }|r|j	                  |      }nt        |j                        }|j                  |      }|j                  |      }|| j                  ||      }t        |t              r| j                  ||      }n
t               }|y |d u }	|j                         D ]  \  }
}|
t        k(  s|
|j                  t              v r'|
t         k(  s|
|j                  t               v r2|j                         D ]  \  }}| j#                  ||||||d|	         y| j#                  |||||
||	        |S )N)r1   T)reverseno_id)rj   )r>   rX   	get_valuer   
subcontextr   doc_baseget_context_for_typeget_id_get_nested_idstr
_to_rdf_idr   r\   get_keysr   r   _key_to_graph)r*   rQ   rf   r3   rg   ra   rc   id_valsubjrj   keyobjrkeyrobjs                 r,   r`   zParser._add_to_graph   su   $%):):4)@d?: MM!,,];!w'7'78..t4%>(($7Ffc"??7F3D7D< $

 	YHCg~(8(8(<!<czSG$4$4S$99"%))+ 
JD$&& $# ' 	
 ""7E7D#sRW"X#	Y& r-   c                    |j                         D ]  \  }}|j                  dk\  s||j                  t              v s.|j                  j                  |      }|r|j                  Xt        |t              r|n|g}|D ]x  }t        |t              s|j                  |      }|s<|j                  |j                  j                  |            }| j                  ||      }t        |t              st|c c S   y Ng?)r\   r4   rs   r   r[   r8   r]   r>   rW   rX   ro   get_context_for_termrp   rq   )	r*   r3   rg   rw   rx   re   objsru   rl   s	            r,   rp   zParser._get_nested_id   s    

 	&HC#%#1A1A$1G*G}}((-DGGO(d3s# 
&C%c40 $^^C0F!%,%A%A#MM--c2&
 "&!4!4Z!E!&#.%
&	&r-   c	           
      |   t        |t              r|}	n|g}	|j                  j                  |      }
|
rr|
j                  }|
j
                  t        k(  r| j                  |      g}	nBt        |
j                  v r
t        |	ig}	n&t        |t              r| j                  ||
|      }	nd }t        ||fv rt        }
t        ||fv r>|j                  r|s|j!                  |      }n|}|	D ]  }| j#                  ||||        y t$        ||fv r|	D ]  }| j#                  ||||        y t&        ||fv r|	D ]  }| j#                  ||||        y |j(                  dk\  r||j+                  t,              v r|j                  j                  |      }
|
r|
j                  y t        |t              r|n|g}|D ]j  }t        |t              s|j/                         D ]D  \  }}||j+                  t0              v r|j3                  |      }| j5                  ||||||       F l y |
r|
j                  n|j7                  |      }|j9                  |
      }g }|	D ]N  }t        |t              r|j;                  |      }||}t        |t              r||z  }>|j=                  |       P |}	|sy |
r|
j>                  r| }| jA                  |      }|r| jB                  sy tE        |      }ntG        |      }|	D ]E  }| jI                  ||||
|      }||r|jK                  |||f       2|jK                  |||f       G y r|   )&r>   rW   r[   r8   r]   typer   _to_typed_json_valuer   	containerrX   _parse_containerr   	TYPE_TERMr   rA   get_contextr`   r   r   r4   rs   r   r\   r   rn   rt   expandr}   get_setappendri   _get_bnodeidr5   r   r   
_to_objectadd)r*   rQ   rf   r3   rv   rw   rx   ri   rj   	obj_nodesre   term_idsubgraphonoder~   nkeynobjrl   pred_uri	flattenedbidpredobj_nodes                          r,   rt   zParser._key_to_graph   sW    c4 II}}  %ggGyyD !66s;<	'"I./	C& 11'4E	GC>!DS'N"$$U"..t4 " F""7HguEF3. " C""7E7EBCW~%" C""7E7EBC??c!cW-=-=d-C&C==$$S)D$S$/3cUD U!#t,"%))+ UJD$w//33 !(!=!=c!BJ&&wz4tTUU "477s(;..t4	 	"C#t$s+#C#t$S 	S!	" 	DLL!kG)'':D(#D! 	-H//'5'4JC{		3d+,		4s+,	-r-   c                    t         |j                  v rfg }|j                         D ]O  \  }}t        |t              s|g}||j                  t              v r||z  }6|D ]  }|j                  ||f        Q |S |j                  dk\  }|rt        |j                  v rt        |j                  v rr|j                         D 	
cg c]V  \  }	}
|	|j                  t              v rt        t        |
i      n(t        |
t              rt        t        |	t        |
i      n|
X c}
}	S |rVt        |j                  v rDt        |j                  v r2|j                         D 	
cg c]  \  }	}
t        t        |
i       c}
}	S |r$t        |j                  v rt        t        |i      gS |rnt        |j                  v r\|j                         D 	
cg c]@  \  }	}
t        |
t              r)|	|j                  t              vrt        t        |	ifi |
n|
B c}
}	S |rt        |j                  v r|j                         D 	
cg c]  \  }	}
t        |
t        t        f      rf|	|j                  t              vrO| j                  |t        |
t              r,t        |j                   t"        k(  r|j%                  |
      n|
in|
|	      n|
 c}
}	S t        |j                  v rg }|j                         D ]  \  }}t        |t              s|g}|D ]  }|r|j&                  r~||j                  t              vrgt        |t              st        |i}|j)                  |j&                  g       }t        |t              s|g}|j                  |       |||j&                  <   |j                  |         |S |gS c c}
}	w c c}
}	w c c}
}	w c c}
}	w r|   )r   r   r\   r>   rW   rs   r   r   r4   r   r   rX   r   r   rq   	_add_typer   r    r   indexr8   )r*   r3   re   rx   r   langvaluesvv11korw   nodesrg   s                 r,   r   zParser._parse_container^  sn   4>>!I #		 4f!&$/$XF7++D11'I# 4!((!T344 oo$5DNN*rT^^/C  IIK Aq	 ((.. eQZ  a& 2q%+,	  Udnn,$..1H14=AD%$==Udnn,%&''R4>>)
  IIK	 Aq a&1G4D4DT4J+J b!W""  TT^^+  IIK Aq a$-!7;K;KD;Q2Q !!S) dii5.@*aH   dnn$I!iik +
U!%."GE! 	+Dtzzc9I9I$9O.O)$5$&:D!%$**b!9)&$7&,XFc*+1TZZ($$T*	++ um >s   AN)N/AN55BN;c                     | j                  |      xs g }|rt        |t              s|g}|j                  |       ||t        <   |S r'   )get_typer>   rW   r   r   )r3   r   r   otypes       r,   r   zParser._add_type  sC      #)rE40GEQ$r-   c                 l   t        |t              r|\  }}|y |rd|v ry t        ||      S t        |t              r=|j	                  |      }	|	j|r| j
                  sy | j                  |||||	      }
|
rC|
S |r|j                  r|j                  t        k(  r| j                  |      }n|y |j                  t        k(  r(t        |t              rt        |j                  |      i}n|j                  t        k(  r;t        |t              r+t        |j                  |      xs |j                  |      i}nt         |j                  t"        |i}nh|y t        |t$              rt        |t&        j(                        S |r|j*                  t,        ur|j*                  }n|j*                  }t        ||      S |j/                  |      }| xr |j1                  |      xs d }|j3                  |      }||j5                  t              v r3| j                  |      }|j1                  |      }|j3                  |      }|s|j7                  t"              |v st"        |v r@|y |rd|v ry t        ||      S |rt        ||j                  |            S t        |      S | j9                  ||||      S )N )r   )datatype)r>   tupler	   rX   get_listrN   	_add_listr   r   r   r   rq   resolver    r   resolve_irir   r   r?   r   doublelanguager   get_languager   rk   rs   get_keyr`   )r*   rQ   rf   r3   re   rg   inlistvaluer   	node_listlistrefr   s               r,   r   zParser._to_object  sh   dE"KE4}t5t,,dD!((.I$$";";..%$	R"N 		99$44T:D\YY"_D#)> 56DYY%'JtS,At 4 Q8K8KD8QRD $))UD9D<dE*"4#**==DMM6==D"++Dt$//##D)86 0 0 6>$!!$'w''--,,U3D''-H%%d+E7??5)T1Ud]}$;u400uw~~h/GHHu~%%%gugtDDr-   c                     | j                  |      }|rt        |      S |j                  |      }| j                  sd|vry t	        |      S )N:)r   r   r   r5   r   )r*   r3   ru   r   uris        r,   rr   zParser._to_rdf_id  sI    '://&)C''CsN#;r-   c                 \    |j                  d      sy |j                  dd      d   }|xs d S )Nz_:   )
startswithsplit)r*   refr   s      r,   r   zParser._get_bnodeid  s0    ~~d#iia ${dr-   c           	         t        |t              s|g}t               }|d }}|D ]q  }	|	|r$|j                  |t        j
                  |f       |}| j                  |||||	d      }
|
F|j                  |t        j                  |
f       t               }s |r2|j                  |t        j
                  t        j                  f       |S t        j                  S )NT)r   )	r>   rW   r   r   r   restr   firstnil)r*   rQ   rf   r3   re   r   
first_subjrv   r   rg   rx   s              r,   r   zParser._add_list  s    )T*"IW
d 	D|		4401//'5'4d/SC{IItSYY,-7D	  IItSXXsww/077Nr-   c           	      ~    t         t        dt        t              z        t        t        j                  | ddd      iS )Nz%sJSON),r   TF)
separators	sort_keysensure_ascii)r   r   rq   r   r   r   dumps)r   s    r,   r   zParser._to_typed_json_value  s8     &CH,-4::*5
 	
r-   )FN)F)FF)rI   rJ   rK   r)   rH   r`   rp   rt   r   staticmethodr   r   rr   r   r   r    r-   r,   rP   rP      sj    
60d&( MR`-DFP  >E@: 
 
r-   rP   )NNNFN)4__doc__r9   rdflib.graphr   rdflib.parserr   rdflibrdflib.namespacer   r   rdflib.termr   r   r	   shared.jsonld.contextr   r   r   shared.jsonld.utilr   r   r   r   shared.jsonld.keysr   r   r   r   r   r   r   r   r   r   r   r   r   r   r    __all__rdflib.utilr#   ImportErrorrq   r   r   rU   rR   rP   r!   r"   objectr   r-   r,   <module>r      s   D  ) (  % . . 8 8     & 8
$	-((&;(#
 SXXe,	 &N6=='' &NX 
0&P
V P
K  		s   &C CC