
    Ib                     n    d 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
 d Zd Z G d de      Zy)	z5Manager to read and modify config data in JSON files.    N)LoggingConfigurable)UnicodeBoolc                     |j                         D ]Q  \  }}t        |t              r"|| vri | |<   t        | |   |       | |   r4| |= 8|| j	                  |d       M|| |<   S y)z^Recursively update one dictionary using another.

    None values will delete their keys.
    N)items
isinstancedictrecursive_updatepop)targetnewkvs       9/usr/lib/python3/dist-packages/notebook/config_manager.pyr
   r
      sq    
 		 1aq	VAY*!91IYJJq$ F1I    c                     t        | j                               D ]B  \  }}||v st        |t              rt	        | |   ||          | |   r3| |= 7|||   k(  s@| |= D y)z?Recursively remove items from dict that are already in defaultsN)listr   r   r	   remove_defaults)datadefaultskeyvalues       r   r   r   %   sf     4::<( "
U(?%&S	8C=9CyS	HSM)S	"r   c                   V    e Zd ZdZ ed      Z ed      Zd Zd Z	d Z
ddZd Zd	 Zy
)BaseJSONConfigManagerzGeneral JSON config manager

    Deals with persisting/storing config in a json file with optionally
    default values in a {section_name}.d directory.
    .Tc                     	 t        j                  | j                  d       y# t        $ r(}|j                  t        j
                  k7  r Y d}~yd}~ww xY w)z,Will try to create the config_dir directory.i  N)osmakedirs
config_dirOSErrorerrnoEEXIST)selfes     r   ensure_config_dir_existsz.BaseJSONConfigManager.ensure_config_dir_exists=   sA    	KK/ 	ww%,,& '	s    # 	AAAc                 \    t         j                  j                  | j                  |dz         S )zPReturns the json filename for the section_name: {config_dir}/{section_name}.jsonz.jsonr   pathjoinr   r#   section_names     r   	file_namezBaseJSONConfigManager.file_nameE   s     ww||DOO\'-ABBr   c                 \    t         j                  j                  | j                  |dz         S )zNReturns the directory name for the section name: {config_dir}/{section_name}.dz.dr'   r*   s     r   	directoryzBaseJSONConfigManager.directoryI   s     ww||DOO\$->??r   c                    |r| j                  |      gng }| j                  rPt        j                  j	                  | j                  |      d      }t        t        j                  |            |z   }| j                  j                  d|dj	                  |             i }|D ]W  }t        j                  j                  |      s#t        |d      5 }t        |t        j                  |             ddd       Y |S # 1 sw Y   exY w)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+   include_rootpathspatternr   r(   fs           r   getzBaseJSONConfigManager.getM   s     3?-.Bggll4>>,#?JG 499W-.6EC\SYS^S^_dSef 	9Dww~~d#$1 9Q$T499Q<89 9	9 9 9s    C??D	c                 `   | j                  |      }| j                          | j                  r4t        j                  |      }| j                  |d      }t        ||       t        j                  |d      }t        |dd      }|5  |j                  |       ddd       y# 1 sw Y   yxY w)	z%Store the given config data.
        F)r<      )indentwr0   r1   N)r,   r%   r3   copydeepcopyr@   r   r:   dumpsr9   write)r#   r+   r   filenamer   json_contentr?   s          r   setzBaseJSONConfigManager.setg   s     >>,/%%'==&Dxx5xAHD(+ zz$q131 	"GGL!	" 	" 	"s   	B$$B-c                 d    | j                  |      }t        ||       | j                  ||       |S )zModify the config section by recursively updating it with new_data.

        Returns the modified config data as a dictionary.
        )r@   r
   rK   )r#   r+   new_datar   s       r   updatezBaseJSONConfigManager.update{   s/    
 xx%x(t$r   N)T)__name__
__module____qualname____doc__r   r   r   r3   r%   r,   r.   r@   rK   rN    r   r   r   r   3   s;     J$ZNC@4"(r   r   )rR   r!   r5   r:   r   rE   traitlets.configr   traitlets.traitletsr   r   r
   r   r   rS   r   r   <module>rV      s8    ;
    	  0 -*"P/ Pr   