Skip to content

Commit

Permalink
Migrate Console Plugin to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
goanpeca committed May 26, 2020
1 parent 18b9703 commit a08548d
Show file tree
Hide file tree
Showing 17 changed files with 837 additions and 439 deletions.
13 changes: 9 additions & 4 deletions spyder/api/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,8 @@ def _register(self):
self.sig_option_changed.connect(self.set_conf_option)
self.is_registered = True

self.update_font()

def _unregister(self):
"""
Disconnect signals and clean up the plugin to be able to stop it while
Expand Down Expand Up @@ -983,7 +985,6 @@ def apply_conf(self, options_set):
options,
container.DEFAULT_OPTIONS,
)

# By using change_options we will not emit sig_option_changed
# when setting the options
# This will also cascade on all children
Expand Down Expand Up @@ -1557,13 +1558,17 @@ def update_title(self):
"""
Update plugin title, i.e. dockwidget or window title.
"""
self.get_widget().update_title()
widget = self.get_widget()
if widget is not None:
widget.update_title()

def update_margins(self, margin):
"""
Update margins of main widget inside dockable plugin.
"""
self.get_widget().update_margins(margin)
widget = self.get_widget()
if widget is not None:
widget.update_margins(margin)

def switch_to_plugin(self, force_focus=False):
"""
Expand All @@ -1581,7 +1586,7 @@ def set_ancestor(self, ancestor_widget):
# ------------------------------------------------------------------------
@property
def dockwidget(self):
return self.get_widget().widget.dockwidget
return self.get_widget().dockwidget

@property
def options_menu(self):
Expand Down
50 changes: 29 additions & 21 deletions spyder/app/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ def register_plugin(self, plugin):
return

# Signals
plugin.sig_focus_changed.connect(self.plugin_focus_changed)
plugin.sig_free_memory_requested.connect(self.free_memory)
plugin.sig_quit_requested.connect(self.close)
plugin.sig_restart_requested.connect(self.restart)
Expand All @@ -341,17 +342,17 @@ def register_plugin(self, plugin):
margin = 0
if CONF.get('main', 'use_custom_margin'):
margin = CONF.get('main', 'custom_margin')

plugin.update_margins(margin)

# First time plugin starts
if plugin.get_conf_option('first_time', True):
logger.info("Tabify {} dockwidget...".format(plugin.NAME))
if isinstance(plugin, SpyderDockablePlugin):
if (isinstance(plugin, SpyderDockablePlugin)
and plugin.NAME != Plugins.Console):
try:
next_to_plugin = self.get_plugin(plugin.TABIFY)
except SpyderAPIError:
next_to_plugin = Plugins.Console
next_to_plugin = self.get_plugin(Plugins.Console)

self.tabify_plugins(plugin, next_to_plugin)

Expand Down Expand Up @@ -1082,19 +1083,25 @@ def create_edit_action(text, tr_text, icon):
self.create_switcher()

# Internal console plugin
logger.info("Loading internal console...")
message = _(
"Spyder Internal Console\n\n"
"This console is used to report application\n"
"internal errors and to inspect Spyder\n"
"internals with the following commands:\n"
" spy.app, spy.window, dir(spy)\n\n"
"Please don't use it to run your code\n\n"
)
CONF.set('internal_console', 'message', message)
CONF.set('internal_console', 'multithreaded', self.multithreaded)
CONF.set('internal_console', 'profile', self.profile)
CONF.set('internal_console', 'commands', [])
CONF.set('internal_console', 'namespace', {})
CONF.set('internal_console', 'show_internal_errors', True)

from spyder.plugins.console.plugin import Console
self.console = Console(self, namespace=None, exitfunc=self.closing,
profile=self.profile,
multithreaded=self.multithreaded,
message=_("Spyder Internal Console\n\n"
"This console is used to report application\n"
"internal errors and to inspect Spyder\n"
"internals with the following commands:\n"
" spy.app, spy.window, dir(spy)\n\n"
"Please don't use it to run your code\n\n"))
self.console.register_plugin()
self.add_plugin(self.console)
self.console = Console(self, configuration=CONF)
self.console.set_exit_function(self.closing)
self.register_plugin(self.console)

# Code completion client initialization
self.set_splash(_("Starting code completion manager..."))
Expand Down Expand Up @@ -1594,7 +1601,7 @@ def post_visible_setup(self):
# Hide Internal Console so that people don't use it instead of
# the External or IPython ones
if self.console.dockwidget.isVisible() and DEV is None:
self.console._toggle_view_action.setChecked(False)
self.console.toggle_view_action.setChecked(False)
self.console.dockwidget.hide()

# Show Help and Consoles by default
Expand Down Expand Up @@ -3267,9 +3274,9 @@ def global_callback(self):

def redirect_internalshell_stdio(self, state):
if state:
self.console.shell.interpreter.redirect_stds()
self.console.redirect_stds()
else:
self.console.shell.interpreter.restore_stds()
self.console.restore_stds()

def open_external_console(self, fname, wdir, args, interact, debug, python,
python_args, systerm, post_mortem=False):
Expand Down Expand Up @@ -4044,7 +4051,7 @@ def run_spyder(app, options, args):
except BaseException:
if main.console is not None:
try:
main.console.shell.exit_interpreter()
main.console.exit_interpreter()
except BaseException:
pass
raise
Expand All @@ -4053,8 +4060,9 @@ def run_spyder(app, options, args):
main.post_visible_setup()

if main.console:
main.console.shell.interpreter.namespace['spy'] = \
Spy(app=app, window=main)
namespace = CONF.get('internal_console', 'namespace', {})
main.console.start_interpreter(namespace)
main.console.set_namespace_item('spy', Spy(app=app, window=main))

# Don't show icons in menus for Mac
if sys.platform == 'darwin':
Expand Down
17 changes: 10 additions & 7 deletions spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,14 @@ def main_window(request):
# Close everything we can think of
window.editor.close_file()
window.projects.close_project()
if window.console.error_dlg:
window.console.close_error_dlg()

if window.console.error_dialog:
window.console.close_error_dialog()

window.switcher.close()
for client in window.ipyconsole.get_clients():
window.ipyconsole.close_client(client=client, force=True)

# Reset cwd
window.explorer.chdir(get_home_dir())

Expand Down Expand Up @@ -2282,11 +2285,11 @@ def test_report_comms_error(qtbot, main_window):
with qtbot.waitSignal(shell.executed, timeout=3000):
shell.execute('ls')

error_dlg = main_window.console.error_dlg
assert error_dlg is not None
assert 'Exception in comms call get_cwd' in error_dlg.error_traceback
assert 'No module named' in error_dlg.error_traceback
main_window.console.close_error_dlg()
error_dialog = main_window.console.error_dialog
assert error_dialog is not None
assert 'Exception in comms call get_cwd' in error_dialog.error_traceback
assert 'No module named' in error_dialog.error_traceback
main_window.console.close_error_dialog()
CONF.set('main', 'show_internal_errors', False)


Expand Down
4 changes: 2 additions & 2 deletions spyder/config/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def test_external_plugin_config(qtbot):
manager.get_shortcut('foo', 'bar', plugin_name='internal_console')

# Change an option in the console
console = Console()
console.set_option('max_line_count', 600)
console = Console(None, configuration=manager)
console.set_conf_option('max_line_count', 600)

# Read config filew directly
user_path = manager.get_user_config_path()
Expand Down
14 changes: 14 additions & 0 deletions spyder/plugins/console/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)

"""
Console Plugin API.
"""

# Local imports
from spyder.plugins.console.widgets.main_widget import (
ConsoleWidgetActions, ConsoleWidgetInternalSettingsSubMenuSections,
ConsoleWidgetMenus, ConsoleWidgetOptionsMenuSections)
Loading

0 comments on commit a08548d

Please sign in to comment.