
    ^hD                       S r SSKJr  SSKrSSKrSSKJr  SSKJ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  SS
KJrJr  SSKJr  SSKJrJrJrJrJr  Sr          SS jrSS jrSS jr     S               SS jjr!        SS jr" S          S!S jjr#S"S jr$     S#                 S$S jjr% S%             S&S jjr& " S S\5      r'g)'z Frontend config storage helpers.    )annotationsN)glob)Any)Draft7Validator)ValidationError)_tz)
APIHandler)ConfigManagerrecursive_update)web   )DEFAULT_LOCALEL10N_SCHEMA_NAMEPSEUDO_LANGUAGE
SYS_LOCALEis_valid_localez.jupyterlab-settingsc           
        SnSnSnSnUb  UR                  S5      u  pn
U Ho  n[        R                  R                  XSXS-   5      n[        R                  R	                  U5      (       d  MM  [        R                  R                  XS5      n Un  O   Uc  [        X5      n[        R                  R	                  U5      (       d  [        R                  " SXG-  5      e[        US	S
9 n [        R                  " U5      n SSS5        [        UWU5      n [        R                  " U5        [!        X5      nUU4$ ! [         a-  nUn[        R                  " SUU[        U5      4-  5      SeSnAff = f! , (       d  f       N{= f! [         a-  nUn[        R                  " SUU[        U5      4-  5      SeSnAff = f)z=Returns a dict containing a parsed and validated JSON schema.zSchema not found: %szFailed parsing schema (%s): %sz!Failed validating schema (%s): %sN:schemas.json  utf-8encoding  )	partitionospathjoinexists_pathr   	HTTPErroropenjsonload	Exceptionstr	_override	Validatorcheck_schema_get_version)schemas_dirschema_name	overrideslabextensions_pathnotfound_errorparse_errorvalidation_errorr   ext_name_plugin_nameext_pathtargetfidschemaenameversions                     Z/home/james-whalen/.local/lib/python3.13/site-packages/jupyterlab_server/settings_utils.py_get_schemar>       s    ,N2K:D %#.#8#8#= [*HWW\\(iY`K`aFww~~f%% ggll8yI + |[.77>>$mmC!677	dW	%	MYYs^F 
& {FI6FNv&
 ;4G7?  	MD--[D#a&>%ABL	M	 
&	%  NmmC!1T3q6N!BCMNsB   *E8,D>F	 >
E5(E00E55E88
F	
G (F;;G c           
     H   [        XS[        5      nSn0 nSnSnSnSn	Sn
[        R                  R	                  U5      (       a  [        R
                  " U5      n[        R                  " UR                  5      R                  5       n	[        R                  " UR                  5      R                  5       n
[        USS9 n UR                  5       =(       d    Un[        R                  " U5      n SSS5        ['        U5      (       a  [)        U5      n UR+                  U5        [/        XEXiU
S	9$ ! [         a*  n[         R"                  " SX[%        U5      4-  5      SeSnAff = f! , (       d  f       N}= f! [,         a  nXq[%        U5      4-  nSn0 n SnANuSnAff = f)
z
Returns a dictionary containing the raw user settings, the parsed user
settings, a validation warning for a schema, and file times.
Fz{}Nz#Failed validating settings (%s): %sz Failed loading settings (%s): %sr   r   r   )rawsettingswarninglast_modifiedcreated)r!   SETTINGS_EXTENSIONr   r   r    stattzutcfromtimestampst_mtime	isoformatst_ctimer#   readjson5loadsr&   r   r"   r'   lenr)   validater   dict)settings_dirr-   r9   r   r@   rA   rB   validation_warningr1   rC   rD   rF   r8   r:   	validators                  r=   _get_user_settingsrU   U   sn   
 E3EFD
CHG>4KMG	ww~~dwwt}++DMM:DDF%%dmm4>>@$)SXhhj'C ;;s+ * 8}}f%		x( GZa   XmmCSV7L)LMSWWX	 *)  	(Q+@@GCH	sB    E)/D2E: 2
E&<%E!!E&&E))
E7:
F!FF!c                :   [        X5      n[        R                  R                  [        R                  R	                  U5      S   S5      n [        USS9 n[        R                  " U5      nUS   sSSS5        $ ! , (       d  f       g= f! [         a     gf = f)zEReturns the package version for a given schema or 'N/A' if not found.r   zpackage.json.origr   r   r<   NzN/A)	r!   r   r   r   splitr#   r$   r%   r&   )r,   r-   r   package_pathr8   packages         r=   r+   r+   }   sz     *D77<<d 3A 68KLL,1SiinG9% 211  s0   
B A<2	B <
B
B 
B 
BBc           	        0 n0 n/ n	[         R                  R                  U 5      (       d
  SU -  /n	/ U	4$ U S-   U-   n
[        U
SS9 Vs/ s H  oPM     nnUR	                  5         U H  n[         R                  R                  X5      n[         R                  R                  U5      u  nnSR                  UUS[        U5      *  /5      R                  SS5      =nnU(       a  [        US	9UU'   M  [        U UUS5      u  nnUb  U" U5      n[        UUU5      nUS
   (       a   U	R                  UR                  S
5      5        [        SUUUS.UD6UU'   M     UGbJ  / nU H+  nUS-   U-   n
UR                  S [        U
SS9 5       5        M-     UR	                  5         U GH   nUR                  [         R                   S5      nUR                  S5      u  nn[         R                  R                  U5      u  nnSR                  UUS[        U5      *  /5      R                  SS5      =nnUU;   a  M  U(       a  [        US	9UU'   M  [        U UX$S9u  nn[        UUU5      nUS
   (       a   U	R                  UR                  S
5      5        [        SUUUS.UD6UU'   GM     UR#                  U5        [%        UR'                  5       SS9 Vs/ s H  nUU   PM
     nnUU	4$ s  snf s  snf )aG  
Returns a tuple containing:
 - the list of plugins, schemas, and their settings,
   respecting any defaults that may have been overridden if `ids_only=False`,
   otherwise a list of dict containing only the ids of plugins.
 - the list of warnings that were generated when
   validating the user overrides against the schemas.
z'Settings directory does not exist at %sz/**/*T)	recursiver   N\/)idrB   r^   r9   r<   z/**/schemas/**/*c              3  $   #    U  H  ov   M     g 7fN ).0r   s     r=   	<genexpr>!_list_settings.<locals>.<genexpr>   s     V1U1Us   zschemas/r/   )reverserb   )r   r   r    r   sortrelpathrW   r   rO   replacerQ   r>   rU   appendpopextendsepupdatesortedkeys)r,   rR   r.   	extensionr/   
translatorids_onlyrA   federated_settingswarningsschema_patternr   schema_pathsschema_pathrel_pathrel_schema_dirschema_base_idr-   r9   r<   user_settingsext_dirschema_path_base_dirkeysettings_lists                              r=   _list_settingsr      s   $  "H)+H77>>+&&=KLH~ 7*Y6N%).D%IJ%ITD%ILJ#77??;<&(ggmmH&=#HH[):C	N?;<

'$
	k  CLHSM)+{ItTOFG%#F+.|[&QMY' 1 1) <= !YCY=YHSM) $, %)G$'99IENVnPT1UVV * 	(L&..rvvs;K!,!2!2:!>Hh +-''--*A'NK #->I!?@!gdC !C+
 ((*.#,"3'"-i# !3<f U +OOM$5$5i$@A +/ +67+>K+"3'7 )> OO&'.4X]]_d.ST.SsXc].SMT8$$G KB Us   	K*K/c                    X;   ae  X    nU H[  nXAS   ;   a?  US   U   S   n[        U[        5      (       a  [        XSU   5        OX4   nXQS   U   S'   MJ  [        X4   S9US   U'   M]     U$ )z3Override default values in the schema if necessary.
propertiesdefault)r   )
isinstancerQ   r   )r-   r9   r.   defaultsr   new_defaultss         r=   r(   r(      s     )C\**%l3C8ClD11$\C=A#+=L7C|$S))4,0,G|$S)  M    c           	        SnSn UR                  S5      u  pg[        R                  R                  X5      n[        R                  R                  XU-   5      n	U(       a=  [        R                  R                  U5      (       d   [        R                  " U5        U	$ U	$ ! [         a    [
        R                  " SXA-  5      Sef = f! [         a*  n
[
        R                  " SXQ[        U
5      4-  5      SeSn
A
ff = f)a  
Returns the local file system path for a schema name in the given root
directory. This function can be used to filed user overrides in addition to
schema files. If the `make_dirs` flag is set to `True` it will create the
parent directory for the calculated path if it does not exist.
zSettings not found (%s)z Failed writing settings (%s): %sr   r   Nr   )
rW   r   r   r   r&   r   r"   r    makedirsr'   )root_dirr-   	make_dirsrr   r0   write_errorpackage_dirplugin
parent_dirr   r:   s              r=   r!   r!     s     /N4KI)//4WW\\(8
ww||J(:; 
33	TKK
# K4K  ImmC!=>DHI  	T--[Q3H%HItS	Ts$   AB  C  %C
C<%C77C<c           	        0 Sp![         R                  R                  U S5      n[        / [	        [         R                  R                  US5      5      Q[	        [         R                  R                  US5      5      Q5      nU[         R                  R                  U S5      [         R                  R                  U S5      /-  nU H  n[         R                  R                  U5      (       d  M)  [        USS9 n UR                  S	5      (       a  [        R                  " U5      nO[        R                  " U5      nUR                  5        H!  u  p[        UR                  U0 5      U	5        M#     S
S
S
5        M     [        SS9nUR!                  S5      R                  5        H!  u  p[        UR                  U0 5      U	5        M#     X4$ ! [         a  n
U
n S
n
A
NmS
n
A
ff = f! , (       d  f       GM2  = f)zGet overrides settings from `app_settings_dir`.

The ordering of paths is:
- {app_settings_dir}/overrides.d/*.{json,json5} (many, namespaced by package)
- {app_settings_dir}/overrides.{json,json5} (singleton, owned by the user)
 zoverrides.dz*.jsonz*.json5zoverrides.jsonzoverrides.json5r   r   z.json5N	labconfig)config_dir_namedefault_setting_overrides)r   r   r   rp   r   r    r#   endswithrM   r%   r$   itemsr   
setdefaultr&   r
   get)app_settings_dirr.   erroroverrides_dall_override_pathsoverrides_pathr8   path_overrides	plugin_idconfigr:   cms               r=   _get_overridesr     s    2u'',,/?K  	
277<<X67	
277<<Y78	
 
%'78
%'89 
 -ww~~n--.73s!**844%*ZZ_N%)YYs^N)7)=)=)?%I$Y%9%9)R%H&Q *@ 43	 -" 
{	3BVV$?@FFH	--i<fE I    43s1   9G(;A8G
G%G G( G%%G((
G8	c           	         0 n/ n	Uc  [        U 5      u  pJU(       aA  [        XXE5      u  pUb  U" U5      n[        X#U5      nUR                  S5      /n	X;US.UEnX4$ [	        UUUUUUS9u  pSU0nX4$ )a  
Get settings.

Parameters
----------
app_settings_dir:
    Path to applications settings.
schemas_dir: str
    Path to schemas.
settings_dir:
    Path to settings.
schema_name str, optional
    Schema name. Default is "".
overrides: dict, optional
    Settings overrides. If not provided, the overrides will be loaded
    from the `app_settings_dir`. Default is None.
labextensions_path: list, optional
    List of paths to federated labextensions containing their own schema files.
translator: Callable[[Dict], Dict] or None, optional
    Translate a schema. It requires the schema dictionary and returns its translation

Returns
-------
tuple
    The first item is a dictionary with a list of setting if no `schema_name`
    was provided (only the ids if `ids_only=True`), otherwise it is a dictionary
    with id, raw, scheme, settings and version keys.
    The second item is a list of warnings. Warnings will either be a list of
    i) strings with the warning messages or ii) `None`.
rB   r_   )r/   rs   rt   rA   )r   r>   rU   rl   r   )r   r,   rR   r-   r.   r/   rs   rt   resultrv   _errorr9   r<   r~   r   s                  r=   get_settingsr   R  s    P FH*+;<	%k	^!'F*<fM!%%i01#'[][  #11!#
 
 r   c                   [         R                  " U5      n[        XXES9u  px[        U5      n	U	R	                  U5        [        XS[        5      n
[        U
SSS9 nUR                  U5        SSS5        g! , (       d  f       g= f)a  
Save ``raw_settings`` settings for ``schema_name``.

Parameters
----------
schemas_dir: str
    Path to schemas.
settings_dir: str
    Path to settings.
schema_name str
    Schema name.
raw_settings: str
    Raw serialized settings dictionary
overrides: dict
    Settings overrides.
labextensions_path: list, optional
    List of paths to federated labextensions containing their own schema files.
rf   Twr   r   N)	rM   rN   r>   r)   rP   r!   rE   r#   write)r,   rR   r-   raw_settingsr.   r/   payloadr9   r4   rT   r   r8   s               r=   save_settingsr     sv    4 kk,'G )IF &!Iw D2DED	dC'	*c		, 
+	*	*s   A66
Bc                  Z   ^  \ rS rSrSr S             SU 4S jjjrSS jrSrU =r$ )	SchemaHandleri  z6Base handler for handler requiring access to settings.c                   > [         T	U ]  " S0 UD6  SnU(       d  [        U5      u  pWXPl        Xl        X l        X0l        X@l        U(       a  SnU R                  R                  X5        gg)zInitialize the handler.NzFailed loading overrides: %srb   )
super
initializer   r.   r   r,   rR   r/   logrB   )
selfr   r,   rR   r/   r.   kwargsr   overrides_warning	__class__s
            r=   r   SchemaHandler.initialize  si     	$V$-.>?I" 0&("4 >HH.6 r   c           	         [        U R                  U R                  U R                  [        U R
                  U R                  S9u  pUR                  S0 5      R                  S5      =(       d    [        nUS:X  a  [        n[        U5      (       d  U[        :w  a  [        nU$ ! [        R                   a)  nSnU R                  R                  XC5        0 n SnANSnAff = f)a	  
Get the current locale as specified in the translation-extension settings.

Returns
-------
str
    The current locale string.

Notes
-----
If the locale setting is not available or not valid, it will default to jupyterlab_server.translation_utils.DEFAULT_LOCALE.
)r-   r.   r/   z4Missing or misshapen translation settings schema:
%sNrA   localer   )r   r   r,   rR   r   r.   r/   r   r"   r   rB   r   r   r   r   r   )r   rA   r4   r:   schema_warningcurrent_locales         r=   get_current_locale SchemaHandler.get_current_locale  s    	&%%  !!,..#'#:#:KH "j"599(CQzY&'N~..>_3T+N }} 	TNHH^/H		s   AB$ $C!8CC!)r   r/   r.   r,   rR   ra   )r   r'   r,   r'   rR   r'   r/   list[str] | Noner.   dict[str, Any] | Noner   r   returnNone)r   r'   )	__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes____classcell__)r   s   @r=   r   r     sc    @ ,077 7 	7
 -7 )7 7 
7 70" "r   r   )
r,   r'   r-   r'   r.   dict[str, Any]r/   r   r   tuple[dict[str, Any], str])rR   r'   r-   r'   r9   r   r   r   )r,   r'   r-   r'   r   r'   )r   NNF)r,   r'   rR   r'   r.   r   rr   r'   r/   r   rs   r   rt   boolr   ztuple[list[Any], list[Any]])r-   r'   r9   r   r.   r   r   r   )Fr   )
r   r'   r-   r'   r   r   rr   r'   r   r'   )r   r'   r   r   )r   NNNF)r   r'   r,   r'   rR   r'   r-   r'   r.   r   r/   r   rs   r   rt   r   r   z tuple[dict[str, Any], list[Any]]ra   )r,   r'   rR   r'   r-   r'   r   r'   r.   r   r/   r   r   r   )(r   
__future__r   r$   r   r   typingr   rM   
jsonschemar   r)   r   jupyter_serverr   rG   jupyter_server.base.handlersr	   &jupyter_server.services.config.managerr
   r   tornador   translation_utilsr   r   r   r   r   rE   r>   rU   r+   r   r(   r!   r   r   r   r   rb   r   r=   <module>r      s   & #  	    3 & $ 3 R   , 222 2 )	2
  2j%P$ +/^%^%^% ^% 	^%
 )^% ^% ^% !^%B,9G. PW #04IL:0n '++/BBB B 	B
 %B )B B B &BV ,0& & &  &  	& 
 &  )&  
& R=J =r   