
    ^h                     n    S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSKJ	r	J
r
  S rS r " S S\5      rg)	z5Manager to read and modify config data in JSON files.    N)LoggingConfigurable)UnicodeBoolc                     UR                  5        H[  u  p#[        U[        5      (       a&  X ;  a  0 X'   [        X   U5        X   (       d  X	 M>  M@  Uc  U R	                  US5        MW  X0U'   M]     g)zVRecursively update one dictionary using another.

None values will delete their keys.
N)items
isinstancedictrecursive_updatepop)targetnewkvs       R/home/james-whalen/.local/lib/python3.13/site-packages/nbclassic/config_manager.pyr
   r
      sh    
 		a	VY*9I  YJJq$ 1I     c                     [        U R                  5       5       HL  u  p#X!;   d  M  [        U[        5      (       a  [	        X   X   5        X   (       d  X	 M>  M@  X1U   :X  d  MJ  X	 MN     g)z?Recursively remove items from dict that are already in defaultsN)listr   r   r	   remove_defaults)datadefaultskeyvalues       r   r   r   %   s[     4::<(
?%&&	8=9y	 ! SM)	 )r   c                   `    \ rS rSrSr\" S5      r\" S5      rS r	S r
S rSS jrS	 rS
 rSrg)BaseJSONConfigManager3   zGeneral JSON config manager

Deals with persisting/storing config in a json file with optionally
default values in a {section_name}.d directory.
.Tc                      [         R                  " U R                  S5        g! [         a)  nUR                  [        R
                  :w  a  e  SnAgSnAff = f)z,Will try to create the config_dir directory.i  N)osmakedirs
config_dirOSErrorerrnoEEXIST)selfes     r   ensure_config_dir_exists.BaseJSONConfigManager.ensure_config_dir_exists=   sA    	KK/ 	ww%,,& '	s   !$ 
AAAc                 \    [         R                  R                  U R                  US-   5      $ )zPReturns the json filename for the section_name: {config_dir}/{section_name}.jsonz.jsonr   pathjoinr    r$   section_names     r   	file_nameBaseJSONConfigManager.file_nameE   s     ww||DOO\'-ABBr   c                 \    [         R                  R                  U R                  US-   5      $ )zNReturns the directory name for the section name: {config_dir}/{section_name}.dz.dr)   r,   s     r   	directoryBaseJSONConfigManager.directoryI   s     ww||DOO\$->??r   c                 D   U(       a  U R                  U5      /O/ nU R                  (       aQ  [        R                  R	                  U R                  U5      S5      n[        [        R                  " U5      5      U-   nU R                  R                  SUSR	                  U5      5        0 nU H\  n[        R                  R                  U5      (       d  M)  [        USS9 n[        U[        R                  " U5      5        SSS5        M^     U$ ! , (       d  f       Mq  = f)zRetrieve the config data for the specified section.

Returns the data as a dictionary, or an empty dictionary if the file
doesn't exist.

When include_root is False, it will not read the root .json file,
effectively returning the default values.
z*.jsonz(Paths used for configuration of %s: 
	%sz
	utf-8encodingN)r.   read_directoryr   r*   r+   r1   sortedgloblogdebugisfileopenr
   jsonload)r$   r-   include_rootpathspatternr   r*   fs           r   getBaseJSONConfigManager.getM   s     3?-.Bggll4>>,#?JG 499W-.6EC\SYS^S^_dSefDww~~d##$1Q$T499Q<8 21   21s   !!D
D	c                 d   U R                  U5      nU R                  5         U R                  (       a1  [        R                  " U5      nU R                  USS9n[        X$5        [        R                  " USS9n[        USSS9nU   UR                  U5        SSS5        g! , (       d  f       g= f)	z%Store the given config data.
        F)r@      )indentwr4   r5   N)r.   r&   r7   copydeepcopyrD   r   r>   dumpsr=   write)r$   r-   r   filenamer   json_contentrC   s          r   setBaseJSONConfigManager.setg   s     >>,/%%'==&Dxx5xAHD+ zz$q131GGL! QQs   B!!
B/c                 `    U R                  U5      n[        X25        U R                  X5        U$ )zwModify the config section by recursively updating it with new_data.

Returns the modified config data as a dictionary.
)rD   r
   rP   )r$   r-   new_datar   s       r   updateBaseJSONConfigManager.update{   s+    
 xx%($r    N)T)__name__
__module____qualname____firstlineno____doc__r   r    r   r7   r&   r.   r1   rD   rP   rT   __static_attributes__rV   r   r   r   r   3   s;     J$ZNC@4"(r   r   )r[   r"   r9   r>   r   rJ   traitlets.configr   traitlets.traitletsr   r   r
   r   r   rV   r   r   <module>r_      s8    ;
    	  0 -*"P/ Pr   