a
    Ÿ³Ša”  ã                   @   s
  d Z ddlmZm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ZddlZddlZddlmZmZ ddlmZ ddlmZmZmZ ddlmZ G dd	„ d	ƒZG d
d„ dejeƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ G dd„ deƒZ!dS )z—
This module provides user configuration file management features for Spyder.

It is based on the ConfigParser module present in the standard library.
é    )Úprint_functionÚunicode_literalsN)Úget_conf_pathÚget_module_source_path)Úconfigparser)Úis_text_stringÚPY2Úto_text_string)Úcheck_versionc                   @   s   e Zd ZdS )Ú	NoDefaultN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú1lib/python3.9/site-packages/spyder/config/user.pyr   #   s   r   c                       sL   e Zd ZdZ‡ fdd„Zdd„ Z‡ fdd„Zdd	„ Zd
d„ Zdd„ Z	‡  Z
S )ÚDefaultsConfigzM
    Class used to save defaults to a file and as UserConfig base class.
    c                    sZ   t rtt| ƒ ¡  ntt| ƒjdd || _|| _t t | j¡¡sVt	 
t | j¡¡ dS )zU
        Class used to save defaults to a file and as UserConfig base class.
        N)Zinterpolation)r   Úsuperr   Ú__init__Ú_nameÚ_pathÚospÚisdirÚdirnameÚosÚmakedirs)ÚselfÚnameÚpath©Ú	__class__r   r   r   /   s    zDefaultsConfig.__init__c                 C   sê   | j rX| d tj¡¡ | j  ¡ D ]*\}}t|ƒ dd¡}| d ||¡¡ q"| d¡ | jD ]†}| d |¡¡ | j|  ¡ D ]X\}}|dkr’q€|dus¦| j	| j
krÈt|ƒ}| dd¡}d ||f¡}| d |¡¡ q€| d¡ q^dS )	zu
        Write method for Python 2.

        The one from configparser fails for non-ascii Windows accounts.
        z[{}]
Ú
z
	z{} = {}
r   Nz = z{}
)Ú	_defaultsÚwriteÚformatÚcpZDEFAULTSECTÚitemsÚstrÚreplaceÚ	_sectionsZ_optcreZOPTCREr	   Újoin)r   ÚfpÚkeyÚvalueZvalue_plus_end_of_lineÚsectionr   r   r   Ú_write>   s"    

zDefaultsConfig._writec                    sV   |   |¡s|  |¡ t|ƒs$t|ƒ}|r>d |||¡}t|ƒ tt| ƒ |||¡ dS )zSet method.z[{}][{}] = {}N)	Úhas_sectionÚadd_sectionr   Úreprr#   Úprintr   r   Úset)r   r-   Úoptionr,   ÚverboseÚtextr   r   r   Ú_set[   s    

zDefaultsConfig._setc                    s˜   ˆ   ¡ }‡ fdd„}z||ƒ W nr ty’   z*t |¡rDt |¡ t d¡ ||ƒ W n4 tyŒ } zt	dƒ t	|ƒ W Y d}~n
d}~0 0 Y n0 dS )z*Save config into the associated .ini file.c                    sN   t j| ddd*}tr"ˆ  |¡ n
ˆ  |¡ W d   ƒ n1 s@0    Y  d S )NÚwúutf-8©Úencoding)ÚioÚopenr   r.   r"   )ÚfpathÚ
configfile©r   r   r   Ú_write_filem   s    z)DefaultsConfig._save.<locals>._write_filegš™™™™™©?zOFailed to write user configuration file to disk, with the exception shown belowN)
Úget_config_fpathÚEnvironmentErrorr   Úisfiler   ÚremoveÚtimeÚsleepÚ	Exceptionr2   )r   r>   rA   Úer   r@   r   Ú_savei   s    	


zDefaultsConfig._savec                 C   s   | j }t |d | j¡¡}|S )z7Return the ini file where this configuration is stored.z{}.ini)r   r   r)   r#   r   )r   r   Zconfig_filer   r   r   rB   †   s    zDefaultsConfig.get_config_fpathc                 C   s6   |D ],\}}|D ]}|| }| j |||dd qqdS )z8Set default values and save to defaults folder location.F)r5   N)r7   )r   Údefaultsr-   Úoptionsr4   Ú	new_valuer   r   r   Úset_defaultsŒ   s    zDefaultsConfig.set_defaults)r   r   r   Ú__doc__r   r.   r7   rJ   rB   rN   Ú__classcell__r   r   r   r   r   *   s   r   c                       s:  e Zd ZdZdZdA‡ fdd„	Zedd	„ ƒZed
d„ ƒZedd„ ƒZ	dd„ Z
edd„ ƒZdBdd„Zdd„ Zdd„ Zdd„ ZdCdd„Zdd„ Zdd„ ZdDd d!„ZdEd"d#„ZdFd$d%„ZdGd&d'„ZdHd)d*„ZdId+d,„ZdJd-d.„Zd/d0„ Zd1d2„ Zef‡ fd3d4„	Zd5d6„ ZdKd7d8„Z ‡ fd9d:„Z!‡ fd;d<„Z"d=d>„ Z#d?d@„ Z$‡  Z%S )LÚ
UserConfigaB  
    UserConfig class, based on ConfigParser.

    Parameters
    ----------
    name: str
        Name of the config
    path: str
        Configuration file will be saved in path/%name%.ini
    defaults: {} or [(str, {}),]
        Dictionary containing options *or* list of tuples (sec_name, options)
    load: bool
        If a previous configuration file is found, load will take the values
        from this existing file, instead of using default values.
    version: str
        version of the configuration file in 'major.minor.micro' format.
    backup: bool
        A backup will be created on version changes and on initial setup.
    raw_mode: bool
        If `True` do not apply any automatic conversion on values read from
        the configuration.
    remove_obsolete: bool
        If `True`, values that were removed from the configuration on version
        change, are removed from the saved configuration file.

    Notes
    -----
    The 'get' and 'set' arguments number and type differ from the overriden
    methods. 'defaults' is an attribute and not a method.
    ÚmainNTFc
                    s  t t| ƒj||d || _|  |¡| _|| _|r4dnd| _|| _|	| _	t
dƒ| _d| _d| _d| _d| _|  |¡| _|r€|  ¡  |r|  ¡ }
|  |
¡ |  |¡}|| _|  | j¡ |  |¡|  |¡kr|rÚ| j|d | j|d	 |rô|  |¡ | j|d
d |du r|  ¡  dS )z(UserConfig class, based on ConfigParser.©r   r   é   r   ÚspyderrK   Zbackupsz.bak©Úversion)Úold_versionF©ÚsaveN)r   rQ   r   Ú_loadÚ_check_versionÚ_versionÚ_backupÚ_rawÚ_remove_obsoleteÚ_external_pluginr   Z_module_source_pathÚ_defaults_folderÚ_backup_folderÚ_backup_suffixÚ_defaults_name_prefixÚ_check_defaultsrK   Ú_make_backupÚget_previous_config_fpathÚ_load_from_iniÚget_versionÚ_old_versionÚ_save_new_defaultsÚ_get_minor_versionÚapply_configuration_patchesÚ_remove_deprecated_optionsÚset_versionÚset_as_defaults)r   r   r   rK   ÚloadrW   ÚbackupÚraw_modeÚremove_obsoleteÚexternal_pluginZprevious_fpathrX   r   r   r   r   ¸   s@    


ÿ

zUserConfig.__init__c                 C   s   | d|   d¡… S )z3Return the 'major.minor' components of the version.NÚ.)ÚrfindrV   r   r   r   rm   ñ   s    zUserConfig._get_minor_versionc                 C   s   | d|   d¡… S )z,Return the 'major' component of the version.Nrw   )ÚfindrV   r   r   r   Ú_get_major_versionö   s    zUserConfig._get_major_versionc                 C   s.   t  d| ¡}| dur*|du r*td | ¡ƒ‚| S )z'Check version is compliant with format.z^(\d+).(\d+).(\d+)$NzDVersion number {} is incorrect - must be in major.minor.micro format)ÚreÚmatchÚ
ValueErrorr#   )rW   Zregex_checkr   r   r   r\   û   s    ÿzUserConfig._check_versionc                 C   s¦   |du r| j i fg}nrt|tƒr.| j |fg}nZt|tƒr€|D ]@\}}t|ƒsPJ ‚t|tƒs^J ‚| ¡ D ]\}}t|ƒsfJ ‚qfq<ntdƒ‚|| _|dur¢| jdd |S )z7Check if defaults are valid and update defaults values.Nz.`defaults` must be a dict or a list of tuples!FrY   )	ÚDEFAULT_SECTION_NAMEÚ
isinstanceÚdictÚlistr   r%   r}   rK   Úreset_to_defaults)r   rK   ÚsecrL   ÚoptÚ_r   r   r   rf     s    

zUserConfig._check_defaultsc                 C   s4   |du r| j }nt|ƒs tdƒ‚t|ƒs0tdƒ‚|S )zCheck section and option types.Nz#Argument 'section' must be a stringz"Argument 'option' must be a string)r~   r   ÚRuntimeError)Úclsr-   r4   r   r   r   Ú_check_section_option  s    z UserConfig._check_section_optionc                 C   s^   |   ¡ }| j||d}tj |¡}t |¡s6t |¡ zt 	||¡ W n t
yX   Y n0 dS )a  
        Make a backup of the configuration file.

        If `old_version` is `None` a normal backup is made. If `old_version`
        is provided, then the backup was requested for minor version changes
        and appends the version number to the backup file.
        )rW   rX   N)rB   Úget_backup_fpath_from_versionr   r   r   r   r   r   ÚshutilZcopyfileÚIOError)r   rW   rX   r>   Zfpath_backupr   r   r   r   rg   *  s    ÿ

zUserConfig._make_backupc                 C   s¨   z‚t rrt |¡r€z<tj|dd}|  |¡ W d  ƒ n1 s@0    Y  W q€ tyn   d|f}t|ƒ Y q€0 n| j|dd W n  t	j
y¢   d}t|ƒ Y n0 dS )z;Load config from the associated .ini file found at `fpath`.r9   r:   NzFailed reading filez*Warning: File contains no section headers.)r   r   rD   r<   r=   Zreadfpr‹   r2   Úreadr$   ZMissingSectionHeaderError)r   r>   r?   Z
error_textr   r   r   ri   ?  s    
,zUserConfig._load_from_inic                 C   s0   t  ¡ }|  |¡\}}| t ||d ¡¡ |S )zRead old defaults.z.ini)r$   ÚConfigParserÚ#get_defaults_path_name_from_versionrŒ   r   r)   )r   rX   Úold_defaultsr   r   r   r   r   Ú_load_old_defaultsR  s    zUserConfig._load_old_defaultsc                 C   s<   |   ¡ \}}t||d}t | ¡ ¡s8| |¡ | ¡  dS )zSave new defaults.rS   N)rŽ   r   r   rD   rB   rN   rJ   )r   rK   r   r   Únew_defaultsr   r   r   rl   Y  s
    
zUserConfig._save_new_defaultsc           
      C   s‚   |   |¡}|D ]n\}}|D ]`}|| }z| ||¡}	W n tjtjfyT   d}	Y n0 |	du sjt|ƒ|	kr|  ||||¡ qqdS )z*Update defaults after a change in version.N)r   Úgetr$   ÚNoSectionErrorÚNoOptionErrorr	   r7   )
r   rK   rX   r5   r   r-   rL   r4   rM   Zold_valr   r   r   Ú_update_defaultsa  s    

zUserConfig._update_defaultsc              
   C   s˜   |   |¡}| ¡ D ]€}|j|| jdD ]j\}}|  ||¡tu r&z2|  ||¡ t| j|| jdƒdkrn|  |¡ W q& t	j
yŽ   |  |¡ Y q&0 q&qdS )zX
        Remove options which are present in the .ini file but not in defaults.
        ©Úrawr   N)r   Úsectionsr%   r_   Úget_defaultr   Úremove_optionÚlenÚremove_sectionr$   r“   )r   rX   r   r-   r4   r…   r   r   r   ro   o  s    
z%UserConfig._remove_deprecated_optionsc                 C   s   |   ¡ S )z1Return the last configuration file used if found.©rB   r@   r   r   r   rh   €  s    z$UserConfig.get_previous_config_fpathc                 C   s   |   ¡ S )zˆ
        Return the configuration path for given version.

        If no version is provided, it returns the current file path.
        r   ©r   rW   r   r   r   Úget_config_fpath_from_version„  s    z(UserConfig.get_config_fpath_from_versionc                 C   sZ   |   ¡ }t t |¡| j¡}t |t |¡¡}|du rFd || j¡}nd ||| j¡}|S )zþ
        Get backup location based on version.

        `old_version` can be used for checking compatibility whereas `version`
        relates to adding the version to the file name.

        To be overridden if versions changed backup location.
        Nú{}{}ú{}-{}{})rB   r   r)   r   rc   Úbasenamer#   rd   )r   rW   rX   r>   r   Z	new_fpathÚbackup_fpathr   r   r   r‰   Œ  s    	ÿz(UserConfig.get_backup_fpath_from_versionc                 C   sT   |r|n| j }t t |  ¡ ¡| j¡}d | j| j|¡}t 	|¡sLt
 |¡ ||fS )zz
        Get defaults location based on version.

        To be overridden if versions changed defaults location.
        z{}-{}-{})r]   r   r)   r   rB   rb   r#   re   r   r   r   r   )r   rX   rW   Zdefaults_pathr   r   r   r   rŽ   Ÿ  s    ÿý

z.UserConfig.get_defaults_path_name_from_versionc                 C   s   dS )z–
        Apply any patch to configuration values on version changes.

        To be overridden if patches to configuration values are needed.
        Nr   )r   rX   r   r   r   rn   ²  s    z&UserConfig.apply_configuration_patchesú0.0.0c                 C   s   |   | jd|¡S )z0Return configuration (not application!) version.rW   )r’   r~   rž   r   r   r   rj   ¼  s    zUserConfig.get_versionc                 C   s"   |   |¡}| j| jd||d dS )z-Set configuration (not application!) version.rW   rY   N)r\   r3   r~   )r   rW   rZ   r   r   r   rp   À  s    
zUserConfig.set_versionc                 C   sR   | j D ]:\}}|dks||kr|D ]}|| }|  ||||¡ q"q|rN|  ¡  dS )zReset config to Default values.N)rK   r7   rJ   )r   rZ   r5   r-   rƒ   rL   r4   r,   r   r   r   r‚   Å  s    zUserConfig.reset_to_defaultsc                 C   st   g | _ |  ¡ D ]`}i }| j|| jdD ]6\}}zt |¡}W n ttfyR   Y n0 |||< q&| j  ||f¡ qdS )z%Set defaults from the current config.r–   N)	rK   r˜   r%   r_   ÚastÚliteral_evalÚSyntaxErrorr}   Úappend)r   r-   Zsecdictr4   r,   r   r   r   rq   Ï  s    
zUserConfig.set_as_defaultsc                 C   s@   |   ||¡}| jD ]$\}}||kr||v r|| } q<qt}|S )z‚
        Get default value for a given `section` and `option`.

        This is useful for type checking in `get` method.
        )rˆ   rK   r   )r   r-   r4   rƒ   rL   r,   r   r   r   r™   Ü  s    zUserConfig.get_defaultc              	      sd  |   ||¡}|  |¡s4|tu r*t |¡‚n
|  |¡ |  ||¡sh|tu rVt ||¡‚n|  |||¡ |S t	t
| ƒj||| jd}|  ||¡}t|tƒr¢t |¡}n¾t|tƒr¶t|ƒ}nªt|tƒrÊt|ƒ}n–t|ƒr8tr`zB| d¡}zt |¡}t|ƒrþ|}W n ttfy   Y n0 W n ttfy4   Y n0 n(zt |¡}W n ttfy^   Y n0 |S )ad  
        Get an option.

        Parameters
        ----------
        section: str
            Section name. If `None` is provide use the default section name.
        option: str
            Option name for `section`.
        default:
            Default value (if not specified, an exception will be raised if
            option doesn't exist).
        r–   r9   )rˆ   r/   r   r$   r“   r0   Z
has_optionr”   r3   r   rQ   r’   r_   r™   r   Úboolr¥   r¦   ÚfloatÚintr   r   Údecoder§   r}   ÚUnicodeEncodeErrorÚUnicodeDecodeError)r   r-   r4   Údefaultr,   Údefault_valuerM   r   r   r   r’   í  sF    










zUserConfig.getc                 C   s0   |   ||¡}| jD ]\}}||kr|||< qdS )zö
        Set Default value for a given `section`, `option`.

        If no defaults exist, no default is created. To be able to set
        defaults, a call to set_as_defaults is needed to create defaults
        based on current values.
        N)rˆ   rK   )r   r-   r4   r°   rƒ   rL   r   r   r   Úset_default*  s    zUserConfig.set_defaultc                 C   s²   |   ||¡}|  ||¡}|tu rFtr4t|ƒr4t|ƒ}|}|  |||¡ t|tƒrZt|ƒ}n8t|t	ƒrnt	|ƒ}n$t|t
ƒr‚t
|ƒ}nt|ƒs’t|ƒ}|  ||||¡ |r®|  ¡  dS )ú‚
        Set an `option` on a given `section`.

        If section is None, the `option` is added to the default section.
        N)rˆ   r™   r   r   r   r1   r±   r   r©   rª   r«   r7   rJ   )r   r-   r4   r,   r5   rZ   r°   r   r   r   r3   7  s$    





zUserConfig.setc                    s   t t| ƒ |¡ |  ¡  dS ©z+Remove `section` and all options within it.N)r   rQ   rœ   rJ   )r   r-   r   r   r   rœ   W  s    zUserConfig.remove_sectionc                    s   t t| ƒ ||¡ |  ¡  dS ©zRemove `option` from `section`.N)r   rQ   rš   rJ   )r   r-   r4   r   r   r   rš   \  s    zUserConfig.remove_optionc                 C   s   t  |  ¡ ¡ dS )z&Remove .ini file associated to config.N)r   rE   rB   r@   r   r   r   Úcleanupa  s    zUserConfig.cleanupc                 C   sX   g }|   |  ¡ ¡ | jD ]:}i }|  |¡D ]\}}|  ||¡||< q*| ||f¡ q|S )z«
        Return in list format.

        The format is [('section1', {'opt-1': value, ...}),
                       ('section2', {'opt-2': othervalue, ...}), ...]
        )ri   rB   r(   r%   r’   r¨   )r   r‘   r-   Zsec_datar4   r…   r   r   r   Úto_liste  s    
zUserConfig.to_list)NTNFFFF)NN)F)N)NN)N)N)r¤   )r¤   T)TFN)FT)&r   r   r   rO   r~   r   Ústaticmethodrm   rz   r\   rf   Úclassmethodrˆ   rg   ri   r   rl   r•   ro   rh   rŸ   r‰   rŽ   rn   rj   rp   r‚   rq   r™   r   r’   r±   r3   rœ   rš   rµ   r¶   rP   r   r   r   r   rQ   —   sL      þ9


	











=
 rQ   c                       sH   e Zd Zdd„ Zddd„Zd‡ fdd„	Zd‡ fdd	„	Zdd
d„Z‡  ZS )ÚSpyderUserConfigc                 C   sF   |   ¡ }| jr|g}n|tj tƒ d¡g}|D ]}t |¡r. qBq.|S )z]
        Override method.

        Return the last configuration file used if found.
        ú
spyder.ini)rB   ra   r   r   r)   r   r   rD   )r   r>   Zprevious_pathsr   r   r   rh   y  s    ü
z*SpyderUserConfig.get_previous_config_fpathNc                 C   s@   |du s| j r|  ¡ }n$t|ddƒr4t tƒ d¡}n|  ¡ }|S )z¢
        Override method.

        Return the configuration path for given version.

        If no version is provided, it returns the current file path.
        Nú51.0.0ú<rº   )ra   rB   r
   r   r)   r   )r   rW   r>   r   r   r   rŸ   •  s    
z.SpyderUserConfig.get_config_fpath_from_versionc                    sh   |rNt |ddƒrNd}tj tƒ |¡}|du r<d || j¡}qdd ||| j¡}ntt| ƒ}| 	||¡}|S )zT
        Override method.

        Make a backup of the configuration file.
        r»   r¼   rº   Nr    r¡   )
r
   r   r   r)   r   r#   rd   r   r¹   r‰   )r   rW   rX   r   r>   r£   Úsuper_classr   r   r   r‰   ¦  s    ÿ
ÿz.SpyderUserConfig.get_backup_fpath_from_versionc                    sf   |rHt |ddƒr.d | j|¡}t tƒ d¡}q^tt| ƒ}| |¡\}}ntt| ƒ}| ¡ \}}||fS )zS
        Override method.

        Get defaults location based on version.
        r»   r¼   z{}-{}rK   )	r
   r#   re   r   r)   r   r   r¹   rŽ   )r   rX   r   r   r½   r   r   r   rŽ   »  s    
ÿ

z4SpyderUserConfig.get_defaults_path_name_from_versionc                 C   s^   |   | j|¡ | jrdS |rZt|ddƒrZt|  dd¡ƒ}|turZ| dd¡}|  dd|¡ dS )zg
        Override method.

        Apply any patch to configuration values on version changes.
        Nz44.1.0r¼   Zipython_consolezstartup/run_linesú,z; )	r•   rK   ra   r
   r	   r’   r   r'   r3   )r   rX   Z	run_linesr   r   r   rn   Ï  s    ÿz,SpyderUserConfig.apply_configuration_patches)N)NN)N)N)	r   r   r   rh   rŸ   r‰   rŽ   rn   rP   r   r   r   r   r¹   w  s
   
r¹   c                   @   sÔ   e Zd ZdZdZd.dd„Zdd	„ Zd
d„ Zedd„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZd/dd„Zedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zefd"d#„Zd0d$d%„Zd1d&d'„Zd(d)„ Zd*d+„ Zd,d-„ ZdS )2ÚMultiUserConfigaF  
    Multiuser config class which emulates the basic UserConfig interface.

    This class provides the same basic interface as UserConfig but allows
    splitting the configuration sections and options among several files.

    The `name` is now a `name_map` where the sections and options per file name
    are defined.
    rU   NTFc
              	   C   s  |   |¡| _|| _|| _|| _|| _|| _|r2dnd| _|| _|	| _	i | _
|  ||¡| _||||||d|	dœ| _|D ]J}
| j |
¡}|
|dœ}| j ¡ }| |¡ |  ¡ }|f i |¤Ž| j
|
< qt| j
 | j¡}| |¡}| |j¡}|sò||kr| j
 ¡ D ]\}}| |j¡ qüdS )z2Multi user config class based on UserConfig class.rT   r   F)r   rK   rr   rW   rs   rt   ru   rv   )r   rK   N)Ú_check_name_mapÚ	_name_mapr   r!   r[   r]   r^   Z	_raw_moder`   ra   Ú_configs_mapÚ_get_defaults_for_name_mapZ_config_defaults_mapZ_config_kwargsr’   ÚcopyÚupdateÚget_config_classÚDEFAULT_FILE_NAMErz   rk   r%   ro   )r   Úname_mapr   rK   rr   rW   rs   rt   ru   rv   r   Z
mod_kwargsZ
new_kwargsZconfig_classZdefault_configZ	major_verZmajor_old_verr…   Úconfigr   r   r   r   í  sN    ÿøþ


ÿzMultiUserConfig.__init__c                 C   s2   |   ||¡}| j |d¡}|du r.| j| j }|S )z:Get the correct configuration based on section and option.N)Ú_get_name_from_maprÂ   r’   rÇ   )r   r-   r4   r   Zconfig_valuer   r   r   Ú_get_config  s
    zMultiUserConfig._get_configc           
      C   sb   g }|  ¡ D ]P\}}|D ]B\}}|D ]4}||f}||vrD| |¡ q$d ||¡}	t|	ƒ‚q$qq|S )z,Check `name_map` follows the correct format.z=Different files are holding the same section/option: "{}/{}"!)r%   r¨   r#   r}   )
r   rÈ   Zsections_optionsr…   Úsec_optsr-   rL   r4   Zsec_optZ	error_msgr   r   r   rÀ   )  s    ÿÿzMultiUserConfig._check_name_mapc                 C   s0   | D ]\}}||kr|} q,qt d |¡ƒ‚|S )z+Get the section contents from the defaults.zsection "{}" not found!)r}   r#   )rK   r-   rƒ   rL   r,   r   r   r   Ú_get_section_from_defaults;  s    z*MultiUserConfig._get_section_from_defaultsc                 C   s.   t }| D ] \}}||kr| |t ¡} q*q|S )z/Get the section,option value from the defaults.)r   r’   )rK   r-   r4   r,   rƒ   rL   r   r   r   Ú_get_option_from_defaultsG  s    z)MultiUserConfig._get_option_from_defaultsc                 C   s@   d}t | ƒD ]\}\}}||kr|} q*q|dur<|  |¡ dS )zRemove section from defaults.N)Ú	enumerateÚpop)rK   r-   Z
idx_removeÚidxrƒ   r…   r   r   r   Ú_remove_section_from_defaultsQ  s    z-MultiUserConfig._remove_section_from_defaultsc                 C   s0   | D ]&\}}||kr||v r|  |¡  q,qdS )z$Remove section,option from defaults.N)rÐ   )rK   r-   r4   rƒ   rL   r   r   r   Ú_remove_option_from_defaults]  s
    
z,MultiUserConfig._remove_option_from_defaultsc           	      C   s€   | j  ¡ D ]p\}}| j |¡j}||kr*q
|D ]J\}}||kr.t|ƒdkrV|    S |D ]}||krZ|      S qZq.q
dS )zT
        Search for section and option on the name_map and return the name.
        r   N)rÁ   r%   rÂ   r’   r~   r›   )	r   r-   r4   r   rÌ   Zdefault_sec_namerƒ   rL   r„   r   r   r   rÊ   f  s    z"MultiUserConfig._get_name_from_mapc                 C   sÂ   i }t  |¡}| ¡ D ]¦\}}g }t|ƒdkr8|||< q|D ]v\}}	t|	ƒdkrj|  ||¡}
|  ||¡ n:i }
|	D ]0}|  |||¡}|tur”||
|< |  |||¡ qr| 	||
f¡ q<|||< q|S )z-Split the global defaults using the name_map.r   )
rÄ   Údeepcopyr%   r›   rÍ   rÒ   rÎ   r   rÓ   r¨   )r‡   rK   rÈ   Zname_map_configZdefaults_copyr   rÌ   Zdefault_map_for_namer-   rL   rƒ   r„   Úvalr   r   r   rÃ   x  s6    

ÿÿÿÿ
z*MultiUserConfig._get_defaults_for_name_mapc                 C   s   t S )z#Return the UserConfig class to use.)r¹   r@   r   r   r   rÆ   ¡  s    z MultiUserConfig.get_config_classc                 C   s>   t ƒ }| j ¡ D ] \}}| ¡ D ]}| |¡ q qtt|ƒƒS )z.Return all sections of the configuration file.)r3   rÂ   r%   r˜   Úaddr   Úsorted)r   r˜   r…   rÉ   r-   r   r   r   r˜   ¥  s
    zMultiUserConfig.sectionsc                 C   s>   |   |d¡}|du r | j| j }| |¡r6|j|dS dS dS )z9Return all the items option/values for the given section.N©r-   )rË   rÂ   rÇ   r/   r%   ©r   r-   rÉ   r   r   r   r%   ®  s    
zMultiUserConfig.itemsc                 C   s   |   |d¡}|j|dS )z-Return all the options for the given section.NrØ   )rË   rL   rÙ   r   r   r   rL   ¹  s    zMultiUserConfig.optionsc                 C   s,   |   ||¡}|du r | j| j }| ||¡S )z‚
        Get Default value for a given `section` and `option`.

        This is useful for type checking in `get` method.
        N)rË   rÂ   rÇ   r™   ©r   r-   r4   rÉ   r   r   r   r™   ¾  s    zMultiUserConfig.get_defaultc                 C   s0   |   ||¡}|du r | j| j }|j|||dS )z„
        Get an `option` on a given `section`.

        If section is None, the `option` is requested from default section.
        N)r-   r4   r¯   )rË   rÂ   rÇ   r’   )r   r-   r4   r¯   rÉ   r   r   r   r’   É  s    zMultiUserConfig.getc                 C   s$   |   ||¡}|j|||||d dS )r²   )r-   r4   r,   r5   rZ   N)rË   r3   )r   r-   r4   r,   r5   rZ   rÉ   r   r   r   r3   Ö  s    
ÿzMultiUserConfig.setc                 C   s$   | j  ¡ D ]\}}|j|d q
dS )z&Reset configuration to Default values.rØ   N)rÂ   r%   r‚   )r   r-   r…   rÉ   r   r   r   r‚   à  s    z!MultiUserConfig.reset_to_defaultsc                 C   s   |   |d¡}| |¡ dS r³   )rË   rœ   rÙ   r   r   r   rœ   å  s    zMultiUserConfig.remove_sectionc                 C   s   |   ||¡}| ||¡ dS r´   )rË   rš   rÚ   r   r   r   rš   ê  s    zMultiUserConfig.remove_optionc                 C   s&   | j  ¡ D ]\}}t | ¡ ¡ q
dS )z/Remove .ini files associated to configurations.N)rÂ   r%   r   rE   rB   )r   r…   rÉ   r   r   r   rµ   ï  s    zMultiUserConfig.cleanup)NTNFFFF)NN)FT)N)r   r   r   rO   rÇ   r   rË   rÀ   r·   rÍ   rÎ   rÒ   rÓ   rÊ   r¸   rÃ   rÆ   r˜   r%   rL   r™   r   r’   r3   r‚   rœ   rš   rµ   r   r   r   r   r¿   á  s<   	   þ
2


	



(	


r¿   c                   @   s   e Zd ZdZdS )ÚPluginConfigzPlugin configuration handler.N)r   r   r   rO   r   r   r   r   rÛ   õ  s   rÛ   c                   @   s   e Zd ZdZdd„ ZdS )ÚPluginMultiConfigz4Plugin configuration handler with multifile support.c                 C   s   t S )N)rÛ   r@   r   r   r   rÆ   ü  s    z"PluginMultiConfig.get_config_classN)r   r   r   rO   rÆ   r   r   r   r   rÜ   ù  s   rÜ   )"rO   Z
__future__r   r   r¥   rÄ   r<   r   Úos.pathr   r   r{   rŠ   rF   Zspyder.config.baser   r   Zspyder.py3compatr   r$   r   r   r	   Zspyder.utils.programsr
   r   r   Úobjectr   rQ   r¹   r¿   rÛ   rÜ   r   r   r   r   Ú<module>   s2   m   cj  