B
    nôb\-;  ã               @   s$  d Z ddlZddlmZ ddlZddlmZ ddlm	Z	 ddl
mZmZ ddlmZ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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/m0Z0 ddl1m2Z2 G dd„ de0ƒZ3dS )zInternal Console Pluginé    N)ÚPYQT5)Úgetopenfilename)ÚSignalÚSlot)ÚQInputDialogÚ	QLineEditÚQMenuÚQVBoxLayout)Ú_ÚDEVÚDEBUGÚdebug_print)ÚCONF)Úicon_manager)Ú	EnvDialog)Úget_error_matchÚremove_backslashesÚgetcwd_or_home)Úadd_actionsÚcreate_actionÚDialogManagerÚmimedata2url)ÚInternalShell)ÚFindReplace)ÚCollectionsEditor)ÚSpyderErrorDialog)ÚSpyderPluginWidget)Úto_text_stringc               @   sh  e Zd ZdZdZeƒ ZeeƒZee	e
e	ƒZddg ddddfdd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zd>dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zeƒ dd „ ƒZeƒ d!d"„ ƒZeƒ d#d$„ ƒZeƒ d?d%d&„ƒZd'd(„ Zd@d*d+„Zd,d-„ Z eƒ d.d/„ ƒZ!eƒ d0d1„ ƒZ"eeƒd2d3„ ƒZ#eeƒd4d5„ ƒZ$eeƒd6d7„ ƒZ%eeƒd8d9„ ƒZ&d:d;„ Z'd<d=„ Z(dS )AÚConsolez
    Console widget
    Zinternal_consoleNFc       
         sF  t rtjˆ ||d nt ˆ |¡ tdƒ tƒ ˆ _ˆ  d¡}t||||ˆ  d¡ˆ  ¡ ||||d
ˆ _	ˆ j	j
 ‡ fdd„¡ ˆ j	j ˆ j¡ ˆ j	j ‡ fdd„¡ ˆ j	j ‡ fd	d„¡ ˆ  ¡  tˆ ƒˆ _ˆ j ˆ j	¡ ˆ j ¡  ˆ  ˆ j¡ tƒ }	|	 ˆ j	¡ |	 ˆ j¡ ˆ  |	¡ ˆ j	 ˆ  d
¡¡ ˆ  d¡ d ˆ _dˆ _dˆ _d S )N)Úmainz$    ..internal console: initializingÚlight_backgroundÚmax_line_count)r    c                s   ˆ j  | d¡S )Nr   )Zshow_messageÚemit)Úmsg)Úself© ú5lib/python3.7/site-packages/spyder/plugins/console.pyÚ<lambda>D   s    z"Console.__init__.<locals>.<lambda>c                  s
   ˆ j  ¡ S )N)Úfocus_changedr"   r%   )r$   r%   r&   r'   F   s    c                s   ˆ j  | ¡S )N)Úredirect_stdior"   )Ústate)r$   r%   r&   r'   I   s    ÚwrapTÚ F)r   r   Ú__init__r   r   Údialog_managerÚ
get_optionr   Úget_plugin_fontÚshellZstatusÚconnectÚgo_to_errorr(   r)   Zinitialize_pluginr   Zfind_widgetZ
set_editorZhideZregister_widget_shortcutsr	   Z	addWidgetZ	setLayoutÚtoggle_wrap_modeZsetAcceptDropsÚ	error_dlgZerror_tracebackÚdismiss_error)
r$   ÚparentÚ	namespaceZcommandsÚmessageZexitfuncZprofileZmultithreadedr    Zlayoutr%   )r$   r&   r-   3   s:    







zConsole.__init__c             C   s"   |  | jj¡ | jj |j¡ dS )zLBind historylog instance to this console
        Not used anymore since v2.0N)Zadd_historyr1   Zhistory_filenameZappend_to_historyr2   )r$   Z
historylogr%   r%   r&   Úset_historylogg   s    zConsole.set_historylogc             C   s   || j _dS )z"Bind help instance to this consoleN)r1   Úhelp)r$   Zhelp_pluginr%   r%   r&   Úset_helpm   s    zConsole.set_helpc             C   s   t dƒS )zReturn widget titlezInternal console)r
   )r$   r%   r%   r&   Úget_plugin_titler   s    zConsole.get_plugin_titlec             C   s   | j S )zq
        Return the widget to give focus to when
        this plugin's dockwidget is raised on top-level
        )r1   )r$   r%   r%   r&   Úget_focus_widgetv   s    zConsole.get_focus_widgetc             C   s   |   ¡ }| j |¡ dS )zUpdate font from PreferencesN)r0   r1   Zset_font)r$   Zfontr%   r%   r&   Úupdate_font}   s    zConsole.update_fontc             C   s   | j  ¡  | j ¡  dS )z3Perform actions before parent main window is closedT)r.   Z	close_allr1   Zexit_interpreter)r$   Z
cancelabler%   r%   r&   Úclosing_plugin‚   s    

zConsole.closing_pluginc             C   s   d S )Nr%   )r$   r%   r%   r&   Úrefresh_pluginˆ   s    zConsole.refresh_pluginc             C   s¼  t | tdƒt d¡tdƒ| jd}|  |ddd¡ t | tdƒdt d	¡td
ƒ| jd}t | tdƒt d¡tdƒ| jd}t | tdƒt d¡tdƒ| jd}t | tdƒdtdƒ| j	d}t | tdƒddtdƒ| j
d}t | tdƒ| jd}| |  d¡¡ t | tdƒ| jd}| |  d¡¡ t | tdƒ| jd}	|	 |  d¡¡ t | tdƒ| jd}
|
 |  d¡¡ ttd ƒ| ƒ}| t d!¡¡ t|||||	|
|fƒ d||||d|g}t| jj|ƒ |S )"z*Return a list of actions related to pluginz&QuitÚexitZQuit)ÚiconÚtipÚ	triggeredr
   zCtrl+Qz&Run...NZ	run_smallzRun a Python script)rE   zEnvironment variables...Úenvironz9Show and edit environment variables (for current session)zShow sys.path contents...ÚsyspathzShow (read-only) sys.pathz	Buffer...zSet maximum line count)rD   rE   zExternal editor path...z#Set external editor executable pathz
Wrap lines)Ztoggledr+   zDisplay balloon tipsÚcalltipszAutomatic code completionzcodecompletion/autozEnter key selects completionzcodecompletion/enter_keyzInternal console settingsZtooloptions)r   r
   ÚimarC   ÚquitZregister_shortcutÚ
run_scriptÚshow_envÚshow_syspathÚchange_max_line_countÚchange_exteditorr4   Z
setCheckedr/   Útoggle_calltipsÚtoggle_codecompletionÚtoggle_codecompletion_enterr   ZsetIconr   r1   Zmenu)r$   Zquit_actionZ
run_actionZenviron_actionZsyspath_actionZbuffer_actionZexteditor_actionZwrap_actionZcalltips_actionZcodecompletion_actionZcodecompenter_actionZoption_menuZplugin_actionsr%   r%   r&   Úget_plugin_actions‹   sh    














zConsole.get_plugin_actionsc             C   s0   | j  | jj¡ | j | ¡ | jj | j¡ dS )z'Register plugin in Spyder's main windowN)r(   r2   r   Zplugin_focus_changedZadd_dockwidgetr1   Úexception_occurred)r$   r%   r%   r&   Úregister_pluginÈ   s    zConsole.register_pluginc             C   s¤   |s| j dks| jrdS t dd¡r„| j dkrvt| ƒ| _ | j jj | j¡ | j j	 | j
¡ | j jj | j¡ | j  ¡  | j  |¡ ntsŒtr | j ¡  | j ¡  dS )z~
        Exception ocurred in the internal console.

        Show a QDialog or the internal console to warn the user.
        Nr   Zshow_internal_errors)r5   r6   r   Úgetr   Z	close_btnZclickedr2   Úclose_error_dlgZrejectedÚremove_error_dlgZdetailsr3   ÚshowZappend_tracebackr   r   Ú
dockwidgetÚraise_)r$   ÚtextZis_tracebackr%   r%   r&   rT   Ï   s    



zConsole.exception_occurredc             C   s    | j j ¡ rd| _| j  ¡  dS )zClose error dialog.TN)r5   Zdismiss_boxZ	isCheckedr6   Zreject)r$   r%   r%   r&   rW   å   s    zConsole.close_error_dlgc             C   s
   d| _ dS )zRemove error dialog.N)r5   )r$   r%   r%   r&   rX   ë   s    zConsole.remove_error_dlgc             C   s   | j  ¡  dS )zQuit mainwindowN)r   Úclose)r$   r%   r%   r&   rJ   ð   s    zConsole.quitc             C   s   | j  tƒ ¡ dS )zShow environment variablesN)r.   rY   r   )r$   r%   r%   r&   rL   õ   s    zConsole.show_envc             C   s2   t ƒ }|jtjdddt d¡d | j |¡ dS )zShow sys.pathzsys.pathTiX  rG   )ÚtitleÚreadonlyÚwidthrC   N)r   ZsetupÚsysÚpathrI   rC   r.   rY   )r$   Úeditorr%   r%   r&   rM   ú   s    zConsole.show_syspathc             C   sÞ   |dkrd| j j ¡  t| tdƒtƒ tdƒd ƒ\}}| j j ¡  |r`t t	 
|¡¡ t	 |¡}ndS t|ƒ t	 |¡}t}d||ƒ||ƒf }|rœ| j  ¡  | jr¾| js¾| j d¡ | j ¡  | j  |d ¡ | j  |¡ dS )zRun a Python scriptNzRun Python scriptzPython scriptsz (*.py ; *.pyw ; *.ipy)zrunfile('%s', args='%s')TÚ
)r1   ZinterpreterZrestore_stdsr   r
   r   Zredirect_stdsÚosÚchdirÚospÚdirnameÚbasenamer   Úabspathr   ÚsetFocusrZ   ZismaximizedZ
setVisibler[   ÚwriteZrun_command)r$   ÚfilenameZsilentZ	set_focusÚargsZ
_selfilterZrbsZcommandr%   r%   r&   rK     s*    


zConsole.run_scriptc             C   s0   t t|ƒƒ}|r,| ¡ \}}|  |t|ƒ¡ dS )zGo to error if relevantN)r   r   ÚgroupsÚedit_scriptÚint)r$   r\   ÚmatchÚfnameZlnbr%   r%   r&   r3     s    zConsole.go_to_erroréÿÿÿÿc             C   sJ   t | dƒrt | jdƒs(| j ||¡ dS |dk	rF| j t |¡|d¡ dS )zEdit scriptr   rc   Nr,   )Úhasattrr   r1   Zexternal_editorÚ	edit_gotor"   rg   rj   )r$   rm   Zgotor%   r%   r&   rp   %  s    
zConsole.edit_scriptc             C   s   | j  t|ƒ¡ | j  ¡  dS )z%Execute lines and give focus to shellN)r1   Úexecute_linesr   rk   )r$   Úlinesr%   r%   r&   rw   /  s    zConsole.execute_linesc             C   sF   t  | tdƒtdƒ|  d¡dd¡\}}|rB| j |¡ |  d|¡ dS )zChange maximum line countZBufferzMaximum line countr!   r   i@B N)r   ZgetIntr
   r/   r1   ZsetMaximumBlockCountÚ
set_option)r$   ZmlcÚvalidr%   r%   r&   rN   4  s    zConsole.change_max_line_countc          	   C   s>   t  | tdƒtdƒtj|  d¡¡\}}|r:|  dt|ƒ¡ dS )zChange external editor pathzExternal editorz External editor executable path:zexternal_editor/pathN)r   ZgetTextr
   r   ZNormalr/   ry   r   )r$   rb   rz   r%   r%   r&   rO   ?  s    zConsole.change_exteditorc             C   s   | j  |¡ |  d|¡ dS )zToggle wrap moder+   N)r1   r4   ry   )r$   Úcheckedr%   r%   r&   r4   I  s    zConsole.toggle_wrap_modec             C   s   | j  |¡ |  d|¡ dS )zToggle calltipsrH   N)r1   Zset_calltipsry   )r$   r{   r%   r%   r&   rP   O  s    zConsole.toggle_calltipsc             C   s   | j  |¡ |  d|¡ dS )z Toggle automatic code completionzcodecompletion/autoN)r1   Zset_codecompletion_autory   )r$   r{   r%   r%   r&   rQ   U  s    zConsole.toggle_codecompletionc             C   s   | j  |¡ |  d|¡ dS )z$Toggle Enter key for code completionzcodecompletion/enter_keyN)r1   Zset_codecompletion_enterry   )r$   r{   r%   r%   r&   rR   [  s    z#Console.toggle_codecompletion_enterc             C   s@   |  ¡ }| ¡ r,t|ƒr"| ¡  q<| ¡  n| ¡ r<| ¡  dS )zWReimplement Qt method
        Inform Qt about the types of data that the widget acceptsN)ÚmimeDataÚhasUrlsr   ÚacceptProposedActionÚignoreÚhasText)r$   ÚeventÚsourcer%   r%   r&   ÚdragEnterEventb  s    

zConsole.dragEnterEventc             C   s^   |  ¡ }| ¡ r&t|ƒ}| j |¡ n,| ¡ rRt| ¡ ƒ}| j d¡ | j 	|¡ | 
¡  dS )z?Reimplement Qt method
        Unpack dropped data and handle itÚeofN)r|   r}   r   r1   Zdrop_pathlistr€   r   r\   Zset_cursor_positionrw   r~   )r$   r   r‚   Zpathlistrx   r%   r%   r&   Ú	dropEventn  s    zConsole.dropEvent)F)NFFN)Nrt   ))Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZCONF_SECTIONr   r(   Úboolr)   Ústrrq   rv   r-   r:   r<   r=   r>   r?   r@   rA   rS   rU   rT   rW   rX   r   rJ   rL   rM   rK   r3   rp   rw   rN   rO   r4   rP   rQ   rR   rƒ   r…   r%   r%   r%   r&   r   *   sH   3
= 


r   )4r‰   re   Úos.pathrb   rg   ra   Zqtpyr   Zqtpy.compatr   Zqtpy.QtCorer   r   Zqtpy.QtWidgetsr   r   r   r	   Zspyder.config.baser
   r   r   r   Zspyder.config.mainr   Zspyder.utilsr   rI   Zspyder.utils.environr   Zspyder.utils.miscr   r   r   Zspyder.utils.qthelpersr   r   r   r   Zspyder.widgets.internalshellr   Zspyder.widgets.findreplacer   Z1spyder.widgets.variableexplorer.collectionseditorr   Zspyder.widgets.reporterrorr   Zspyder.pluginsr   Zspyder.py3compatr   r   r%   r%   r%   r&   Ú<module>   s(   