a
     ³Šay/  ã                   @   s¼   d Z ddlZddlmZ ddlZddlmZmZm	Z	m
Z
mZmZmZmZ ddlmZ ddlmZ ddlmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ edƒZG dd„ deƒZdS )z&Main interpreter entry in Preferences.é    N)ÚQButtonGroupÚ	QGroupBoxÚQInputDialogÚQLabelÚ	QLineEditÚQMessageBoxÚQPushButtonÚQVBoxLayout)Úget_translation)ÚPluginConfigPage)ÚPY2Úto_text_string)Úprograms)Úget_list_conda_envs_cache)Úget_python_executable)Úget_list_pyenv_envs_cacheZspyderc                       sP   e Zd Z‡ fdd„Z‡ fdd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	‡  Z
S )ÚMainInterpreterConfigPagec                    s  t ƒ  ||¡ | j| _d | _d | _d | _tƒ }tƒ }i |¥|¥}|  	d¡}| 
¡ D ]"}|| \}}	||vrR| |¡ qR|  d|¡ |  	dtƒ ¡}
|  	d¡r tƒ }
t |
¡sº|  dtƒ ¡ n|
 d¡rØ|  d|
 dd¡¡ |  	d¡s|  	d¡sú|  dd¡ | |
¡ |  ¡  d S )NÚcustom_interpreters_listÚ
executableÚdefaultúpythonw.exeú
python.exeÚcustom_interpreterú )ÚsuperÚ__init__Úperform_adjustmentsZapply_callbackÚcus_exec_radioÚpyexec_editÚcus_exec_combor   r   Ú
get_optionÚkeysÚappendÚ
set_optionr   ÚospÚisfileÚendswithÚreplaceZ_add_to_custom_interpretersÚ!validate_custom_interpreters_list)ÚselfZpluginÚparentZ	conda_envZ	pyenv_envZenvsÚvalid_custom_listÚenvÚpathÚ_r   ©Ú	__class__© úFlib/python3.9/site-packages/spyder/plugins/maininterpreter/confpage.pyr   !   s8    




ÿ

z"MainInterpreterConfigPage.__init__c                    s   t ƒ  ¡  d S )N)r   Ú
initialize)r)   r/   r1   r2   r3   L   s    z$MainInterpreterConfigPage.initializec              	   C   sà  | j }ttdƒƒ}t|ƒ}ttdƒƒ}| jtdƒd|d| _| jtdƒd|d| _tj	dkrjtd	ƒd
 }nd }t
ƒ }| |¡ | | j¡ | | j¡ |  ¡  | jtdƒ|  d¡d|ddtjd| _| jj | jj¡ | jj | jj¡ | | j¡ | |¡ | jj ¡ | _ttdƒƒ}ttdƒƒ}| d¡ |tdƒddtdƒd}	|tdƒdtdƒd}
ttdƒƒ}|j | j¡ t
ƒ }| |¡ | |	¡ | |
¡ | |¡ | |¡ t
ƒ }| |¡ | |¡ | d¡ |  |¡ d S )NzPython interpreterz5Select the Python interpreter for all Spyder consolesz#Default (i.e. the same as Spyder's)r   )Zbutton_groupz%Use the following Python interpreter:ZcustomÚntZExecutablesz (*.exe)zRecent custom interpretersr   r   T)ÚfiltersZdefault_line_editZadjust_to_contentsZvalidate_callbackzUser Module Reloader (UMR)zŽUMR forces Python to reload modules which were imported when executing a file in a Python or IPython console with the <i>runfile</i> function.z
Enable UMRzumr/enableda{  This option will enable the User Module Reloader (UMR) in Python/IPython consoles. UMR forces Python to reload deeply modules during import when running a Python script using the Spyder's builtin function <b>runfile</b>.<br><br><b>1.</b> UMR may require to restart the console in which it will be called (otherwise only newly imported modules will be reloaded when executing files).<br><br><b>2.</b> If errors occur when re-running a PyQt-based program, please check that the Qt objects are properly destroyed (e.g. you may have to use the attribute <b>Qt.WA_DeleteOnClose</b> on your main window, using the <b>setAttribute</b> method))Zmsg_if_enabledZmsg_warningzShow reloaded modules listzumr/verbosezCPlease note that these changes will be applied only to new consoles)Zmsg_infoz'Set UMR excluded (not reloaded) modulesé   )Zcreate_checkboxr   r.   r   r   Zcreate_radiobuttonÚdef_exec_radior   ÚosÚnamer	   Z	addWidgetr(   Zcreate_file_comboboxr    r   Zis_python_interpreterr   ZtoggledZconnectZsetDisabledZ
setEnabledZ	setLayoutÚcomboboxZlineEditr   ZsetWordWrapr   ZclickedÚset_umr_namelistZ
addStretch)r)   ZnewcbZpyexec_groupZ	pyexec_bgZpyexec_labelr5   Zpyexec_layoutZ	umr_groupZ	umr_labelZumr_enabled_boxZumr_verbose_boxZumr_namelist_btnZ
umr_layoutZvlayoutr1   r1   r2   Ú
setup_pageO   s†    ýý

ù	

ÿüýÿ







z$MainInterpreterConfigPage.setup_pagec                 C   sœ   t  |¡sd S tjd }z,ddg}tj||i d}t| ¡ d ƒ}W n( tyZ   |}Y n t	yl   Y dS 0 ||kr˜t
 | tdƒtdƒ||f t
j¡ dS )	Nr   z-cz&import sys; print(sys.version_info[0]))r,   FÚWarningaI  You selected a <b>Python %d</b> interpreter for the console but Spyder is running on <b>Python %d</b>!.<br><br>Although this is possible, we recommend you to install and run Spyder directly with your selected interpreter, to avoid seeing false warnings and errors due to the incompatible syntax between these two Python versions.T)r$   r%   ÚsysÚversion_infor   Zrun_programÚintZcommunicateÚIOErrorÚ
ValueErrorr   Úwarningr.   ÚOk)r)   ZpyexecZspyder_versionÚargsÚprocZconsole_versionr1   r1   r2   Úwarn_python_compatibility¯   s*    

úöz3MainInterpreterConfigPage.warn_python_compatibilityc                 C   s<  t  | tdƒtdƒtjd |  d¡¡¡\}}|r8t|ƒ}|r(| dd¡ 	d¡}g }g }|D ]j}t
r´tdd	„ |D ƒƒrŽt |¡r²| |¡ qÈt | td
ƒtdƒtj¡ | |¡ q^t |¡r^| |¡ q^d t|ƒt|ƒ t|ƒ ¡}|rt | tdƒtdƒ| tj¡ t | tdƒtdƒtj¡ ng }|  d|¡ dS )z"Set UMR excluded modules name listZUMRz=Set the list of excluded modules as this: <i>numpy, scipy</i>z, zumr/namelistr   Ú ú,c                 s   s   | ]}t |ƒd k V  qdS )é€   N)Úord)Ú.0Úcr1   r1   r2   Ú	<genexpr>ß   ó    z=MainInterpreterConfigPage.set_umr_namelist.<locals>.<genexpr>r=   znYou are working with Python 2, this means that you can not import a module that contains non-ascii characters.z;The following modules are not installed on your machine:
%szKPlease note that these changes will be applied only to new IPython consolesN)r   ZgetTextr.   r   ZNormalÚjoinr    r   r'   Úsplitr   Úallr   Zis_module_installedr"   r   rC   rD   ÚsetZinformationr#   )r)   Z	argumentsZvalidZnamelistZfixed_namelistZnon_ascii_namelistZmodule_nameZinvalidr1   r1   r2   r;   Í   s^    ú
ú
ÿÿûûz*MainInterpreterConfigPage.set_umr_namelistc                 C   s<   |   d¡}g }|D ]}t |¡r| |¡ q|  d|¡ dS )z8Check that the used custom interpreters are still valid.r   N)r    r$   r%   r"   r#   )r)   Úcustom_listr+   Úvaluer1   r1   r2   r(     s    

z;MainInterpreterConfigPage.validate_custom_interpreters_listc                 C   s„   | j  ¡ s€| j ¡ }t |¡}| d¡r4| dd¡}| jj	j
}||vrj||g }| jj	 ¡  | jj	 |¡ | j |¡ |  |¡ dS )z@Perform some adjustments to the page after applying preferences.r   r   N)r7   Z	isCheckedr   Útextr$   Únormpathr&   r'   r   r:   ÚchoicesÚclearZaddItemsZsetTextrG   )r)   r   rT   r1   r1   r2   r     s    





z-MainInterpreterConfigPage.perform_adjustments)Ú__name__Ú
__module__Ú__qualname__r   r3   r<   rG   r;   r(   r   Ú__classcell__r1   r1   r/   r2   r      s   +`8
r   ) Ú__doc__r8   Úos.pathr-   r$   r>   Zqtpy.QtWidgetsr   r   r   r   r   r   r   r	   Zspyder.api.translationsr
   Zspyder.api.preferencesr   Zspyder.py3compatr   r   Zspyder.utilsr   Zspyder.utils.condar   Zspyder.utils.miscr   Zspyder.utils.pyenvr   r.   r   r1   r1   r1   r2   Ú<module>   s   (