o
    ȷe		                     @   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   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   O/var/www/ideatree/venv/lib/python3.10/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   sJ   z
t | jd W d S  ty$ } z|jtjkr W Y d }~d S d }~ww )Ni  )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 )Nz.json)r   pathjoinr   )r   section_namer   r   r   	file_name1   s   zBaseJSONConfigManager.file_namec                 C   sT   |  |}tj|r(t|dd}t|W  d   S 1 s!w   Y  dS i 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.
        utf-8encodingN)r   r   r   isfileopenjsonload)r   r   filenamefr   r   r   get4   s   
$zBaseJSONConfigManager.getc                 C   sZ   |  |}|   t|ddd}| tj||dd W d   dS 1 s&w   Y  dS )zStore the given config data.wr   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   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   )
r3   r   r#   r   traitlets.configr   traitlets.traitletsr   r   r   r   r   r   r   <module>   s    