B
    nôb\kM  ã               @   sÜ   d Z ddl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
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mZ ddlmZ ddlmZmZmZmZm Z  G dd„ deeeƒZ!dS )z&
Shell Widget for the IPython Console
é    N)ÚSignal)ÚQMessageBox)ÚCONF)Ú_)Úconfig_shortcut)ÚPY2Úto_text_string)Úencoding)Úprograms)Úsyntaxhighlighters)Úcreate_qss_styleÚcreate_style_class)ÚMessageCheckBox)ÚControlWidgetÚDebuggingWidgetÚ
HelpWidgetÚNamepaceBrowserWidgetÚPageControlWidgetc                   sl  e Zd ZdZeeƒZeeƒZeeƒZeeƒZ	ee
eƒZeƒ Zeƒ Zeƒ ZeeƒZee
ƒZeƒ Zee
ƒZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zd>dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z dd„ Z!d?d d!„Z"d"d#„ Z#d$d%„ Z$d&d'„ Z%d(d)„ Z&d*d+„ Z'd,d-„ Z(d.d/„ Z)‡ fd0d1„Z*d2d3„ Z+d@d4d5„Z,d6d7„ Z-d8d9„ Z.‡ fd:d;„Z/‡ fd<d=„Z0‡  Z1S )AÚShellWidgetzb
    Shell widget for the IPython Console

    This is the widget in charge of executing code
    c                sb   t | _t| _d| _tt| ƒj||Ž || _|| _	|| _
|| _d| _|  ¡ | _d | _|  | j¡ d S )NTÚ )r   Zcustom_controlr   Zcustom_page_controlZcustom_editÚsuperr   Ú__init__Ú	ipyclientÚadditional_optionsÚinterpreter_versionsÚexternal_kernelÚ_cwdÚcreate_shortcutsZ	shortcutsÚ_kernel_replyÚ set_bracket_matcher_color_schemeÚsyntax_style)Úselfr   r   r   r   ÚargsÚkw)Ú	__class__© úBlib/python3.7/site-packages/spyder/widgets/ipythonconsole/shell.pyr   <   s    
zShellWidget.__init__c             C   s   | j  | jj¡ dS )z!Set exit callback for this shell.N)Zexit_requestedZconnectr   Zexit_callback)r!   r%   r%   r&   Úset_exit_callbackV   s    zShellWidget.set_exit_callbackc             C   s   | j d k	r| j jrdS dS d S )NTF)Úkernel_clientZchannels_running)r!   r%   r%   r&   Ú
is_runningZ   s    
zShellWidget.is_runningc             C   s   d}| j rdS |  |¡ dS )z'Determine if the kernel is from Spyder.z1getattr(get_ipython().kernel, 'set_value', False)N)Ú_readingÚsilent_exec_method)r!   Úcoder%   r%   r&   Úis_spyder_kernela   s    zShellWidget.is_spyder_kernelc             C   sR   t jdkr| dd¡}| jsNd |¡}| jr>| j d| ¡ n
|  |¡ || _	dS )z$Set shell current working directory.Úntú\z\\z'get_ipython().kernel.set_cwd(u'''{}''')ú!N)
ÚosÚnameÚreplacer   Úformatr*   r(   ÚinputÚsilent_executer   )r!   Údirnamer,   r%   r%   r&   Úset_cwdi   s    


zShellWidget.set_cwdc             C   s   d}| j rdS |  |¡ dS )z¥Update current working directory.

        Retrieve the cwd and emit a signal connected to the working directory
        widget. (see: handle_exec_method())
        zget_ipython().kernel.get_cwd()N)r*   r+   )r!   r,   r%   r%   r&   Úget_cwdw   s    zShellWidget.get_cwdc             C   s(   t j| |d}| ¡ }| jj |¡ dS )z)Set color scheme for matched parentheses.)ÚparentÚcolor_schemeN)ÚshZBaseSHZget_matched_p_colorZ_bracket_matcherr4   ZsetBackground)r!   r;   ZbshZmpcolorr%   r%   r&   r   ƒ   s    z,ShellWidget.set_bracket_matcher_color_schemeTc             C   s\   |   |¡ t|ƒ\| _}|| _|  ¡  |  ¡  |r>| jdd |sN|  d¡ n
|  d¡ dS )zSet color scheme of the shell.T)Úclearz%colors linuxz%colors lightbgN)r   r   Ústyle_sheetr    Z_style_sheet_changedÚ_syntax_style_changedÚresetr6   )r!   r;   r@   Z
dark_colorr%   r%   r&   Úset_color_scheme‰   s    
zShellWidget.set_color_schemec             C   s   d}| j rdS |  |¡ dS )z%Ask the kernel for sys.path contents.z"get_ipython().kernel.get_syspath()N)r*   r+   )r!   r,   r%   r%   r&   Úget_syspath—   s    zShellWidget.get_syspathc             C   s   d}| j rdS |  |¡ dS )z)Ask the kernel for environment variables.zget_ipython().kernel.get_env()N)r*   r+   )r!   r,   r%   r%   r&   Úget_envŸ   s    zShellWidget.get_envc       
      C   s¼   yddl m} W n tk
r(   d}Y nX d| jd  dd| jd  |g}d |¡}| jd	 }| jd
 }t d¡}|rŠ|rŠ|rŠd}|| }| jd }|r¤d}	||	 }|r¸|r¸d}	||	 }|S )z-Banner for IPython widgets with pylab messager   )Úquick_guider   z
Python %s
Úpython_versionz@Type "copyright", "credits" or "license" for more information.

z.IPython %s -- An enhanced Interactive Python.
Úipython_versionZpylabZautoload_pylabZ
matplotlibz@
Populating the interactive namespace from numpy and matplotlib
ZsympyzÙ
These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
zµ
Warning: pylab (numpy and matplotlib) and symbolic math (sympy) are both 
enabled at the same time. Some pylab functions are going to be overrided by 
the sympy module (e.g. plot)
)ZIPython.core.usagerD   Ú	Exceptionr   Újoinr   r
   Zis_module_installed)
r!   rD   Zbanner_partsÚbannerZpylab_oZautoload_pylab_oZmpl_installedZpylab_messageZsympy_oÚlinesr%   r%   r&   Úlong_banner¨   s0    






zShellWidget.long_bannerc             C   s   d| j d | j d f }|S )z/Short banner with Python and QtConsole versionszPython %s -- IPython %srE   rF   )r   )r!   rI   r%   r%   r&   Úshort_bannerÖ   s    zShellWidget.short_bannerc             C   s    | j r|  d¡ n
|  d¡ d S )Nr=   z%clear)r*   Údbg_exec_magicÚexecute)r!   r%   r%   r&   Úclear_consoleÞ   s    zShellWidget.clear_consolec             C   s   t  dd¡}| jd|d d S )NÚipython_consoleÚshow_reset_namespace_warningT)ÚsilentÚwarning)r   ÚgetÚreset_namespace)r!   rS   r%   r%   r&   Ú_reset_namespaceä   s    zShellWidget._reset_namespaceFc             C   s4  t dƒ}t dƒ}|r®ttj| d}| |¡ | t dƒ¡ | tjtjB ¡ | 	tj¡ | 
d¡ | d¡ | |¡ | ¡ }t dd| ¡  ¡ | ¡  | j_|tjkr®d	S yj| jrÄ|  d
d¡ nR|rú|ræ|  ¡  | jt dƒdd |  d¡ |  ¡  n
|  d¡ | js|  d¡ W n tk
r.   Y nX d	S )z>Reset the namespace by removing all names defined by the user.zRemove all variableszMAll user-defined variables will be removed. Are you sure you want to proceed?)Ziconr:   zDon't show again.FTrP   rQ   Nr@   z-fz&<br><br>Removing all variables...
<hr>)Zbefore_promptz	%reset -fz,get_ipython().kernel.close_all_mpl_figures())r   r   r   ÚWarningZsetWindowTitleZset_checkbox_textZsetStandardButtonsZYesZNoZsetDefaultButtonZset_checkedZset_check_visibleZsetTextZexec_r   ÚsetZ
is_checkedr   Zreset_warningr*   rM   r@   Z_append_htmlr6   Zrefresh_namespacebrowserrN   r   ÚAttributeError)r!   rS   rR   ÚmessageZ	reset_strZwarn_strZboxZanswerr%   r%   r&   rU   è   sB    









zShellWidget.reset_namespacec       	         sº   t ˆ jjddˆ d}t ˆ jddˆ d}t ˆ jjddˆ d}t ‡ fdd„dd	ˆ d}t ‡ fd
d„ddˆ d}t ˆ jjddˆ d}t ˆ jjddˆ d}t ˆ jjddˆ d}||||||||gS )z Create shortcuts for ipyconsole.ZConsolezInspect current object)Úcontextr2   r:   zClear shellrP   zRestart kernelc                  s
   ˆ j  ¡ S )N)Ú
new_clientÚemitr%   )r!   r%   r&   Ú<lambda>"  s    z.ShellWidget.create_shortcuts.<locals>.<lambda>znew tabc                  s   ˆ   ¡ S )N)rV   r%   )r!   r%   r&   r^   %  s    zreset namespaceZarray_builderzenter array inlinezenter array tableZconsolez
clear line)	r   Z_controlZinspect_current_objectrO   r   Úrestart_kernelZenter_array_inlineZenter_array_tableÚ
clear_line)	r!   ÚinspectrO   r_   Znew_tabrU   Zarray_inlineZarray_tabler`   r%   )r!   r&   r     s2    





zShellWidget.create_shortcutsc             C   s2   y| j jt|ƒdd W n tk
r,   Y nX dS )z8Execute code in the kernel without increasing the promptT)rR   N)r(   rN   r   rY   )r!   r,   r%   r%   r&   r6   6  s    zShellWidget.silent_executec             C   s\   t t ¡ ƒ}t |ƒ}| jdkr"dS | jjdd||id}|| j|< |  |d¡| jd |< dS )a‡  Silently execute a kernel method and save its reply

        The methods passed here **don't** involve getting the value
        of a variable but instead replies that can be handled by
        ast.literal_eval.

        To get a value see `get_value`

        Parameters
        ----------
        code : string
            Code that contains the kernel method as part of its
            string

        See Also
        --------
        handle_exec_method : Method that deals with the reply

        Note
        ----
        This is based on the _silent_exec_callback method of
        RichJupyterWidget. Therefore this is licensed BSD
        Nr   T)rR   Úuser_expressionsr+   rN   )r   ÚuuidZuuid1r(   rN   Ú_kernel_methodsZ_ExecutionRequestZ_request_info)r!   r,   Z
local_uuidZmsg_idr%   r%   r&   r+   =  s    


zShellWidget.silent_exec_methodc             C   s8  |d   d¡}|sdS x|D ]}|| jkr| j| }|| }|  d¡}d|kr|dk	r|d|kr|t |d ¡}t |¡}nd}| j |¡ n”d|krÖ|dk	rÂd|krÂt |d ¡}t |¡}	nd}	| j |	¡ nNd|kr0|dk	rd|krt |d ¡| _tr t	 
| j¡| _nd	| _| j | j¡ nôd
|krp|dk	r^d|kr^t |d ¡}
nd}
| j |
¡ n´d|kr°|dk	ržd|kržt |d ¡}nd}| j |¡ ntd|krî|dk	r$d|kr$|d }d|kr$| j | ¡ n6|dk	rd|krt |d ¡| _nd| _| j ¡  | j |¡ qW dS )z¿
        Handle data returned by silent executions of kernel methods

        This is based on the _handle_exec_callback of RichJupyterWidget.
        Therefore this is licensed BSD.
        Zcontentrb   NÚdataZget_namespace_viewz
text/plainZget_var_propertiesr9   r   rB   rC   ÚgetattrZSpyderKernel)rT   rd   ÚastZliteral_evalÚsig_namespace_viewr]   Úsig_var_propertiesr   r   r	   Zto_unicode_from_fsÚsig_change_cwdÚsig_show_syspathÚsig_show_envÚsig_is_spykernelr   Úsig_got_replyÚpop)r!   ÚmsgZuser_expZ
expressionÚmethodZreplyre   ÚliteralZviewZ
propertiesZsyspathÚenvr-   r%   r%   r&   Úhandle_exec_methodb  s^    








zShellWidget.handle_exec_methodc             C   sb   d}|  ¡ }x,|D ]$}| d¡sd|ks0d|krd}P qW |r^tdƒ}|  |d ¡ |  d¡ d	S )
zy
        Mayavi plots require the Qt backend, so we try to detect if one is
        generated to change backends
        Fú#zimport mayavizfrom mayaviTz"Changing backend to Qt4 for MayaviÚ
z%gui inline
%gui qt4N)Ú
splitlinesÚ
startswithr   Z_append_plain_textr6   )r!   ÚcommandZcalling_mayavirJ   ÚlrZ   r%   r%   r&   Úset_backend_for_mayavi£  s    

z"ShellWidget.set_backend_for_mayavic             C   s0   |  d¡r,t| ¡ ƒdkr,d|kr,|  |¡ dS )zÃ
        If the user is trying to change Matplotlib backends with
        %matplotlib, send the same command again to the kernel to
        correctly change it.

        Fixes issue 4002
        z%matplotlibé   ZinlineN)rx   Úlenrw   r6   )r!   ry   r%   r%   r&   Úchange_mpl_backend´  s    
zShellWidget.change_mpl_backendc             C   s   |   |¡ |  ¡  dS )zV
        Reimplemented to reset the prompt if the error comes after the reply
        N)Z_process_execute_errorZ_show_interpreter_prompt)r!   rp   r%   r%   r&   Ú_handle_errorÂ  s    
zShellWidget._handle_errorc                s   t t| ƒ |¡}| j |¡S )z$Reimplement the IPython context menu)r   r   Ú_context_menu_maker   Zadd_actions_to_context_menu)r!   ÚposZmenu)r$   r%   r&   r€   É  s    zShellWidget._context_menu_makec             C   s,   | j r
dS | jd }|r |  ¡ S |  ¡ S dS )zh
        Reimplement banner creation to let the user decide if he wants a
        banner or not
        r   Zshow_bannerN)r   r   rK   rL   )r!   Zshow_banner_or%   r%   r&   Ú_banner_defaultÎ  s    
zShellWidget._banner_defaultc             C   s$   |rt dƒnt dƒ}| j |¡ d S )NzKernel died, restartingzKernel restarting)r   Úsig_kernel_restartedr]   )r!   Zdiedrp   r%   r%   r&   Ú_kernel_restarted_messageÜ  s    z%ShellWidget._kernel_restarted_messagec             C   s@   | j dkrdS | jr.t| jƒ| j _| j  ¡  n| j  | j¡ dS )z@Refresh the highlighting with the current syntax style by class.N)Ú_highlighterr    r   Z_styleZ_clear_cachesZset_style_sheetr>   )r!   r%   r%   r&   r?   à  s    
z!ShellWidget._syntax_style_changedc             C   s   | j sd| j_| j ¡  dS )z'Emit a signal when the prompt is ready.TN)r*   r…   Zhighlighting_onÚsig_prompt_readyr]   )r!   r%   r%   r&   Ú_prompt_started_hookë  s    z ShellWidget._prompt_started_hookc                s   | j  ¡  tt| ƒ |¡S )z7Reimplement Qt method to send focus change notification)Úfocus_changedr]   r   r   ÚfocusInEvent)r!   Úevent)r$   r%   r&   r‰   ò  s    
zShellWidget.focusInEventc                s   | j  ¡  tt| ƒ |¡S )z7Reimplement Qt method to send focus change notification)rˆ   r]   r   r   ÚfocusOutEvent)r!   rŠ   )r$   r%   r&   r‹   ÷  s    
zShellWidget.focusOutEvent)T)FTF)T)2Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úobjectrh   ri   rk   rl   ÚstrÚintZsig_pdb_steprˆ   r\   rn   rm   rƒ   r†   rj   r   r'   r)   r-   r8   r9   r   rA   rB   rC   rK   rL   rO   rV   rU   r   r6   r+   rt   r{   r~   r   r€   r‚   r„   r?   r‡   r‰   r‹   Ú__classcell__r%   r%   )r$   r&   r      sT   

	.
0%A
r   )"r   rg   r1   rc   Zqtpy.QtCorer   Zqtpy.QtWidgetsr   Zspyder.config.mainr   Zspyder.config.baser   Zspyder.config.guir   Zspyder.py3compatr   r   Zspyder.utilsr	   r
   r   r<   Zspyder.utils.ipython.styler   r   Zspyder.widgets.helperwidgetsr   Zspyder.widgets.ipythonconsoler   r   r   r   r   r   r%   r%   r%   r&   Ú<module>	   s    