B
    nôb\i[  ã               @   s¸  d Z ddlmZ ddlmZ ddlmZmZm	Z	m
Z
 ddlmZmZmZmZmZmZmZmZmZmZ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# dd	l$m%Z% dd
l&m'Z( ddl)m*Z* edƒZ+edƒZ,edƒZ-dZ.dZ/dZ0dZ1dZ2dZ3dZ4edƒZ5dZ6edƒZ7edƒZ8edƒZ9edƒZ:edƒZ;edƒZ<G dd„ de=ƒZ>d d!„ Z?d"d#„ Z@d$d%„ ZAG d&d'„ d'eƒZBG d(d)„ d)eƒZCG d*d+„ d+eCƒZDG d,d-„ d-eCƒZEG d.d/„ d/e#ƒZFdS )0z>Run configurations related dialogs and widgets and data modelsé    N)Úgetexistingdirectory)ÚQSizeÚQtÚSignalÚSlot)ÚQButtonGroupÚ	QCheckBoxÚ	QComboBoxÚQDialogÚQDialogButtonBoxÚQFrameÚQGridLayoutÚ	QGroupBoxÚQHBoxLayoutÚQLabelÚ	QLineEditÚQMessageBoxÚQPushButtonÚQRadioButtonÚQSizePolicyÚQStackedWidgetÚQVBoxLayoutÚQWidget)Ú_)ÚCONF)ÚGeneralConfigPage)Úto_text_string)Úicon_manager)Úgetcwd_or_homezExecute in current consolezExecute in a dedicated consolez&Execute in an external system terminalzdefault/interpreter/currentzdefault/interpreter/dedicatedzdefault/interpreter/systermz!default/wdir/use_script_directoryzdefault/wdir/use_cwd_directoryz default/wdir/use_fixed_directoryzdefault/wdir/fixed_directoryz"Always show %s on a first file runZopen_on_firstrunz%Remove all variables before executionz+Directly enter debugging when errors appearz0Interact with the Python console after executionz(The directory of the file being executedzThe current working directoryzThe following directory:c               @   sB   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚRunConfigurationzRun configurationNc             C   st   d | _ d | _d | _d | _d | _d | _d | _d | _d | _d | _	d | _
d | _d | _d | _d | _|  tjddi d¡ d S )NÚrunZdefaultconfiguration)Údefault)ÚargsÚargs_enabledÚwdirÚwdir_enabledÚcurrentÚsystermÚinteractÚpost_mortemÚpython_argsÚpython_args_enabledÚclear_namespaceÚfile_dirÚcw_dirÚ	fixed_dirÚdirÚsetr   Úget)ÚselfÚfname© r5   ú7lib/python3.7/site-packages/spyder/plugins/runconfig.pyÚ__init__9   s     zRunConfiguration.__init__c             C   s
  |  dd¡| _|  dd¡| _|  dt  dtd¡¡| _|  dt  dtd¡¡| _|  d	t  dd	d¡¡| _|  d
t  dd
d¡¡| _	|  dd¡| _
|  dd¡| _|  dt  ddd¡¡| _|  dt  dtd¡¡| _|  dt  dtd¡¡| _|  dt  dtd¡¡| _|  dd¡| _d S )Nr"   Ú zargs/enabledFr&   r    Tr'   r(   r)   r*   zpython_args/enabledr,   r-   r.   r/   r0   )r2   r"   r#   r   ÚCURRENT_INTERPRETER_OPTIONr&   ÚSYSTERM_INTERPRETER_OPTIONr'   r(   r)   r*   r+   r,   ÚWDIR_USE_SCRIPT_DIR_OPTIONr-   ÚWDIR_USE_CWD_DIR_OPTIONr.   ÚWDIR_USE_FIXED_DIR_OPTIONr/   r0   )r3   Úoptionsr5   r5   r6   r1   L   s*    zRunConfiguration.setc             C   sB   | j | j| j| j| j| j| j| j| j| j	| j
| j| j| j| jdœS )N)zargs/enabledr"   zworkdir/enabledZworkdirr&   r'   r(   r)   zpython_args/enabledr*   r,   r-   r.   r/   r0   )r#   r"   r%   r$   r&   r'   r(   r)   r+   r*   r,   r-   r.   r/   r0   )r3   r5   r5   r6   r2   c   s    zRunConfiguration.getc             C   s   | j S )N)r0   )r3   r5   r5   r6   Úget_working_directoryv   s    z&RunConfiguration.get_working_directoryc             C   s   | j r| jS dS d S )Nr8   )r#   r"   )r3   r5   r5   r6   Úget_argumentsy   s    zRunConfiguration.get_argumentsc             C   s   | j r| jS dS d S )Nr8   )r+   r*   )r3   r5   r5   r6   Úget_python_arguments   s    z%RunConfiguration.get_python_arguments)N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r7   r1   r2   r?   r@   rA   r5   r5   r5   r6   r   6   s   
r   c              C   sV   t  ddd¡} y dd„ t  ddg ¡D ƒd | … S  tk
rP   t  ddg ¡ g S X d S )Nr    Úhistoryé   c             S   s"   g | ]\}}t  |¡r||f‘qS r5   )ÚospÚisfile)Ú.0Úfilenamer>   r5   r5   r6   ú
<listcomp>‰   s   z+_get_run_configurations.<locals>.<listcomp>Úconfigurations)r   r2   Ú
ValueErrorr1   )Úhistory_countr5   r5   r6   Ú_get_run_configurations†   s    rP   c             C   s(   t  ddd¡}t  dd| d |… ¡ d S )Nr    rF   rG   rM   )r   r2   r1   )rM   rO   r5   r5   r6   Ú_set_run_configurations‘   s    rQ   c             C   s8   t ƒ }x,|D ]$\}}| |krtƒ }| |¡ |S qW dS )z'Return script *fname* run configurationN)rP   r   r1   )r4   rM   rK   r>   Úrunconfr5   r5   r6   Úget_run_configuration–   s    
rS   c               @   sB   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚRunConfigOptionszRun configuration optionsNc             C   sX  t  | |¡ d | _tƒ | _t dtd¡}tt	dƒƒ}t
ƒ }| |¡ ttƒ| _| | j¡ ttƒ| _| | j¡ ttƒ| _| | j¡ tt	dƒƒ}tƒ }| |¡ ttƒ| _| | jdd¡ ttƒ| _| | jdd¡ tt	dƒƒ| _| | jdd¡ tƒ | _| jj | jj¡ | j d¡ | | jdd¡ tt	d	ƒƒ}t
ƒ }| |¡ tt ƒ| _!| | j!¡ tt"ƒ| _#| | j#¡ t$ƒ }	tt%ƒ| _&|	 | j&¡ tƒ | _'| j&j | j'j¡ | j' d¡ |	 | j'¡ t(t) *d
¡d| ƒ}
|
 +t	dƒ¡ |
j, | j-¡ |	 |
¡ | .|	¡ tt	dƒƒ}| /d¡ | jj |j¡ tƒ }| |¡ tt0ƒ| _1| | j1dddd¡ tt	dƒƒ| _2| | j2dd¡ tƒ | _3| j2j | j3j¡ | j3 d¡ | j3 +t	dƒ¡ | | j3dd¡ t4ƒ }| 5t4j6¡ | 7t4j8¡ tt9t	dƒ ƒ| _:| j:j, | j;¡ | j: <|¡ t
ƒ }| |¡ | |¡ | |¡ | |¡ | |¡ | | j:¡ |  |¡ d S )Nr    FÚConsolezGeneral settingsr   é   zCommand line options:é   zWorking Directory settingsZDirOpenIconr8   zSelect directoryzExternal system terminalTéÿÿÿÿé   z4<b>-u</b> is added to the other options you set herezthis dialog)=r   r7   r0   r   rR   r   r2   ÚALWAYS_OPEN_FIRST_RUN_OPTIONr   r   r   Ú	setLayoutr   ÚCURRENT_INTERPRETERÚcurrent_radioÚ	addWidgetÚDEDICATED_INTERPRETERÚdedicated_radioÚSYSTERM_INTERPRETERÚsysterm_radior   r   ÚCLEAR_ALL_VARIABLESÚclear_var_cbÚPOST_MORTEMÚpost_mortem_cbÚclo_cbr   Úclo_editÚtoggledÚconnectÚ
setEnabledÚFILE_DIRÚfile_dir_radioÚCW_DIRÚ	cwd_radior   Ú	FIXED_DIRÚfixed_dir_radioÚwd_editr   ÚimaÚiconZ
setToolTipÚclickedÚselect_directoryÚ	addLayoutÚsetDisabledÚINTERACTÚinteract_cbÚpclo_cbÚ	pclo_editr   ZsetFrameShapeZHLineZsetFrameShadowZSunkenÚALWAYS_OPEN_FIRST_RUNÚfirstrun_cbÚset_firstrun_oÚ
setChecked)r3   ÚparentZ
firstrun_oÚinterpreter_groupÚinterpreter_layoutZcommon_groupZcommon_layoutÚ
wdir_groupÚwdir_layoutZfixed_dir_layoutZ
browse_btnÚexternal_groupÚexternal_layoutZhlineÚlayoutr5   r5   r6   r7   ¢   s’    




















zRunConfigOptions.__init__c             C   sH   t | j ¡ ƒ}t |¡stƒ }t| td ƒ|ƒ}|rD| j |¡ || _	dS )zSelect directoryN)
r   rr   ÚtextrH   Úisdirr   r   r   ÚsetTextr0   )r3   ZbasedirZ	directoryr5   r5   r6   rv     s    
z!RunConfigOptions.select_directoryc             C   s   | j  |¡ | j | j j¡ | j | j j¡ | j jrB| j	 d¡ n"| j j
rX| j d¡ n| j d¡ | j | j j¡ | j | j j¡ | j | j j¡ | j | j j¡ | j | j j¡ | j | j j¡ | j | j j¡ | j | j j¡ | j j| _| j | j¡ d S )NT)rR   r1   rg   r€   r#   rh   r‹   r"   r&   r]   r'   rb   r`   rz   r(   rf   r)   r{   r+   r|   r*   rd   r,   rm   r-   ro   r.   rq   r/   r0   rr   )r3   r>   r5   r5   r6   r1     s$    
zRunConfigOptions.setc             C   sÈ   | j  ¡ | j_t| j ¡ ƒ| j_| j ¡ | j_	| j
 ¡ | j_| j ¡ | j_| j ¡ | j_| j ¡ | j_t| j ¡ ƒ| j_| j ¡ | j_| j ¡ | j_| j ¡ | j_| j ¡ | j_| j ¡ | j_| j ¡ S )N)rg   Ú	isCheckedrR   r#   r   rh   r‰   r"   r]   r&   rb   r'   rz   r(   rf   r)   r{   r+   r|   r*   rd   r,   rm   r-   ro   r.   rq   r/   rr   r0   r2   )r3   r5   r5   r6   r2   &  s    zRunConfigOptions.getc             C   sH   t | j ¡ ƒ}| j ¡ r"t |¡r&dS t | t	dƒt	dƒ| ¡ dS d S )NTzRun configurationz:The following working directory is not valid:<br><b>%s</b>F)
r   rr   r‰   rq   rŒ   rH   rŠ   r   Zcriticalr   )r3   r$   r5   r5   r6   Úis_valid6  s    zRunConfigOptions.is_validc             C   s   t  dt| j ¡ ¡ d S )Nr    )r   r1   rZ   r~   rŒ   )r3   r5   r5   r6   r   @  s    zRunConfigOptions.set_firstrun_o)N)
rB   rC   rD   rE   r7   rv   r1   r2   r   r   r5   r5   r5   r6   rT       s   
e

rT   c               @   sJ   e Zd ZdZeeƒZddd„Zdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ ZdS )ÚBaseRunConfigDialogz)Run configuration dialog box, base widgetNc             C   s<   t  | |¡ |  tj¡ |  t d¡¡ tƒ }|  	|¡ d S )NZrun_settings)
r
   r7   ZsetAttributer   ZWA_DeleteOnCloseZsetWindowIconrs   rt   r   r[   )r3   r   rˆ   r5   r5   r6   r7   I  s
    zBaseRunConfigDialog.__init__c             G   s:   |   ¡ }x,|D ]$}t|tƒr(| |¡ q| |¡ qW dS )z-Add widgets/spacing to dialog vertical layoutN)rˆ   Ú
isinstanceÚintÚ
addSpacingr^   )r3   Zwidgets_or_spacingsrˆ   Zwidget_or_spacingr5   r5   r6   Úadd_widgetsV  s
    

zBaseRunConfigDialog.add_widgetsc             C   sp   t |ƒ}| tdƒt j¡}|j | j¡ |j | j¡ |j	 | j
¡ tƒ }| d¡ | |¡ |  ¡  |¡ dS )z8Create dialog button box and add it to the dialog layoutÚRunrV   N)r   Z	addButtonr   Z
AcceptRoleru   rj   Úrun_btn_clickedZacceptedÚacceptZrejectedZrejectr   Ú
addStretchr^   rˆ   rw   )r3   ZstdbtnsZbboxZrun_btnZ	btnlayoutr5   r5   r6   Úadd_button_box_  s    

z"BaseRunConfigDialog.add_button_boxc             C   s    t  | |¡ | j |  ¡ ¡ dS )zn
        Reimplement Qt method to be able to save the widget's size from the
        main application
        N)r
   ÚresizeEventÚsize_changeÚemitÚsize)r3   Zeventr5   r5   r6   r˜   k  s    zBaseRunConfigDialog.resizeEventc             C   s   dS )zRun button was just clickedNr5   )r3   r5   r5   r6   r”   s  s    z#BaseRunConfigDialog.run_btn_clickedc             C   s   t ‚dS )z4Setup Run Configuration dialog with filename *fname*N)ÚNotImplementedError)r3   r4   r5   r5   r6   Úsetupw  s    zBaseRunConfigDialog.setup)N)rB   rC   rD   rE   r   r   r™   r7   r’   r—   r˜   r”   r   r5   r5   r5   r6   rŽ   E  s   
	rŽ   c               @   s8   e Zd ZdZddd„Zdd„ Zeƒ dd„ ƒZd	d
„ ZdS )ÚRunConfigOneDialogz1Run configuration dialog box: single file versionNc             C   s   t  | |¡ d | _d | _d S )N)rŽ   r7   rK   Úrunconfigoptions)r3   r   r5   r5   r6   r7   ~  s    zRunConfigOneDialog.__init__c             C   sX   || _ t| ƒ| _| j t|ƒ ¡ ¡ |  | j¡ |  tj	¡ |  
tdƒt |¡ ¡ dS )z4Setup Run Configuration dialog with filename *fname*zRun settings for %sN)rK   rT   rŸ   r1   r   r2   r’   r—   r   ÚCancelÚsetWindowTitler   rH   Úbasename)r3   r4   r5   r5   r6   r   ƒ  s    
zRunConfigOneDialog.setupc             C   sB   | j  ¡ sdS tƒ }| d| j| j  ¡ f¡ t|ƒ t | ¡ dS )zReimplement Qt methodNr   )	rŸ   r   rP   ÚinsertrK   r2   rQ   r
   r•   )r3   rM   r5   r5   r6   r•   Œ  s    
zRunConfigOneDialog.acceptc             C   s   | j jS )N)rŸ   rR   )r3   r5   r5   r6   Úget_configuration–  s    z$RunConfigOneDialog.get_configuration)N)	rB   rC   rD   rE   r7   r   r   r•   r¤   r5   r5   r5   r6   rž   |  s
   
	
rž   c               @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚRunConfigDialogz3Run configuration dialog box: multiple file versionNc             C   s"   t  | |¡ d | _d | _d | _d S )N)rŽ   r7   Úfile_to_runÚcomboÚstack)r3   r   r5   r5   r6   r7   ž  s    zRunConfigDialog.__init__c             C   s   t | j ¡ ƒ| _dS )zRun button was just clickedN)r   r§   ZcurrentTextr¦   )r3   r5   r5   r6   r”   ¤  s    zRunConfigDialog.run_btn_clickedc             C   s   t tdƒƒ}tƒ | _| j d¡ | j tj¡ | j tj	tj
¡ tƒ | _tƒ }x>t|ƒD ]\}\}}||krXP qXW | d|t|ƒ ¡ f¡ d}x:|D ]2\}}t| ƒ}| |¡ | j |¡ | j |¡ q”W | jj | jj¡ | j |¡ |  || jd| j¡ |  tjtjB ¡ |  tdƒ¡ dS )z4Setup Run Configuration dialog with filename *fname*zSelect a run configuration:rG   r   é
   zRun configuration per fileN)r   r   r	   r§   ZsetMaxVisibleItemsZsetSizeAdjustPolicyZAdjustToMinimumContentsLengthZsetSizePolicyr   Z	ExpandingZFixedr   r¨   rP   Ú	enumerater£   r   r2   rT   r1   ZaddItemr^   ZcurrentIndexChangedrj   ZsetCurrentIndexr’   r—   r   ZOkr    r¡   )r3   r4   Zcombo_labelrM   ÚindexrK   r>   Úwidgetr5   r5   r6   r   ¨  s,    
zRunConfigDialog.setupc             C   s~   g }xbt | j ¡ ƒD ]P}t| j |¡ƒ}| j |¡}|| j ¡ krN| ¡ sNdS | 	¡ }| 
||f¡ qW t|ƒ t | ¡ dS )zReimplement Qt methodN)Úranger¨   Úcountr   r§   ZitemTextr¬   ZcurrentIndexr   r2   ÚappendrQ   r
   r•   )r3   rM   r«   rK   rŸ   r>   r5   r5   r6   r•   É  s    zRunConfigDialog.accept)N)rB   rC   rD   rE   r7   r”   r   r•   r5   r5   r5   r6   r¥   œ  s
   
!r¥   c               @   s6   e Zd ZdZdZedƒZe d¡Z	dd„ Z
dd„ ZdS )	ÚRunConfigPagez'Default Run Settings configuration pager    r“   c             C   s€  t tdƒƒ}| d¡ ttdƒƒ}t|ƒ}| jttd|d| _| jt	t
d|d| _| jttd|d| _tƒ }| |¡ | | j¡ | | j¡ | | j¡ ttdƒƒ}|  tdd¡}|  tdd¡}tƒ }| |¡ | |¡ | |¡ ttd	ƒƒ}	t|	ƒ}
t td
ƒƒ}| d¡ | jttd|
d}| jttd|
d}| jttd|
d}|  dttƒ ¡}|j |j ¡ |j |j!¡ |j |j!¡ t"ƒ }| |¡ | |¡ tƒ }| |¡ | |¡ | |¡ | #|¡ |	 |¡ ttdƒƒ}|  t$dd¡}tƒ }| |¡ | |¡ |  t%tdƒ t&d¡}tƒ }| |¡ | 'd¡ | |¡ | |¡ | |	¡ | |¡ | |¡ | (d¡ |  |¡ d S )NzœThe following are the default options for running files.These options may be overriden using the <b>Configuration per file</b> entry of the <b>Run</b> menu.TrU   )Zbutton_groupFzGeneral settingsr)   r,   zWorking Directory settingszDefault working directory is:r8   zExternal system terminalr(   zRun Settings dialogr©   rV   ))r   r   ZsetWordWrapr   r   Zcreate_radiobuttonr\   r9   r]   r_   ÚDEDICATED_INTERPRETER_OPTIONr`   ra   r:   rb   r   r[   r^   Zcreate_checkboxre   rc   rl   r;   rn   r<   rp   r=   Zcreate_browsedirÚWDIR_FIXED_DIR_OPTIONr   ri   rj   rk   rx   r   rw   ry   r}   rZ   r‘   r–   )r3   Zabout_labelr‚   Zinterpreter_bgrƒ   Zgeneral_groupr)   Zclear_variablesZgeneral_layoutr„   Zwdir_bgZ
wdir_labelZdirname_radioro   Zthisdir_radioZ
thisdir_bdZthisdir_layoutr…   r†   Zinteract_afterr‡   r~   Zvlayoutr5   r5   r6   Ú
setup_pageß  s”    


























zRunConfigPage.setup_pagec             C   s   d S )Nr5   )r3   r>   r5   r5   r6   Úapply_settings:  s    zRunConfigPage.apply_settingsN)rB   rC   rD   rE   ZCONF_SECTIONr   ÚNAMErs   rt   ZICONr³   r´   r5   r5   r5   r6   r°   Ø  s   
[r°   )GrE   Úos.pathÚpathrH   Zqtpy.compatr   Zqtpy.QtCorer   r   r   r   Zqtpy.QtWidgetsr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zspyder.config.baser   Zspyder.config.mainr   Zspyder.plugins.configdialogr   Zspyder.py3compatr   Zspyder.utilsr   rs   Zspyder.utils.miscr   r\   r_   ra   r9   r±   r:   r;   r<   r=   r²   r}   rZ   rc   re   ry   rl   rn   rp   Úobjectr   rP   rQ   rS   rT   rŽ   rž   r¥   r°   r5   r5   r5   r6   Ú<module>   sL   PP
 &7 <