B
    nôb\…2  ã               @   s¸   d Z ddlm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mZ ddlmZ dd	lmZ dd
lmZ G dd„ deƒZdS )zShortcut managementé    )Úprint_functionN)ÚQButtonGroupÚ	QGroupBoxÚQInputDialogÚQLabelÚ	QLineEditÚQMessageBoxÚQPushButtonÚQVBoxLayout)Ú_)ÚGeneralConfigPage)ÚPY2Úis_text_stringÚto_text_string)Úicon_manager)Úget_python_executable)Úprogramsc               @   sj   e Zd ZdZedƒZe d¡Zdd„ Z	dd„ Z
dd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚMainInterpreterConfigPageZmain_interpreterzPython interpreterZpythonc             C   s¬   t  | ||¡ d | _d | _d | _|  dtƒ ¡}|  d¡r>tƒ }t |¡sX|  	dtƒ ¡ n| 
d¡rv|  	d| dd¡¡ |  d¡s¨|  d¡s–|  	dd¡ |  |¡ |  ¡  d S )NÚ
executableÚdefaultzpythonw.exez
python.exeÚcustom_interpreterú )r   Ú__init__Úcus_exec_radioÚpyexec_editÚcus_exec_comboÚ
get_optionr   ÚospÚisfileÚ
set_optionÚendswithÚreplaceÚset_custom_interpreters_listÚ!validate_custom_interpreters_list)ÚselfÚparentÚmainr   © r'   ú=lib/python3.7/site-packages/spyder/plugins/maininterpreter.pyr   "   s"    





z"MainInterpreterConfigPage.__init__c             C   s   t  | ¡ d S )N)r   Ú
initialize)r$   r'   r'   r(   r)   @   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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 interpretersÚcustom_interpreters_listr   T)ÚfiltersZdefault_line_editZadjust_to_contentsz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toggledZconnectZsetDisabledZ
setEnabledZ	setLayoutZcomboboxZlineEditr   ZsetWordWrapr	   ZclickedÚset_umr_namelistZ
addStretch)r$   ZnewcbZpyexec_groupZ	pyexec_bgZpyexec_labelr,   Zpyexec_layoutZ	umr_groupZ	umr_labelZumr_enabled_boxZumr_verbose_boxZumr_namelist_btnZ
umr_layoutZvlayoutr'   r'   r(   Ú
setup_pageC   sj    
















z$MainInterpreterConfigPage.setup_pagec             C   sx   | j  ¡ sdS tƒ }t|ƒs*t| ¡ dƒ}||kr6dS t |¡rJ|  |¡stt	 
| tdƒtdƒt	j¡ | j d¡ dS dS )z/Custom Python executable value has been changedFzutf-8ÚWarningz‰You selected an invalid Python interpreter for the console so the previous interpreter will stay. Please make sure to select a valid one.T)r   Ú	isCheckedr   r   r   ZtoUtf8r   Úis_python_interpreterÚwarn_python_compatibilityr   Úwarningr   ÚOkr.   Z
setChecked)r$   ÚpyexecZ
def_pyexecr'   r'   r(   Úpython_executable_changed—   s    


z3MainInterpreterConfigPage.python_executable_changedc             C   sš   t  |¡sd S tjd }y(ddg}t ||¡}t| ¡ d ƒ}W n* tk
rX   |}Y n t	k
rj   dS X ||kr–t
 | tdƒtdƒ||f t
j¡ dS )Nr   z-cz&import sys; print(sys.version_info[0])Fr3   aI  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   r7   r   r8   )r$   r9   Zspyder_versionÚargsÚprocZconsole_versionr'   r'   r(   r6   ª   s     

z3MainInterpreterConfigPage.warn_python_compatibilityc             C   s@  t  | tdƒtdƒtjd |  d¡¡¡\}}|r<t|ƒ}|r,| dd¡ 	d¡}g }g }xr|D ]j}t
r¶tdd	„ |D ƒƒrt |¡r´| |¡ qÊt | td
ƒtdƒtj¡ | |¡ q`t |¡r`| |¡ q`W 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Úcr'   r'   r(   ú	<genexpr>Ð   s    z=MainInterpreterConfigPage.set_umr_namelist.<locals>.<genexpr>r3   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:
%szRPlease note that these changes will be applied only to new Python/IPython consolesN)r   ZgetTextr   r   ZNormalÚjoinr   r   r!   Úsplitr   Úallr   Zis_module_installedÚappendr   r7   r8   ÚsetZinformationr   )r$   Z	argumentsZvalidZnamelistZfixed_namelistZnon_ascii_namelistZmodule_nameZinvalidr'   r'   r(   r1   Á   s@    



z*MainInterpreterConfigPage.set_umr_namelistc             C   s6   |   d¡}||kr2|tƒ kr2| |¡ |  d|¡ dS )z9Update the list of interpreters used and the current one.r+   N)r   r   rL   r   )r$   ÚvalueÚcustom_listr'   r'   r(   r"   ê   s    

z6MainInterpreterConfigPage.set_custom_interpreters_listc             C   sT   |   d¡}g }x4|D ],}t |¡rt |¡r|tƒ kr| |¡ qW |  d|¡ dS )z8Check that the used custom interpreters are still valid.r+   N)r   r   r   r   r5   r   rL   r   )r$   rO   Zvalid_custom_listrN   r'   r'   r(   r#   ñ   s    


z;MainInterpreterConfigPage.validate_custom_interpreters_listc             C   sˆ   | j  ¡ sd| j ¡ }t |¡}| d¡r4| dd¡}|  |¡}|rd|  	|¡ |  
d|¡ |  
d|¡ | j ¡ sz|  
dd¡ | j ¡  d S )Nzpythonw.exez
python.exer   r   rB   )r.   r4   r   Útextr   Únormpathr    r!   r:   r"   r   r&   Úapply_settings)r$   Zoptionsr   Zchanger'   r'   r(   rR   û   s    






z(MainInterpreterConfigPage.apply_settingsN)Ú__name__Ú
__module__Ú__qualname__ZCONF_SECTIONr   ÚNAMEÚimaZiconZICONr   r)   r2   r:   r6   r1   r"   r#   rR   r'   r'   r'   r(   r      s   
T)
r   ) Ú__doc__Z
__future__r   r/   Úos.pathÚpathr   r;   Zqtpy.QtWidgetsr   r   r   r   r   r   r	   r
   Zspyder.config.baser   Zspyder.plugins.configdialogr   Zspyder.py3compatr   r   r   Zspyder.utilsr   rW   Zspyder.utils.miscr   r   r   r'   r'   r'   r(   Ú<module>   s   (