
    Je\O                        d Z ddlZddlZddlmZ ddl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 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 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.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5  ejl                  e7      Z8 G d d      Z9 G d d      Z: G d d      Z; G d d      Z< G d de      Z=y) z
Main Console widget.
    N)getopenfilename)QtSignalSlot)QActionQInputDialog	QLineEditQVBoxLayout)PYSIDE2)SpyderAPIError)PLUGIN_REGISTRY_)PluginMainWidget)on_conf_change)InstallerInternalError)DEVget_debug_level)InternalShell)to_text_string)	EnvDialog)get_error_matchgetcwd_or_homeremove_backslashes)DialogManagermimedata2url)CollectionsEditor)FindReplace)SpyderErrorDialogc                   ,    e Zd ZdZdZdZdZdZdZdZ	dZ
y	)
ConsoleWidgetActionsenvironment_actionexternal_editor_actionmax_line_count_actionQuit
run_actionsys_path_actiontoggle_code_completion_actiontoggle_wrap_actionN)__name__
__module____qualname__EnvironmentExternalEditorMaxLineCountr%   RunSysPathToggleCodeCompletion
ToggleWrap     L/usr/lib/python3/dist-packages/spyder/plugins/console/widgets/main_widget.pyr!   r!   5   s/    &K-N*L D
CG ;%Jr5   r!   c                       e Zd ZdZy)ConsoleWidgetMenusinternal_settings_submenuN)r*   r+   r,   InternalSettingsr4   r5   r6   r8   r8   E   s    2r5   r8   c                       e Zd ZdZdZy) ConsoleWidgetOptionsMenuSectionsrun_sectionquit_sectionN)r*   r+   r,   r0   r%   r4   r5   r6   r<   r<   I   s    
CDr5   r<   c                       e Zd ZdZy),ConsoleWidgetInternalSettingsSubMenuSectionsmainN)r*   r+   r,   Mainr4   r5   r6   r@   r@   N   s    Dr5   r@   c                       e Zd Z eeee      Z e       Z e       Z ee      Z	 ee
      Z	 d% fd	Zd Zd Z ed      d        Z ed      d        Z ed	      d
        Zd Zd Zd Zd Zd Zd Zd Zd Z ee
      d%d       Zd Zd Z e       d        Zd Z  e       d        Z! e       	 	 d&d       Z"d Z#d'dZ$d Z% e       d%d       Z& e       d%d       Z'd Z(d  Z)d! Z*d" Z+d# Z,d$ Z- xZ.S )(ConsoleWidgetc                     t         	   |||       t        j                  d       d _        d _        t        d      }|j                         }|j                  }|j                  }t                _        d  _        t        g | j                  d      ||       _        t!                _         j%                  d        j"                  j'                   j                          j"                  j)                           j                  j+                   j                  d             t-               }|j/                  d	       |j1                   j                         |j1                   j"                          j3                  |        j                  j4                  j7                   j4                          j                  j8                  j7                   j:                          j                  j<                  j7                   j<                          j                  j>                  j7                   j@                          j                  jB                  j7                   jB                          j                  jD                  j7                   jD                          j                  jF                  j7                   fd
       y )NzInitializing... FzSpyder Internal Console

This console is used to report application
internal errors and to inspect Spyder
internals with the following commands:
  spy.app, spy.window, dir(spy)

Please do not use it to run your code

max_line_count)commandsmessagerG   profilemultithreadedTwrapr   c                 <    j                   j                  | d      S )Nr   )sig_show_status_messageemit)msgselfs    r6   <lambda>z(ConsoleWidget.__init__.<locals>.<lambda>   s    4499#qA r5   )$super__init__loggerinfoerror_tracebackdismiss_errorr   get_command_line_optionsrJ   rK   r   dialog_manager	error_dlgr   get_confshellr   find_widgetsetAcceptDrops
set_editorhidetoggle_wrap_moder
   
setSpacing	addWidget	setLayoutsig_help_requestedconnectsig_exception_occurredhandle_exceptionsig_focus_changedsig_go_to_error_requestedgo_to_errorsig_redirect_stdio_requestedsig_refreshedsig_show_status_requested)
rQ   namepluginparentrI   cli_optionsrJ   rK   layout	__class__s
   `        r6   rT   zConsoleWidget.__init__m   s$   vv.%&  "" 8
 557%%#11 ,o"==)9:'

 't, 	D!##DJJ/

##DMM&$9: !$))*v 	

%%--d.E.EF

))11$2G2GH

$$,,T-C-CD

,,44T5E5EF

//77--	/

  ((););<

,,44A	Cr5   c                     t        d      S )NzInternal consoler   rQ   s    r6   	get_titlezConsoleWidget.get_title   s    #$$r5   c                      j                  t        j                  t        d      t        d       j	                  d       j
                  t        j                  ddt        j                  	       _
         j                  t        j                  t        d      t        d       j	                  d	       j                  
      } j                  t        j                  t        d      t        d       j	                  d       j                  
      } j                  t        j                  t        d      t        d       j	                  d       j                   
      } j                  t        j"                  t        d      t        d       j$                        } j                  t        j&                  t        d      t        d       j(                        } j                  t        j*                  t        d       fd j-                  d            } j                  t        j.                  t        d       fd j-                  d            } j1                  t2        j4                  t        d       j	                  d            }||||fD ]$  }	 j7                  |	|t8        j:                          &  j=                         }
||||fD ]$  }	 j7                  |	|
t>        j                          &  j7                   j                  |
t>        j                           j@                  jC                   j-                  d!      d"       y )#Nz&Quitr%   exitr   T)texttipicon	triggeredcontextshortcut_contextregister_shortcutmenurolez&Run...zRun a Python file	run_small)r{   r|   r}   r~   zEnvironment variables...z9Show and edit environment variables (for current session)environzShow sys.path contents...zShow (read-only) sys.pathsyspathz	Buffer...zSet maximum line count)r{   r|   r~   zExternal editor path...z#Set external editor executable pathz
Wrap linesc                 (    j                  d|       S )NrL   set_confvalrQ   s    r6   rR   z%ConsoleWidget.setup.<locals>.<lambda>   s    fc : r5   rL   )r{   toggledinitialzAutomatic code completionc                 (    j                  d|       S )Ncodecompletion/autor   r   s    r6   rR   z%ConsoleWidget.setup.<locals>.<lambda>   s    .CS I r5   r   zInternal console settingstooloptions)r}   )menusectionexternal_editor/pathrF   )"create_actionr!   r%   r   create_iconsig_quit_requestedr   ApplicationShortcutr   QuitRolequit_actionr0   
run_scriptr-   show_envr1   show_syspathr/   change_max_line_countr.   change_exteditorr3   r\   r2   create_menur8   r:   add_item_to_menur@   rB   get_options_menur<   r]   set_external_editor)rQ   r&   environ_actionsyspath_actionbuffer_actionexteditor_actionwrap_actioncodecompletion_actioninternal_settings_menuitemoptions_menus   `          r6   setupzConsoleWidget.setup   s1   -- %%7&	!!&)--** "%% . 

 '' $$9%&!!+.oo ( 

 ++ ,,-.  !!),mm , 
 ++ ((./-.!!),'' , 
 ** --;*+00	 + 
  -- //,-78++	 . 
 (( ++<:MM&)	 ) 
 !% 2 2 55./IMM"78	 !3 !
 "&!1!1//)*!!-0 "2 "

 #K1F%' 	D!!+DII " 	 ,,.+- 	D!!!8<< " 	 	499 	 	
 	

&&MM012	7r5   rG   )optionc                 :    | j                   j                  |       y N)r]   setMaximumBlockCountrQ   values     r6   max_line_count_updatez#ConsoleWidget.max_line_count_update  s    

''.r5   rL   c                 :    | j                   j                  |       y r   )r]   rb   r   s     r6   wrap_mode_updatezConsoleWidget.wrap_mode_update  s    

##E*r5   r   c                 <    | j                   j                  |d       y )NrF   )r]   r   r   s     r6   external_editor_updatez$ConsoleWidget.external_editor_update  s    

&&ub1r5   c                      y r   r4   rw   s    r6   update_actionszConsoleWidget.update_actions  s    r5   c                     | j                   S r   )r]   rw   s    r6   get_focus_widgetzConsoleWidget.get_focus_widget  s    zzr5   c                     |j                         }|j                         r-t        |      r|j                          y|j	                          y|j                         r|j                          yy)zl
        Reimplement Qt method.

        Inform Qt about the types of data that the widget accepts.
        N)mimeDatahasUrlsr   acceptProposedActionignorehasText)rQ   eventsources      r6   dragEnterEventzConsoleWidget.dragEnterEvent  sR     !>>F#**,^^&&( r5   c                 p   |j                         }|j                         r't        |      }| j                  j	                  |       n_|j                         rOt        |j                               }| j                  j                  d       | j                  j                  |       |j                          y)zT
        Reimplement Qt method.

        Unpack dropped data and handle it.
        eofN)r   r   r   r]   drop_pathlistr   r   r{   set_cursor_positionexecute_linesr   )rQ   r   r   pathlistliness        r6   	dropEventzConsoleWidget.dropEvent.  s     !>>#F+HJJ$$X.^^"6;;=1EJJ**51JJ$$U+""$r5   c                 :    | j                   j                  |       y)z5
        Start internal console interpreter.
        N)r]   start_interpreter)rQ   	namespaces     r6   r   zConsoleWidget.start_interpreterA  s     	

$$Y/r5   c                     |j                  | j                  j                         | j                  j                  j	                  |j
                         y)za
        Bind historylog instance to this console.

        Not used anymore since v2.0.
        N)add_historyr]   history_filenamesig_append_to_history_requestedrg   append_to_history)rQ   
historylogs     r6   set_historylogzConsoleWidget.set_historylogG  s=     	tzz::;

22::((	*r5   c                 &    || j                   _        y)z5
        Bind help instance to this console.
        N)r]   help)rQ   help_plugins     r6   set_helpzConsoleWidget.set_helpQ  s     &

r5   c                     t        | d      | _        | j                  j                  | j                  dd             | j                  j	                          y)z+Report an issue with the SpyderErrorDialog.T)	is_reportselected
appearancer   N)r   _report_dlgset_color_schemer\   showrw   s    r6   report_issuezConsoleWidget.report_issueW  sL    ,TTB))$-- +8 +. 	/r5   c                    |j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|s|s| j                  | j                  ryt        ||z          t        j
                  }d	}	|t        |d
t        |d            }
|
|v }	d}|	s7|j                  dd      }|t        d
 d      |dk(  rt        d
 d      | j                  dd      r| j                  t        |       | _        | j                  j                  | j                  dd             | j                  j                  j                  | j                         | j                  j                  j                  j                  | j                          | j                  j#                  |       |r@| j                  j%                  |       | j                  j&                  j)                  d       |rJ| j                  j*                  j-                  |       | j                  j.                  j)                  d	       |r@| j                  j0                  j-                  |       | j                  j3                  d       | j                  j5                  |       | j                  j7                          yt8        s
t;               r| j=                  d	d	       yy)a  
        Exception ocurred in the internal console.

        Show a QDialog or the internal console to warn the user.

        Handle any exception that occurs during Spyder usage.

        Parameters
        ----------
        error_data: dict
            The dictionary containing error data. The expected keys are:
            >>> error_data= {
                "text": str,
                "is_traceback": bool,
                "repo": str,
                "title": str,
                "label": str,
                "steps": str,
            }
        sender: spyder.api.plugins.SpyderPluginV2, optional
            The sender plugin. Default is None.

        Notes
        -----
        The `is_traceback` key indicates if `text` contains plain text or a
        Python error traceback.

        The `title` and `repo` keys indicate how the error data should
        customize the report dialog and Github error submission.

        The `label` and `steps` keys allow customizing the content of the
        error dialog.
        r{   Nis_tracebackFtitlerF   labelstepsTNAMECONF_SECTIONzspyder-ide/spyderrepozExternal plugin 'zr' does not define 'repo' key in the 'error_data' dictionary in the form my-org/my-repo (only Github is supported).z=' 'repo' key needs to be different from the main Spyder repo.show_internal_errorsrA   r   r   r   )getr[   rX   r   r   internal_pluginsgetattrr   r\   r   r   rejectedrg   remove_error_dlgdetailsrk   rl   set_github_repo_org	set_titler   
setEnabled
main_labelsetText
submit_btn
steps_textset_require_minimum_lengthappend_tracebackr   r   r   change_visibility)rQ   
error_datasenderr{   r   r   r   r   r   is_internal_pluginsender_namer   s               r6   ri   zConsoleWidget.handle_exception^  s   F ~~fd+!~~ne<w+w+w+ lt~~/E%%ut|, +;; "! ?AK!,0@!@ #!>>&$/D|$'} 5A A  **$'} 5; ; 
 ==/=@~~%!24!8//MM*lMCE''//0E0EF&&@@HH$$& NN..t4((/$$//6))11%8))44T:))11%899%@NN++D1NN!O%""4. &r5   c                 R    | j                   r| j                   j                          yy)z%
        Close error dialog.
        N)r[   rejectrw   s    r6   close_error_dlgzConsoleWidget.close_error_dlg  s      >>NN!!# r5   c                     | j                   j                  j                         rd| _        t        r%| j                   j                  ddd       d| _         y| j                   j                          d| _         y)z&
        Remove error dialog.
        TN)r[   dismiss_box	isCheckedrX   r   
disconnectrw   s    r6   r   zConsoleWidget.remove_error_dlg  s]     >>%%//1!%DNN%%dD$7  NN%%'r5   c                 N    | j                   j                  t        |              y)z-
        Show environment variables.
        rr   N)rZ   r   r   rw   s    r6   r   zConsoleWidget.show_env  s    
 	  $!78r5   c                 "    t         j                  S )z(
        Return the `sys.path`.
        )syspathrw   s    r6   get_sys_pathzConsoleWidget.get_sys_path  s     xxr5   c                     t        |       }|j                  t        j                  dd| j	                  d             | j
                  j                  |       y)z"
        Show `sys.path`.
        r  zsys.pathTr   )r   readonlyr}   N)r   r   r
  r  r   rZ   r   )rQ   editors     r6   r   zConsoleWidget.show_syspath  sP    
 #$/HH!!),	 	 	
 	  (r5   c                    || j                   j                  j                          t        | t	        d      t               t	        d      dz         \  }}| j                   j                  j                          |r>t        j                  t        j                  |             t        j                  |      }nyt        j                  d|       t        j                  |      }t        }d ||      d ||      d}|r| j                   j!                          | j#                  d	d	       | j                   j%                  |d
z          | j                   j'                  |       y)z&
        Run a Python script.
        NzRun Python filezPython filesz (*.py ; *.pyw ; *.ipy)zRunning script with %sz	runfile('z	', args='z')T
)r]   interpreterrestore_stdsr   r   r   redirect_stdsoschdirospdirnamebasenamerU   debugabspathr   setFocusr   writerun_command)rQ   filenamesilent	set_focusargs
_selfilterrbscommands           r6   r   zConsoleWidget.run_script  s    JJ""//1#2#$ .!$==	$ Hj JJ""002X./<<1-t4;;x( 03Hs4yIJJ!tT*

&

w'r5   c                     t        t        |            }|r/|j                         \  }}| j                  |t	        |             yy)z*
        Go to error if relevant.
        N)r   r   groupsedit_scriptint)rQ   r{   matchfnamelnbs        r6   rl   zConsoleWidget.go_to_error  s>      t 45JE3UCH- r5   c                     |M| j                   j                  ||       | j                  j                  t	        j
                  |      |d       yy)z
        Edit script.
        NrF   )r]   external_editorsig_edit_goto_requestedrO   r  r  )rQ   r  gotos      r6   r(  zConsoleWidget.edit_script"  sD     JJ&&x6((--ckk(.CT2N  r5   c                     | j                   j                  t        |             | j                   j                          y)z8
        Execute lines and give focus to shell.
        N)r]   r   r   r  )rQ   r   s     r6   r   zConsoleWidget.execute_lines+  s+     	

  !67

r5   c                     d}|>t        j                  | t        d      t        d      | j                  d      dd      \  }}|r| j	                  d|       yy)z-"
        Change maximum line count.
        TNBufferzMaximum line countrG   r   i@B )r   getIntr   r\   r   )rQ   r   valids      r6   r   z#ConsoleWidget.change_max_line_count2  sa    
 ='..(&'./LE5 MM*E2 r5   c           	          d}|Kt        j                  | t        d      t        d      t        j                  | j                  d            \  }}|r| j                  dt        |             yy)z.
        Change external editor path.
        TNzExternal editorz External editor executable path:r   )r   getTextr   r	   Normalr\   r   r   )rQ   r  r5  s      r6   r   zConsoleWidget.change_exteditorE  sk    
 <&..#$45  45KD% MM0.2FG r5   c                 &    || j                   _        y)ze
        Set the callback function to execute when the `exit_interpreter` is
        called.
        N)r]   exitfunc)rQ   funcs     r6   set_exit_functionzConsoleWidget.set_exit_functionW  s    
 #

r5   c                 :    | j                   j                  |       y)z1
        Set font of the internal shell.
        N)r]   set_font)rQ   fonts     r6   r>  zConsoleWidget.set_font^  s     	

D!r5   c                 L    | j                   j                  j                          y)zJ
        Redirect stdout and stderr when using open file dialogs.
        N)r]   r  r  rw   s    r6   r  zConsoleWidget.redirect_stdsd  s     	

,,.r5   c                 L    | j                   j                  j                          y)zI
        Restore stdout and stderr when using open file dialogs.
        N)r]   r  r  rw   s    r6   r  zConsoleWidget.restore_stdsj  s     	

++-r5   c                 J    || j                   j                  j                  |<   y)zT
        Add an object to the namespace dictionary of the internal console.
        N)r]   r  r   )rQ   rp   r   s      r6   set_namespace_itemz ConsoleWidget.set_namespace_itemp  s     26

((.r5   c                 8    | j                   j                          y)z
        Exit the internal console interpreter.

        This is equivalent to requesting the main application to quit.
        N)r]   exit_interpreterrw   s    r6   rE  zConsoleWidget.exit_interpreterv  s     	

##%r5   r   )NFFN)N)/r*   r+   r,   r   strr)  r/  rj   rn   ro   dictrf   rT   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ri   r  r   r   r  r   r   rl   r(  r   r   r   r<  r>  r  r  rC  rE  __classcell__)ru   s   @r6   rD   rD   T   s    %S#s3  HM !'s:C|%[7z +,/ -/ 6"+ #+ 122 32
)%&0*&  
$Zf/ f/P$ 
V9 9 
V) ) 
V@E (  (D.O 
V3 3$ 
VH H"#"/.6&r5   rD   )>__doc__loggingr  os.pathr  r  r
  qtpy.compatr   qtpy.QtCorer   r   r   qtpy.QtWidgetsr   r   r	   r
   qtpyr   spyder.api.exceptionsr   'spyder.api.plugin_registration.registryr   spyder.api.translationsr   spyder.api.widgets.main_widgetr   spyder.api.config.decoratorsr   spyder.utils.installersr   spyder.config.baser   r   ,spyder.plugins.console.widgets.internalshellr   spyder.py3compatr   spyder.utils.environr   spyder.utils.miscr   r   r   spyder.utils.qthelpersr   r    spyder.widgets.collectionseditorr   spyder.widgets.findreplacer   spyder.widgets.reporterrorr   	getLoggerr*   rU   r!   r8   r<   r@   rD   r4   r5   r6   <module>ra     s     	  
 ( ( ( H H  1 C % ; 7 : 3 F + *3 3 > > 2 8 
		8	$
& & 3 3 
 h&$ h&r5   