B
    3\O                 @   s   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Zddlm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)PY3)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   6lib/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errnoZ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_nameH   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   	directoryL   s    zBaseJSONConfigManager.directoryc          
   C   s   |r|  |gng }| jr@tj| |d}tt|| }| j	d|d| i }xB|D ]:}tj
|r`tj|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ioopenr	   jsonload)r   r#   include_rootpathspatternr   r!   fr   r   r   getP   s    	
zBaseJSONConfigManager.getc          	   C   s   |  |}|   | jr:t|}| j|dd}t|| tj|dd}t	r^t
j|ddd}n
t|d}| || W d	Q R X d	S )
z%Store the given config data.
        F)r1      )indentwzutf-8)r&   wbN)r$   r    r'   copydeepcopyr5   r   r/   dumpsr   r-   r.   write)r   r#   r   filenamer   Zjson_contentr4   r   r   r   setj   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.
        )r5   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%   r5   r?   r@   r   r   r   r   r   6   s   
r   )rD   r   r)   r-   r/   r   r:   Zsixr   Ztraitlets.configr   Ztraitlets.traitletsr   r   r	   r   r   r   r   r   r   <module>   s   