B
    nôb\÷i  ã               @   sü  d Z ddlmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlZddlmZmZmZmZ ddlmZ ddlmZmZmZmZmZmZmZ dd	lmZmZmZ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+ ddl,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= eddƒZ>e ?e>ddd¡Z@e ?e>dd¡ZAeBe ?eAd¡ƒ C¡ ZDeBe ?eAd¡ƒ C¡ ZEeBe ?eAd¡ƒ C¡ ZFy
ejG W n eHk
rÜ   eje_GY nX d d!„ ZIG d"d#„ d#ee;ƒZJdS )$zP
Client widget for the IPython Console

This is the widget used on all its tabs
é    )Úabsolute_importN)ÚTemplate)ÚThread)ÚQUrlÚQTimerÚSignalÚSlot)ÚQKeySequence)ÚQHBoxLayoutÚQLabelÚQMenuÚQMessageBoxÚQToolButtonÚQVBoxLayoutÚQWidget)Ú_Úget_image_pathÚget_module_source_pathÚrunning_under_pytest)Úget_fontÚget_shortcut)Úicon_manager)Ú
sourcecode)Ú
get_coding)ÚRemoteEnvDialog)Úget_temp_dir)Úadd_actionsÚcreate_actionÚcreate_toolbuttonÚDialogManagerÚMENU_SEPARATOR)Úto_text_string)ÚWebView)ÚShellWidget)ÚSaveHistoryMixin)ÚCollectionsEditorZspyderZutilsÚhelpZstaticZcssZipythonZ	templatesz
blank.htmlzloading.htmlzkernel_error.htmlc             C   s   t | d}| ¡  |S )zo
    Call a function in a simple thread, to prevent blocking

    Taken from the Jupyter Qtconsole project
    )Útarget)r   Ústart)ÚfÚt© r+   úClib/python3.7/site-packages/spyder/widgets/ipythonconsole/client.pyÚ
backgroundF   s    
r-   c            	       sÐ  e Zd ZdZd ejd e ¡ ¡Z	ddgZ
eeeƒZdW‡ fd	d
„	Zedd„ ƒZedd„ ƒZedd„ ƒZdd„ ZdXdd„Zdd„ Zdd„ Zeƒ 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+d,„ Z!dYd-d.„Z"d/d0„ Z#d1d2„ Z$eƒ d3d4„ ƒZ%eeƒd5d6„ ƒZ&eƒ d7d8„ ƒZ'eƒ d9d:„ ƒZ(eƒ d;d<„ ƒZ)eƒ d=d>„ ƒZ*d?d@„ Z+ee,ƒdAdB„ ƒZ-ee,ƒdCdD„ ƒZ.dEdF„ Z/dZdGdH„Z0dIdJ„ Z1ee2ƒdKdL„ ƒZ3dMdN„ Z4dOdP„ Z5dQdR„ Z6dSdT„ Z7dUdV„ Z8‡  Z9S )[ÚClientWidgetz¡
    Client widget for the IPython Console

    This is a widget composed of a shell widget and a WebView info widget
    to print different messages there.
    z{0}## ---({1})---é   z# -*- coding: utf-8 -*-z+# *** Spyder Python Console History Log ***NFTc                sˆ  t tˆƒ ˆ ¡ t ˆ|¡ |ˆ_|ˆ_|ˆ_|	ˆ_|
ˆ_|ˆ_	|ˆ_
|ˆ_|ˆ_|ˆ_d ˆ_d ˆ_d ˆ_t d¡ˆ_g ˆ_dˆ_d ˆ_dˆ_t|ˆ|||ddˆ_tˆƒˆ_ˆ ¡  ˆ ¡ ˆ_ˆ ¡  d ˆ_ t! "¡ ˆ_#t$ˆƒˆ_%t&ƒ }ˆ '¡ }t(ƒ }| )ˆ *¡ ¡ | +d¡ x|D ]}| )|¡ qW | ,|¡ | -dddd¡ | )ˆj¡ | )ˆj¡ ˆ .|¡ ‡ ‡fdd„ˆ_/t0ƒ ˆ_1ˆ 2¡  d S )NÚstopTF)ZconfigZ	ipyclientÚadditional_optionsÚinterpreter_versionsÚexternal_kernelZlocal_kernelr   c                  s   ˆ j ˆdS )N)Zclient)Zclose_clientr+   )ÚpluginÚselfr+   r,   Ú<lambda>¦   s    z'ClientWidget.__init__.<locals>.<lambda>)3Úsuperr.   Ú__init__r$   Úid_Úconnection_fileÚhostnameÚmenu_actionsÚslaver3   Ú
given_nameÚshow_elapsed_timeÚreset_warningÚask_before_restartÚoptions_buttonÚstop_buttonÚreset_buttonÚimaÚiconÚ	stop_iconÚhistoryZallow_renameÚ
stderr_dirÚis_error_shownr#   Úshellwidgetr"   Ú
infowidgetÚset_infowidget_fontÚ_create_loading_pageÚloading_pageÚ_show_loading_pageÚ
time_labelÚtimeÚ	monotonicÚt0r   Ztimerr   Úget_toolbar_buttonsr
   Z	addWidgetÚcreate_time_labelZ
addStretchZ	addLayoutZsetContentsMarginsZ	setLayoutÚexit_callbackr   Údialog_managerÚupdate_time_label_visibility)r5   r4   r9   Zhistory_filenameZconfig_optionsr1   r2   r:   r;   r<   r=   r3   r>   r?   r@   rA   ZvlayoutZtoolbar_buttonsZhlayoutZbutton)Ú	__class__)r4   r5   r,   r8   b   s`    	








zClientWidget.__init__c             C   s(   | j dk	r$t | j ¡}| d¡d S dS )zGet kernel idNz.jsonr   )r:   ÚospÚbasenameÚsplit)r5   Z	json_filer+   r+   r,   Ú	kernel_id¯   s    
zClientWidget.kernel_idc          	   C   sf   d}| j dk	rb| jd }| jdk	r2t | j|¡}n0yt tƒ |¡}W n ttfk
r`   d}Y nX |S )z&Filename to save kernel stderr output.Nz.stderr)r:   r^   rI   r[   Újoinr   ÚIOErrorÚOSError)r5   Ústderr_filer+   r+   r,   rb   ¶   s    



zClientWidget.stderr_filec             C   sD   | j dk	r<ytj| j ddd}W q@ tk
r8   d}Y q@X nd}|S )zGet handle to stderr_file.NÚwzutf-8)Úencoding)rb   ÚcodecsÚopenÚ	Exception)r5   Zhandler+   r+   r,   Ústderr_handleÅ   s    
zClientWidget.stderr_handlec             C   s4   y| j  ¡  t | j¡ W n tk
r.   Y nX dS )z.Remove stderr_file associated with the client.N)rh   ÚcloseÚosÚremoverb   rg   )r5   r+   r+   r,   Úremove_stderr_fileÔ   s
    
zClientWidget.remove_stderr_filec             C   s  |r|   ¡  ¡  | 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¡ | jj | j¡ | jj | j¡ | jj | jj¡ | j| jjdd | jj | j¡ | jj | j¡ dS )z-Configure shellwidget after kernel is startedF)ÚresetN)Úget_controlZsetFocusrK   Zset_exit_callbackZ	executingÚconnectZadd_to_historyZset_backend_for_mayaviZexecutedÚupdate_historyZrefresh_namespacebrowserÚenable_stop_buttonÚdisable_stop_buttonZsig_kernel_restartedÚkernel_restarted_messageZchange_mpl_backendZsig_show_syspathÚshow_syspathZsig_show_envÚshow_envZget_cwdÚset_color_schemeÚsyntax_styleÚsig_prompt_readyÚ_hide_loading_pageÚ_show_mpl_backend_errors)r5   Z
give_focusr+   r+   r,   Úconfigure_shellwidgetß   s,    



z"ClientWidget.configure_shellwidgetc             C   s   | j  d¡ d S )NT)rC   Z
setEnabled)r5   r+   r+   r,   rq     s    zClientWidget.enable_stop_buttonc             C   s   | j js| j d¡ d S )NT)rK   Z
_executingrC   ÚsetDisabled)r5   r+   r+   r,   rr     s    z ClientWidget.disable_stop_buttonc             C   s.   | j  d¡ | jjs|  ¡  n| j d¡ dS )z;Method to handle what to do when the stop button is pressedTÚexitN)rC   r|   rK   Z_readingÚinterrupt_kernelZwrite_to_stdin)r5   r+   r+   r,   Ústop_button_click_handler  s    
z&ClientWidget.stop_button_click_handlerc             C   sx   t  |¡}|r| |d¡}| dd¡}tdƒ}ttƒ}|jt||d}| j 	|t
 t¡¡ | j ¡  | j ¡  d| _dS )z0Show kernel initialization errors in infowidget.z<br>ú-z&#8209z*An error ocurred while starting the kernel)Úcss_pathÚmessageÚerrorTN)r   Zget_eol_charsÚreplacer   r   ÚKERNEL_ERRORÚ
substituteÚCSS_PATHrL   ÚsetHtmlr   ÚfromLocalFilerK   ÚhideÚshowrJ   )r5   rƒ   Zeolr‚   Zkernel_error_templateÚpager+   r+   r,   Úshow_kernel_error(  s    


zClientWidget.show_kernel_errorc             C   sb   | j dkrJ| jdkrtdƒ}n| j}| jd d | jd  }|d | }n| j d | jd  }|S )zReturn client nameNZConsoleZint_idú/Zstr_idú )r>   r;   r   r9   )r5   ÚnameZ	client_idr+   r+   r,   Úget_nameB  s    


zClientWidget.get_namec             C   s$   | j j}|r| ¡ r|S | j jS dS )z4Return the text widget (or similar) to give focus toN)rK   Z_page_controlÚ	isVisibleÚ_control)r5   Zpage_controlr+   r+   r,   rn   Q  s    zClientWidget.get_controlc             C   s   | j jS )z&Get kernel associated with this client)rK   Úkernel_manager)r5   r+   r+   r,   Ú
get_kernelZ  s    zClientWidget.get_kernelc             C   s¨   t | tdƒt d¡| jd}t | tdƒ| jd| _t | tdƒt d¡| jjd}t | tdƒt d	¡| jj	d}| j 
| j¡ |t||| jg}| jd
k	r | j| S |S d
S )zReturn options menuzRemove all variablesÚ
editdelete)rF   Ú	triggeredzShow elapsed time)ZtoggledzShow environment variablesÚenvironzShow sys.path contentsÚsyspathN)r   r   rE   rF   Úreset_namespaceÚset_elapsed_time_visibleZshow_time_actionrK   Zget_envZget_syspathZ
setCheckedr?   r    r<   )r5   Zreset_actionZ
env_actionZsyspath_actionZadditional_actionsr+   r+   r,   Úget_options_menu^  s0    




zClientWidget.get_options_menuc             C   s  g }| j dkrBt| tdƒ| jtdƒd| _ |  ¡  | j j | j¡ | j dk	rX| | j ¡ | j	dkr†t| tdƒt
 d¡tdƒ| jd| _	| j	dk	rœ| | j	¡ | jdkrø|  ¡ }|røt| td	ƒt
 d
¡d| _| j tj¡ t| ƒ}t||ƒ | j |¡ | jdk	r| | j¡ |S )zReturn toolbar buttons list.NZStopzStop the current command)ÚtextrF   ÚtipZRemover–   zRemove all variables)r   rF   rž   r—   ZOptionsZtooloptions)r   rF   )rC   r   r   rG   rr   Zclickedro   r   ÚappendrD   rE   rF   rš   rB   rœ   ZsetPopupModer   ZInstantPopupr   r   ZsetMenu)r5   ÚbuttonsZoptionsÚmenur+   r+   r,   rU     s@    





z ClientWidget.get_toolbar_buttonsc          	   C   sÈ   t | tdƒttddƒƒt d¡| jd}t | tdƒttddƒƒ| jd}t | td	ƒttd
dƒƒt d¡| jd}t | tdƒttddƒƒ| j	d}t | tdƒt d¡| j
d}t|d||||d|fƒ |S )z*Add actions to IPython widget context menuzInspect current objectZconsolezinspect current objectZMessageBoxInformation)rF   r—   zClear line or blockz
clear line)r—   zRemove all variablesZipython_consolezreset namespacer–   zClear consolezclear shellz&Quitr}   N)r   r   r	   r   rE   rF   Úinspect_objectÚ
clear_linerš   Úclear_consolerW   r   )r5   r¡   Zinspect_actionZclear_line_actionZreset_namespace_actionZclear_console_actionZquit_actionr+   r+   r,   Úadd_actions_to_context_menu«  s4    










z(ClientWidget.add_actions_to_context_menuc             C   s   | j j |¡ || j _dS )zSet IPython widget's fontN)rK   r“   ZsetFontÚfont)r5   r¦   r+   r+   r,   Úset_fontÍ  s    zClientWidget.set_fontc             C   s   t dd}| j |¡ dS )zSet font for infowidgetZ	rich_font)ZoptionN)r   rL   r§   )r5   r¦   r+   r+   r,   rM   Ò  s    
z ClientWidget.set_infowidget_fontc             C   s,   y| j  ||¡ W n tk
r&   Y nX dS )zSet IPython color scheme.N)rK   rv   ÚAttributeError)r5   Zcolor_schemerm   r+   r+   r,   rv   ×  s    zClientWidget.set_color_schemec             C   s<   |   ¡ dk	r| js| jj ¡  | jjdk	r8t| jjjƒ dS )zShutdown kernelN)r•   r=   rK   r”   Zshutdown_kernelZkernel_clientr-   Zstop_channels)r5   r+   r+   r,   Úshutdownà  s    zClientWidget.shutdownc             C   s(   y| j  ¡  W n tk
r"   Y nX dS )z7Interrupt the associanted Spyder kernel if it's runningN)rK   Zrequest_interrupt_kernelÚRuntimeError)r5   r+   r+   r,   r~   ç  s    zClientWidget.interrupt_kernelc          
   C   sü   | j }tƒ s<| jr<tdƒ}tjtjB }t | tdƒ||¡}nd}|tjksVtƒ sV| jsø|jræ| j	 
¡ rx| j	 ¡  | ¡  y|jj| jd W n8 tk
rÄ } z|jtdƒ| dd W dd}~X Y qøX |  |j¡ |jtdƒd	d n|jtd
ƒdd dS )zŠ
        Restart the associated kernel.

        Took this code from the qtconsole project
        Licensed under the BSD license
        z,Are you sure you want to restart the kernel?zRestart kernel?N)ÚstderrzError restarting kernel: %s
T)Úbefore_promptz!<br>Restarting kernel...
<hr><br>Fz.Cannot restart a kernel not started by Spyder
)rK   r   rA   r   r   ZYesZNoZquestionr”   rL   r’   rŠ   r‹   Úrestart_kernelrh   rª   Z_append_plain_textrv   rw   Ú_append_html)r5   Úswr‚   r    ÚresultÚer+   r+   r,   r­   ð  s6    






zClientWidget.restart_kernelc             C   sX   | j s@y|  ¡ }W n tk
r*   d}Y nX |rT|  d| ¡ n| jjd| dd dS )z$Show kernel restarted/died messages.Nz<tt>%s</tt>z<br>%s<hr><br>F)r¬   )rJ   Ú_read_stderrrg   r   rK   r®   )r5   Úmsgr«   r+   r+   r,   rs     s    
z%ClientWidget.kernel_restarted_messagec             C   s   | j j ¡  dS )z2Show how to inspect an object with our Help pluginN)rK   r“   Zinspect_current_object)r5   r+   r+   r,   r¢   2  s    zClientWidget.inspect_objectc             C   s   | j  ¡  dS )zClear a console lineN)rK   Z_keyboard_quit)r5   r+   r+   r,   r£   7  s    zClientWidget.clear_linec             C   s   | j  ¡  dS )zClear the whole consoleN)rK   r¤   )r5   r+   r+   r,   r¤   <  s    zClientWidget.clear_consolec             C   s   | j j| jdd dS )z>Resets the namespace by removing all names defined by the userT)Zwarningr‚   N)rK   rš   r@   )r5   r+   r+   r,   rš   A  s    
zClientWidget.reset_namespacec             C   s   | j j| _d S )N)rK   Z_historyrH   )r5   r+   r+   r,   rp   G  s    zClientWidget.update_historyc             C   s>   |dk	r6t ƒ }|j|dddt d¡d | j |¡ ndS dS )zShow sys.path contents.Nzsys.path contentsTiX  r™   )ÚtitleÚreadonlyÚwidthrF   )r%   ZsetuprE   rF   rX   r‹   )r5   r™   Zeditorr+   r+   r,   rt   J  s    
zClientWidget.show_syspathc             C   s   | j  t|ƒ¡ dS )zShow environment variables.N)rX   r‹   r   )r5   Úenvr+   r+   r,   ru   U  s    zClientWidget.show_envc             C   s   | j dkrtƒ | _ | j S )z=Create elapsed time label widget (if necessary) and return itN)rQ   r   )r5   r+   r+   r,   rV   Z  s    
zClientWidget.create_time_labelc             C   s|   | j dkrdS t ¡ | j }|dk r2t ¡ | _d}|dkr@d}nd}|rNd}nd}d|t |t |¡¡f }| j  |¡ dS )	zText to show in time_label.Nr   i€Q z%d %H:%M:%Sz%H:%M:%Sz#AAAAAAz#AA6655z(<span style='color: %s'><b>%s</b></span>)rQ   rR   rS   rT   ZstrftimeZgmtimeZsetText)r5   ÚendZelapsed_timeZfmtZcolorr   r+   r+   r,   Ú	show_time`  s     

zClientWidget.show_timec             C   s   | j  | j¡ dS )zUpdate elapsed time visibility.N)rQ   Ú
setVisibler?   )r5   r+   r+   r,   rY   w  s    z)ClientWidget.update_time_label_visibilityc             C   s    || _ | jdk	r| j |¡ dS )z%Slot to show/hide elapsed time label.N)r?   rQ   rº   )r5   Ústater+   r+   r,   r›   {  s    
z%ClientWidget.set_elapsed_time_visiblec             C   sB   t tƒ}tdƒ}tjdkr&| dd¡}tdƒ}|jt||d}|S )z5Create html page to show while the kernel is startingzloading_sprites.pngÚntú\rŽ   zConnecting to kernel...)r   Úloading_imgr‚   )	r   ÚLOADINGr   rj   r   r„   r   r†   r‡   )r5   Zloading_templater¾   r‚   rŒ   r+   r+   r,   rN   ƒ  s    
z!ClientWidget._create_loading_pagec             C   s.   | j  ¡  | j ¡  | j | jt t¡¡ dS )z+Show animation while the kernel is loading.N)	rK   rŠ   rL   r‹   rˆ   rO   r   r‰   r‡   )r5   r+   r+   r,   rP     s    


zClientWidget._show_loading_pagec             C   s4   | j  ¡  | j ¡  | j  t¡ | jj | j¡ dS )z1Hide animation shown while the kernel is loading.N)	rL   rŠ   rK   r‹   rˆ   ÚBLANKrx   Ú
disconnectry   )r5   r+   r+   r,   ry   –  s    

zClientWidget._hide_loading_pagec             C   sB   t | jdƒ}z&| ¡ }|sdS t|ƒ}t||ƒ}|S | ¡  X dS )z#Read the stderr file of the kernel.ÚrbÚ N)rf   rb   Úreadr   r!   ri   )r5   r)   Zstderr_textrd   r+   r+   r,   r²     s    
zClientWidget._read_stderrc             C   s&   | j s| j d¡ | jj | j¡ dS )zT
        Show possible errors when setting the selected Matplotlib backend.
        z/get_ipython().kernel._show_mpl_backend_errors()N)r3   rK   Zsilent_executerx   rÁ   rz   )r5   r+   r+   r,   rz   µ  s
    z%ClientWidget._show_mpl_backend_errors)	NNNFFNFTT)T)T)F):Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úformatrj   ÚlineseprR   ZctimeZ	SEPARATORZINITHISTORYr   ÚstrZappend_to_historyr8   Úpropertyr^   rb   rh   rl   r{   rq   rr   r   r   r   r‘   rn   r•   rœ   rU   r¥   r§   rM   rv   r©   r~   r­   rs   r¢   r£   r¤   rš   rp   Úobjectrt   ru   rV   r¹   rY   Úboolr›   rN   rP   ry   r²   rz   Ú__classcell__r+   r+   )rZ   r,   r.   T   sb   
     E
5
	#*"
		.
r.   )KrÈ   Z
__future__r   re   rj   Úos.pathÚpathr[   Ústringr   Z	threadingr   rR   Zqtpy.QtCorer   r   r   r   Z
qtpy.QtGuir	   Zqtpy.QtWidgetsr
   r   r   r   r   r   r   Zspyder.config.baser   r   r   r   Zspyder.config.guir   r   Zspyder.utilsr   rE   r   Zspyder.utils.encodingr   Zspyder.utils.environr   Zspyder.utils.programsr   Zspyder.utils.qthelpersr   r   r   r   r    Zspyder.py3compatr!   Zspyder.widgets.browserr"   Zspyder.widgets.ipythonconsoler#   Zspyder.widgets.mixinsr$   Z1spyder.widgets.variableexplorer.collectionseditorr%   Z
UTILS_PATHr_   r‡   ZTEMPLATES_PATHrf   rÄ   rÀ   r¿   r…   rS   r¨   r-   r.   r+   r+   r+   r,   Ú<module>   sF   $

