
,]c           @   s  d  Z  d d l Z d d l Z d d l j Z d d l Z d d l Z d d l 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 d d	 l 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) d d l* m+ Z+ m, Z, d d l- m. Z/ y d d l0 Z0 Wn e1 k
 re2 Z0 n Xd d l3 m4 Z4 d d l5 m6 Z6 m7 Z7 m8 Z8 d d l9 m: Z: d d l; m< Z< d d l= m> Z> d d l? m@ Z@ mA ZA mB ZB d d lC mD ZD d d lE mF ZF d d lG mH ZH mI ZI d d lJ mK ZL d d lJ mM ZM mN ZN mO ZO d d lP mQ ZQ d d lR mS ZS mT ZT d d lU mV ZV d d lW mX ZX d d lY mZ ZZ d Z[ e4 j\ d e6 d  d  e[ d! e] d" Z^ e4 j\ d# e6 d$  d  e^ d! e] d% Z_ e4 j\ d& e6 d'  d  e_ eA r5d( n d) Z` e4 j\ d* e6 d+  d  e` d, Za e4 j\ d- e6 d.  d  ea d! e] d/   Zb d0 e2 e2 d1 d2  Zc d3 e f d4     YZd d5 e> f d6     YZe d7 e< f d8     YZf d S(9   s+   
IPython Console plugin based on QtConsole
iN(   t   find_connection_file(   t   jupyter_config_dirt   jupyter_runtime_dir(   t   QtKernelClient(   t   QtKernelManager(   t   PYQT5(   t   getopenfilename(   t   Qtt   Signalt   Slot(   t   QApplicationt	   QCheckBoxt   QDialogt   QDialogButtonBoxt   QFormLayoutt   QGridLayoutt	   QGroupBoxt   QHBoxLayoutt   QLabelt	   QLineEditt   QMessageBoxt   QPushButtont
   QTabWidgett   QVBoxLayoutt   QWidget(   t   Configt   load_pyconfig_files(   t   tunnel(   t   dependencies(   t   _t   get_conf_patht   get_home_dir(   t   CONF(   t   SpyderPluginWidget(   t   PluginConfigPage(   t	   is_stringt   PY2t   to_text_string(   t   SpyderKernelSpec(   t   create_qss_style(   t   create_actiont   MENU_SEPARATOR(   t   icon_manager(   t   encodingt   programst
   sourcecode(   t   get_temp_dir(   t   get_error_matcht   remove_backslashes(   t   FindReplace(   t   ClientWidget(   t   Tabss   >=0.7.3t   sympys+   Symbolic mathematics in the IPython Consolet   required_versiont   optionals   >=0.21t   cythons'   Run Cython files in the IPython Consoles   >=4.2.0t	   qtconsoles   Integrate the IPython consoles
   >=4.0;<6.0s   >=4.0t   IPythons&   IPython interactive python environments   >=2.0.0t
   matplotlibs*   Display 2D graphics in the IPython Consolec         C   s   t  j |   d  S(   N(   t   pexpectt   run(   t   cmd(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   _stop_tunnelY   s    s	   127.0.0.1g?c         C   s	  t  d  k r t d   n  d } | r8 | d | 7} n  d | k rj | j d  \ } }	 | d |	 7} n  d | | f }
 t  j |
 d t \ } } | s$t | | j d  d	 | j d
  ! } d | | | | | f }
 t  j |
 d t \ } } | s$t j	 t
 |
 j d d d   | Sn  d | | | | | | f }
 t j j   } | j d d   d } t  j |
 d | } t } xt ry | j | d g d d } | d k rH| j d  d } t d  | } t j |  t d  | t j t j Bt j  } | t j k r&| j d  wqH| j d  t t d    n  | d k rp| d  k	 rp| j |  n  WnS t  j k
 rqqt  j k
 r| j rt t d  |
   q| j SqX| s| d  k rt t d    n  | j |  t } qWd  S(    Ns(   pexpect unavailable, use paramiko_tunnels   ssh s   -i t   :s    -p %ss   %s -O check %st   withexitstatuss   (pid=i   t   )s&   %s -O forward -L 127.0.0.1:%i:%s:%i %ss
   -O forwards	   -O canceli   s/   %s -f -S none -L 127.0.0.1:%i:%s:%i %s sleep %it   SSH_ASKPASSs,   Are you sure you want to continue connectingt   envs   [Pp]assword:t   timeoutg?i    t   @isf   The authenticity of host <b>%s</b> can't be established. Are you sure you want to continue connecting?t   Warningt   yest   nos1   The authenticity of the host can't be establisheds   Tunnel '%s' failed to starts    Could not connect to remote host(   R;   t   Nonet   ImportErrort   splitR<   t   Truet   intt   findt   atexitt   registerR>   t   replacet   ost   environt   copyt   popt   spawnt   Falset   expectR   R   t   questiont   Yest   Not   sendlinet   RuntimeErrort   TIMEOUTt   EOFt
   exitstatust   pid(   t   selft   lportt   rportt   servert   remoteipt   keyfilet   passwordRD   t   ssht   portR=   t   outputR`   Ra   RC   t
   ssh_newkeyR   t   failedt   it   hostRY   t   reply(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   openssh_tunnel\   sn    )
			t   KernelConnectionDialogc           B   sA   e  Z d  Z d d  Z d   Z d   Z e d d d   Z RS(   s?   Dialog to connect to existing kernels (either local or remote).c   	         s-  t  t   j |   j t d   t t d   } | j t  | j t	 j
  t t d   } t    _  j j t d    j j d  t t d   } | j j  j  t   } | j |  | j  j  | j |  t t d    _ t t d       j t  t    _  j j t d	   t    _  j j t d
    j j t j  t    _  j j t d   t t d     j j  j  t   } | j  j  | j   t     j t d   j   j t d   j   j t d  |  t t j  t j! Bt	 j"    _#  j# j$ j  j%   j# j& j  j'  t(   } | j |  | j) |  | j  j  | j    | j)   | j  j#       f d   } |  j j*     j j+ j |  d  S(   Ns   Connect to an existing kernels   Please select the JSON connection file (<i>e.g.</i> <tt>kernel-3764.json</tt>) or enter the 4-digit ID (<i>e.g.</i> <tt>3764</tt>) of the existing kernel to connect to, and enter the SSH host name and credentials if a remote kernel.s   Kernel ID/Connection file:s$   ID number or path to connection filei   t   Browses!   This is a remote kernel (via SSH)s   <b>Note:</b> If connecting to a remote kernel, only the SSH keyfile <i>or</i> the Password field need to be completed, unless the keyfile is protected with a passphrase.s   username@hostname:ports.   Remote user password or SSH keyfile passphrases   Path to SSH keyfile (optional)s
   Host name:s	   Password:s   SSH keyfile:c            sw   x3    j   j  j  g D] } | j |   q Wx: t  j    D]& }  j d |  j   j |   qI Wd  S(   Ni   (   t   hnt   pwt   kft
   setEnabledt   ranget   rowCountt   itemAtt   widget(   t   statet   widRn   (   t   credential_labelt   kf_open_btnRb   t   ssh_form(    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   ssh_set_enabled   s
    (,   t   superRr   t   __init__t   setWindowTitleR   R   t   setWordWrapRL   t   setAlignmentR   t   AlignJustifyR   t   cft   setPlaceholderTextt   setMinimumWidthR   t   clickedt   connectt   select_connection_fileR   t	   addWidgetR   t   rm_cbRt   Ru   t   setEchoModet   PasswordRv   t   select_ssh_keyR   t   addRowR   t   Okt   Cancelt
   Horizontalt   accept_btnst   acceptedt   acceptt   rejectedt   rejectR   t	   addLayoutt
   checkStatet   stateChanged(	   Rb   t   parentt
   main_labelt   cf_labelt   cf_open_btnt	   cf_layoutt	   kf_layoutt   layoutR   (    (   R~   R   Rb   R   s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR      sf    			c         C   s6   t  |  t d  t   d  d } |  j j |  d  S(   Ns   Select kernel connection files   *.json;;*.*i    (   R   R   R   R   t   setText(   Rb   R   (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR      s    c         C   s6   t  |  t d  t   d  d } |  j j |  d  S(   Ns   Select SSH keyfiles
   *.pem;;*.*i    (   R   R   R   Rv   R   (   Rb   Rv   (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR     s    c   	      C   s  | s t  |   } n  | j   } t | j j    } | t j k } | r d   } | j j   | | j	 j    | | j
 j    | | j j    | f S| j j   } t j |  t j |  } } | d k r| j d  rt j t   d | d  } n  | d  d  d  | f Sd  S(   Nc         S   s   |  r
 |  Sd  S(   N(   RI   (   t   arg(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   <lambda>  t    R   s   .jsons   kernel-(   Rr   t   exec_t   boolR   R   R   t   AcceptedR   t   textRt   Rv   Ru   t   ospt   dirnamet   basenamet   endswitht   joinR   RI   (	   R   t   dialogt   resultt	   is_remoteR   t   falsy_to_nonet   patht   _dirt   filename(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   get_connection_parameters
  s"    	 N(	   t   __name__t
   __module__t   __doc__RI   R   R   R   t   staticmethodR   (    (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyRr      s   V		t   IPythonConsoleConfigPagec           B   s   e  Z d    Z d   Z RS(   c         C   s#   t  j |  | |  d   |  _ d  S(   Nc           S   s
   t  d  S(   Ns   IPython console(   R   (    (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR   '  R   (   R"   R   t   get_name(   Rb   t   pluginR   (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR   %  s    c   P      C   s8  |  j  } t t d   } | t d  d d t d  } | t d  d d t d  } | t d	  d
  } | t d  d  } | t d  d d t d  } | t d  d  } | t d  d d t d  }	 t   }
 |
 j |  |
 j |  |
 j |  |
 j |  |
 j |  |
 j |  |
 j |	  | j |
  t t d   } t t d   } | j t  t d  d f t d  d f t d  d f g } |  j	 t d  d | d  } t   } | j |  | j |  | j |  t t d    } |  j
 t d!  t d"  d# d$ d% d& d' d( d) d t d*  } t   } | j |  | j |  t t d+   } | t d,  d-  } | t d.  d/ d t d0  } | j |  j d-   | j j | j  t   } | j |  | j |  | j |  t d1  } t d2  } t t d3   } t t d4  | | f  } | j t  | d f | d f d d g } t j d8 k r| j d  n  t j j d;  r| j d  | j d  n  t r| j d  n  | j d  t |  } |  j	 t dD  d | dE dF d d t dG  } t   } | j |  | j |  | j |  | j |  j d-   | j j | j  t t dH   } t t dI   } | j t  d d f }  |  j	 t dL  d |  dM dF d }! |  j
 t dN  dO dP t dQ  dR d$ dS d& dT d( dU d t dV  }" |  j
 t dW  dO dP t dX  dY d$ d d& dZ d( d d t d[  }# |  j
 t d\  dO dP t dX  d] d$ d d& dZ d( d d t d^  }$ | t d_  d` d t da  }% t   }& |& j |  t   }' |' j |! j d d  |' j |! j d d  |' j |" j d d  |' j |" j d d  |' j |" j d d  |' j |# j d7 d  |' j |# j d7 d  |' j |# j d7 d  |' j |$ j d: d  |' j |$ j d: d  |' j |$ j d: d  |' j |% d= d d d:  t   }( |( j |'  |( j d  |& j |(  | j |&  | j |  j d-   | j j | j  t t db   }) t t dc   }* |* j t  |  j t dd  de df dg t j  }+ t   }, |, j |*  |, j |+  |) j |,  t t dh   }- t t di   }. |. j t  | t dj  dk t!  }/ |  j" df dl df  }0 |0 j t!  |/ j j |0 j  t   }1 |1 j |.  |1 j |/  |1 j |0  |- j |1  t t dm   }2 t t dn   }3 |3 j t  | t do  dp d dq }4 t   }5 |5 j |3  |5 j |4  |2 j |5  t t dr   }6 t t ds   }7 |7 j t  | t dt  du d dv }8 t   }9 |9 j |7  |9 j |8  |6 j |9  t t dw   }: t t dx   }; |; j t  t dy  }< t dz  }= t d{  d f |< d f |= d f f }> |  j	 t d|  |> d} dF d d t d~  |< |= f }? t   }@ |@ j |;  |@ j |?  |: j |@  t t d   }A t t d   }B |B j t  | t d  d d t d  }C t   }D |D j |B  |D j |C  |A j |D  t t d   }E t t d   }F |F j t  |  j t d  d df t d  dg t j  }G |  j t d  d df t d  dg t j  }H t   }I |I j |F  t   }J |J j |G j d d  |J j |G j# d d  |J j |H j d d  |J j |H j# d d  |I j |J  |E j |I  t t d   }K | t d  d  }L t   }M |M j |L  |K j |M  t$   }N |N j% |  j& | | |  t d   |N j% |  j& | | |  t d   |N j% |  j& |) |-  t d   |N j% |  j& |2 |6 |: |A |E |K  t d   t   }O |O j |N  |  j |O  d  S(   Nt	   Interfaces   Display initial bannert   show_bannert   tipsW   This option lets you hide the message shown at
the top of the console when it's opened.s9   Use a pager to display additional text inside the consolet	   use_pagers}   Useful if you don't want to fill the console with long help or completion texts.
Note: Use the Q key to get out of the pager.s   Display balloon tipst   show_calltipss#   Ask for confirmation before closingt   ask_before_closings?   Ask for confirmation before removing all user-defined variablest   show_reset_namespace_warnings]   This option lets you hide the warning message shown
when resetting the namespace from Spyder.s   Show elapsed timet   show_elapsed_times&   Ask for confirmation before restartingt   ask_before_restartsO   This option lets you hide the warning message shown
when restarting the kernel.s   Completion Types%   Decide what type of completion to uset	   Graphicali    t   Terminali   t   Plaini   s   Completion:s      t   completion_types   Source codes	   Buffer:  s    linest   buffer_sizet   min_it   max_i@B t   stepid   s|   Set the maximum number of lines of text shown in the
console before truncation. Specifying -1 disables it
(not recommended!)s!   Support for graphics (Matplotlib)s   Activate supportt   pylabs*   Automatically load Pylab and NumPy moduless   pylab/autoloads   This lets you load graphics support without importing 
the commands to do plots. Useful to work with other
plotting libraries different to Matplotlib or to develop 
GUIs with Spyder.t   Inlinet	   Automatics   Graphics backends   Decide how graphics are going to be displayed in the console. If unsure, please select <b>%s</b> to put graphics inside the console or <b>%s</b> to interact with them (through zooming and panning) in a separate window.t   Qt5t   Qt4i   t   darwins   OS Xi   t   linuxt   Gtk3i   t   Gtki   t   Wxi   t   Tkinteri   s   Backend:s   pylab/backendt   defaults>   This option will be applied the next time a console is opened.s   Inline backends8   Decide how to render the figures created by this backendt   PNGt   SVGs   Format:s   pylab/inline/figure_formats   Resolution:s     t    t   dpis   pylab/inline/resolutioni2   i  g?s/   Only used when the format is PNG. Default is 72s   Width:t   inchess   pylab/inline/widthi   s   Default is 6s   Height:s   pylab/inline/heights   Default is 4s#   Use a tight layout for inline plotss   pylab/inline/bbox_inchess   Sets bbox_inches to "tight" when
plotting inline with matplotlib.
When enabled, can cause discrepancies
between the image displayed inline and
that created using savefig.s   Run codes   You can run several lines of code when a console is started. Please introduce each one separated by commas, for example:<br><i>import os, import sys</i>s   Lines:s   startup/run_linesR   t	   alignments
   Run a filess   You can also run a whole file at startup instead of just some lines (This is similar to have a PYTHONSTARTUP file).s   Use the following file:s   startup/use_run_files   startup/run_files   Jedi completions   Enable Jedi-based <tt>Tab</tt> completion in the IPython console; similar to the greedy completer, but without evaluating the code.<br><b>Warning:</b> Slows down your console when working with large dataframes!s*   Use Jedi completion in the IPython consolet   jedi_completersu   <b>Warning</b>: Slows down your console when working with large dataframes!<br>Allows completion of nested lists etc.s   Greedy completions  Enable <tt>Tab</tt> completion on elements of lists, results of function calls, etc, <i>without</i> assigning them to a variable, like <tt>li[0].&lt;Tab&gt;</tt> or <tt>ins.meth().&lt;Tab&gt;</tt> <br><b>Warning:</b> Due to a bug, IPython's greedy completer requires a leading <tt>&lt;Space&gt;</tt> for some completions; e.g.  <tt>np.sin(&lt;Space&gt;np.&lt;Tab&gt;</tt> works while <tt>np.sin(np.&lt;Tab&gt; </tt> doesn't.s,   Use greedy completion in the IPython consolet   greedy_completersd   <b>Warning</b>: It can be unsafe because the code is actually evaluated when you press <tt>Tab</tt>.t   Autocalls   Autocall makes IPython automatically call any callable object even if you didn't type explicit parentheses.<br>For example, if you type <i>str 43</i> it becomes <i>str(43)</i> automatically.t   Smartt   Fullt   Offs   Autocall:  t   autocalls   On <b>%s</b> mode, Autocall is not applied if there are no arguments after the callable. On <b>%s</b> mode, all callable objects are automatically called (even if no arguments are present).s   Symbolic Mathematicss   Perfom symbolic operations in the console (e.g. integrals, derivatives, vector calculus, etc) and get the outputs in a beautifully printed style (it requires the Sympy module).s   Use symbolic matht   symbolic_mathsn   This option loads the Sympy library to work with.<br>Please refer to its documentation to learn how to use it.t   Promptss=   Modify how Input and Output prompts are shown in the console.s   Input prompt:t	   in_promptsH   Default is<br>In [&lt;span class="in-prompt-number"&gt;%i&lt;/span&gt;]:s   Output prompt:t
   out_promptsI   Default is<br>Out[&lt;span class="out-prompt-number"&gt;%i&lt;/span&gt;]:s   Windows adjustmentssD   Hide command line output windows generated by the subprocess module.t   hide_cmd_windowst   Displayt   Graphicst   Startups   Advanced Settings(   R   i   (   R   i   (   s   OS Xi   (   R   i   (   R   i   (   R   i   (   R   i   (   R   i    (   R   i   ('   t   create_checkboxR   R   R   R   t	   setLayoutR   R   RL   t   create_comboboxt   create_spinboxRw   t
   get_optiont   toggledR   t   syst   platformt   appendt
   startswithR$   t   tupleR   t   labelt   comboboxt   plabelt   spinboxt   slabelR   R   t
   addStretcht   create_lineeditR   R   RW   t   create_browsefilet   textboxR   t   addTabt
   create_tab(P   Rb   t   newcbt   interface_groupt
   banner_boxt	   pager_boxt   calltips_boxt   ask_boxt   reset_namespace_boxt   show_time_boxt   ask_restart_boxt   interface_layoutt
   comp_groupt
   comp_labelt
   completerst   comp_boxt   comp_layoutt   source_code_groupt   buffer_spint   source_code_layoutt   pylab_groupt	   pylab_boxt   autoload_pylab_boxt   pylab_layoutt   inlinet	   automatict   backend_groupt
   bend_labelt   backendst   backend_boxt   backend_layoutt   inline_groupt   inline_labelt   formatst
   format_boxt   resolution_spint
   width_spint   height_spint   bbox_inches_boxt   inline_v_layoutt   inline_layoutt   inline_h_layoutt   run_lines_groupt   run_lines_labelt   run_lines_editt   run_lines_layoutt   run_file_groupt   run_file_labelt
   file_radiot   run_file_browsert   run_file_layoutt
   jedi_groupt
   jedi_labelt   jedi_boxt   jedi_layoutt   greedy_groupt   greedy_labelt
   greedy_boxt   greedy_layoutt   autocall_groupt   autocall_labelt   smartt   fullt   autocall_optst   autocall_boxt   autocall_layoutt   sympy_groupt   sympy_labelt	   sympy_boxt   sympy_layoutt   prompts_groupt   prompts_labelt   in_prompt_editt   out_prompt_editt   prompts_layoutt   prompts_g_layoutt   windows_groupR   t   windows_layoutt   tabst   vlayout(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt
   setup_page)  s   					3																'										(   R   R   R   RX  (    (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR   #  s   	t   IPythonConsolec        
   B   sV  e  Z d  Z d Z e Z e Z e   Z	 e e
 e e
 f e
 e e
 e f  Z e d  Z e dC e 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 d   Z d   Z  d   Z! d   Z" d   Z# e$ e d  Z% d   Z& e'   e' e  e' e
  e' e e
  e' e e  e' e e
 e  e$ d e d        Z( e'   d    Z) e d  Z* e' e+ e+  d    Z, d   Z- d   Z. d   Z/ e$ d   Z0 dC dC e d!  Z1 d"   Z2 d#   Z3 d$   Z4 d%   Z5 d&   Z6 d'   Z7 e' e
  d(    Z8 e d)  Z9 d*   Z: d+   Z; d,   Z< dC dC d- d.  Z= e d/  Z> e d0  Z? d1   Z@ d2   ZA d d3  ZB d4   ZC d5   ZD d6   ZE d7   ZF d8   ZG d9   ZH d:   ZI e'   d;    ZJ e'   d<    ZK e'   d=    ZL d>   ZM d?   ZN d@   ZO dA   ZP dB   ZQ RS(D   sa   
    IPython Console plugin

    This is a widget with tabs where each one is a ClientWidget
    t   ipython_consolesh   The directory {} is not writable and it is required to create IPython consoles. Please make it writable.c      	   C   sm  t  r t j |  | d | n t j |  |  d
 |  _ d
 |  _ d |  _ g  |  _ g  |  _ t	 |  _
 t |  _ | |  _ | |  _ | |  _ |  j   |  j d
 k	 r t j t j |   s t j t j |   q n  t   } t |  |  j d t d d d d |  _ t |  j d  r;t j d k r;|  j j t  n  |  j j j |  j  |  j j   j  j |  j!  |  j j   j" j |  j#  |  j j$ |  j%  t j d k rt&   } | j' d	  t( |  } | j) d d d d  | j* |  j  | j* |  n | j* |  j  t+ |   |  _, |  j, j-   |  j. |  j,  | j* |  j,  |  j/ |  |  j0 t  d
 S(   s   Ipython Console constructor.t   maini    t   rename_tabst
   split_chart   /t   split_indext   setDocumentModeR   s   tab-containerN(1   R   R!   R   RI   t	   tabwidgett   menu_actionst   master_clientst   clientst	   filenamesRW   t   mainwindow_closeRL   t   create_new_client_if_emptyt   testingt   test_dirt   test_no_stderrt   initialize_pluginR   t   isdirR   RR   t   makedirsR   R3   t   hasattrR   R   R`  t   currentChangedR   t   refresh_plugint   tabBart   tabMovedt   move_tabt   sig_change_namet   rename_tabs_after_changet   set_close_functiont   close_clientR   t   setObjectNameR   t   setContentsMarginsR   R1   t   find_widgett   hidet   register_widget_shortcutsR   t   setAcceptDrops(   Rb   R   Rh  Ri  Rj  R   t   tab_containert
   tab_layout(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR     sT    										
	
	c         C   s1   |  j    } x |  j D] } | j |  q Wd S(   s   Update font from PreferencesN(   t   get_plugin_fontRd  t   set_font(   Rb   t   fontt   client(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   update_font  s    c         C   sa  d } |  j    } d } t j d d  } d } t j d d  } d } |  j |  }	 d	 }
 |  j |
  } d
 } |  j |  } x |  j D] } | j   } | | k r | j |  n  | | k r | d k	 r | j |  n  | | k r | j	 |  n  | | k r)| j
 j |	  | j |	  n  |
 | k rA| | _ n  | | k r | | _ q q Wd S(   s*   Apply configuration file's plugin settingst   plugin_fontt   connect_to_oit   helps   connect/ipython_consolet   color_scheme_namet   color_schemest   selectedR   R   R   N(   R  R    t   getR   Rd  t   get_controlR  RI   t   set_help_enabledt   set_color_schemet   show_time_actiont
   setCheckedt   set_elapsed_time_visiblet   reset_warningR   (   Rb   t   optionst   font_nt   font_ot   help_nt   help_ot   color_scheme_nt   color_scheme_ot   show_time_nt   show_time_ot   reset_namespace_nt   reset_namespace_ot   ask_before_restart_nt   ask_before_restart_oR  t   control(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   apply_plugin_settings  s6    c         C   so   | r^ |  j  j   |  j  j   |  j sk |  j j rH |  j d t  q[ |  j d t  qk n |  j  j	   d S(   s   Toggle viewt
   give_focusN(
   t
   dockwidgett   showt   raise_Rd  R[  t   is_setting_upt   create_new_clientRW   RL   R{  (   Rb   t   checked(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   toggle_view  s    	c         C   s
   t  d  S(   s   Return widget titles   IPython console(   R   (   Rb   (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   get_plugin_title  s    c         C   s   t  j d  S(   s   Return widget iconRZ  (   t   imat   icon(   Rb   (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   get_plugin_icon  s    c         C   s)   |  j  j   } | d k	 r% | j   Sd S(   sq   
        Return the widget to give focus to when
        this plugin's dockwidget is raised on top-level
        N(   Ra  t   currentWidgetRI   R  (   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   get_focus_widget  s    c         C   s?   t  |  _ x/ |  j D]$ } | j   | j   | j   q Wt  S(   s3   Perform actions before parent main window is closed(   RL   Rf  Rd  t   shutdownt   remove_stderr_filet   close(   Rb   t
   cancelableR  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   closing_plugin(  s    	

c         C   s  d } |  j j   r |  j j   } | j   } | j   g  | j   D] } | d g ^ qG } t | g   d  } | j   g | } n d } g  } |  j	 j
 |  |  j j i | t j 6 | r | j } |  j j j t |   |  j j j |  n  |  j   |  j j   d S(   s   Refresh tabwidgetiiN(   RI   Ra  t   countR  R  t   setFocust   get_toolbar_buttonst   sumt   create_time_labelRz  t
   set_editort   set_corner_widgetsR   t   TopRightCornert   shellwidgetR[  t   variableexplorert   set_shellwidget_from_idt   idR  t	   set_shellt   update_tabs_textt   update_plugin_titlet   emit(   Rb   R  R  t   bt   buttonst   widgetst   sw(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyRp  1  s$    
%	
c      	   C   sI  t  |  t d  d t j d  d |  j d t j } |  j | d d d d t  |  t d  d t j d	  d |  j d t j } |  j | d d d d t  |  t d
  d d t d  d |  j
 } t  |  t d  d t j d  d |  j } |  j j } | j d |  | t | | g 7} | t | | t | g |  _ |  j S(   s+   Return a list of actions related to plugin.s   Open an &IPython consoleR  RZ  t	   triggeredt   contextt   names   New tabs   Restart kernelt   restarts   Connect to an existing kernels:   Open a new IPython console connected to an existing kernels
   Rename tabt   renamei    N(   R(   R   R  R  R  R   t   WidgetWithChildrenShortcutt   register_shortcutt   restart_kernelRI   t   create_client_for_kernelt   tab_name_editorR[  t   consoles_menu_actionst   insertR)   Rb  (   Rb   t   create_client_actiont   restart_actiont   connect_to_kernel_actiont   rename_tab_actiont   main_consoles_menu(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   get_plugin_actionsH  s:    			
c            sG    j  j      j j   j  j    j j   j  j j    j t t	 t t
 f j   f 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   f d      j  j j j   j    j  j j j   f d      j   d S(   s'   Register plugin in Spyder's main windowc            s     j  j j |  | | d | S(   Nt   processevents(   R[  t   editort   load(   t   fnamet   linenot   wordR  (   Rb   (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR   w  s    c            s(     j  |  t j |   d t t t t  S(   NR   (   t
   run_scriptR   R   RW   RL   (   R  (   Rb   (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR     s   c            s(     j  |  t j |   d t t t t  S(   NR   (   R  R   R   RW   RL   (   R  (   Rb   (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR     s   N(   R[  t   add_dockwidgett   focus_changedR   t   plugin_focus_changedt	   edit_gotoR  R  t   strRM   R   t   breakpoints_savedt   set_spyder_breakpointst   run_in_current_ipyclientR  t   workingdirectoryt   set_current_console_wdt$   set_current_client_working_directoryRa  Ro  t   update_working_directoryt   explorert   open_interpretert   create_client_from_pathR<   t   projectst   _remove_old_stderr_files(   Rb   (    (   Rb   s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   register_pluginp  s"    


c         C   s)   g  |  j  D] } t | t  r
 | ^ q
 S(   s   Return clients list(   Rd  t
   isinstanceR2   (   Rb   t   cl(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   get_clients  s    c         C   sI   t  j   } x6 |  j   D]( } | | k s= | | j   k r | Sq Wd S(   s(   Return current client with focus, if anyN(   R
   t   focusWidgetR  R  (   Rb   R{   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   get_focus_client  s    c         C   s#   |  j  j   } | d k	 r | Sd S(   s$   Return the currently selected clientN(   Ra  R  RI   (   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   get_current_client  s    c         C   s#   |  j    } | d k	 r | j Sd S(   s,   Return the shellwidget of the current clientN(   R  RI   R  (   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   get_current_shellwidget  s    c            ss  d   } t  j |  d d k }	 |	 r1 t  n  t }
  rL  j   } nC  j |  } | d k r  j | d |	  j   } t }
 n  | d k	 r=| j   d k	 r)d | r d n d | |  f  | r  d | |  7 n  | r	 d	 | |  7 n  | r d
 7 n   d 7 nJ d  | rB d 7 n   d t	 |  7 | rs d | |  7 n  y | j
 j rn | j
 j r| j
 j t d  d t d S r j      nN |
 r| j
 j d  n | j
 j d  | j
 j j      f d    Wn t k
 r/n X j   n2 t j  t d  t d  t  j |  t j  d S(   s)   Run script in current or dedicated clientc         S   s   t  t |    S(   N(   R0   R%   (   R   (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR     R   i   s   .pyxt	   is_cythons   %s('%s't	   debugfilet   runfiles   , args='%s's   , wdir='%s's   , post_mortem=TrueRA   s   %run s   -d s   "%s"s    %ss[   <br><b>Please exit from debugging before trying to run a file in this console.</b>
<hr><br>t   before_promptNs   %clearc              s    j       S(   N(   t   execute_code(    (   t   clear_variablest   current_clientt   lineRb   (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR     s   RF   sg   No IPython console is currently available to run <b>%s</b>.<br><br>Please open a new one and try again.(   R   t   splitextRW   R  t   get_client_for_fileRI   t   create_client_for_fileRL   t
   get_kernelR%   R  t
   _executingt   _readingt   _append_htmlR   R  t   silent_executet   executet   sig_prompt_readyR   t   AttributeErrort   switch_to_pluginR   t   warningR   R   (   Rb   R   t   wdirt   argst   debugt   post_mortemR  R   t   normR  t   is_new_clientR  (    (   R   R  R  Rb   s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR    sd    					c         C   s,   |  j    } | d k	 r( | j |  n  d S(   s%   Set current client working directory.N(   R  RI   t   set_cwd(   Rb   t	   directoryR  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR    s    c         C   s,   | r( |  j  j j | d t d t n  d S(   s]   Set current working directory.
        In the workingdirectory and explorer plugins.
        t   refresh_explorert   refresh_consoleN(   R[  R  t   chdirRL   RW   (   Rb   R   (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   set_working_directory  s    c         C   s)   |  j    } | d k	 r% | j   n  d S(   s(   Update working directory to console cwd.N(   R  RI   t   get_cwd(   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR    s    c         C   s   |  j    } | d k	 r | j r$ n | sh y | j j   Wn t k
 rN n X| j d t d t  n% | r | r | j d t d t  n  y | j	 t
 |   Wn t k
 r n X|  j   |  j   j   j   n  d S(   s   Execute code instructions.R  t   silentN(   R  RI   R  R  t
   disconnectt	   TypeErrort   reset_namespaceRW   RL   R  R%   R  t   activateWindowR  R  R  (   Rb   t   linesR  R   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR    s$    	
c         C   sD   |  j    } | d  k	 r@ y | j |  Wq@ t k
 r< q@ Xn  d  S(   N(   R  RI   t   write_to_stdinR  (   Rb   R  R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR#    s    R   c         C   s  |  j  d 7_  t d t |  j   d d  } |  j   } |  j d  } |  j d  } |  j d  } t |  d | d	 t d
  d |  j   d |  j   d |  j	   d | d |  j
 d | d | d | 
}	 |  j d k	 r |  j |	 _ n  |  j |	 d |	 j   d | | d k r8|  j j t    }
 |	 j |
  d St j d d  st j d d  } t j d d | d d } | s|	 j t d   d Sn  |  j |	 d | |	 j j d k rd S|  j |	  d S(   s   Create a new clienti   t   int_idt   str_idt   AR   R   R   t   id_t   history_filenames
   history.pyt   config_optionst   additional_optionst   interpreter_versionst   connection_fileRb  R  R  R   Nt   main_interpreterR   t
   executablet   spyder_kernelst   interpretert   versions   <1.0.0so  Your Python environment or installation doesn't have the <tt>spyder-kernels</tt> module or the right version of it installed. Without this module is not possible for Spyder to create a console for you.<br><br>You can install it by running in a system terminal:<br><br><tt>conda install spyder-kernels=0.*</tt><br><br>or<br><br><tt>pip install spyder-kernels==0.*</tt>R  (   Rc  t   dictR%   t   _new_connection_fileR   R2   R   R)  R*  R+  Rb  Ri  RI   t
   stderr_dirt   add_tabR   t   permission_error_msgt   formatR   t   show_kernel_errorR    R  R,   t   is_module_installedR   t   connect_client_to_kernelR  t   kernel_managert   register_client(   Rb   R  R   R  t	   client_idR   R   R  R   R  t	   error_msgt   pyexect   has_spyder_kernels(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR  &  sL    				
c         C   sH   t  j |   } | \ } } } } } | s. d S|  j | | | |  d S(   s/   Create a client connected to an existing kernelN(   Rr   R   t   _create_client_for_kernel(   Rb   t   connect_outputR,  t   hostnamet   sshkeyRh   t   ok(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR  f  s    c            s   | j  }   j r d n | j }   j | | d | \ } } t |  rt | d k rt d | j _ | j |  d S  j	 s | j
 j |   f d   | j j |   f d   n  | j d t d t  | j } | | _ | | _ d S(   s   Connect a client to its kernelR  Nc            s     j  |   S(   N(   t   process_started(   t   c(   Rb   (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR     R   c            s     j  |   S(   N(   t   process_finished(   RG  (   Rb   (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR     R   t   shellt   iopub(   R,  Rj  RI   t   stderr_handlet'   create_kernel_manager_and_kernel_clientR#   R  R;  R8  Rh  t   started_channelsR   t   stopped_channelst   start_channelsRL   t   kernel_client(   Rb   R  R  R,  RK  t   kmt   kcR  (    (   Rb   s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR:  q  s&    						c         C   s,   t  j |  r( |  j j | d d  n  d S(   s   Handle %edit magic petitions.i   R   N(   R+   t   is_text_fileR  R  (   Rb   R   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt	   edit_file  s    c         C   s  y/ t  d g t    } t i | j d 6 } Wn t   } n Xt   } d | j _ |  j d  } i d d 6d d 6d	 d
 6} | | | j _ |  j d  } | r d | j _ n d | j _ |  j d  } | | j _ |  j d  } | | j _	 |  j d  }	 |  j d  }
 |	 r*|	 | j _
 n  |
 r?|
 | j _ n  t j d d  } t |  d } | | j _ | | j _ | j |  | S(   s   
        Generate a Trailets Config instance for shell widgets using our
        config system

        This lets us create each widget with its own config
        s   jupyter_qtconsole_config.pyt   JupyterWidgett   richR   t   droplisti    t   ncursesi   t   plaini   R   t   insidet   noneR   R   R   R   R  R  (   R   R   R   RU  t   kindR   t   gui_completiont   pagingt   enable_calltipsR   R   R   R    R  R'   t   style_sheett   syntax_stylet   _merge(   Rb   t   full_cfgt   cfgt   spy_cfgt   completion_type_ot   completionst   pager_ot
   calltips_ot   buffer_size_ot   in_prompt_ot   out_prompt_ot   color_schemeR`  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR)    s>    		c         C   s*  t  j d d  rS d d l m } t d t j j d  d j   d | j  } n d d	 l	 } i  } t  j d d
  } d | } d | } x | | g D] } y% t
 j |  } | j   \ }	 }
 Wn | j k
 r d }	 n X|	 j   j d  d j   }	 d | k r|	 | d <q |	 | d <q W| S(   s+   Python and IPython versions used by clientsR-  R   i(   t   releaset   python_versions   
i    t   ipython_versionNR.  s5   %s -c 'import sys; print(sys.version.split("\n")[0])'s:   %s -c 'import IPython.core.release as r; print(r.version)'R   R9   (   R    R  t   IPython.coreRn  R2  R   R1  RK   t   stript
   subprocessR,   t   run_shell_commandt   communicatet   CalledProcessErrort   decode(   Rb   Rn  t   versionsRs  R?  t   py_cmdt   ipy_cmdR=   t   procRk   t   _err(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR+    s.    
c      
   C   sI   t  d |  j d  d |  j d  d |  j d  d |  j d   } | S(   ss   
        Additional options for shell widgets that are not defined
        in JupyterWidget config options
        R   t   autoload_pylabs   pylab/autoloadR4   R   R   (   R2  R   (   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR*    s    c            s  | j  d |  | j } | j } | j } | j j   j  | j j   j  | j j |   f d   | j	 j   j
  d } t j d d  r t   }   j j d k	 r
  j j j   d k	 r
  j j j   } q
n' t j d d  r
t j d d  } n  t j |  rK  j d k	 rK| j |  | rK| j   qKn    j j d k	 r| j   j j  | j t j d d	   n    j j d k	 r  j j j | j  | j j   j j j  n  | j   j    | j  j   f d
    | j! j   j"    j# j$ |  | r}| j  j   f d    | j% j   j&  | j% j   j&  | j' j   j# j(  n  d S(   s   Register new clientR  c            s     j  |  | |  S(   N(   t   pdb_has_stopped(   R  R  R  (   Rb   (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR     s    R   t
   workingdirs%   console/use_project_or_home_directorys   console/use_fixed_directorys   console/fixed_directoryR  s   connect/ipython_consolec              s     j  j   S(   N(   R  R  (    (   Rb   (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR   5  R   c              s     j  j   S(   N(   R  R  (    (   Rb   (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR   =  R   N()   t   configure_shellwidgetR  t   _controlt   _page_controlt
   new_clientR   R  t   go_to_errort   sig_pdb_stept   custom_edit_requestedRT  R    R  R   R[  R  RI   t   get_active_projectt   get_active_project_pathR   Rl  R  R  R  t   set_helpR  t
   historylogt   add_historyR(  t   append_to_historyR  R  R  t   sig_change_cwdR  Rz  R  t   visibility_changedRp  t   show_find_widgetR  (   Rb   R  R  R  R  t   page_controlt   cwd_path(    (   Rb   s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR<     sJ    						c         C   s  |  j  j   s d S| d k	 rD |  j  j |  } | d k rD d Sn  | d k rn | d k rn |  j  j   } n  | d k	 r |  j  j |  } n  y | j   Wn t k
 r n Xy | j j	 j
 | j  Wn t t f k
 r n X|  j r| rt } |  j d  rOt j |  |  j   t d  t j t j B } | t j k rOd Sn  t |  j |   d k rt j |  |  j   t d  t j t j B } n  | j   | t j k r|  j |  qn  |  j |  } t |  d k rt j | j  r| j   n  | j   |  j  j |  j  j |   |  j  j! |  y |  j" j# |  Wn t$ k
 rdn X|  j%   |  j  j   r|  j& r|  j'   n  |  j( j)   d S(   s<   Close client tab from index or widget (or close current tab)NiR   s"   Do you want to close this console?i    sQ   Do you want to close all other consoles connected to the same kernel as this one?(*   Ra  R  RI   t   indexOft   currentIndexR{   t   stop_button_click_handlerR]   t   timerRD   R  t	   show_timeR  Rf  RL   R   R   RY   R  R   RZ   R[   t   lent   get_related_clientsR  t   close_related_clientsR   t   existst   stderr_fileR  R  t	   removeTabRd  t   removeRe  RU   t
   IndexErrorR  Rg  R  R  R  (   Rb   t   indexR  t   forcet	   close_allR  t   related_clients(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyRw  B  s^    		
$

c         C   s:   x3 t  |  j  D]" \ } } t |  | k r | Sq Wd S(   s   Return client index from idN(   t	   enumerateRd  R  (   Rb   R=  R  R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   get_client_index_from_id  s    c         C   sO   g  } xB |  j    D]4 } | j | j k r | | k	 r | j |  q q W| S(   sY   
        Get all other clients that are connected to the same kernel as `client`
        (   R  R,  R   (   Rb   R  R  R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR    s    c         C   s:   |  j  |  } x$ | D] } |  j d | d t  q Wd S(   s4   Close all clients related to *client*, except itselfR  R  N(   R  Rw  RL   (   Rb   R  R  R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR    s    c         C   s   d |  _  t |  _ x t t |  j   D]{ } |  j d } y | j   WnA t k
 r } t j	 |  t
 d  t
 d  j |  t j  n X|  j d | d t  q( W|  j d t  t |  _ d S(	   s   
        Restart the console

        This is needed when we switch projects to update PYTHONPATH
        and the selected interpreter
        i    iRF   st   It was not possible to restart the IPython console when switching to this project. The error was<br><br><tt>{0}</tt>R  R  R  N(   Rc  RW   Rg  Rx   R  Rd  R  t	   ExceptionR   R  R   R7  R   Rw  RL   R  (   Rb   Rn   R  t   e(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR    s    		c         C   sD   |  j  t t t t f j | | d t  |  j   | j j   d S(   s9   Python debugger has just stopped at frame (fname, lineno)R   N(	   R  R  RM   R   R  RW   R!  R  R  (   Rb   R  R  R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR~    s    )
c         C   s%   x |  j  D] } | j j   q
 Wd S(   s'   Set Spyder breakpoints into all clientsN(   Rd  R  R  (   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR    s    c         C   s'   |  j    |  j   } | j |  d S(   s.   Create a client with its cwd pointing to path.N(   R  R  R  (   Rb   R   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR    s    
c         C   s]   |  j  d | d |  |  j d 8_ |  j   } t | _ |  j |  } |  j | |  d S(   s2   Create a client to execute code related to a file.R   R  i   N(   R  Rc  R  RW   t   allow_renamet   disambiguate_fnamet   rename_client_tab(   Rb   R   R  R  t   tab_text(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR    s    	c         C   sZ   d } xM t |  j    D]9 \ } } |  j | | k r |  j j |  | } Pq q W| S(   s(   Get client associated with a given file.N(   RI   R  R  Re  Ra  t   setCurrentIndex(   Rb   R   R  t   idxR  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR    s    c         C   ss   |  j  |  } x] | D]U } | j d k	 r | j   | j | _ | j j j | j  | j j d  Pq q Wd S(   s#   Set elapsed time for slave clients.i  N(	   R  R  RI   R  t   t0RD   R   R  t   start(   Rb   R  R  R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   set_elapsed_time  s    
c         O   s3   t  j d k r t j | |   St |  | |  Sd  S(   Nt   nt(   RR   R  t	   zmqtunnelt   paramiko_tunnelRq   (   Rb   R  t   kwargs(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt
   ssh_tunnel  s    i
   c      	   C   s   t  j d  } | d | d | d | d f } | d } x< t | |  D]+ \ }	 }
 |  j |	 |
 | | | | |  qK Wt |  S(   sx   
        Tunnel connections to a kernel via ssh.

        Remote ports are specified in the connection info ci.
        i   t
   shell_portt
   iopub_portt
   stdin_portt   hb_portt   ip(   R  t   select_random_portst   zipR  R   (   Rb   t   connection_infoRC  RD  Rh   RD   t   lportst   rportst	   remote_ipt   lpt   rp(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   tunnel_to_kernel  s    
c         C   s)   t  j d d |  j j    t d |  S(   s(   Create a kernel spec for our own kernelsR[  t   spyder_pythonpathR  (   R    t   setR[  t   get_spyder_pythonpathR&   (   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   create_kernel_spec  s    c         C   s   |  j  d |  } y t d | d d d t  } Wn3 t k
 rf t d  j t j    } | d f SX| | _	 y | j
 d |  Wn3 t k
 r t d  j t j    } | d f SX| j   } d | j _ | | f S(	   s!   Create kernel manager and client.R  R,  t   configt   autorestarts    The error is:<br><br><tt>{}</tt>t   stderrg      2@N(   R  R   RI   RL   R  R   R7  t	   tracebackt
   format_exct   _kernel_spect   start_kernelR  t
   hb_channelt   time_to_dead(   Rb   R,  RK  R  t   kernel_specR;  R>  RP  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyRL    s$    	c         C   s3   |  j    } | d k	 r/ |  j   | j   n  d S(   s!   Restart kernel of current client.N(   R  RI   R  R  (   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR  3  s    
c            s   |  | j  }   j j d k	 r7   j j j   n    j j d k	 r   j j j    j    j   | j	 j
    f d    n  d S(   sq   
        Connect an external kernel to the Variable Explorer and Help, if
        it is a Spyder kernel.
        c              s     j  j j t    S(   N(   R[  R  t   remove_shellwidgetR  (    (   Rb   R  (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR   G  s    N(   RP  R[  R  RI   R  R  t   add_shellwidgett   set_namespace_view_settingst   refresh_namespacebrowserRN  R   (   Rb   R  RR  (    (   Rb   R  s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   connect_external_kernel:  s    	

c         C   s   |  j  j |  |  j j | |  } |  j j | |  |  j j |  |  j rk |  j j	 rk |  j
   n  |  j   | j   j   |  j   d S(   s   Add tabN(   Rd  R   Ra  R  Re  R  R  R  R[  R  R  R!  R  R  R  (   Rb   R{   R  R   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR5  K  s    
c         C   se   |  j  j |  } |  j j |  } |  j  j | |  |  j j | |  |  j   |  j j   d S(   sU   
        Move tab (tabs themselves have already been moved by the tabwidget)
        N(   Re  RU   Rd  R  R  R  R  (   Rb   t
   index_fromt   index_toR   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyRs  W  s    
c         C   s2   g  |  j  D] } | r
 | ^ q
 } t j | |  S(   s)   Generate a file name without ambiguation.(   Re  R-   R  (   Rb   R  R   t   files_path_list(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR  b  s    c         C   s{   yc x\ t  |  j  D]K \ } } |  j | } | rN |  j | |  j |   q |  j | d  q WWn t k
 rv n Xd S(   s   Update the text from the tabs.N(   R  Re  Rd  R  R  RI   R  (   Rb   R  R  R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR  h  s    	c         C   sJ   |  j  t |   } | d k	 r- | | _ n  |  j j | | j    d S(   s   Rename client's tabN(   R  R  RI   t
   given_nameRa  t
   setTabTextR   (   Rb   R  R  R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR  w  s    c         C   s   |  j    } t } xE |  j   D]7 } t |  t |  k r | | j k r t } Pq q W| j r d | k r | r |  j | |  n |  j | d  | j r d | k r | r x* |  j	 |  D] } |  j | |  q Wn  d S(   s#   Rename tabs after a change in name.u   /N(
   R  RW   R  R  R  RL   R  R  RI   R  (   Rb   R  R  t   repeatedR  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyRu    s    'c         C   s,   |  j  j   } |  j  j   j j |  d S(   s   Trigger the tab name editor.N(   Ra  R  Rq  R  t   edit_tab(   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR    s    c         C   sn   t  t |   } | rj | j   \ } } y) |  j j t j |  t |  d  Wqj t k
 rf qj Xn  d S(   s   Go to error if relevantR   N(	   R/   R%   t   groupsR  R  R   t   abspathRM   t
   ValueError(   Rb   R   t   matchR  t   lnb(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR    s    )c         C   s'   d d l  m } |  j j j |  d S(   s   Show intro to IPython helpi(   t   interactive_usageN(   t   IPython.core.usageR  R[  R  t   show_rich_text(   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt
   show_intro  s    c         C   s-   d d l  m } |  j j j | d t d S(   s   Show qtconsole helpi(   t   gui_referencet   collapseN(   t   qtconsole.usageR  R[  R  R  RL   (   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   show_guiref  s    c         C   s'   d d l  m } |  j j j |  d S(   s   Show IPython Cheat Sheeti(   t   quick_referenceN(   R  R  R[  R  t   show_plain_text(   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   show_quickref  s    c         C   s   t  j t    sD y t j t    WqD t t f k
 r@ d SXn  d } xc | s t t	 j
    j d  d } t j j t   d |  } t j j |  s | n d } qM W| S(   s   
        Generate a new connection file

        Taken from jupyter_client/console_app.py
        Licensed under the BSD license
        R   t   -is   kernel-%s.jsonN(   R   Rl  R   RR   Rm  t   IOErrort   OSErrorRI   R  t   uuidt   uuid4RK   R   R   R  (   Rb   R   t   ident(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR3    s    	"c         C   sZ   |  j  j d  k	 r+ |  j  j j | j  n  |  j  j d  k	 rV |  j  j j | j  n  d  S(   N(   R[  R  RI   R  R  R  R  (   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyRF    s    c         C   s5   |  j  j d  k	 r1 |  j  j j t | j   n  d  S(   N(   R[  R  RI   R  R  R  (   Rb   R  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyRH    s    c         C   s  yI t  j |  } t  j |  } | r- | n d  } t d | d |  } Wn; t t f k
 r t j |  t	 d  t	 d  |  d  SXd  } d  } t
 }	 t d  d }
 d  } x |  j   D] } | | j k r | j   d  k	 r | j   } n  | j } | d  k r| j d } n  | j } t | j d  } | |
 k rK| }
 qKq q W| d  k r|  j d 7_ t |  j  } t }	 n  t d | d t |
 d   } |  j d	  } |  j d
  } |  j d  } t |  d | d | d t d  d |  j   d |  j   d |  j   d | d |  j d | d |	 d t d	 | d | d | } |  j d  k	 rg|  j | _ n  t d |  } y | j    Wn= t! k
 r} t j |  t	 d  t	 d  t |   d  SX| d  k	 ryp t d | j" d | j# d | j$ d | j% d | j&  } |  j' | | | |  } | \ | _# | _$ | _% | _& Wqt! k
 r} t j |  t	 d  t	 d   t |   d  SXn  | j(   | | j) _* | | j) _+ |	 r| j) j, j- |  j.  | j) j/   n  |	 s|  j0 |  n  |  j1 | d! | j2   |  j3 |  d  S("   NR   R   R9   s   Unable to connect to <b>%s</b>R&  i   R$  R%  R   R   R   R'  R  R(  s
   history.pyR)  R*  R+  R,  Rb  RC  t   external_kernelt   slaveR  s   Connection errorsS   An error occurred while trying to load the kernel connection file. The error was:

R  R  R  R  R  s+   Could not open ssh tunnel. The error was:

R  (4   R   R   R   RI   R    R  t   UnboundLocalErrorR   t   criticalR   RW   t   ordR  R,  R  R'  R  Rc  R%   RL   R2  t   chrR   R2   R   R)  R*  R+  Rb  Ri  R4  R   t   load_connection_fileR  R  R  R  R  R  R  RO  R  RP  R;  t   sig_is_spykernelR   R  t   is_spyder_kernelR  R5  R   R<  (   Rb   R,  RC  RD  Rh   t   cf_patht   cf_filenamet	   master_idR  R  t	   slave_ordR;  R  t   new_slave_ordR=  R   R  R   R  RP  R  R  t   newports(    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyRA    s    											"	

c         C   s   t  j d k r t   } xg t  j |  D]S } t j |  d d k r( y t  j t j | |   Wq{ t k
 rw q{ Xq( q( Wn  d S(   s   
        Remove stderr files left by previous Spyder instances.

        This is only required on Windows because we can't
        clean up stderr files while Spyder is running on it.
        R  i   s   .stderrN(	   RR   R  R.   t   listdirR   R  R  R   R  (   Rb   t   tmpdirR  (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyR  S  s    	N(R   R   R   R   t   CONF_SECTIONR   t   CONFIGWIDGET_CLASSRW   t   DISABLE_ACTIONS_WHEN_HIDDENR   R  R  RM   R   R  R   R6  RI   R   R  R  R  R  R  R  R  Rp  R  R  R  R  R  R  R  R  R  R  RL   R  R#  R	   R  R  R:  t   objectRT  R)  R+  R*  R<  Rw  R  R  R  R  R~  R  R  R  R  R  R  R  R  RL  R  R  R5  Rs  R  R  R  Ru  R  R  R  R  R  R3  RF  RH  RA  R  (    (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyRY    s   	$B										(						I				
		$:	?		BJ								
		
!													z(g   R   RO   RR   t   os.pathR   R   R  R   R  t   jupyter_client.connectR    t   jupyter_core.pathsR   R   t   qtconsole.clientR   t   qtconsole.managerR   t   qtpyR   t   qtpy.compatR   t   qtpy.QtCoreR   R   R	   t   qtpy.QtWidgetsR
   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   traitlets.config.loaderR   R   t   zmq.sshR   R  R;   RJ   RI   t   spyderR   t   spyder.config.baseR   R   R   t   spyder.config.mainR    t   spyder.pluginsR!   t   spyder.plugins.configdialogR"   t   spyder.py3compatR#   R$   R%   t   spyder.utils.ipython.kernelspecR&   t   spyder.utils.ipython.styleR'   t   spyder.utils.qthelpersR(   R)   t   spyder.utilsR*   R  R+   R,   R-   t   spyder.utils.programsR.   t   spyder.utils.miscR/   R0   t   spyder.widgets.findreplaceR1   t   spyder.widgets.ipythonconsoleR2   t   spyder.widgets.tabsR3   t   SYMPY_REQVERt   addRL   t   CYTHON_REQVERt   QTCONSOLE_REQVERt   IPYTHON_REQVERt   MATPLOTLIB_REQVERR>   Rq   Rr   R   RY  (    (    (    s<   lib/python2.7/site-packages/spyder/plugins/ipythonconsole.pyt   <module>	   st   d
	J| l