
    ^hX                       S r SSKJr  SSKrSSKrSSKrSSKJr  SSKJ	r	  SSK
Jr  SSKJrJr  SS	KJr  SS
KJr  SSKJrJrJr  SSKJr  SSKJrJr  0 r\" 5       r/ SQrS rS r S$S jr!S%S jr"S&S jr#S r$Sr%Sr&S r' " S S\5      r(S r)  S%SSS.           S'S jjjr*              S(S jr+S r,SSSSSS\\4                 S)S  jjr-            S*S! jr.          S+S" jr/       S,S# jr0g)-zNotebook format validators.    )annotationsN)deepcopy)Path)dedent)AnyOptional   )import_item)generate_corpus_id)ValidationError_validator_for_nameget_current_validator)get_version)DuplicateCellIdMissingIDFieldWarning)r   get_validatorisvalidNotebookValidationErrorbetter_validation_error	normalizevalidateiter_validatec                    [        U [        5      (       a2  U R                  5        H  u  pUS:X  a  SO
[        U5      nX U'   M     U $ [        U [        5      (       a!  [        U 5       H  u  p2[        U5      X'   M     U $ )z relax any `additionalProperties`additionalPropertiesT)
isinstancedictitems_relax_additional_propertieslist	enumerate)objkeyvalueis       L/home/james-whalen/.local/lib/python3.13/site-packages/nbformat/validator.pyr   r   %   sz    #t))+JC55;WX];^  H	 & J 
C		!#HA1%8CF 'J    c                v    U S   S   S   R                  SS05        U S   S   S   R                  SS05        U $ )NdefinitionscelloneOf$refz#/definitions/unrecognized_celloutputz!#/definitions/unrecognized_output)append)schemas    r%   _allow_undefinedr/   3   sM    
=&!'*116;\2]^
=(#G,33V=`4abMr&   Fc                   U c  SSK Jn  Un [        SU -  5      n[        USS5      nUc  UnU(       a  [	        U5      O	[        5       nUR                  X4nU[        ;  a5   [        XPUS9n	Xa:  a  [        U	5      n	[        U	5      n	U" U	5      [        U'   U(       a%   [        XPUS9n	[        U	5      n	U" U	5      [        U'   [        U   $ ! [         a     gf = f! [         a     gf = f)z%Load the JSON schema into a ValidatorNr	   )current_nbformatznbformat.v%snbformat_minorr   versionversion_minor) r1   r
   getattrr   r   name
validators_get_schema_jsonAttributeErrorr   r/   )
r4   r5   relax_add_propsr8   r1   vcurrent_minorcurrent_validatorversion_tupleschema_jsons
             r%   r   r   9   s   &"NW,-AA/3M%59+D1?T?V&++WDMJ&	*1][K (6{CK*;7K$5k$B
=!	*1][K 3;?$5k$B
=!m$$-  		  		s$   !
C
 
C 

CC
C'&C'c                   X4U R                   ;   a:  [        [        U R                  5      R                  U R                   X4   -  5      nOUX R
                  :  a9  [        [        U R                  5      R                  U R                   S   -  5      nOSn[        U5      e[        U5      R                  SS9 n[        R                  " U5      nSSS5        U$ ! , (       d  f       W$ = f)zJ
Gets the json schema from a given imported library and nbformat version.
NNz-Cannot find appropriate nbformat schema file.utf8)encodingN)
nbformat_schemastrr   __file__parentr2   r;   openjsonload)r=   r4   r5   schema_pathmsgfrA   s          r%   r:   r:   e   s     	1#4#44$qzz*11A4E4EwF^4__`	))	)$qzz*11A4E4El4SST=S!!	k					0Aiil 
1 
1	0s   9C
C)c           	     b   [        U 5      n [        R                  " 5          [        R                  " S[        S9  [        R                  " S[
        S9  [        XX#SS9  SSS5         X:w  a  [        eg! , (       d  f       N= f! [         a     X:w  a  [        egf = f! X:w  a  [        ef = f)zChecks whether the given notebook JSON conforms to the current
notebook format schema. Returns True if the JSON is valid, and
False otherwise.

To see the individual errors that were encountered, please use the
`validate` function instead.
ignore)categoryF)repair_duplicate_cell_idsNT)	r   warningscatch_warningsfilterwarningsDeprecationWarningr   r   AssertionErrorr   )nbjsonrefr4   r5   origs        r%   r   r   v   s     FD!$$&##H7IJ##H7LMV'TYZ ' >    '&   >   
 >   s:   B >A5 B 5
B?B 
BB! BB! !B.c                H    U (       d  gSSR                  S U  5       5      -  $ )z
(from jsonschema._utils.format_as_index, copied to avoid relying on private API)

Construct a single string containing indexing operations for the indices.

For example, [1, 2, "foo"] -> [1][2]["foo"]
r6   z[%s]z][c              3  8   #    U  H  n[        U5      v   M     g 7fN)repr).0indexs     r%   	<genexpr>#_format_as_index.<locals>.<genexpr>   s     ?wed5kkws   )join)indicess    r%   _format_as_indexrf      s$     DII?w????r&      @   c                P   [        U [        5      (       a  [        U R                  5       5      S[          VVs0 s H  u  pU[        U5      _M     nnn[        UR                  S5      [        5      (       a  S[        U S   5      -  /US'   [        UR                  S5      [        5      (       a  S[        U S   5      -  /US'   [        U 5      [        :  a  S[        U 5      [        -
  -  US'   U$ [        U [        5      (       a[  U S[          Vs/ s H  n[        U5      PM     nn[        U 5      [        :  a$  UR                  S[        U 5      [        -
  -  5        U$ [        U [        5      (       a#  U S[         n[        U 5      [        :  a  US-  nU$ U $ s  snnf s  snf )	z~Truncate objects for use in validation tracebacks

Cell and output lists are squashed, as are long strings, lists, and dicts.
Ncellsz...%i cells...outputsz...%i outputs...z%i keys truncatedz...z...%i items truncated...)r   r   r   r   _ITEM_LIMIT_truncate_objgetlenr-   rG   
_STR_LIMIT)r!   kr=   truncated_dictitemtruncated_listtruncated_strs          r%   rm   rm      sy   
 #t:>syy{:KL[:YZ:Y$!!]1--:YZn((1488'7#c'l:K'K&LN7#n((3T::);c#i.>Q)Q(RN9%s8k!$73s8k;Q$RN5!#t:=l{:KL:K$--:KLs8k!!!"<C;@V"WX#sKZ(s8j U"MJ' [ Ms   FF#c                  2    \ rS rSrSrSS jrS rS r\rSr	g)	r      z[Schema ValidationError with truncated representation

to avoid massive verbose tracebacks.
Nc                ~    Xl         [        U R                   SU5      U l        U R                   R                  U l        g)zInitialize the error class.rZ   N)originalr7   rZ   message)selfry   rZ   s      r%   __init__ NotebookValidationError.__init__   s,     4==%5}},,r&   c                .    [        U R                  U5      $ )z Get an attribute from the error.)r7   ry   )r{   r"   s     r%   __getattr__#NotebookValidationError.__getattr__   s    t}}c**r&   c                r   U R                   n[        UR                  5      nSR                  UR                  SSR                  UR                  U R                  =(       d    S[        [        UR                  5      SS 5      5      SS[        UR                  5      -  [        R                  " USS	9/5      $ )
zRCustom str for validation errors

avoids dumping full schema and notebook to logs

r6   zFailed validating {!r} in {}{}:notebookNzOn instance%s:N   )width)ry   rm   instancerd   rz   format	validatorrZ   rf   r   relative_schema_pathrelative_pathpprintpformat)r{   errorr   s      r%   __unicode__#NotebookValidationError.__unicode__   s    
  0yy188OOHH*
$T%*D*D%Ecr%JK
  #3E4G4G#HHxr2
 	
r&   )rz   ry   rZ   r^   )
__name__
__module____qualname____firstlineno____doc__r|   r   r   __str____static_attributes__ r&   r%   r   r      s    
-+
. Gr&   r   c                   [        U R                  5      (       d  U $ U R                  S   nSnUR                  S5      (       a  [        U R                  [
        5      (       aB  SU R                  ;   a  U R                  S   S-   nOSU R                  ;   a  U R                  S   nU(       a   [        U R                  UUUS9  [        X5      $ ! [         aa  nU R                  R                  UR                  5        U R                  Ul        [        XQU5      nUR                  c  XFl        Us SnA$ SnAf[         a     Nf = f)zGet better ValidationError on oneOf failures

oneOf errors aren't informative.
if it's a cell type or output_type error,
try validating directly based on the type for a better error message
r   NOf	cell_type_celloutput_typer3   )ro   rM   endswithr   r   r   r   r   r   extendr   rZ   	Exceptionr   )r   r4   r5   r"   rZ   	sub_errorbetters          r%   r   r      s    u  !!


B
C
C
||Dennd++enn,nn[1G;%..0nn]3NN#"/	$ #5.. # ##**9+B+BC*/*=*=	'0]S::%!$J  s%   -C 
EAD4.E4EEr<   strip_invalid_metadatac          	     b    [        U 5      n [        U 5      u  pVUc  UnUc  Un[        U UUSUUS9$ )a4  
Normalise a notebook prior to validation.

This tries to implement a couple of normalisation steps to standardise
notebooks and make validation easier.

You should in general not rely on this function and make sure the notebooks
that reach nbformat are already in a normal form. If not you likely have a bug,
and may have security issues.

Parameters
----------
nbdict : dict
    notebook document
version : int
version_minor : int
relax_add_props : bool
    Whether to allow extra property in the Json schema validating the
    notebook.
strip_invalid_metadata : bool
    Whether to strip metadata that does not exist in the Json schema when
    validating the notebook.

Returns
-------
changes : int
    number of changes in the notebooks
notebook : dict
    deep-copy of the original object with relevant changes.

Tr   )r   r   
_normalize)nbdictr4   r5   r<   r   nbdict_versionnbdict_version_minors          r%   r   r     sP    N fF+6v+>(N ,'5 r&   c                   SnX4S:  a  U S    H@  nSU;  d  M  [         R                  " S[        SS9  U(       d  M.  [        5       US'   US-  nMB     [	        5       nU S    Hu  nSU;  a  M  US   n	X;   aO  U(       a7  [        5       n
XS'   US-  n[         R                  " S	U	< S
U
< S3[
        SS9  OSU	 S3n[        U5      eUR                  U	5        Mw     U(       a  U[        XX$S9-  nX`4$ )ar  
Private normalisation routine.

This function attempts to normalize the `nbdict` passed to it.

As `_normalize()` is currently used both in `validate()` (for
historical reasons), and in the `normalize()` public function,
`_normalize()` does currently mutate `nbdict`.
Ideally, once `validate()` stops calling `_normalize()`, `_normalize()`
may stop mutating `nbdict`.

r   )      rj   ida)  Cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.   
stacklevelr	   zNon-unique cell id z detected. Corrected to .zNon-unique cell id 'z' detected.r<   )	rT   warnr   r   setr   r   add_strip_invalida_metadata)r   r4   r5   rS   r<   r   changesr)   seen_idscell_idnew_idrN   s               r%   r   r   E  s!   ( G6)7OD4& * 	 -,!3!5DJqLG $$ 57OD44jG",/1F!'JqLGMM-g[8PQWPZZ[\'#$ 1	EC)#..LL!% $& +]
 	
 ?r&   c                R    [         R                  " [        SU  S35      [        SS9  g )N`a7  ` kwargs of validate has been deprecated for security
        reasons, and will be removed soon.

        Please explicitly use the `n_changes, new_notebook = nbformat.validator.normalize(old_notebook, ...)` if you wish to
        normalise your notebook. `normalize` is available since nbformat 5.5.0

        r   r   )rT   r   r   rW   )fields    r%   	_dep_warnr     s/    MM% 	 	
 	r&   c           	        [        U[        5      (       d  Ub   eU[        L a  SnO[        S5        U[        L a  SnO[        S5        U b  OUb  Un OSn[	        U5      eUc  [        U 5      u  pUc  U	nUc  U
nOUc  Su  p#Uc<  [        U[        5      (       d   e[        U[        5      (       d   e[        U UUUUUS9  [        U UUUUUS	9 H  nUe   g)
a!  Checks whether the given notebook dict-like object
conforms to the relevant notebook format schema.

Parameters
----------
nbdict : dict
    notebook document
ref : optional, str
    reference to the subset of the schema we want to validate against.
    for example ``"markdown_cell"``, `"code_cell"` ....
version : int
version_minor : int
relax_add_props : bool
    Whether to allow extra properties in the JSON schema validating the notebook.
    When True, all known fields are validated, but unknown fields are ignored.
nbjson
repair_duplicate_cell_ids : bool
    Deprecated since 5.5.0 - will be removed in the future.
strip_invalid_metadata : bool
    Deprecated since 5.5.0 - will be removed in the future.

Returns
-------
None

Raises
------
ValidationError if not valid.

Notes
-----
Prior to Nbformat 5.5.0 the `validate` and `isvalid` method would silently
try to fix invalid notebook and mutate arguments. This behavior is deprecated
and will be removed in a near future.

Please explicitly call `normalize` if you need to normalize notebooks.
NFr   TrS   z0validate() missing 1 required argument: 'nbdict')r	   r   r   )rZ   r4   r5   r<   r   )	r   rG   _deprecatedr   	TypeErrorr   intr   r   )r   rZ   r4   r5   r<   rY   rS   r   rN   r   r   r   s               r%   r   r     s   ^ c33;..,!&*+ K/$(!-. 		@n
{/:6/B,?$G 0M	!%
{'3''''-----%+#9	
 #'5 r&   c                   [        XUS9nU(       d  SU SU S3n[        U5      eUR                  " U /UQ76 n[        U5      n[	        U5      (       a/  UR
                  S:w  a  [        UUUSS9nUR                  " U /UQ76 $ [        U5      $ )Nr   zNo schema for validating vr   
 notebooks
jsonschemar4   r5   r<   r8   )r   r   iter_errorsr   ro   r8   iter)	r   r4   r5   r<   argsr   rN   r   errorss	            r%   _get_errorsr      s     goVI*7)1]O:Nc""''66K+F
6{{y~~5!'+	
	 $$V3d33<r&   c                   [        XX#5      nSn[        [        U5      5      S:  Ga  [        UUUSS9nU(       d  SU SU S3n[	        U5      eUR                  U 5      nUR                  U5      nSU;   a&  US    H  n	U S   R                  U	S5        US	-  nM     S
U;   Ga;  [        U R                  S
S5      5      n
[        U
5       GH  nSUS
   U   R                  ;   d  M  U S
   U   S   nUS
   U   R                  S   R                  S    Vs/ s H  nUS   PM
     nnSU S3nX;   d  Mi  UR                  U5      nUS
   U   R                  S   R                   Hs  nUR                  nUR                  S   U:H  n[        U5      S:H  =(       a    US   S:H  nU(       d  MG  U(       d  MP  U S
   U   S   R                  US	   S5        US	-  nMu     GM     U$ s  snf )a  
This function tries to extract metadata errors from the validator and fix
them if necessary. This mostly mean stripping unknown keys from metadata
fields, or removing metadata fields altogether.

Parameters
----------
nbdict : dict
    notebook document
version : int
version_minor : int
relax_add_props : bool
    Whether to allow extra property in the Json schema validating the
    notebook.

Returns
-------
int
    number of modifications

r   r   r   zNo jsonschema for validating vr   r   metadataNr	   rj   r*   r   r+   z#/definitions/r      )r   ro   r   r   r   r   
error_treepoprn   ranger   r.   ra   contextr   rM   )r   r4   r5   r<   r   r   r   rN   r   r"   number_of_cellscell_idxintended_cell_typerZ   schemas_by_indexcell_type_definition_nameschema_indexr   rel_patherror_for_intended_schemais_top_level_metadata_keys                        r%   r   r     s2   0 -IFG
4<1!'+	
	 27)1]O:VC!#&&&&v.))&1
#!*-z"&&sD11 . j !&**Wa"89O!/2
 j1(;BBB)/)B;)O& $.g#6x#@#G#G#P#W#WX_#`(#`C F#` % ( 3AAS@TTY0Z-0D'7'='=>W'X%/%8%B%I%I'%R%Z%ZE',':':H8=8I8I!8LP\8\5 #H 2 Px{j7P 6  98=V=V &w 9* E I I(ST+W[ \ '1 &[ 30 N#(s   G!c           	   #  n  #    U b  OUb  Un OSn[        U5      eUc  [        U 5      u  p#U(       a   [        U UUUSSU-  05      nO U(       a  [	        XX45         [        XX45      nU H  n
[        XU5      v   M     g! [         a  n	U	v    Sn	A	gSn	A	ff = f! [         a  n	U	v    Sn	A	gSn	A	ff = f7f)aX  Checks whether the given notebook dict-like object conforms to the
relevant notebook format schema.

Returns a generator of all ValidationErrors if not valid.

Notes
-----
To fix: For security reasons, this function should *never* mutate its `nbdict` argument, and
should *never* try to validate a mutated or modified version of its notebook.

Nz5iter_validate() missing 1 required argument: 'nbdict'r+   z#/definitions/%s)r   r   r   r   r   r   )r   rZ   r4   r5   r<   rY   r   rN   r   er   s              r%   r   r   ^  s     * 		En!,V!4

	 +c12F "$VmU	 -QF
 %emDD %  	G	  	G	sX   /B5A? B5B 'B5?
B	BB5BB5
B2$B-(B5-B22B5)NNFNrC   )NNN)r   r   r4   Optional[int]r5   r   r<   boolr   r   returntuple[int, Any])r   r   r4   r   r5   r   rS   r   r<   r   r   r   r   r   )r   r   rZ   zOptional[str]r4   r   r5   r   r<   r   rY   r   rS   r   r   r   r   None)r   r   r4   r   r5   r   r<   r   r   r   r   r   )
r   r   r4   r   r5   r   r<   r   r   r   )NNNNFNF)1r   
__future__r   rK   r   rT   copyr   pathlibr   textwrapr   typingr   r   _importsr
   corpus.wordsr   json_compatr   r   r   readerr   r   r   r9   objectr   __all__r   r/   r   r:   r   rf   rl   rp   rm   r   r   r   r   r   r   r   r   r   r   r&   r%   <module>r      s9   ! #         ! , T T  <
h	)%X"!.@ 
8'o 'T&/V "#'4
 "#(444 !4
 4 !4 4nBBB B  $	B
 B !B BJ" !#'!&1#.cc	c c !	c
 c c  $c !c 
cL.1DHQT*FFF.1FDHFFT  <Er&   