
,]c           @   s  d  Z  d d l m Z d d l 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 d d l m Z d d l m Z d d	 l m Z m Z 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' m( Z( d d l) m* Z* e$ e _$ d   Z+ d e f d     YZ, d e- f d     YZ. d e f d     YZ/ d e" f d     YZ0 d S(   s7   Internal shell widget : PythonShellWidget + Interpreteri(   t   timeN(   t
   QEventLoopt   QObjectt   Signalt   Slot(   t   QMessageBox(   t	   getargtxtt   getdoct	   getobjdirt	   getsource(   t   get_versions(   t   Interpreter(   t   builtinst   to_binary_stringt   to_text_string(   t   icon_manager(   t   programs(   t   get_error_matcht   getcwd_or_home(   t   create_action(   t   PythonShellWidget(   t   oedit(   t   _t   DEBUGt   get_conf_path(   t   CONFc         C   s:   |  d k r2 t   } d | d | d | d f S|  Sd S(   s   Create internal shell banners   Python %s %dbits [%s]t   pythont   bitnesst   systemN(   t   NoneR
   (   t   messaget   versions(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   create_banner0   s
    	t	   SysOutputc           B   sJ   e  Z d  Z e   Z d   Z d   Z d   Z d   Z e	 d    Z
 RS(   s   Handle standard I/O queuec         C   s)   t  j |   g  |  _ t j   |  _ d  S(   N(   R   t   __init__t   queuet	   threadingt   Lockt   lock(   t   self(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR"   >   s    	c         C   s;   |  j  j   |  j j |  |  j  j   |  j j   d  S(   N(   R&   t   acquireR#   t   appendt   releaset
   data_availt   emit(   R'   t   val(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   writeC   s    c         C   s9   |  j  j   d j |  j  } g  |  _ |  j  j   | S(   Nt    (   R&   R(   t   joinR#   R*   (   R'   t   s(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   empty_queueI   s
    	c         C   s   d  S(   N(    (   R'   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   flushQ   s    c         C   s   t  S(   N(   t   False(   R'   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   closedU   s    (   t   __name__t
   __module__t   __doc__R   R+   R"   R.   R2   R3   t   propertyR5   (    (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR!   :   s   					t   WidgetProxyDatac           B   s   e  Z RS(    (   R6   R7   (    (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR:   Y   s   t   WidgetProxyc           B   s   e  Z d  Z e e  Z e e  Z e e e  Z e e  Z	 d   Z
 d   Z d   Z e d  Z d   Z d d  Z d   Z RS(	   s"   Handle Shell widget refresh signalc         C   s#   t  j |   d  |  _ | |  _ d  S(   N(   R   R"   R   t
   input_datat   input_condition(   R'   R=   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR"   d   s    	c         C   s   |  j  j |  d  S(   N(   t   sig_new_promptR,   (   R'   t   prompt(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt
   new_prompti   s    c         C   s   |  j  j |  d  S(   N(   t   sig_set_readonlyR,   (   R'   t   state(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   set_readonlyl   s    c         C   s   |  j  j | |  d  S(   N(   t   sig_editR,   (   R'   t   filenamet   external_editor(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   edito   s    c         C   s   |  j  t k	 S(   s&   Return True if input data is available(   R<   R:   (   R'   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   data_availabler   s    R/   c         C   s   t  |  _ |  j j |  d  S(   N(   R:   R<   t   sig_wait_inputR,   (   R'   R?   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt
   wait_inputv   s    	c         C   s4   |  j  j   | |  _ |  j  j   |  j  j   d  S(   N(   R=   R(   R<   t   notifyR*   (   R'   t   cmd(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt	   end_inputz   s    	(   R6   R7   R8   R   t   strR>   t   boolRA   RD   RI   R"   R@   RC   R4   RG   RH   RJ   RM   (    (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR;   \   s   				t   InternalShellc           B   s  e  Z d  Z e e  Z e   Z e e  Z e   Z d# d# g  d# d d# d# e
 e e d 
 Z d   Z d   Z d   Z d   Z d   Z d d	  Z d
   Z d   Z e   d    Z d   Z d d  Z e
 e
 d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z  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+ RS($   sA   Shell base widget: link between PythonShellWidget and Interpreteri,  c         C   s  t  j |  | t d  |  |  j |
  |	 |  _ |  j |  | d  k	 r[ |  j |  n  d  |  _ t	 |  _
 t	 |  _ |  j j |  j  d   } | d  } |  j |  g  |  _ | |  _ | |  _ | |  _ d  |  _ |  j |  |  j j d  t t d  r|  j j |  j  n  d  S(   Ns   history_internal.pyc         S   s   t  j d |   S(   Nt   internal_console(   R   t   get(   t   option(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   <lambda>   R/   s   codecompletion/case_sensitiveR/   t   open_in_spyder(   R   R"   R   t   set_light_backgroundt   multithreadedt   setMaximumBlockCountR   t   set_fontt
   input_loopR4   t
   input_modet   interruptedt   sig_keyboard_interruptt   connectt   keyboard_interruptt   set_codecompletion_caset
   eventqueuet   exitfunct   commandsR   t   interpretert   start_interpretert   statusR,   t   hasattrR   t   go_to_errort   open_with_external_spyder(   R'   t   parentt	   namespaceRc   R   t   max_line_countt   fontRb   t   profileRW   t   light_backgroundt   getcfgt   case_sensitive(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR"      s0    											c         C   sl  |  j    |  j d k	 r) |  j j   n  t | |  j t t t  |  _ |  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 r |  j j   n  t |  j  } |  j | d t x* |  j D] } |  j | d t  d t  q"W|  j |  j j!  |  j" j#   |  j S(   s   Start Python interpreterR?   t   historyR@   N($   t   clearRd   R   t   closingR   Rb   R!   R;   R   t   stdout_writeR+   R^   t   stdout_availt   stderr_writet   stderr_availt   widget_proxyRA   t   setReadOnlyR>   R@   RD   t   edit_scriptRI   RJ   RW   t   startR    R   R.   t   TrueRc   t   run_commandR4   t   p1t   refreshR,   (   R'   Rk   t   bannerRL   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyRe      s(    
	c         C   sB   t  |  j _ |  j r1 |  j j j t d   n  |  j j   d S(   s   Exit interpreters   
N(   R}   Rd   t	   exit_flagRW   t   stdin_writeR.   R   t   restore_stds(   R'   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   exit_interpreter   s    	c         C   s9   t  |  } | r" |  j |  n |  j   j |  d  S(   N(   R   RF   Rj   R{   (   R'   RE   RF   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR{      s    c         C   s,   |  j  j j   } | r( |  j |  n  d S(   s@   Data is available in stdout, let's empty the queue and write it!N(   Rd   Ru   R2   R.   (   R'   t   data(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyRv      s    c         C   sB   |  j  j j   } | r> |  j | d t |  j d t  n  d S(   s@   Data is available in stderr, let's empty the queue and write it!t   errorN(   Rd   Rw   R2   R.   R}   R3   (   R'   R   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyRx      s    R/   c         C   sF   |  j  |  |  j   t |  _ t   |  _ |  j j   d |  _ d S(   s"   Wait for input (raw_input support)N(   R@   t   setFocusR}   R[   R   RZ   t   exec_R   (   R'   R?   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyRJ      s    
	c         C   s-   t  |  _ |  j j   |  j j j |  d S(   s   End of wait_input modeN(   R4   R[   RZ   t   exitRd   Ry   RM   (   R'   RL   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyRM      s    	c         C   sT   t  j |   t |  t d  d t j d  d |  j |  _ |  j j	 |  j  d S(   s$   Reimplement PythonShellWidget methods   Help...t   icont   DialogHelpButtont	   triggeredN(
   R   t   setup_context_menuR   R   t   imaR   t   helpt   help_actiont   menut	   addAction(   R'   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR     s
    c         C   si   t  j |  t d  d t d  t d  t d  t d  t d  t d  t d	  t d
  f  d S(   s   Help on Spyder consolet   Helps  <b>%s</b>
                          <p><i>%s</i><br>    edit foobar.py
                          <p><i>%s</i><br>    xedit foobar.py
                          <p><i>%s</i><br>    run foobar.py
                          <p><i>%s</i><br>    clear x, y
                          <p><i>%s</i><br>    !ls
                          <p><i>%s</i><br>    object?
                          <p><i>%s</i><br>    result = oedit(object)
                          s   Shell special commands:s   Internal editor:s   External editor:s   Run script:s   Remove references:s   System commands:s   Python help:s   GUI-based editor:N(   R   t   aboutR   (   R'   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR     s    							c         C   sG   t  t |   } | rC | j   \ } } t j | t |   n  d S(   s   Load file in external Spyder's editor, if available
        This method is used only for embedded consoles
        (could also be useful if we ever implement the magic %edit command)N(   R   R   t   groupsR   RU   t   int(   R'   t   textt   matcht   fnamet   lnb(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyRi   #  s    ic         C   s   t  j d d  } t  j d d  } yK | g } | d k r^ | r^ | j d j | |   n  t j | |  Wn" t k
 r |  j d |  n Xd S(   sc   Edit in an external editor
        Recommended: SciTE (e.g. to go to line where an error did occur)RQ   s   external_editor/paths   external_editor/gotolinei    s   %s%ds"   External editor was not found: %s
N(   R   RR   R)   t   formatR   t   run_programt   OSErrort   write_error(   R'   RE   t   gotot   editor_patht   goto_optiont   args(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyRF   ,  s    		c         C   s8   t  j |  d | d | |  j r4 t |  _ t  n  d S(   s"   Reimplement ShellBaseWidget methodR   R?   N(   R   R3   R\   R4   t   KeyboardInterrupt(   R'   R   R?   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR3   <  s    		c         C   s9   |  j    |  j |  j j r( |  j j n	 |  j j  d S(   s"   Reimplement ShellBaseWidget methodN(   Rs   R@   Rd   t   moret   p2R   (   R'   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   clear_terminalE  s    
c         C   s   |  j  rh t   } x! t d  D] } |  j |  q W|  j d d t   |  |  j |  j j  n |  j |  |  j   d S(   t   on_enteri
   u   
<Δt>=%dms
g      Y@N(	   Rn   R    t   ranget   execute_commandt   insert_textR@   Rd   R   t    _InternalShell__flush_eventqueue(   R'   t   commandt   t0R   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR   L  s    		c         C   s$   |  j  |  r d S|  j |  d S(   sO   
        Reimplement Qt Method
        Enhanced keypress event handler
        N(   t   preprocess_keyeventt   postprocess_keyevent(   R'   t   event(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   keyPressEventY  s    c         C   s3   x, |  j  r. |  j  j d  } |  j |  q Wd S(   s   Flush keyboard event queuei    N(   Ra   t   popR   (   R'   t
   past_event(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   __flush_eventqueuec  s    c         C   sn   |  j  r |  j j   nQ |  j j ra |  j d  t |  j _ |  j |  j j  |  j j   n	 t	 |  _
 d S(   s   Simulate keyboard interrupts   
KeyboardInterrupt
N(   RW   Rd   t   raise_keyboard_interruptR   R   R4   R@   R   t   resetbufferR}   R\   (   R'   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR_   j  s    	c         C   sq   xj | j    D]\ } | j   } | j d  r4 q n  |  j | t j d t |  j | d  |  j   q Wd S(   s   
        Execute a set of lines as multiple command
        lines: multiple lines of text to be executed as single commands
        t   #R3   s   
N(	   t
   splitlinest   stript
   startswithR.   t   ost   linesepR}   R   R3   (   R'   t   linest   linet   stripped_line(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   execute_linesw  s    c         C   sa   |  j  r |  j |  d S| j d  r6 | d  } n  | d k rP |  j   d S|  j |  d S(   s[   
        Execute a command
        cmd: one-line command only, with '
' at the end
        Ns   
it   cls(   R[   RM   t   endswithR   R~   (   R'   RL   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR     s    	
c         C   s   | s d } n | r% |  j  |  n  |  j s d | k rt |  j j j t | d   |  j j   |  j j   q |  j t	 d  d t
 n |  j j j t | d   d S(   s   Run command in interpreterR/   t   inputs   
s   In order to use commands like "raw_input" or "input" run Spyder with the multithread option (--multithread) from a system terminalR   N(   t   add_to_historyRW   Rd   R   R.   R   t   run_lineR   R,   R   R}   (   R'   RL   Rr   R@   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR~     s    		
c         C   s   |  j  j |  S(   s   Is text a valid object?(   Rd   t   eval(   R'   R   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   _eval  s    c         C   s)   |  j  |  \ } } | r% t |  Sd S(   s   Return dir(object)N(   R   R   (   R'   t   objtxtt   objt   valid(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   get_dir  s    c         C   s   t  |  j j j    S(   s   Return shell globals() keys(   t   listRd   Rk   t   keys(   R'   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   get_globals_keys  s    c         C   s   t  j t    S(   s'   Return shell current directory list dir(   R   t   listdirR   (   R'   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   get_cdlistdir  s    c         C   s)   |  j  |  \ } } | r% t |  Sd S(   s   Is object callable?N(   R   t   callable(   R'   R   R   R   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt
   iscallable  s    c         C   s)   |  j  |  \ } } | r% t |  Sd S(   s   Get func/method argument listN(   R   R   (   R'   R   R   R   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   get_arglist  s    c         C   s&   |  j  |  \ } } | r" | j Sd S(   s   Get object __doc__N(   R   R8   (   R'   R   R   R   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt
   get__doc__  s    c         C   s)   |  j  |  \ } } | r% t |  Sd S(   s#   Get object documentation dictionaryN(   R   R   (   R'   R   R   R   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   get_doc  s    c         C   s)   |  j  |  \ } } | r% t |  Sd S(   s   Get object sourceN(   R   R	   (   R'   R   R   R   (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt
   get_source  s    c         C   s   |  j  j | |  S(   s    Return True if object is defined(   Rd   t
   is_defined(   R'   R   t   force_import(    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyR     s    N(,   R6   R7   R8   R   RN   Rf   R   Rh   t   focus_changedR   R4   R}   R"   Re   R   R{   Rv   Rx   RJ   RM   R   R   R   Ri   RF   R3   R   R   R   R   R_   R   R   R~   R   R   R   R   R   R   R   R   R   R   (    (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyRP      sL   		,															
													(1   R8   R    R   R$   t   qtpy.QtCoreR   R   R   R   t   qtpy.QtWidgetsR   t   spyder_kernels.utils.dochelpersR   R   R   R	   t   spyderR
   t   spyder.interpreterR   t   spyder.py3compatR   R   R   t   spyder.utilsR   R   R   t   spyder.utils.miscR   R   t   spyder.utils.qthelpersR   t   spyder.widgets.shellR   t,   spyder.widgets.variableexplorer.objecteditorR   t   spyder.config.baseR   R   R   t   spyder.config.mainR   R    R!   t   objectR:   R;   RP   (    (    (    s;   lib/python2.7/site-packages/spyder/widgets/internalshell.pyt   <module>   s.   
""		
%