
,]c           @   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  d e e e f d     YZ! d S(   s&   
Shell Widget for the IPython Console
iN(   t   Signal(   t   QMessageBox(   t   CONF(   t   _(   t   config_shortcut(   t   PY2t   to_text_string(   t   encoding(   t   programs(   t   syntaxhighlighters(   t   create_qss_stylet   create_style_class(   t   MessageCheckBox(   t   ControlWidgett   DebuggingWidgett
   HelpWidgett   NamepaceBrowserWidgett   PageControlWidgett   ShellWidgetc           B   s  e  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 d   Z d   Z d   Z d   Z d   Z d   Z d   Z e d  Z d	   Z d
   Z d   Z d   Z d   Z  d   Z! e" e e" d  Z# d   Z$ d   Z% d   Z& d   Z' d   Z( d   Z) d   Z* d   Z+ d   Z, e d  Z- d   Z. d   Z/ d   Z0 d   Z1 RS(   sb   
    Shell widget for the IPython Console

    This is the widget in charge of executing code
    c         O   s   t  |  _ t |  _ t |  _ t t |   j | |   | |  _	 | |  _
 | |  _ | |  _ d |  _ |  j   |  _ d  |  _ |  j |  j  d  S(   Nt    (   R   t   custom_controlR   t   custom_page_controlt   Truet   custom_editt   superR   t   __init__t	   ipyclientt   additional_optionst   interpreter_versionst   external_kernelt   _cwdt   create_shortcutst	   shortcutst   Nonet   _kernel_replyt    set_bracket_matcher_color_schemet   syntax_style(   t   selfR   R   R   R   t   argst   kw(    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyR   <   s    									c         C   s   |  j  j |  j j  d S(   s!   Set exit callback for this shell.N(   t   exit_requestedt   connectR   t   exit_callback(   R%   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   set_exit_callbackV   s    c         C   s'   |  j  d  k	 r |  j  j r t St Sd  S(   N(   t   kernel_clientR!   t   channels_runningR   t   False(   R%   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt
   is_runningZ   s    c         C   s$   d } |  j  r d S|  j |  d S(   s'   Determine if the kernel is from Spyder.u1   getattr(get_ipython().kernel, 'set_value', False)N(   t   _readingt   silent_exec_method(   R%   t   code(    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   is_spyder_kernela   s    	c         C   sy   t  j d k r$ | j d d  } n  |  j su d j |  } |  j r\ |  j j d |  n |  j |  | |  _	 n  d S(   s$   Set shell current working directory.t   ntu   \u   \\u'   get_ipython().kernel.set_cwd(u'''{}''')u   !N(
   t   ost   namet   replaceR   t   formatR0   R,   t   inputt   silent_executeR   (   R%   t   dirnameR2   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   set_cwdi   s    		c         C   s$   d } |  j  r d S|  j |  d S(   s   Update current working directory.

        Retrieve the cwd and emit a signal connected to the working directory
        widget. (see: handle_exec_method())
        u   get_ipython().kernel.get_cwd()N(   R0   R1   (   R%   R2   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   get_cwdw   s    	c         C   s;   t  j d |  d |  } | j   } |  j j j |  d S(   s)   Set color scheme for matched parentheses.t   parentt   color_schemeN(   t   sht   BaseSHt   get_matched_p_colort   _bracket_matcherR8   t   setBackground(   R%   R?   t   bsht   mpcolor(    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyR#      s    c         C   s   |  j  |  t |  \ |  _ } | |  _ |  j   |  j   | rX |  j d t  n  | sn |  j d  n |  j d  d S(   s   Set color scheme of the shell.t   clears   %colors linuxs   %colors lightbgN(	   R#   R
   t   style_sheetR$   t   _style_sheet_changedt   _syntax_style_changedt   resetR   R:   (   R%   R?   RK   t
   dark_color(    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   set_color_scheme   s    	

c         C   s$   d } |  j  r d S|  j |  d S(   s%   Ask the kernel for sys.path contents.u"   get_ipython().kernel.get_syspath()N(   R0   R1   (   R%   R2   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   get_syspath   s    	c         C   s$   d } |  j  r d S|  j |  d S(   s)   Ask the kernel for environment variables.u   get_ipython().kernel.get_env()N(   R0   R1   (   R%   R2   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   get_env   s    	c   
      C   s   y d d l  m } Wn t k
 r- d } n Xd |  j d d d |  j d | g } d j |  } |  j d	 } |  j d
 } t j d  } | r | r | r d } | | } n  |  j d } | r d }	 | |	 } n  | r | r d }	 | |	 } n  | S(   s-   Banner for IPython widgets with pylab messagei(   t   quick_guideR   s
   Python %s
t   python_versions@   Type "copyright", "credits" or "license" for more information.

s.   IPython %s -- An enhanced Interactive Python.
t   ipython_versiont   pylabt   autoload_pylabt
   matplotlibs@   
Populating the interactive namespace from numpy and matplotlib
t   sympys   
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)
s   
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)
(   t   IPython.core.usageRP   t	   ExceptionR   t   joinR   R   t   is_module_installed(
   R%   RP   t   banner_partst   bannert   pylab_ot   autoload_pylab_ot   mpl_installedt   pylab_messaget   sympy_ot   lines(    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   long_banner   s0    
	c         C   s"   d |  j  d |  j  d f } | S(   s/   Short banner with Python and QtConsole versionss   Python %s -- IPython %sRQ   RR   (   R   (   R%   R\   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   short_banner   s    
c         C   s*   |  j  r |  j d  n |  j d  d  S(   NRG   s   %clear(   R0   t   dbg_exec_magict   execute(   R%   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   clear_console   s    	c         C   s,   t  j d d  } |  j d t d |  d  S(   Nt   ipython_consolet   show_reset_namespace_warningt   silentt   warning(   R   t   gett   reset_namespaceR   (   R%   Rk   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   _reset_namespace   s    c         C   s  t  d  } t  d  } | r t d t j d |   } | j |  | j t  d   | j t j t j B | j	 t j  | j
 t  | j t  | j |  | j   } t j d d | j    | j   |  j _ | t j k r d Sn  y |  j r|  j d	 d
  nr | r^| rD|  j   |  j t  d  d t n  |  j d  |  j   n |  j d  |  j s|  j d  n  Wn t k
 rn Xd S(   s>   Reset the namespace by removing all names defined by the user.s   Remove all variablessM   All user-defined variables will be removed. Are you sure you want to proceed?t   iconR>   s   Don't show again.Rh   Ri   NRK   s   -fs&   <br><br>Removing all variables...
<hr>t   before_prompts	   %reset -fs,   get_ipython().kernel.close_all_mpl_figures()(   R   R   R   t   Warningt   setWindowTitlet   set_checkbox_textt   setStandardButtonst   Yest   Not   setDefaultButtont   set_checkedR.   t   set_check_visibleR   t   setTextt   exec_R   t   sett
   is_checkedR   t   reset_warningR0   Re   RK   t   _append_htmlR:   t   refresh_namespacebrowserRf   R   t   AttributeError(   R%   Rk   Rj   t   messaget	   reset_strt   warn_strt   boxt   answer(    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyRm      sB    	

	c   	         s?  t    j j d d d d d   } t    j d d d d d   } t    j j d d d d d   } t    f d	   d d d d
 d   } t    f d   d d d d d   } t    j j d d d d d   } t    j j d d d d d   } t    j j d d d d d   } | | | | | | | | g S(   s    Create shortcuts for ipyconsole.t   contextt   ConsoleR6   s   Inspect current objectR>   s   Clear shellRh   s   Restart kernelc              s     j  j   S(   N(   t
   new_clientt   emit(    (   R%   (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   <lambda>"  R   s   new tabc              s
     j    S(   N(   Rn   (    (   R%   (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyR   %  R   s   reset namespacet   array_builders   enter array inlines   enter array tablet   consoles
   clear line(	   R   t   _controlt   inspect_current_objectRg   R   t   restart_kernelt   enter_array_inlinet   enter_array_tablet
   clear_line(	   R%   t   inspectRg   R   t   new_tabRm   t   array_inlinet   array_tableR   (    (   R%   sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyR     s2    		c         C   s8   y  |  j  j t |  d t Wn t k
 r3 n Xd S(   s8   Execute code in the kernel without increasing the promptRj   N(   R,   Rf   R   R   R   (   R%   R2   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyR:   6  s     c         C   s   t  t j    } t  |  } |  j d k r1 d S|  j j d d t d i | | 6} | |  j | <|  j | d  |  j	 d | <d S(   s  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   Rj   t   user_expressionsR1   Rf   (
   R   t   uuidt   uuid1R,   R!   Rf   R   t   _kernel_methodst   _ExecutionRequestt   _request_info(   R%   R2   t
   local_uuidt   msg_id(    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyR1   =  s    	c         C   s  | d j  d  } | s d Sx| D]} | |  j k r$ |  j | } | | } | j  d  } d | k r | d k	 r d | k r t j | d  } t j |  } n d } |  j j |  nd | k r#| d k	 r
d | k r
t j | d  } t j |  }	 n d }	 |  j j |	  nd | k r| d k	 r~d | k r~t j | d  |  _ t	 rt
 j |  j  |  _ qn	 d	 |  _ |  j j |  j  n@d
 | k r| d k	 rd | k rt j | d  }
 n d }
 |  j j |
  n d | k rC| d k	 r*d | k r*t j | d  } n d } |  j j |  n d | k r| d k	 rd | k r| d } d | k r|  j j |   qqnG | d k	 rd | k rt j | d  |  _ n	 d |  _ |  j j   |  j j |  q$ q$ Wd S(   s   
        Handle data returned by silent executions of kernel methods

        This is based on the _handle_exec_callback of RichJupyterWidget.
        Therefore this is licensed BSD.
        t   contentR   Nt   datat   get_namespace_views
   text/plaint   get_var_propertiesR=   R   RN   RO   t   getattrt   SpyderKernel(   Rl   R   R!   t   astt   literal_evalt   sig_namespace_viewR   t   sig_var_propertiesR   R   R   t   to_unicode_from_fst   sig_change_cwdt   sig_show_syspatht   sig_show_envt   sig_is_spykernelR"   t   sig_got_replyt   pop(   R%   t   msgt   user_expt
   expressiont   methodt   replyR   t   literalt   viewt
   propertiest   syspatht   envR3   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   handle_exec_methodb  s^    
	
	c         C   s   t  } | j   } xB | D]: } | j d  s d | k sF d | k rS t } PqS q q W| r t d  } |  j | d  |  j d  n  d S(   sy   
        Mayavi plots require the Qt backend, so we try to detect if one is
        generated to change backends
        t   #s   import mayavis   from mayavis!   Changing backend to Qt for Mayavis   
s   %gui inline
%gui qtN(   R.   t
   splitlinest
   startswithR   R   t   _append_plain_textR:   (   R%   t   commandt   calling_mayaviRb   t   lR   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   set_backend_for_mayavi  s    c         C   sJ   | j  d  rF t | j    d k rF d | k rF |  j |  qF n  d S(   s   
        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
        s   %matplotlibi   t   inlineN(   R   t   lenR   R:   (   R%   R   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   change_mpl_backend  s    c         C   s   |  j  |  |  j   d S(   sV   
        Reimplemented to reset the prompt if the error comes after the reply
        N(   t   _process_execute_errort   _show_interpreter_prompt(   R%   R   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   _handle_error  s    c         C   s(   t  t |   j |  } |  j j |  S(   s$   Reimplement the IPython context menu(   R   R   t   _context_menu_makeR   t   add_actions_to_context_menu(   R%   t   post   menu(    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyR     s    c         C   s8   |  j  r d S|  j d } | r* |  j   S|  j   Sd S(   sh   
        Reimplement banner creation to let the user decide if he wants a
        banner or not
        R   t   show_bannerN(   R   R   Rc   Rd   (   R%   t   show_banner_o(    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   _banner_default  s    	
c         C   s2   | r t  d  n	 t  d  } |  j j |  d  S(   Ns   Kernel died, restartings   Kernel restarting(   R   t   sig_kernel_restartedR   (   R%   t   diedR   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   _kernel_restarted_message  s    c         C   sX   |  j  d k r d S|  j rA t |  j  |  j  _ |  j  j   n |  j  j |  j  d S(   s@   Refresh the highlighting with the current syntax style by class.N(   t   _highlighterR!   R$   R   t   _stylet   _clear_cachest   set_style_sheetRH   (   R%   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyRJ     s    	c         C   s)   |  j  s% t |  j _ |  j j   n  d S(   s'   Emit a signal when the prompt is ready.N(   R0   R   R   t   highlighting_ont   sig_prompt_readyR   (   R%   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   _prompt_started_hook  s    	c         C   s#   |  j  j   t t |   j |  S(   s7   Reimplement Qt method to send focus change notification(   t   focus_changedR   R   R   t   focusInEvent(   R%   t   event(    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyR     s    c         C   s#   |  j  j   t t |   j |  S(   s7   Reimplement Qt method to send focus change notification(   R   R   R   R   t   focusOutEvent(   R%   R   (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyR     s    (2   t   __name__t
   __module__t   __doc__R    t   objectR   R   R   R   t   strt   intt   sig_pdb_stepR   R   R   R   R   R   R   R   R+   R/   R3   R<   R=   R#   R   RM   RN   RO   Rc   Rd   Rg   Rn   R.   Rm   R   R:   R1   R   R   R   R   R   R   R   RJ   R   R   R   (    (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyR      sT   															.			0			%	A								("   R   R   R5   R   t   qtpy.QtCoreR    t   qtpy.QtWidgetsR   t   spyder.config.mainR   t   spyder.config.baseR   t   spyder.config.guiR   t   spyder.py3compatR   R   t   spyder.utilsR   R   R	   R@   t   spyder.utils.ipython.styleR
   R   t   spyder.widgets.helperwidgetsR   t   spyder.widgets.ipythonconsoleR   R   R   R   R   R   (    (    (    sB   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.pyt   <module>	   s    (