B
    9d                 @   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   sp   xj|  D ]^\}}t|trJ|| kr,i | |< t| | | | | sh| |= q
|dkr`| |d q
|| |< q
W 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   d/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/notebook/config_manager.pyr      s    
r   c             C   sd   x^t |  D ]N\}}||krt|trJt| | ||  | | s\| |= q||| kr| |= qW 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   sH   yt | jd W n0 tk
rB } z|jtjkr2 W dd}~X Y nX dS )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   s   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   s    zBaseJSONConfigManager.file_namec             C   s   t j| j|d S )zNReturns the directory name for the section name: {config_dir}/{section_name}.dz.d)r   r!   r"   r   )r   r#   r   r   r   	directoryI   s    zBaseJSONConfigManager.directoryc          
   C   s   |r|  |gng }| jr@tj| |d}tt|| }| j	d|d| i }x@|D ]8}tj
|r`t|dd}t|t| W dQ R X q`W |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
	z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   st   |  |}|   | jr:t|}| j|dd}t|| tj|dd}t	|ddd}| |
| W dQ R X dS )	z%Store the given config data.
        F)r0      )indentwzutf-8)r&   N)r$   r    r'   copydeepcopyr4   r   r.   dumpsr-   write)r   r#   r   filenamer   Zjson_contentr3   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.
        )r4   r   r=   )r   r#   Znew_datar   r   r   r   update{   s    

zBaseJSONConfigManager.updateN)T)__name__
__module____qualname____doc__r   r   r   r'   r    r$   r%   r4   r=   r>   r   r   r   r   r   3   s   
r   )rB   r   r)   r.   r   r8   Ztraitlets.configr   Ztraitlets.traitletsr   r   r   r   r   r   r   r   r   <module>   s   