o
    i                     @   sl   d 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
 dd Zdd ZG d	d
 d
eZdS )z5Manager to read and modify config data in JSON files.    N)LoggingConfigurable)UnicodeBoolc                 C   sl   |  D ]/\}}t|tr$|| vri | |< t| | | | | s#| |= q|du r/| |d q|| |< qdS )z^Recursively update one dictionary using another.

    None values will delete their keys.
    N)items
isinstancedictrecursive_updatepop)targetnewkv r   R/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/notebook/config_manager.pyr      s   

r   c                 C   s`   t |  D ]'\}}||v r-t|tr$t| | ||  | | s#| |= q||| kr-| |= qdS )z?Recursively remove items from dict that are already in defaultsN)listr   r   r   remove_defaults)datadefaultskeyvaluer   r   r   r   %   s   
r   c                   @   sR   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dS )BaseJSONConfigManagerzGeneral JSON config manager

    Deals with persisting/storing config in a json file with optionally
    default values in a {section_name}.d directory.
    .Tc              
   C   sJ   z
t | jd W dS  ty$ } z|jtjkr W Y d}~dS d}~ww )z,Will try to create the config_dir directory.i  N)osmakedirs
config_dirOSErrorerrnoEEXIST)selfer   r   r   ensure_config_dir_exists=   s   z.BaseJSONConfigManager.ensure_config_dir_existsc                 C      t j| j|d S )zPReturns the json filename for the section_name: {config_dir}/{section_name}.jsonz.jsonr   pathjoinr   r   section_namer   r   r   	file_nameE      zBaseJSONConfigManager.file_namec                 C   r!   )zNReturns the directory name for the section name: {config_dir}/{section_name}.dz.dr"   r%   r   r   r   	directoryI   r(   zBaseJSONConfigManager.directoryc              	   C   s   |r|  |gng }| jr tj| |d}tt|| }| j	d|d| i }|D ]&}tj
|rUt|dd}t|t| W d   n1 sPw   Y  q/|S )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encodingN)r'   read_directoryr   r#   r$   r)   sortedgloblogdebugisfileopenr   jsonload)r   r&   include_rootpathspatternr   r#   fr   r   r   getM   s   	zBaseJSONConfigManager.getc                 C   s   |  |}|   | jrt|}| j|dd}t|| tj|dd}t	|ddd}| |
| W d   dS 1 s>w   Y  dS )	z%Store the given config data.
        F)r6      )indentwr*   r+   N)r'   r    r-   copydeepcopyr:   r   r4   dumpsr3   write)r   r&   r   filenamer   json_contentr9   r   r   r   setg   s   


"zBaseJSONConfigManager.setc                 C   s$   |  |}t|| | || |S )zModify the config section by recursively updating it with new_data.

        Returns the modified config data as a dictionary.
        )r:   r   rD   )r   r&   new_datar   r   r   r   update{   s   

zBaseJSONConfigManager.updateN)T)__name__
__module____qualname____doc__r   r   r   r-   r    r'   r)   r:   rD   rF   r   r   r   r   r   3   s    
r   )rJ   r   r/   r4   r   r>   traitlets.configr   traitlets.traitletsr   r   r   r   r   r   r   r   r   <module>   s    