
    G@dN                        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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l.m/Z/ ddl0m1Z1 ddl2m3Z3  ej4        e5          Z6 G d d          Z7 G d d          Z8 G d d          Z9 G d d          Z: G d de          Z;dS )z
Main Console widget.
    N)getopenfilename)QtSignalSlot)QActionQInputDialog	QLineEditQVBoxLayout)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
d	S )
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     Jlib/python3.11/site-packages/spyder/plugins/console/widgets/main_widget.pyr    r    4   s;        &K-N*L D
CG ;%JJJr4   r    c                       e Zd ZdZdS )ConsoleWidgetMenusinternal_settings_submenuN)r)   r*   r+   InternalSettingsr3   r4   r5   r7   r7   D   s        2r4   r7   c                       e Zd ZdZdZdS ) ConsoleWidgetOptionsMenuSectionsrun_sectionquit_sectionN)r)   r*   r+   r/   r$   r3   r4   r5   r;   r;   H   s        
CDDDr4   r;   c                       e Zd ZdZdS ),ConsoleWidgetInternalSettingsSubMenuSectionsmainN)r)   r*   r+   Mainr3   r4   r5   r?   r?   M   s        DDDr4   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 )+ConsoleWidgetNc                 v    t                                          |||           t                              d           d _        d _        t          d          }|                                }|j        }|j	        }t                       _        d  _        t          g |                     d          ||           _        t!                      _                             d            j                             j                    j                                          j                                                 d                     t-                      }|                    d	           |                     j                   |                     j                                        |            j        j                             j                    j        j                             j                    j        j                             j                    j        j                             j                     j        j!                             j!                    j        j"                             j"                    j        j#                             fd
           d S )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messagerF   profilemultithreadedTwrapr   c                 :    j                             | d          S )Nr   )sig_show_status_messageemit)msgselfs    r5   <lambda>z(ConsoleWidget.__init__.<locals>.<lambda>   s    499#qAA r4   )$super__init__loggerinfoerror_tracebackdismiss_errorr   get_command_line_optionsrI   rJ   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)
rP   namepluginparentrH   cli_optionsrI   rJ   layout	__class__s
   `        r5   rS   zConsoleWidget.__init__l   s   vv...%&&&  "" 8
 
 5577%#1 ,oo"==)9::'
 
 

 't,, 	D!!!##DJ///
##DMM&$9$9::: !$$$)***v 	
%--d.EFFF
)11$2GHHH
$,,T-CDDD
,44T5EFFF
/77-	/ 	/ 	/
 (();<<<
,44AAAA	C 	C 	C 	C 	Cr4   c                      t          d          S )NzInternal consoler   rP   s    r5   	get_titlezConsoleWidget.get_title   s    #$$$r4   c                                           t          j        t          d          t          d                               d           j        t          j        ddt          j	        	  	         _
                              t          j        t          d          t          d                               d	           j        
          }                      t          j        t          d          t          d                               d           j        
          }                      t          j        t          d          t          d                               d           j        
          }                      t          j        t          d          t          d           j                  }                      t          j        t          d          t          d           j                  }                      t          j        t          d           fd                     d                    }                      t          j        t          d           fd                     d                    }                     t2          j        t          d                               d                    }||||fD ]$}	                     |	|t8          j                    %                                 }
||||fD ]$}	                     |	|
t>          j                    %                      j
        |
t>          j                     j         !                                         d!          d"           d S )#Nz&Quitr$   exitr   T)texttipicon	triggeredcontextshortcut_contextregister_shortcutmenurolez&Run...zRun a Python file	run_small)rz   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)rz   r{   r}   zExternal editor path...z#Set external editor executable pathz
Wrap linesc                 0                         d|           S )NrK   set_confvalrP   s    r5   rQ   z%ConsoleWidget.setup.<locals>.<lambda>   s    fc : : r4   rK   )rz   toggledinitialzAutomatic code completionc                 0                         d|           S )Ncodecompletion/autor   r   s    r5   rQ   z%ConsoleWidget.setup.<locals>.<lambda>   s    .CS I I r4   r   zInternal console settingstooloptions)r|   )menusectionexternal_editor/pathrE   )"create_actionr    r$   r   create_iconsig_quit_requestedr   ApplicationShortcutr   QuitRolequit_actionr/   
run_scriptr,   show_envr0   show_syspathr.   change_max_line_countr-   change_exteditorr2   r[   r1   create_menur7   r9   add_item_to_menur?   rA   get_options_menur;   r\   set_external_editor)rP   r%   environ_actionsyspath_actionbuffer_actionexteditor_actionwrap_actioncodecompletion_actioninternal_settings_menuitemoptions_menus   `          r5   setupzConsoleWidget.setup   s   -- %7&		!!&))-* "% . 

 

 '' $9%&&!!+..o ( 
 

 ++ ,-..   !!),,m , 
 
 ++ (.//-..!!),,' , 
 
 ** -;*++0	 + 
 
  -- /,--788+	 . 
 
 (( +<::::MM&))	 ) 
 
 !% 2 2 5.//IIIIMM"788	 !3 !
 !
 "&!1!1/)**!!-00 "2 "
 "

 #K1F%' 	 	D!!+DI "     ,,..+- 	 	D!!!8< "     	49 	 	
 	
 	
 	
&&MM0112	7 	7 	7 	7 	7r4   rF   )optionc                 :    | j                             |           d S N)r\   setMaximumBlockCountrP   values     r5   max_line_count_updatez#ConsoleWidget.max_line_count_update
  s    
''.....r4   rK   c                 :    | j                             |           d S r   )r\   ra   r   s     r5   wrap_mode_updatezConsoleWidget.wrap_mode_update  s    
##E*****r4   r   c                 <    | j                             |d           d S )NrE   )r\   r   r   s     r5   external_editor_updatez$ConsoleWidget.external_editor_update  s     
&&ub11111r4   c                     d S r   r3   rv   s    r5   update_actionszConsoleWidget.update_actions  s    r4   c                     | j         S r   )r\   rv   s    r5   get_focus_widgetzConsoleWidget.get_focus_widget  s
    zr4   c                     |                                 }|                                r;t          |          r|                                 dS |                                 dS |                                r|                                 dS dS )zl
        Reimplement Qt method.

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

        Unpack dropped data and handle it.
        eofN)r   r   r   r\   drop_pathlistr   r   rz   set_cursor_positionexecute_linesr   )rP   r   r   pathlistliness        r5   	dropEventzConsoleWidget.dropEvent-  s     !!>> 	,#F++HJ$$X....^^ 	,"6;;==11EJ**5111J$$U+++""$$$$$r4   c                 :    | j                             |           dS )z5
        Start internal console interpreter.
        N)r\   start_interpreter)rP   	namespaces     r5   r   zConsoleWidget.start_interpreter@  s      	
$$Y/////r4   c                     |                     | j        j                   | j        j                            |j                   dS )za
        Bind historylog instance to this console.

        Not used anymore since v2.0.
        N)add_historyr\   history_filenamesig_append_to_history_requestedrf   append_to_history)rP   
historylogs     r5   set_historylogzConsoleWidget.set_historylogF  sJ     	tz:;;;
2::(	* 	* 	* 	* 	*r4   c                     || j         _        dS )z5
        Bind help instance to this console.
        N)r\   help)rP   help_plugins     r5   set_helpzConsoleWidget.set_helpP  s     &
r4   c                     t          | d          | _        | j                            |                     dd                     | j                                         dS )z+Report an issue with the SpyderErrorDialog.T)	is_reportselected
appearancer   N)r   _report_dlgset_color_schemer[   showrv   s    r5   report_issuezConsoleWidget.report_issueV  sg    ,TTBBB))$-- +8 +. +. 	/ 	/ 	/r4   c                    |                     dd          }|                     dd          }|                     dd          }|                     dd          }|                     dd          }|s	|s| j        | j        rdS t          ||z              t          j        }d	}	|#t          |d
t          |d                    }
|
|v }	d}|	sD|                     dd          }|t          d|
 d          |dk    rt          d|
 d          |                     dd          r| j        t          |           | _        | j        
                    |                     dd                     | j        j                            | j                   | j        j        j                            | j                   | j                            |           |r9| j                            |           | j        j                            d           |r>| j        j                            |           | j        j                            d	           |r9| j        j                            |           | j                            d           | j                            |           | j                                         dS t8          st;                      r|                     d	d	           dS dS )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.
        rz   Nis_tracebackFtitlerE   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_errorsr@   r   r   r   )getrZ   rW   r   r   internal_pluginsgetattrr   r[   r   r   rejectedrf   remove_error_dlgdetailsrj   rk   set_github_repo_org	set_titler   
setEnabled
main_labelsetText
submit_btn
steps_textset_require_minimum_lengthappend_tracebackr   r   r   change_visibility)rP   
error_datasenderrz   r   r   r   r   r   is_internal_pluginsender_namer   s               r5   rh   zConsoleWidget.handle_exception]  s?   F ~~fd++!~~ne<<w++w++w++  	l 	t~/E% 0FFut|,,, +; "! ? ?A AK!,0@!@ #! 	>>&$//D|$A A A A   ***$; ; ; ;  
 ==/=@@ 	/~%!24!8!8//MM*lMCCE E E'//0EFFF&@HH$& & & N..t444 7((///$//666 ;)11%888)44T::: A)11%88899%@@@N++D111N!!!!! 	/O%% 	/""4.....	/ 	/r4   c                 J    | j         r| j                                          dS dS )z%
        Close error dialog.
        N)rZ   rejectrv   s    r5   close_error_dlgzConsoleWidget.close_error_dlg  s2     > 	$N!!#####	$ 	$r4   c                     | j         j                                        rd| _        | j                                          d| _         dS )z&
        Remove error dialog.
        TN)rZ   dismiss_box	isCheckedrW   
disconnectrv   s    r5   r   zConsoleWidget.remove_error_dlg  sD     >%//11 	&!%D!!###r4   c                 V    | j                             t          |                      dS )z-
        Show environment variables.
        rq   N)rY   r   r   rv   s    r5   r   zConsoleWidget.show_env  s,    
 	  $!7!7!788888r4   c                     t           j        S )z(
        Return the `sys.path`.
        )syspathrv   s    r5   get_sys_pathzConsoleWidget.get_sys_path  s     xr4   c                     t          |           }|                    t          j        dd|                     d                     | j                            |           dS )z"
        Show `sys.path`.
        r  zsys.pathTr   )r   readonlyr|   N)r   r   r	  r
  r   rY   r   )rP   editors     r5   r   zConsoleWidget.show_syspath  sj    
 #$///H!!),,	 	 	
 	
 	
 	  (((((r4   Fc                    || j         j                                         t          | t	          d          t                      t	          d          dz             \  }}| j         j                                         |r;t          j        t          j
        |                     t          j        |          }ndS t                              d|           t          j        |          }t          }d ||          d ||          d}|r| j                                          |                     d	d	           | j                             |d
z              | j                             |           dS )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basenamerT   debugabspathr   setFocusr   writerun_command)rP   filenamesilent	set_focusargs
_selfilterrbscommands           r5   r   zConsoleWidget.run_script  sd    J"//111#2#$$  .!!$==	$ $ Hj J"00222 X..///<11-t444;x(( 03Hss4yyyyI 	"J!!!tT***
&&&
w'''''r4   c                     t          t          |                    }|r<|                                \  }}|                     |t	          |                     dS dS )z*
        Go to error if relevant.
        N)r   r   groupsedit_scriptint)rP   rz   matchfnamelnbs        r5   rk   zConsoleWidget.go_to_error  s^      t 4 455 	.JE3UCHH-----	. 	.r4   c                     |K| j                             ||           | j                            t	          j        |          |d           dS dS )z
        Edit script.
        NrE   )r\   external_editorsig_edit_goto_requestedrN   r  r  )rP   r  gotos      r5   r'  zConsoleWidget.edit_script  sU     J&&x666(--ck(.C.CT2NNNNN  r4   c                     | j                             t          |                     | j                                          dS )z8
        Execute lines and give focus to shell.
        N)r\   r   r   r  )rP   r   s     r5   r   zConsoleWidget.execute_lines'  s<     	
  !6!6777
r4   c                     d}|It          j        | t          d          t          d          |                     d          dd          \  }}|r|                     d|           dS dS )z-"
        Change maximum line count.
        TNBufferzMaximum line countrF   r   i@B )r   getIntr   r[   r   )rP   r   valids      r5   r   z#ConsoleWidget.change_max_line_count.  s    
 ='.(&''.// LE5  	3MM*E22222	3 	3r4   c           	          d}|Rt          j        | t          d          t          d          t          j        |                     d                    \  }}|r%|                     dt          |                     dS dS )z.
        Change external editor path.
        TNzExternal editorz External editor executable path:r   )r   getTextr   r	   Normalr[   r   r   )rP   r
  r5  s      r5   r   zConsoleWidget.change_exteditorA  s    
 <&.#$$455 455 KD%  	HMM0.2F2FGGGGG	H 	Hr4   c                     || j         _        dS )ze
        Set the callback function to execute when the `exit_interpreter` is
        called.
        N)r\   exitfunc)rP   funcs     r5   set_exit_functionzConsoleWidget.set_exit_functionS  s    
 #
r4   c                 :    | j                             |           dS )z1
        Set font of the internal shell.
        N)r\   set_font)rP   fonts     r5   r>  zConsoleWidget.set_fontZ  s      	
D!!!!!r4   c                 B    | j         j                                         dS )zJ
        Redirect stdout and stderr when using open file dialogs.
        N)r\   r  r  rv   s    r5   r  zConsoleWidget.redirect_stds`  s!     	
,,.....r4   c                 B    | j         j                                         dS )zI
        Restore stdout and stderr when using open file dialogs.
        N)r\   r  r  rv   s    r5   r  zConsoleWidget.restore_stdsf  s!     	
++-----r4   c                 .    || j         j        j        |<   dS )zT
        Add an object to the namespace dictionary of the internal console.
        N)r\   r  r   )rP   ro   r   s      r5   set_namespace_itemz ConsoleWidget.set_namespace_iteml  s     26
(...r4   c                 8    | j                                          dS )z
        Exit the internal console interpreter.

        This is equivalent to requesting the main application to quit.
        N)r\   exit_interpreterrv   s    r5   rE  zConsoleWidget.exit_interpreterr  s     	
##%%%%%r4   r   )NFFN)Nr,  )/r)   r*   r+   r   strr(  r/  ri   rm   rn   dictre   rS   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rh   r  r   r   r  r   r   rk   r'  r   r   r   r<  r>  r  r  rC  rE  __classcell__)rt   s   @r5   rC   rC   S   sX        %fS#s33  FHHM !'s:C :C :C :C :C :C|% % %[7 [7 [7z ^+,,,/ / -,/ ^6"""+ + #"+ ^12222 2 322    
) ) )% % %&0 0 0* * *& & &      
T$ZZf/ f/ f/ Zf/P$ $ $   
TVV9 9 V9   
TVV) ) V) 
TVV@E (  (  ( V (D. . .O O O O   
TVV3 3 3 V3$ 
TVVH H H VH"# # #" " "/ / /. . .6 6 6& & & & & & &r4   rC   )<__doc__loggingr  os.pathr
  r  r	  qtpy.compatr   qtpy.QtCorer   r   r   qtpy.QtWidgetsr   r   r	   r
   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)   rT   r    r7   r;   r?   rC   r3   r4   r5   <module>r_     s     				       



 ( ' ' ' ' ' ( ( ( ( ( ( ( ( ( ( H H H H H H H H H H H H 1 0 0 0 0 0 C C C C C C % % % % % % ; ; ; ; ; ; 7 7 7 7 7 7 : : : : : : 3 3 3 3 3 3 3 3 F F F F F F + + + + + + * * * * * *3 3 3 3 3 3 3 3 3 3 > > > > > > > > > > > > > > 2 2 2 2 2 2 8 8 8 8 8 8 
	8	$	$
& & & & & & & & 3 3 3 3 3 3 3 3       
       e& e& e& e& e&$ e& e& e& e& e&r4   