
    i_                       d Z ddlmZ ddlZddlZddlmZmZmZm	Z	m
Z
mZmZ ddl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mZ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%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 d	dl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 ddgZ8 e  e9ejt                        e.e0      Z;dZ< G d dejz                  j|                        Z?	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ@ G d d      Z>y)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

    )annotationsN)TYPE_CHECKINGAnyDictIterableListOptionalUnion)ConjunctiveGraphGraph)RDFXSD)InputSourceURLInputSource)BNodeIdentifiedNodeLiteralNodeURIRef   )UNDEFContextTerm)CONTEXTGRAPHIDINCLUDEDINDEXJSONLANGLISTNESTNONEREVSETTYPEVALUEVOCAB)_HAS_ORJSONVOCAB_DELIMScontext_from_urlinputsourcejsonorjsonsource_to_jsonJsonLDParserto_rdfTc                  `     e Zd Z fdZ	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ xZS )r/   c                *    t         t        |           y N)superr/   __init__)self	__class__s    LC:\Projects\mas-dev\.venv\Lib\site-packages\rdflib/plugins/parsers/jsonld.pyr5   zJsonLDParser.__init__R   s    lD*,    c
           
        |dvrt        j                  d|z         |s5|j                  |j                         xs |j	                         xs d      }|}|s;t        |d      r/t        |d      r#t        rt        |t              sJ t        |      }	 t        |      }	 t        |j	                               j                  }t        |||	      \  }}|t        ||      }|j                   s"t#        |j$                  |j&                  	      }n|}t)        |||||t+        |      |
       y# t        $ r d}Y w xY w# t        $ r d}Y w xY w)a'  Parse JSON-LD from a source document.

        The source document can be JSON or HTML with embedded JSON script
        elements (type attribute = "application/ld+json"). To process as HTML
        ``source.content_type`` must be set to "text/html" or
        "application/xhtml+xml".

        :param source: InputSource with JSON-formatted data (JSON or HTML)

        :param sink: Graph to receive the parsed triples

        :param version: parse as JSON-LD version, defaults to 1.1

        :param encoding: character encoding of the JSON (should be "utf-8"
            or "utf-16"), defaults to "utf-8"

        :param base: JSON-LD `Base IRI <https://www.w3.org/TR/json-ld/#base-iri>`_, defaults to None

        :param context: JSON-LD `Context <https://www.w3.org/TR/json-ld/#the-context>`_, defaults to None

        :param generalized_rdf: parse as `Generalized RDF <https://www.w3.org/TR/json-ld/#relationship-to-rdf>`_, defaults to False

        :param extract_all_scripts: if source is an HTML document then extract
            all script elements, defaults to False (extract only the first
            script element). This is ignored if ``source.system_id`` contains
            a fragment identifier, in which case only the script element with
            matching id attribute is extracted.

        )utf-8zutf-16z9JSON should be encoded as unicode. Given encoding was: %s urllinks皙?Nbase)store
identifier)	skolemize)warningswarn
absolutizegetPublicIdgetSystemIdhasattrr   
isinstancer   r+   float
ValueErrorr   fragment	Exceptionr.   context_awarer   rB   rC   r0   bool)r6   sourcesinkversionrD   encodingrA   contextgeneralized_rdfextract_all_scriptskwargscontext_datafragment_iddata	html_base	conj_sinks                   r8   parsezJsonLDParser.parseU   sR   ` ..MM)+34
 ??6#5#5#7#U6;M;M;O#USUVD 6767;S!&.9996v>L	GnG
	 !3!3!56??K )>QRi )$/D !!(tzzdooVII!	
-  	G	  	K	s$   D.  #D? .D<;D<?EE)r?   Fr;   NNFF)rR   r   rS   r   rT   rL   rD   rQ   rU   Optional[str]rA   r`   rV   LOptional[Union[List[Union[Dict[str, Any], str, None]], Dict[str, Any], str]]rW   Optional[bool]rX   rb   rY   r   returnNone)__name__
__module____qualname__r5   r_   __classcell__)r7   s   @r8   r/   r/   Q   s    - ")" */.3!_
_
 _
 	_

 _
  _
 _

_
 (_
  ,!_
" #_
$ 
%_
r9   c                    t        ||      }|r|j                  |       t        |||      }	|	j                  | ||      S )N)rA   rT   )rW   allow_lists_of_listsrD   )r   loadParserr_   )
r\   datasetrA   rZ   rT   rW   rj   rD   rV   parsers
             r8   r0   r0      sG    " 41G\"'1F
 <<gw//r9   c                  ,   e Zd Z	 	 	 d	 	 	 	 	 ddZddZ	 d	 	 	 	 	 	 	 	 	 	 	 ddZddZ	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZe	dd       Z
	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd	Zdd
ZddZ	 	 	 	 	 	 	 	 	 	 	 	 ddZe	dd       Zedd       Zy)rl   Nc                L    || _         || _        ||nt        | _        i | _        y r3   )rD   rW   ALLOW_LISTS_OF_LISTSrj   invalid_uri_to_bnode)r6   rW   rj   rD   s       r8   r5   zParser.__init__   s5     #. $/ !% 	!
 79!r9   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)rK   listdictgetr   rk   rA   vocabbindtermsitemsidendswithr*   rP   default_context_add_to_graph)r6   r\   rV   rm   
topcontext	resourceslocal_contextnametermgraphnodes              r8   r_   zParser.parse   s    
dD!Id# HHW-M]GLL9!
Ii.&K	==LLw}}-!----/JD$ww477++L9T477+ 0
 ,3+@+@''gDwwjI  r9   c                :   t        |t              r|j                  |      ry t        |v r5|s3|t           }|r|j	                  |      }nt        |j                        }|j                  |      }|j                  |      }|$| j                  ||      }|t        |      dkD  r|}t        |t              r| j                  ||      }	n&t               }	| j                  r|	j                         }	|	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 )Nr@   r   T)reverseno_id)r   )rK   ru   	get_valuer   
subcontextr   doc_baseget_context_for_typeget_id_get_nested_idlenstr
_to_rdf_idr   rD   rz   get_keysr   r$   _key_to_graph)r6   rm   r   rV   r   r   r   id_val	nested_idsubjr   keyobjrkeyrobjs                  r8   r~   zParser._add_to_graph   s    $%):):4)@d?: MM!,,];!w'7'78 ..t4%>++GT:I$Y!);"fc"??7F3D7D~~~~'< $

HCg~(8(8(<!<czSG$4$4S$99"%))+JD$&& $# ' 	 #. ""7E7D#sRW"X# %& r9   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 Nr?   )rz   rT   r   r"   ry   rv   r{   rK   rt   ru   r   get_context_for_termr   r   )	r6   rV   r   r   r   r   objsr   r   s	            r8   r   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!&#.%   %r9   c	           
     j   t        |t              r|}	n|g}	j                  j                  |      }
|
r{|
j                  }|
j
                  t        k(  r| j                  |      g}	nKt        |
j                  v r| j                  |	      g}	n&t        |t              r| j                  |
|      }	nd }t        ||fv rt        }
t        ||fv rV|j                   r,|s*t"        rt        |t$              sJ |j'                  |      }n|}|	D ]  }| j)                  |||        y t*        ||fv r|	D ]  }| j)                  |||        y t,        ||fv r|	D ]  }| j)                  |||        y j.                  dk\  r|j1                  t2              v rj                  j                  |      }
|
r|
j                  y t        |t              r|n|g}|D ]j  }t        |t              s|j5                         D ]D  \  }}|j1                  t6              v rj9                  |      }| j;                  ||||||       F l y |
r|
j                  nj=                  |      }j?                  |
      dfd |	      }	|sy |
r|
j@                  r| }| jC                  |      }|r5| jD                  sy tG        |      }| jH                  r|jI                         }ntK        |      }|	D ]E  }| jM                  |||
|      }||r|jO                  |||f       2|jO                  |||f       G y )Nr?   c                    g }| D ]T  }t        |t              rj                  |      }||}t        |t              r| |      z  }D|j	                  |       V |S r3   )rK   ru   get_setrt   append)n	flattenedr   r   rV   flattens       r8   r   z%Parser._key_to_graph.<locals>.flatten  sf    Ic4("??3/D'"c4(-I  %  r9   )r   zIterable[Any]rc   	List[Any])(rK   rt   ry   rv   r{   typer   _to_typed_json_valuer!   	container_expand_nested_listru   _parse_containerr&   	TYPE_TERMr   rP   r   r   get_contextr~   r%   r   rT   r   r"   rz   r   r   r   expandr   r   _get_bnodeidrW   r   rD   r   
_to_objectadd)r6   rm   r   rV   r   r   r   r   r   	obj_nodesr   term_idsubgraphonoder   nkeynobjr   pred_uribidpredobj_noder   s      `                  @r8   r   zParser._key_to_graphS  s    c4 II}}  %ggGyyD !66s;<	'!55i@A	C& 11'4E	GC>!DS'N"$$U %g/?@@@"..t4 """7HguE #3. """7E7EB #W~%"""7E7EB #??c!cW-=-=d-C&C==$$S)D$S$/3cUD!#t,"%))+JD$w//33 !(!=!=c!BJ&&wz4tT #.  "477s(;..t4	 I&	DLL!kG )'':D~~~~'(#D!H//'5'4JC{		3d+,		4s+, "r9   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   rz   rK   rt   r   r#   r   rT   r   r   ru   r   r&   r   	_add_typer   r(   r   indexrv   )r6   rV   r   r   r   langvaluesvv11kor   nodesr   s                 r8   r   zParser._parse_container  ss    4>>!I #		f!&$/$XF7++D11'I#!((!T3 $ !, oo$5DNN*rT^^/C  IIK (DAq G,,T22 %$4>q$4Gr1eQ/0QO (  Udnn,$..1H14=AD%$==Udnn,%&''R4>>)  IIK (DAq "!T*q8H8H8N/N "a&A& (  TT^^+  IIK (DAq "!dC[1aw?O?OPT?U6U NN  *!S1  dii56H!2aP!" 
 ( " dnn$I!iik
U!%."GE!Dtzzc9I9I$9O.O)$5$&:D!%$**b!9)&$7&,XFc*+1TZZ($$T* " * uy >s   AN)N/AN55BN;c                    | j                  |      xs g }|rt        |t              s|g}|j                  |       ||t        <   |S r3   )get_typerK   rt   r   r&   )rV   r   r   otypes       r8   r   zParser._add_type  sC      #)rE40GEQ$r9   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)rK   tupler   ru   get_listrj   	_add_listr   r   r   r   r   resolver(   r   resolve_irir&   r'   rL   r   doublelanguager   get_languager   r   r   get_keyr~   )r6   rm   r   rV   r   r   inlistvaluer   	node_listlistrefr   s               r8   r   zParser._to_object   sj    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Dr9   c                ~   | j                  |      }|r)t        |      }| j                  r|j                         S |S |j                  |      }| j                  sd|vry t        |      }t        |      sH|| j                  vr+t        t        j                  d            | j                  |<   | j                  |   }|S )N:   )
r   r   rD   r   rW   r   r   rr   secretstoken_urlsafe)r6   rV   r   r   burir   s          r8   r   zParser._to_rdf_ido  s    'c
A~~{{}$H//&)C''CsN#)#;Dt9!:!::8=g>S>STV>W8XD--f5008Kr9   c                \    |j                  d      sy |j                  dd      d   }|xs d S )Nz_:   )
startswithsplit)r6   refr   s      r8   r   zParser._get_bnodeid  s0    ~~d#iia ${dr9   c           	     l   t        |t              s|g}t               }| j                  r t        |t              r|j                         }|d }}|D ]  }	|	|r$|j	                  |t
        j                  |f       |}| j                  |||||	d      }
|
F|j	                  |t
        j                  |
f       t               }| j                  st        |t              s|j                         } |r2|j	                  |t
        j                  t
        j                  f       |S t
        j                  S )NT)r   )
rK   rt   r   rD   r   r   restr   firstnil)r6   rm   r   rV   r   r   
first_subjr   r   r   r   s              r8   r   zParser._add_list  s    )T*"I+07
>>jU;#--/J  dD|		4401//'5'4d/SC{IItSYY,-7D~~*T5"9~~'# & IItSXXsww/077Nr9   c                   t         rFt        j                  | t        j                  t        j                  z        j                  d      }nt        j                  | ddd      }t        t        j                  t        |iS )N)optionr;   ),r   TF)
separators	sort_keysensure_ascii)r)   r-   dumpsOPT_SORT_KEYSOPT_NON_STR_KEYSdecoder,   r&   r   r   r'   )r   
val_strings     r8   r   zParser._to_typed_json_value  sk    $ll++f.E.EE fWo 
 *5J #((:
 	
r9   c                |    |D cg c]%  }t        |t              r| j                  |      n|' }}t        |iS c c}w r3   )rK   rt   r   r!   )clsr   r   results       r8   r   zParser._expand_nested_list  sL     OX
NW*Q*=C##A&1Di 	 
 f~
s   *9)FNF)rW   rQ   rj   rb   rD   rQ   )r\   r   rV   r   rm   r   rc   r   )F)rm   r   r   r   rV   r   r   r   r   rQ   rc   Optional[Node])rV   r   r   Dict[str, Any]rc   r`   )FF)rm   r   r   r   rV   r   r   r   r   r   r   r   r   rQ   r   rQ   rc   rd   )rV   r   r   r   r   r   rc   r   )rV   r   r   r   r   r   rc   r   )rm   r   r   r   rV   r   r   Optional[Term]r   r   r   rQ   rc   r   )rV   r   r   r   rc   zOptional[IdentifiedNode])r   r   rc   r`   )rm   r   r   r   rV   r   r   r   r   r   rc   r   )r   r   rc   zDict[str, str])r   r   rc   zDict[str, List[Any]])re   rf   rg   r5   r_   r~   r   r   r   staticmethodr   r   r   r   r   r   classmethodr    r9   r8   rl   rl      s    !&/3	99 -9 	9F !== = 	=
 = = 
=@&6 r-r- r- 	r-
 r- r- r- r- r- 
r-hNN&*N1?N	N`   MEME ME 	ME
 ME ME ME 
ME^$)) ) 	)
 ) ) 
)V 
 
  r9   rl   )NNNFNF)r\   r   rm   r   rA   r`   rZ   ra   rT   zOptional[float]rW   rQ   rj   rb   rD   rQ   )A__doc__
__future__r   r   rE   typingr   r   r   r   r   r	   r
   rdflib.parserrdflibrdflib.graphr   r   rdflib.namespacer   r   r   r   rdflib.termr   r   r   r   r   shared.jsonld.contextr   r   r   shared.jsonld.keysr   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   shared.jsonld.utilr)   r*   r+   r,   r-   r.   __all__r   r   r   rq   rn   rl   r/   r0   r   r9   r8   <module>r     s  D #   L L L  0 % 5 D D 8 8    "  8
$SXXe,	 c
6=='' c
R  	#!+/0
00 0	0 0 0 )0 08u ur9   