a
    õ£Be¯	  ã                   @  sr   d Z ddlm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dd	œd
d„ZG dd„ de	ƒZdS )z6Manager to read and modify config data in JSON files.
é    )ÚannotationsN)ÚAny)ÚLoggingConfigurable)ÚUnicodezdict[Any, Any]ÚNone)ÚtargetÚnewÚreturnc                 C  sl   |  ¡ D ]^\}}t|tƒrH|| vr*i | |< t| | |ƒ | | sf| |= 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)r   r   ÚkÚv© r   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitlets/config/manager.pyr      s    
r   c                   @  sj   e Zd ZdZedƒZddœdd„Zdddœd	d
„Zdddœdd„Zddddœdd„Z	ddddœdd„Z
dS )ÚBaseJSONConfigManagerzYGeneral JSON config manager

    Deals with persisting/storing config in a json file
    Ú.r   )r	   c              
   C  sJ   zt  | jd¡ W n2 tyD } z|jtjkr0‚ W Y d }~n
d }~0 0 d S )Nií  )ÚosÚmakedirsÚ
config_dirÚOSErrorÚerrnoÚEEXIST)ÚselfÚer   r   r   Úensure_config_dir_exists-   s
    z.BaseJSONConfigManager.ensure_config_dir_existsÚstr)Úsection_namer	   c                 C  s   t j | j|d ¡S )Nz.json)r   ÚpathÚjoinr   )r   r   r   r   r   Ú	file_name4   s    zBaseJSONConfigManager.file_namer   c                 C  sV   |   |¡}tj |¡rNt|dd��}t |¡W  d  ƒ S 1 sB0    Y  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.
        úutf-8©ÚencodingN)r"   r   r    ÚisfileÚopenÚjsonÚload)r   r   ÚfilenameÚfr   r   r   Úget7   s
    
*zBaseJSONConfigManager.get)r   Údatar	   c                 C  sX   |   |¡}|  ¡  t|ddd�}|�  tj||dd� W d  ƒ n1 sJ0    Y  dS )zStore the given config data.Úwr#   r$   é   )ÚindentN)r"   r   r'   r(   Údump)r   r   r-   r*   r+   r   r   r   ÚsetD   s
    
zBaseJSONConfigManager.set)r   Únew_datar	   c                 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   r2   )r   r   r3   r-   r   r   r   ÚupdateM   s    

zBaseJSONConfigManager.updateN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r"   r,   r2   r4   r   r   r   r   r   %   s   	r   )r8   Ú
__future__r   r   r(   r   Útypingr   Ztraitlets.configr   Ztraitlets.traitletsr   r   r   r   r   r   r   Ú<module>   s   