o
    tf                     @  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Z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targetStrDictnewreturnNonec                 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)r   r   kv r   f/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/jupyter_server/config_manager.pyr      s   

r   datadefaultsc                 C  s`   t |  D ]'\}}||v r-t|tr$t| | ||  | | s#| |= 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Zdd
dZ	dddZ
ddddZdddZd ddZdS )!BaseJSONConfigManagerzGeneral 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 dS  ty$ } z|jtjkr W Y d}~dS d}~ww )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section_namestrc                 C     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_nameH      zBaseJSONConfigManager.file_namec                 C  r'   )zNReturns the directory name for the section name: {config_dir}/{section_name}.dz.dr(   r+   r   r   r   	directoryL   r-   zBaseJSONConfigManager.directoryinclude_rootbooldict[str, t.Any]c              
   C  s   |r|  |gng }| jr tj| |d}tt|| }| j	d|d| i }|D ]A}tj
|rptj|rpt|dd%}z
t|t| W n tjjy`   | jd| Y nw W d   n1 skw   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encodingzInvalid JSON in %s, skippingN)r,   read_directoryr   r)   r*   r.   sortedgloblogdebugisfilegetsizeopenr   jsonloaddecoderJSONDecodeErrorwarning)r"   r%   r/   pathspatternr   r)   fr   r   r   getP   s,   	zBaseJSONConfigManager.getr   t.Anyc                 C  s   |  |}|   | jrt|}| j|dd}t|| tj|dd}t	|ddd}|
| W d   dS 1 s<w   Y  dS )	zStore the given config data.F)r/      )indentwr2   r3   N)r,   r$   r5   copydeepcopyrE   r   r=   dumpsr<   write)r"   r%   r   filenamer   Zjson_contentrD   r   r   r   setq   s   


"zBaseJSONConfigManager.setnew_datac                 C  s$   |  |}t|| | || |S )zModify the config section by recursively updating it with new_data.

        Returns the modified config data as a dictionary.
        )rE   r   rO   )r"   r%   rP   r   r   r   r   update   s   

zBaseJSONConfigManager.updateN)r	   r
   )r%   r&   r	   r&   )T)r%   r&   r/   r0   r	   r1   )r%   r&   r   rF   r	   r
   )r%   r&   rP   rF   r	   r1   )__name__
__module____qualname____doc__r   r   r   r5   r$   r,   r.   rE   rO   rQ   r   r   r   r   r   6   s    



!r   )r   r   r   r   r	   r
   )r   r   r   r   r	   r
   )rU   
__future__r   rJ   r    r7   r=   r   typingtZtraitlets.configr   Ztraitlets.traitletsr   r   Dictr&   Anyr   r   r   r   r   r   r   r   <module>   s    

