a
    ø£BeÇ  ã                   @  s¤   d Z ddlm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mZ e	jee	jf Zddddœd	d
„Zddddœdd„ZG dd„ deƒZdS )z5Manager to read and modify config data in JSON files.é    )ÚannotationsN)ÚLoggingConfigurable)ÚBoolÚUnicodeÚStrDictÚ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/jupyter_server/config_manager.pyr      s    
r   )ÚdataÚdefaultsr
   c                 C  s`   t |  ¡ ƒD ]N\}}||v rt|tƒrHt| | || ƒ | | sZ| |= q||| kr| |= qdS )z?Recursively remove items from dict that are already in defaultsN)Úlistr   r   r   Úremove_defaults)r   r   ÚkeyÚvaluer   r   r   r   (   s    
r   c                   @  s†   e Zd ZdZe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d„Zddddœdd„Zddddœdd„ZdS )ÚBaseJSONConfigManagerz�General JSON config manager

    Deals with persisting/storing config in a json file with optionally
    default values in a {section_name}.d directory.
    Ú.Tr   )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 )z,Will try to create the config_dir directory.ií  N)Ú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 )zPReturns the json filename for the section_name: {config_dir}/{section_name}.jsonz.json©r   ÚpathÚjoinr   ©r"   r&   r   r   r   Ú	file_nameG   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.dr'   r*   r   r   r   Ú	directoryK   s    zBaseJSONConfigManager.directoryÚboolzt.Any)r&   Úinclude_rootr
   c              	   C  s°   |r|   |¡gng }| jr@tj |  |¡d¡}tt |¡ƒ| }| j 	d|d |¡¡ i }|D ]L}tj 
|¡r^t|dd�� }t|t |¡ƒ W d  ƒ q^1 s 0    Y  q^|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
	úutf-8©ÚencodingN)r+   Úread_directoryr   r(   r)   r,   ÚsortedÚglobÚlogÚdebugÚisfileÚopenr   ÚjsonÚload)r"   r&   r.   ÚpathsÚpatternr   r(   Úfr   r   r   ÚgetO   s    	ý0zBaseJSONConfigManager.get)r&   r   r
   c                 C  s„   |   |¡}|  ¡  | jr:t |¡}| j|dd�}t||ƒ tj|dd�}t	|ddd��}| 
|¡ W d  ƒ n1 sv0    Y  dS )	zStore the given config data.F)r.   é   )ÚindentÚwr/   r0   N)r+   r$   r2   ÚcopyÚdeepcopyr>   r   r9   Údumpsr8   Úwrite)r"   r&   r   Úfilenamer   Zjson_contentr=   r   r   r   Úsetm   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   rG   )r"   r&   rH   r   r   r   r   Úupdate~   s    

zBaseJSONConfigManager.updateN)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r2   r$   r+   r,   r>   rG   rI   r   r   r   r   r   5   s   r   )rM   Ú
__future__r   rB   r    r4   r9   r   ÚtypingÚtZtraitlets.configr   Ztraitlets.traitletsr   r   ÚDictr%   ÚAnyr   r   r   r   r   r   r   r   Ú<module>   s   