B
    Nc		                 @   sP   d 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 ZG dd deZ	dS )	z6Manager to read and modify config data in JSON files.
    N)LoggingConfigurable)Unicodec             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   7lib/python3.7/site-packages/traitlets/config/manager.pyr      s    
r   c               @   s@   e Zd ZdZedZdd Zdd Zdd Zd	d
 Z	dd Z
dS )BaseJSONConfigManagerzYGeneral JSON config manager

    Deals with persisting/storing config in a json file
    .c          
   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 )Ni  )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 )Nz.json)r   pathjoinr   )r   section_namer   r   r   	file_name1   s    zBaseJSONConfigManager.file_namec          	   C   s>   |  |}tj|r6t|dd}t|S Q R X ni S dS )zRetrieve the config data for the specified section.

        Returns the data as a dictionary, or an empty dictionary if the file
        doesn't exist.
        zutf-8)encodingN)r   r   r   isfileopenjsonload)r   r   filenamefr   r   r   get4   s
    
zBaseJSONConfigManager.getc          	   C   sD   |  |}|   t|ddd}| tj||dd W dQ R X dS )zStore the given config data.wzutf-8)r      )indentN)r   r   r   r    dump)r   r   datar"   r#   r   r   r   setA   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   r*   )r   r   Znew_datar)   r   r   r   updateJ   s    

zBaseJSONConfigManager.updateN)__name__
__module____qualname____doc__r   r   r   r   r$   r*   r+   r   r   r   r   r   "   s   	r   )
r/   r   r    r   Ztraitlets.configr   Ztraitlets.traitletsr   r   r   r   r   r   r   <module>   s   