U
    -e	                     @  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|| kr*i | |< t| | | | | sf| |= q|dk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   Y/var/www/html/Darija-Ai-Train/env/lib/python3.8/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  sH   zt | jd W n0 tk
rB } z|jtjkr2 W 5 d }~X Y nX 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  sJ   |  |}tj|rBt|dd}t|W  5 Q R  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.
        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  sD   |  |}|   t|ddd}| tj||dd W 5 Q R X 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 )zModify 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   