
    Xh                        d Z ddlmZ ddlZddlZddlZddlZddlZddlZ	ddl
mZ ddlmZmZ eee	j"                  f   Zd
dZddZ G d d	e      Zy)z5Manager to read and modify config data in JSON files.    )annotationsN)LoggingConfigurable)BoolUnicodec                    |j                         D ]Q  \  }}t        |t              r"|| vri | |<   t        | |   |       | |   r4| |= 8|| j	                  |d       M|| |<   S y)z^Recursively update one dictionary using another.

    None values will delete their keys.
    N)items
isinstancedictrecursive_updatepop)targetnewkvs       Q/var/www/html/myenv/lib/python3.12/site-packages/jupyter_server/config_manager.pyr   r      sq    
 		 1aq	VAY*!91IYJJq$ F1I    c                    t        | j                               D ]B  \  }}||v st        |t              rt	        | |   ||          | |   r3| |= 7|||   k(  s@| |= D y)z?Recursively remove items from dict that are already in defaultsN)listr   r	   r
   remove_defaults)datadefaultskeyvalues       r   r   r   )   sf     4::<( 
U(?%&S	8C=9CyS	(3-'Ir   c                  b    e Zd ZdZ ed      Z ed      ZddZddZ	ddZ
dddZddZdd	Zy
)BaseJSONConfigManagerzGeneral JSON config manager

    Deals with persisting/storing config in a json file with optionally
    default values in a {section_name}.d directory.
    .Tc                    	 t        j                  | j                  d       y# t        $ r(}|j                  t        j
                  k7  r Y d}~yd}~ww xY w)z,Will try to create the config_dir directory.i  N)osmakedirs
config_dirOSErrorerrnoEEXIST)selfes     r   ensure_config_dir_existsz.BaseJSONConfigManager.ensure_config_dir_exists@   sA    	KK/ 	ww%,,& '	s    # 	AAAc                \    t         j                  j                  | j                  |dz         S )zPReturns the json filename for the section_name: {config_dir}/{section_name}.jsonz.jsonr   pathjoinr    r$   section_names     r   	file_namezBaseJSONConfigManager.file_nameH   s     ww||DOO\G-CDDr   c                \    t         j                  j                  | j                  |dz         S )zNReturns the directory name for the section name: {config_dir}/{section_name}.dz.dr(   r+   s     r   	directoryzBaseJSONConfigManager.directoryL   s     ww||DOO\D-@AAr   c                   |r| j                  |      gng }| j                  rPt        j                  j	                  | j                  |      d      }t        t        j                  |            |z   }| j                  j                  d|dj	                  |             i }|D ]x  }t        j                  j                  |      s#t        j                  j                  |      sCt        |d      5 }	 t        |t        j                  |             ddd       z |S # t        j                   j"                  $ r | j                  j%                  d|       Y Gw xY w# 1 sw Y   xY w)a  Retrieve 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encodingzInvalid JSON in %s, skippingN)r-   read_directoryr   r)   r*   r/   sortedgloblogdebugisfilegetsizeopenr   jsonloaddecoderJSONDecodeErrorwarning)r$   r,   include_rootpathspatternr   r)   fs           r   getzBaseJSONConfigManager.getP   s.    3?-.Bggll4>>,#?JG 499W-.6E8KK	

 "$ 	ODww~~d#(=$1 OQO(tyy|<O O	O   <<77 O(()GNOO Os*   3E5D  9EEEEE(	c                \   | j                  |      }| j                          | j                  r4t        j                  |      }| j                  |d      }t        ||       t        j                  |d      }t        |dd      5 }|j                  |       ddd       y# 1 sw Y   yxY w)	zStore the given config data.F)rA      )indentwr1   r2   N)r-   r&   r4   copydeepcopyrE   r   r<   dumpsr;   write)r$   r,   r   filenamer   json_contentrD   s          r   setzBaseJSONConfigManager.setq   s    >>,/%%'==&Dxx5xAHD(+ zz$q1(C'2 	"aGGL!	" 	" 	"s   B""B+c                d    | j                  |      }t        ||       | j                  ||       |S )zModify the config section by recursively updating it with new_data.

        Returns the modified config data as a dictionary.
        )rE   r   rP   )r$   r,   new_datar   s       r   updatezBaseJSONConfigManager.update   s/    
 xx%x(t$r   N)returnNone)r,   strrT   rV   )T)r,   rV   rA   boolrT   dict[str, t.Any])r,   rV   r   t.AnyrT   rU   )r,   rV   rR   rY   rT   rX   )__name__
__module____qualname____doc__r   r    r   r4   r&   r-   r/   rE   rP   rS    r   r   r   r   6   s<     J$ZNEBB""r   r   )r   StrDictr   r_   rT   rU   )r   r_   r   r_   rT   rU   )r]   
__future__r   rJ   r"   r6   r<   r   typingttraitlets.configr   traitlets.traitletsr   r   r
   rV   Anyr_   r   r   r   r^   r   r   <module>rf      sP    ; #     	  0 -
sAEEz
*
T/ Tr   