
    Jepk                     t   d Z ddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlZddlmZ ddlmZmZmZmZmZ ddlmZmZmZ ddlmZmZ ddlmZmZm Z m!Z! dd	l"m#Z#  ejH                  e%      Z&g d
Z' G d de(      Z)	  e)       Z*y# e+$ r ddl,m-Z-m.Z.  e-j^                         Z0e0 e-ddg      Z0e0jc                  d        e.jd                  dd ed      e.jf                  e.jh                        Z5e5e.jf                  k(  r! e         e.jl                  dd ed              ejn                  d       Y yw xY w)zL
Configuration manager providing access to user/site/project configuration.
    N)AnyDictListOptionalSetTuple)PrefixedTuple)_get_conf_pathsget_conf_pathget_home_dirreset_config_files)CONF_VERSIONDEFAULTSNAME_MAP)ConfigurationKeyConfigurationObserver)
UserConfigMultiUserConfig	NoDefaultcp)check_version)r
   array_builderconsolefind_replacec                   n   e Zd ZdZ	 	 d*dZd Zd Zd Zd+dZd Z	d	 Z
	 d+d
ededee   fdZ	 	 d,d
edee   dee   fdZd Z	 d-dededefdZdededefdZdefdZdefdZdedefdZdedefdZd Zd Zd Zd Zd Zd Ze fdZ!	 	 d.dZ"d  Z#d! Z$d" Z%d/d#Z&d+d$Z'd+d%Z(d+d&Z)d' Z*d( Z+d) Z,y)0ConfigurationManagerzN
    Configuration manager to provide access to user/site/project config.
    Nc                    |r|n| j                         }t        j                  |      st        j                  |       t               }t        }t        |      D ]i  }t        j                  j                  |d      }t        j                  j                  |      sCt        d||dt        ddd      }|j                         }k || _        || _        t!        t"        ||dt        ddd      | _        | j$                  j&                  d   j(                  | _        i | _        i | _        i | _        t3        j4                         | _        g | _        | j;                          y)zV
        Configuration manager to provide access to user/site/project config.
        z
spyder.inispyderFT)pathdefaultsloadversionbackupraw_moderemove_obsoleteN)get_user_config_pathospisdirosmakedirsr   r   reversedr    joinisfiler   r   to_list_parent_active_project_callbackr   r   _user_config_configs_map_old_versionold_spyder_version_plugin_configs_project_configs
_observersweakrefWeakKeyDictionary_observer_map_keys_disabled_options"remove_deprecated_config_locations)	selfparentactive_project_callback	conf_pathr    
conf_pathssite_defaults
conf_fpathsite_configs	            7/usr/lib/python3/dist-packages/spyder/config/manager.py__init__zConfigurationManager.__init__+   sH   
 &y4+D+D+FyyKK $%
 !*- 	6Ii>Jww~~j)("*( !$)	 !, 3 3 5	6 (?%+" !	
 **84AA 	  " !#  #*";";"= "$ 	//1    c                 r    |j                   }|| j                  v r| j                  j                  |d        y y N)CONF_SECTIONr6   pop)r>   plugin_instanceconf_sections      rF   unregister_pluginz&ConfigurationManager.unregister_pluginy   s6    &334///  $$\48 0rH   c                    |j                   }|j                  r|r| j                  |      }|j                  }|r|nd}|j                  }|r|ndg i}|j
                  }|| j                  v rt        dj                  |            t        |||d|dddd	      }t        | j                  dd      r%	 |j                          t        |||d|dddd	      }||f| j                  |<   y	y	y	# t        $ r Y 3w xY w)
zRegister plugin configuration.z0.0.0r   z*A plugin with section "{}" already exists!TF)r    r!   r"   r#   r$   r%   r&   external_pluginz54.0.0<N)rK   	CONF_FILEget_plugin_config_pathr   _CONF_NAME_MAPCONF_DEFAULTSr6   RuntimeErrorformatr   r   r5   cleanupEnvironmentError)r>   plugin_classrN   r    r#   name_mapr!   plugin_configs           rF   register_pluginz$ConfigurationManager.register_plugin~   s2   #00!!l..|<D"//G!(ggG#22H#+x(BH#11Ht333" $--3VL-AC C ,! % $
M T44hD!))+
 !0%#!$)$(
! 3?1ND  .] '3!< ( s   2C* *	C65C6c                     t        j                  t               d      }t        j                  |      rt	        j
                  |       yy)z"Removing old .spyder.ini location.z.spyder.iniN)r(   r-   r   r.   r*   remove)r>   old_locations     rF   r=   z7ConfigurationManager.remove_deprecated_config_locations   s1    xx>::l#IIl# $rH   c                     || j                   }|S || j                  v r| j                  |   \  }}|S | j                   }|S )zM
        Return the active user or project configuration for plugin.
        )r2   r6   )r>   sectionconfigr
   s       rF   get_active_confz$ConfigurationManager.get_active_conf   sX    
 ?&&F  ,,,,,W5IAv
  &&FrH   c                     t               }t        j                  |d      }t        j                  |      st	        j
                  |       |S )z#Return the user configuration path.rd   )r   r(   r-   r)   r*   r+   )r>   	base_pathr    s      rF   r'   z)ConfigurationManager.get_user_config_path   s5    !O	xx	8,yyKKrH   c                     t               }t        j                  |d      }|t        d      t        j                  |d|      }t        j                  |      st        j                  |       |S )z%Return the plugin configuration path.pluginsz&Plugin needs to define `CONF_SECTION`!)r   r(   r-   rW   r)   r*   r+   )r>   plugin_folderrg   r    s       rF   rT   z+ConfigurationManager.get_plugin_config_path   sY    !O	xx	9- GHHxx	9m<yyKKrH   observerrc   optionc                 f   | j                   j                  |i       }||nd}|j                  |t        j                               }||hz  }|||<   || j                   |<   | j                  j                  |i       }|j                  |t        i             }||hz  }|||<   || j                  |<   y)a  
        Register an `observer` object to listen for changes in the option
        `option` on the configuration `section`.

        Parameters
        ----------
        observer: ConfigurationObserver
            Object that conforms to the `ConfigurationObserver` protocol.
        section: str
            Name of the configuration section that contains the option
            :param:`option`
        option: Optional[ConfigurationKey]
            Name of the option on the configuration section :param:`section`
            that the object is going to suscribe to. If None, the observer
            will observe any changes on any of the options of the configuration
            section.
        N	__section)r8   getr9   WeakSetr;   set)r>   rk   rc   rl   section_sets
option_setobserver_section_setssection_sets           rF   observe_configurationz*ConfigurationManager.observe_configuration   s    * **7B7!-;!%%fgoo.?@
xj 
)V#/  $ 7 7 ; ;Hb I+//RAx)4g&,A)rH   c                    || j                   vry| j                   |   }|\||   }| j                  |   }|/|D ]  }||   }|j                  |        |j                  |       y||   }|j                  |       y|D ]3  }||   }| j                  |   }|D ]  }||   }|j                  |        5 | j                   j                  |       y)a`  
        Remove an observer to prevent it to receive further changes
        on the values of the option `option` of the configuration section
        `section`.

        Parameters
        ----------
        observer: ConfigurationObserver
            Object that conforms to the `ConfigurationObserver` protocol.
        section: Optional[str]
            Name of the configuration section that contains the option
            :param:`option`. If None, the observer is unregistered from all
            options for all sections that it has registered to.
        option: Optional[ConfigurationKey]
            Name of the configuration option on the configuration
            :param:`section` that the observer is going to be unsubscribed
            from. If None, the observer is unregistered from all the options of
            the section `section`.
        N)r;   r8   r`   rL   )r>   rk   rc   rl   observer_sectionssection_optionssection_observersoption_observerss           rF   unobserve_configurationz,ConfigurationManager.unobserve_configuration  s   . 4222 33H=/8O $ 8~- 6F'8'@$$++H56 "%%g.#4V#<  ''1, 6"3G"<$(OOG$<!- 6F'8'@$$++H566 ##''1rH   c                 H    | j                   D ]  }| j                  |        y)zV
        Notify all the observers subscribed to all the sections and options.
        N)r8   notify_section_all_observers)r>   rc   s     rF   notify_all_observersz)ConfigurationManager.notify_all_observers1  s%      	7G--g6	7rH   recursive_notificationc                    |r| j                  |       t        |t              rn|rlt        |      }|g k7  r[t        |      }t	        |      dk(  r|d   }| j                  ||      }| j                  |||       |j                  d       |g k7  rZyy|dk(  r| j                  |       y| j                  ||      }| j                  |||       y)a  
        Notify observers of a change in the option `option` of configuration
        section `section`.

        Parameters
        ----------
        section: str
            Name of the configuration section whose option did changed.
        option: ConfigurationKey
            Name/Path to the option that did changed.
        recursive_notification: bool
            If True, all objects that observe all changes on the
            configuration section and objects that observe partial tuple paths
            are notified. For example if the option `opt` of section `sec`
            changes, then the observers for section `sec` are notified.
            Likewise, if the option `(a, b, c)` changes, then observers for
            `(a, b, c)`, `(a, b)` and a are notified as well.
           r   rn   N)_notify_section
isinstancetuplelistlenro   _notify_optionrL   )r>   rc   rl   r   option_listtuple_optionvalues          rF   notify_observersz%ConfigurationManager.notify_observers8  s    , "  )fe$)? v,K#$[1{#q(#/?L,7##G\5A# # $$$W-&1##GVU;rH   r   c                    | j                   j                  |i       }|j                  |t        i             }||f| j                  v rt        j                  d| d|        y t        |      dkD  rt        j                  d| d|        t        |      D ]  }	 |j                  |||        y # t        $ r | j                  |       Y 6w xY w)Nz8Don't send notification to observers of disabled option z in configuration section r   z%Sending notification to observers of z! option in configuration section )r8   ro   rq   r<   loggerdebugr   r   on_configuration_changerW   r|   )r>   rc   rl   r   rz   r{   rk   s          rF   r   z#ConfigurationManager._notify_optionf  s     OO//<,00RAV 6 66LLJ(4WI? !"Q&LL7x @))0	3
 -. 	7H700%H	7   7,,X67s   B22CCc                 f    t        | j                  |      xs g       }| j                  |d|       y )Nrn   )dictitemsr   )r>   rc   section_valuess      rF   r   z$ConfigurationManager._notify_section~  s,    djj17R8G[.ArH   c                 r   | j                   |   }t               }t        j                  |d       |D ]7  }t	        |t
              r|j                  |       %	 | j                  ||       9 |D ]  }	 | j                  ||        y# t        j                  $ r Y jw xY w# t        j                  $ r Y Gw xY w)z?Notify all the observers subscribed to any option of a section.rn   N)	r8   r	   CONFr   r   r   add_pathr   NoOptionError)r>   rc   r{   section_prefixrl   prefixs         rF   r~   z1ConfigurationManager.notify_section_all_observers  s    ??73&g{3& 		F&%(''/))'6:		 % 	F%%gv6	 ''   ## s$   B2B BB B65B6c                 t    t         j                  d| d|        | j                  j                  ||f       y)z0Disable notitications for `option` in `section`.z!Disable notifications for option  option in section N)r   r   r<   appendr>   rc   rl   s      rF   disable_notificationsz*ConfigurationManager.disable_notifications  s=    /x7Ji	
 	%%w&78rH   c                     t         j                  d| d|        	 | j                  j                  ||f       y# t        $ r Y yw xY w)z9Restore notitications for disabled `option` in `section`.z!Restore notifications for option r   N)r   r   r<   r`   
ValueErrorr   s      rF   restore_notificationsz*ConfigurationManager.restore_notifications  sR    /x7Ji	
	""))7F*;< 		s   ; 	AAc                 r    | j                  |      r|| j                  vr|| j                  |<   yy|| _        y)z
        Register configuration with `root_path`.

        Useful for registering project configurations as they are opened.
        N)is_project_rootr7   _site_config)r>   	root_pathrd   s      rF   register_configz$ConfigurationManager.register_config  s?     	* 5 5539%%i0 6 !'DrH   c                 B    | j                   }| j                   r |       S y)z5Return the `root_path` of the current active project.N)r1   )r>   callbacks     rF   get_active_projectz'ConfigurationManager.get_active_project  s#    00((: )rH   c                      y)z;Check if `root_path` corresponds to a valid spyder project.F )r>   r   s     rF   r   z$ConfigurationManager.is_project_root  s    rH   c                     t        j                  |dd      }t        j                  |      st        j                  |       yy)z&Return the project configuration path.z.spyprojrd   N)r(   r-   r)   r*   r+   )r>   project_rootr    s      rF   get_project_config_pathz,ConfigurationManager.get_project_config_path  s0    xxj(;yyKK rH   c                 F    | j                  |      }|j                  |      S )z9Return all the items option/values for the given section.)re   r   r>   rc   rd   s      rF   r   zConfigurationManager.items  s!    %%g.||G$$rH   c                 F    | j                  |      }|j                  |      S )z-Return all the options for the given section.)re   optionsr   s      rF   r   zConfigurationManager.options  s!    %%g.~~g&&rH   c                    | j                  |      }t        |t              rt        |      dk(  r|d   }t        |t              rt|d   }|dd }|d   }|j	                  ||i       }|}	|D ]  }
|	j	                  |
i       }	 |	j	                  |d      }| |}|t
        u rt        j                  ||      |S |j	                  |||      }|S )z
        Get an `option` on a given `section`.

        If section is None, the `option` is requested from default section.
        r   r   r   )rc   rl   defaultN)re   r   r   r   ro   r   r   r   )r>   rc   rl   r   rd   base_optionintermediate_optionslast_option	base_confnext_ptroptr   s               rF   ro   zConfigurationManager.get  s     %%g.fe$V)9AYFfe$ )K#)!B<  *K

R # AI H+ 1#<<R01 LLd3E}i'**67;;  JJwvwJOErH   c                 <   |}t        |t              rM|d   }	|dd }
|d   }| j                  ||	i       }|}|
D ]  }|j                  |i       }|||<   |} |||<   |}|	}| j                  |      }|j	                  |||||       |r| j                  |||       yy)z
        Set an `option` on a given `section`.

        If section is None, the `option` is added to the default section.
        r   r   r   )rc   rl   r   verbosesaveN)r   r   ro   re   rq   r   )r>   rc   rl   r   r   r   r   notificationoriginal_optionr   r   r   r   conf_ptrr   r   rd   s                    rF   rq   zConfigurationManager.set  s     !fe$ )K#)!B<  *K+r:I H+ $#<<R0 (#$
 %*H[!E F%%g.

76" 	 	/!!*@B rH   c                     | j                  |      }t        |t              r4|d   }|dd }|d   }|j                  ||      }|}|D ]  }	||	   }	 ||   S |j                  ||      S )z
        Get Default value for a given `section` and `option`.

        This is useful for type checking in `get` method.
        r   r   r   )re   r   r   get_default)
r>   rc   rl   rd   r   r   r   base_defaultr   r   s
             rF   r   z ConfigurationManager.get_default  s     %%g.fe$ )K#)!B<  *K!--g{CL#H+ )#C=) K((!!'622rH   c                 H    | j                  |      }|j                  |       y)z+Remove `section` and all options within it.N)re   remove_sectionr   s      rF   r   z#ConfigurationManager.remove_section*  s     %%g.g&rH   c                 @   | j                  |      }t        |t              rk|d   }|dd }|d   }| j                  ||      }|}|D ]  }	||	   }	 ||v r7|j	                  |       | j                  |||       | j                  ||       yy|j                  ||       y)zRemove `option` from `section`.r   r   r   N)re   r   r   ro   rL   rq   r   remove_option)
r>   rc   rl   rd   r   r   r   r   r   r   s
             rF   r   z"ConfigurationManager.remove_option/  s    %%g.fe$ )K $*!B<  !*K +6I !H+ )#C=) h&[)+	:%%g{; '
   &1rH   c                     | j                  |      }|j                  |       |r%|| j                  |       y| j                          yy)zReset config to Default values.rc   N)re   reset_to_defaultsr~   r   )r>   rc   r   rd   s       rF   r   z&ConfigurationManager.reset_to_defaultsP  sK    %%g.   1"11':))+	 rH   c                    |j                         }| j                  }|| j                  v r,| j                  |   \  }}|j                  s| j                  }|S || j                  v r,| j                  |   \  }}|j                  s| j                  }|S || j                  j	                         t
        z   v r| j                  }|S t        t        d            )z
        Return the shortcut configuration for global or plugin configs.

        Context must be either '_' for global or the name of a plugin.
        z=Shortcut context must match '_' or the plugin `CONF_SECTION`!)lowerr2   r6   rS   sectionsEXTRA_VALID_SHORTCUT_CONTEXTSr   r
   )r>   contextplugin_namerd   r[   s        rF   _get_shortcut_configz)ConfigurationManager._get_shortcut_config\  s     --/""$...#'#7#7#D L&  ))**   ,,,#'#7#7#@ L&  ))**  **33589 :&&F
  Q  8 9 : :rH   c                 r    | j                  ||      }|j                  d|dz   |j                         z         S )z
        Get keyboard shortcut (key sequence string).

        Context must be either '_' for global or the name of a plugin.
        	shortcuts/)r   ro   r   )r>   r   namer   rd   s        rF   get_shortcutz!ConfigurationManager.get_shortcut|  s6     **7K@zz+w}tzz|'CDDrH   c                 Z    | j                  ||      }|j                  d|dz   |z   |       y)z
        Set keyboard shortcut (key sequence string).

        Context must be either '_' for global or the name of a plugin.
        r   r   N)r   rq   )r>   r   r   keystrr   rd   s         rF   set_shortcutz!ConfigurationManager.set_shortcut  s/     **7K@

;# 4f=rH   c                 N    ddl m} | j                  ||      } ||||||      }|S )z
        Create a Shortcut namedtuple for a widget.

        The data contained in this tuple will be registered in our shortcuts
        preferences page.
        r   )_config_shortcut)spyder.config.guir   r   )r>   actionr   r   r?   r   r   scs           rF   config_shortcutz$ConfigurationManager.config_shortcut  s0     	7""7D1fgtVVD	rH   c              #   ^  K   | j                   j                  d      D ],  \  }}|dk(  rd|vs|j                  dd      \  }}|||f . | j                  j                         D ]B  \  }\  }}|j                  d      }|s|D ]!  \  }}|j                  dd      \  }}|||f # D yw)z Iterate over keyboard shortcuts.r   enableadditional_configurationr   r   N)r2   r   splitr6   )r>   context_namer   r   r   __r]   r   s           rF   iter_shortcutsz#ConfigurationManager.iter_shortcuts  s     $($5$5$;$;K$H 	, L&x')= , 2 23 :tV++	, (,';';'A'A'C 	0#B#]!''4E,1 0(L&$0$6$6sA$>MGT!4//0	0s   ,B-AB-)B-c                     | j                   j                  d       | j                  j                         D ]  \  }\  }}|j                  d        y)z+Reset keyboard shortcuts to default values.r   r   N)r2   r   r6   r   )r>   r   r]   s      rF   reset_shortcutsz$ConfigurationManager.reset_shortcuts  sT    ++K+@'+';';'A'A'C 	A#B#]++K+@	ArH   )NNNrJ   )NN)T)FTTT)NT)-__name__
__module____qualname____doc__rG   rO   r^   r=   re   r'   rT   r   strr   r   rv   r|   r   boolr   r   r   r   r~   r   r   r   r   r   r   r   r   r   ro   rq   r   r   r   r   r   r   r   r   r   r   r   rH   rF   r   r   &   s    =AL2\9
1Of$
" DH#B(=#B'*#B '//?&@#BN :>EI-2*?-2)1#-2 )11A(B-2^7 9=,<"%,<!1,< 26,<\7c 73C 7!70Bs BC 09S 9:J 9	S 	:J 	'%
'
 ,5 < ?C6:B>3*'
2B,@E>0"ArH   r   )QApplicationQMessageBoxSpyderz--no-sandboxzThere was an error while loading Spyder configuration options. You need to reset them for Spyder to be able to launch.

Do you want to proceed?z$Spyder configuration files resetted!)8r   loggingr*   os.pathr    r(   typingr   r   r   r   r   r   r9   spyder.api.utilsr	   spyder.config.baser
   r   r   r   r   spyder.config.mainr   r   r   spyder.config.typesr   r   spyder.config.userr   r   r   r   spyder.utils.programsr   	getLoggerr   r   r   objectr   r   	Exceptionqtpy.QtWidgetsr   r   instanceappsetApplicationNamecriticalYesNoreset_replyinformation_exitr   rH   rF   <module>r
     s7  
  	  8 8  +H H ? ? G I I / 
		8	$! L
A6 L
A^!D 8  ,


!C {Hn56x(&+&&h	 $ 	% 	)K koo%(45	7 BHHQK5s   ?B B-D76D7