
,]c           @@  s  d  Z  d d l m Z d d l Z d d l Z d d l j 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/ m0 Z0 m1 Z1 m2 Z2 m3 Z3 d d l4 m5 Z5 d d l6 m7 Z7 d d l8 m9 Z9 d d l: m; Z; d d l< m= Z= e d d  Z> e j? e> d d d  Z@ e j? e> d d  ZA eB e j? eA d   jC   ZD eB e j? eA d   jC   ZE eB e j? eA d   jC   ZF y e jG Wn eH k
 re j e _G n Xd    ZI d! e e; f d"     YZJ d S(#   sP   
Client widget for the IPython Console

This is the widget used on all its tabs
i    (   t   absolute_importN(   t   Template(   t   Thread(   t   QUrlt   QTimert   Signalt   Slot(   t   QKeySequence(   t   QHBoxLayoutt   QLabelt   QMenut   QMessageBoxt   QToolButtont   QVBoxLayoutt   QWidget(   t   _t   get_image_patht   get_module_source_patht   running_under_pytest(   t   get_fontt   get_shortcut(   t   icon_manager(   t
   sourcecode(   t
   get_coding(   t   RemoteEnvDialog(   t   get_temp_dir(   t   add_actionst   create_actiont   create_toolbuttont   DialogManagert   MENU_SEPARATOR(   t   to_text_string(   t   WebView(   t   ShellWidget(   t   SaveHistoryMixin(   t   CollectionsEditort   spydert   utilst   helpt   statict   csst   ipythont	   templatess
   blank.htmls   loading.htmls   kernel_error.htmlc         C@  s   t  d |   } | j   | S(   so   
    Call a function in a simple thread, to prevent blocking

    Taken from the Jupyter Qtconsole project
    t   target(   R   t   start(   t   ft   t(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt
   backgroundF   s    
t   ClientWidgetc        
   B@  sC  e  Z d  Z d j e j d e j    Z d d g Z	 e
 e e  Z d, d, d, e e d, e e e d 	 Z e d    Z e d    Z e d    Z d	   Z e d
  Z d   Z d   Z 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& e   d    Z' e e  d    Z( e   d    Z) e   d    Z* e   d    Z+ e   d    Z, d    Z- e e.  d!    Z/ e e.  d"    Z0 d#   Z1 e d$  Z2 d%   Z3 e e4  d&    Z5 d'   Z6 d(   Z7 d)   Z8 d*   Z9 d+   Z: RS(-   s   
    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.
    s   {0}## ---({1})---i   s   # -*- coding: utf-8 -*-s+   # *** Spyder Python Console History Log ***c         @  s5  t  t   j    t j  |  |  _ |  _ |  _ |	  _ |
  _ |  _	 |  _
 |  _ |  _ |  _ d   _ d   _ d   _ t j d   _ g   _ t  _ d   _ t  _ t d | d  d | d | d | d t   _ t    _  j    j     _!  j"   d   _# t$ j%    _& t'    _( t)   }  j*   } t+   } | j,  j-    | j. d  x | D] } | j, |  qW| j/ |  | j0 d d d d  | j,  j  | j,  j   j1 |     f d	    _2 t3    _4  j5   d  S(
   Nt   stopt   configt	   ipyclientt   additional_optionst   interpreter_versionst   external_kernelt   local_kerneli    c           @  s     j  d   S(   Nt   client(   t   close_client(    (   t   plugint   self(    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt   <lambda>   t    (6   t   superR0   t   __init__R"   t   id_t   connection_filet   hostnamet   menu_actionst   slaveR6   t
   given_namet   show_elapsed_timet   reset_warningt   ask_before_restartt   Nonet   options_buttont   stop_buttont   reset_buttont   imat   icont	   stop_icont   historyt   Truet   allow_renamet
   stderr_dirt   Falset   is_error_shownR!   t   shellwidgetR    t
   infowidgett   set_infowidget_fontt   _create_loading_paget   loading_paget   _show_loading_paget
   time_labelt   timet	   monotonict   t0R   t   timerR   t   get_toolbar_buttonsR   t	   addWidgett   create_time_labelt
   addStretcht	   addLayoutt   setContentsMarginst	   setLayoutt   exit_callbackR   t   dialog_managert   update_time_label_visibility(   R;   R:   R@   t   history_filenamet   config_optionsR4   R5   RA   RB   RC   RD   R6   RE   RF   RG   RH   t   vlayoutt   toolbar_buttonst   hlayoutt   button(    (   R:   R;   sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR?   b   s`    																		

			c         C@  s6   |  j  d k	 r2 t j |  j   } | j d  d Sd S(   s   Get kernel ids   .jsoni    N(   RA   RI   t   ospt   basenamet   split(   R;   t	   json_file(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt	   kernel_id   s    c         C@  s   d } |  j d k	 r |  j d } |  j d k	 rI t j |  j |  } q y t j t   |  } Wq t t f k
 r d } q Xn  | S(   s&   Filename to save kernel stderr output.s   .stderrN(	   RI   RA   Ru   RS   Rq   t   joinR   t   IOErrort   OSError(   R;   t   stderr_file(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyRy      s    c         C@  sU   |  j  d k	 rK y t j |  j  d d d } WqQ t k
 rG d } qQ Xn d } | S(   s   Get handle to stderr_file.t   wt   encodings   utf-8N(   Ry   RI   t   codecst   opent	   Exception(   R;   t   handle(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt   stderr_handle   s    c         C@  s9   y! |  j  j   t j |  j  Wn t k
 r4 n Xd S(   s.   Remove stderr_file associated with the client.N(   R   t   closet   ost   removeRy   R~   (   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt   remove_stderr_file   s
    c         C@  sm  | r |  j    j   n  |  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  |  j j j |  j  |  j j j |  j j  |  j |  j j d t |  j j j |  j  |  j j j |  j  d S(   s-   Configure shellwidget after kernel is startedt   resetN(   t   get_controlt   setFocusRV   t   set_exit_callbackt	   executingt   connectt   add_to_historyt   set_backend_for_mayavit   executedt   update_historyt   refresh_namespacebrowsert   enable_stop_buttont   disable_stop_buttont   sig_kernel_restartedt   kernel_restarted_messaget   change_mpl_backendt   sig_show_syspatht   show_syspatht   sig_show_envt   show_envt   get_cwdt   set_color_schemet   syntax_styleRT   t   sig_prompt_readyt   _hide_loading_paget   _show_mpl_backend_errors(   R;   t
   give_focus(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt   configure_shellwidget   s,    
c         C@  s   |  j  j t  d  S(   N(   RK   t
   setEnabledRQ   (   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR     s    c         C@  s#   |  j  j s |  j j t  n  d  S(   N(   RV   t
   _executingRK   t   setDisabledRQ   (   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR     s    c         C@  s=   |  j  j t  |  j j s) |  j   n |  j j d  d S(   s;   Method to handle what to do when the stop button is pressedt   exitN(   RK   R   RQ   RV   t   _readingt   interrupt_kernelt   write_to_stdin(   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt   stop_button_click_handler  s    c         C@  s   t  j |  } | r* | j | d  } n  | j d d  } t d  } t t  } | j d t d | d |  } |  j j	 | t
 j t   |  j j   |  j j   t |  _ d S(	   s0   Show kernel initialization errors in infowidget.s   <br>t   -s   &#8209s*   An error ocurred while starting the kernelt   css_patht   messaget   errorN(   R   t   get_eol_charst   replaceR   R   t   KERNEL_ERRORt
   substitutet   CSS_PATHRW   t   setHtmlR   t   fromLocalFileRV   t   hidet   showRQ   RU   (   R;   R   t   eolR   t   kernel_error_templatet   page(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt   show_kernel_error(  s    	c         C@  s   |  j  d k rc |  j d k r- t d  } n	 |  j } |  j d d |  j d } | d | } n |  j  d |  j d } | S(   s   Return client namet   Consolet   int_idu   /t   str_idu    N(   RE   RI   RB   R   R@   (   R;   t   namet	   client_id(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt   get_nameB  s    	c         C@  s0   |  j  j } | r" | j   r" | S|  j  j Sd S(   s4   Return the text widget (or similar) to give focus toN(   RV   t   _page_controlt	   isVisiblet   _control(   R;   t   page_control(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR   Q  s    c         C@  s
   |  j  j S(   s&   Get kernel associated with this client(   RV   t   kernel_manager(   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt
   get_kernelZ  s    c         C@  s   t  |  t d  d t j d  d |  j } t  |  t d  d |  j |  _ t  |  t d  d t j d  d |  j j } t  |  t d	  d t j d
  d |  j j	 } |  j j
 |  j  | t | | |  j g } |  j d k	 r |  j | S| Sd S(   s   Return options menus   Remove all variablesRN   t
   editdeletet	   triggereds   Show elapsed timet   toggleds   Show environment variablest   environs   Show sys.path contentst   syspathN(   R   R   RM   RN   t   reset_namespacet   set_elapsed_time_visiblet   show_time_actionRV   t   get_envt   get_syspatht
   setCheckedRF   R   RC   RI   (   R;   t   reset_actiont
   env_actiont   syspath_actiont   additional_actions(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt   get_options_menu^  s0    c      
   C@  s  g  } |  j  d k rh t |  d t d  d |  j d t d  |  _  |  j   |  j  j j |  j  n  |  j  d k	 r | j	 |  j   n  |  j
 d k r t |  d t d  d t j d  d t d  d	 |  j |  _
 n  |  j
 d k	 r | j	 |  j
  n  |  j d k r|  j   } | rt |  d t d
  d t j d  |  _ |  j j t j  t |   } t | |  |  j j |  qn  |  j d k	 r| j	 |  j  n  | S(   s   Return toolbar buttons list.t   textt   StopRN   t   tips   Stop the current commandt   RemoveR   s   Remove all variablesR   t   Optionst   tooloptionsN(   RK   RI   R   R   RO   R   t   clickedR   R   t   appendRL   RM   RN   R   RJ   R   t   setPopupModeR   t   InstantPopupR
   R   t   setMenu(   R;   t   buttonst   optionst   menu(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyRa     s@    	
	!c      	   C@  s1  t  |  t d  t t d d   d t j d  d |  j } t  |  t d  t t d d   d |  j } t  |  t d	  t t d
 d   d t j d  d |  j } t  |  t d  t t d d   d |  j	 } t  |  t d  d t j d  d |  j
 } t | d | | | | d | f  | S(   s*   Add actions to IPython widget context menus   Inspect current objectt   consoles   inspect current objectRN   t   MessageBoxInformationR   s   Clear line or blocks
   clear lines   Remove all variablest   ipython_consoles   reset namespaceR   s   Clear consoles   clear shells   &QuitR   N(   R   R   R   R   RM   RN   t   inspect_objectt
   clear_lineR   t   clear_consoleRh   R   RI   (   R;   R   t   inspect_actiont   clear_line_actiont   reset_namespace_actiont   clear_console_actiont   quit_action(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt   add_actions_to_context_menu  s4    		!c         C@  s#   |  j  j j |  | |  j  _ d S(   s   Set IPython widget's fontN(   RV   R   t   setFontt   font(   R;   R   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt   set_font  s    c         C@  s#   t  d d  } |  j j |  d S(   s   Set font for infowidgett   optiont	   rich_fontN(   R   RW   R   (   R;   R   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyRX     s    c         C@  s/   y |  j  j | |  Wn t k
 r* n Xd S(   s   Set IPython color scheme.N(   RV   R   t   AttributeError(   R;   t   color_schemeR   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR     s    c         C@  s[   |  j    d k	 r/ |  j r/ |  j j j   n  |  j j d k	 rW t |  j j j  n  d S(   s   Shutdown kernelN(	   R   RI   RD   RV   R   t   shutdown_kernelt   kernel_clientR/   t   stop_channels(   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt   shutdown  s    c         C@  s)   y |  j  j   Wn t k
 r$ n Xd S(   s7   Interrupt the associanted Spyder kernel if it's runningN(   RV   t   request_interrupt_kernelt   RuntimeError(   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR     s    c         C@  sL  |  j  } t   rY |  j rY t d  } t j t j B} t j |  t d  | |  } n d } | t j k s t   s |  j rH| j	 r,|  j
 j   r |  j
 j   | j   n  y | j	 j d |  j  Wn0 t k
 r } | j t d  | d t qEX|  j | j  | j t d  d t qH| j t d  d t n  d S(	   s   
        Restart the associated kernel.

        Took this code from the qtconsole project
        Licensed under the BSD license
        s,   Are you sure you want to restart the kernel?s   Restart kernel?t   stderrs   Error restarting kernel: %s
t   before_prompts!   <br>Restarting kernel...
<hr><br>s.   Cannot restart a kernel not started by Spyder
N(   RV   R   RH   R   R   t   Yest   Not   questionRI   R   RW   R   R   R   t   restart_kernelR   R   t   _append_plain_textRQ   R   R   t   _append_htmlRT   (   R;   t   swR   R   t   resultt   e(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR    s6    		
	
c         C@  sn   |  j  sP y |  j   } Wn t k
 r2 d } n X| rj |  j d |  qj n |  j j d | d t d S(   s$   Show kernel restarted/died messages.s   <tt>%s</tt>s   <br>%s<hr><br>R   N(   RU   t   _read_stderrR~   RI   R   RV   R  RT   (   R;   t   msgR   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR     s    	
c         C@  s   |  j  j j   d S(   s2   Show how to inspect an object with our Help pluginN(   RV   R   t   inspect_current_object(   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR   2  s    c         C@  s   |  j  j   d S(   s   Clear a console lineN(   RV   t   _keyboard_quit(   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR   7  s    c         C@  s   |  j  j   d S(   s   Clear the whole consoleN(   RV   R   (   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR   <  s    c         C@  s    |  j  j d |  j d t  d S(   s>   Resets the namespace by removing all names defined by the usert   warningR   N(   RV   R   RG   RQ   (   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR   A  s    c         C@  s   |  j  j |  _ d  S(   N(   RV   t   _historyRP   (   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR   G  s    c         C@  s^   | d k	 rV t   } | j | d d d t d d d t j d  |  j j |  n d Sd S(	   s   Show sys.path contents.t   titles   sys.path contentst   readonlyt   widthiX  RN   R   N(   RI   R#   t   setupRQ   RM   RN   Ri   R   (   R;   R   t   editor(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR   J  s    	c         C@  s   |  j  j t |   d S(   s   Show environment variables.N(   Ri   R   R   (   R;   t   env(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR   U  s    c         C@  s%   |  j  d k r t   |  _  n  |  j  S(   s=   Create elapsed time label widget (if necessary) and return itN(   R\   RI   R	   (   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyRc   Z  s    c         C@  s   |  j  d k r d St j   |  j } | d k  rJ t j   |  _ d } n  | d
 k r_ d } n d } | rt d } n d } d	 | t j | t j |   f } |  j  j |  d S(   s   Text to show in time_label.Ni    i   i  s   %d %H:%M:%Ss   %H:%M:%Ss   #AAAAAAs   #AA6655s(   <span style='color: %s'><b>%s</b></span>iQ (   R\   RI   R]   R^   R_   t   strftimet   gmtimet   setText(   R;   t   endt   elapsed_timet   fmtt   colorR   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt	   show_time`  s     			c         C@  s   |  j  j |  j  d S(   s   Update elapsed time visibility.N(   R\   t
   setVisibleRF   (   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyRj   w  s    c         C@  s/   | |  _  |  j d k	 r+ |  j j |  n  d S(   s%   Slot to show/hide elapsed time label.N(   RF   R\   RI   R  (   R;   t   state(    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR   {  s    	c         C@  sj   t  t  } t d  } t j d k r< | j d d  } n  t d  } | j d t d | d |  } | S(	   s5   Create html page to show while the kernel is startings   loading_sprites.pngt   nts   \t   /s   Connecting to kernel...R   t   loading_imgR   (	   R   t   LOADINGR   R   R   R   R   R   R   (   R;   t   loading_templateR   R   R   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyRY     s    	c         C@  s=   |  j  j   |  j j   |  j j |  j t j t   d S(   s+   Show animation while the kernel is loading.N(	   RV   R   RW   R   R   RZ   R   R   R   (   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR[     s    c         C@  sD   |  j  j   |  j j   |  j  j t  |  j j j |  j  d S(   s1   Hide animation shown while the kernel is loading.N(	   RW   R   RV   R   R   t   BLANKR   t
   disconnectR   (   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR     s    c         C@  s]   t  |  j d  } z9 | j   } | s+ d St |  } t | |  } | SWd | j   Xd S(   s#   Read the stderr file of the kernel.t   rbR=   N(   R}   Ry   t   readR   R   R   (   R;   R-   t   stderr_textR{   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR    s    c         C@  s6   |  j  s |  j j d  n  |  j j j |  j  d S(   sT   
        Show possible errors when setting the selected Matplotlib backend.
        s/   get_ipython().kernel._show_mpl_backend_errors()N(   R6   RV   t   silent_executeR   R$  R   (   R;   (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR     s
    		
N(;   t   __name__t
   __module__t   __doc__t   formatR   t   linesepR]   t   ctimet	   SEPARATORt   INITHISTORYR   t   strt   append_to_historyRI   RT   RQ   R?   t   propertyRu   Ry   R   R   R   R   R   R   R   R   R   R   R   R   Ra   R   R   RX   R   R   R   R  R   R   R   R   R   R   t   objectR   R   Rc   R  Rj   t   boolR   RY   R[   R   R  R   (    (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyR0   T   sb   	E	5		
						#	*	"						.							(K   R+  t
   __future__R    R|   R   t   os.patht   pathRq   t   stringR   t	   threadingR   R]   t   qtpy.QtCoreR   R   R   R   t
   qtpy.QtGuiR   t   qtpy.QtWidgetsR   R	   R
   R   R   R   R   t   spyder.config.baseR   R   R   R   t   spyder.config.guiR   R   t   spyder.utilsR   RM   R   t   spyder.utils.encodingR   t   spyder.utils.environR   t   spyder.utils.programsR   t   spyder.utils.qthelpersR   R   R   R   R   t   spyder.py3compatR   t   spyder.widgets.browserR    t   spyder.widgets.ipythonconsoleR!   t   spyder.widgets.mixinsR"   t1   spyder.widgets.variableexplorer.collectionseditorR#   t
   UTILS_PATHRv   R   t   TEMPLATES_PATHR}   R&  R#  R!  R   R^   R   R/   R0   (    (    (    sC   lib/python2.7/site-packages/spyder/widgets/ipythonconsole/client.pyt   <module>   sF   "4"(	