B
    äŠ<[	  ã               @   sd   d 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
 dd„ ZG dd	„ d	eƒZdS )
z6Manager to read and modify config data in JSON files.
é    N)ÚPY3)Ú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 )ÚBaseJSONConfigManagerz]General 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_name3   s    zBaseJSONConfigManager.file_namec          	   C   s@   |   |¡}tj |¡r8tj|dd}t |¡S Q R X ni S dS )z¡Retrieve 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ÚioÚopenÚjsonÚload)r   r   ÚfilenameÚfr   r   r   Úget6   s
    
zBaseJSONConfigManager.getc          	   C   sV   |   |¡}|  ¡  tr(tj|ddd}n
t|dƒ}| tj||dd W dQ R X dS )z%Store the given config data.
        Úwzutf-8)r   Úwbé   )ÚindentN)r   r   r   r    r!   r"   Údump)r   r   Údatar$   r%   r   r   r   ÚsetC   s    

zBaseJSONConfigManager.setc             C   s$   |   |¡}t||ƒ |  ||¡ |S )z‡Modify 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   ÚupdateP   s    

zBaseJSONConfigManager.updateN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r&   r-   r.   r   r   r   r   r   $   s   r   )r2   r   r    r"   r   Zsixr   Ztraitlets.configr   Ztraitlets.traitletsr   r   r   r   r   r   r   Ú<module>   s   