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 21, 2020
1 parent 4058707 commit 1cccca0
Show file tree
Hide file tree
Showing 16 changed files with 756 additions and 422 deletions.
46 changes: 27 additions & 19 deletions spyder/app/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,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_redirect_stdio_requested.connect(
Expand Down Expand Up @@ -1073,19 +1074,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 @@ -1585,7 +1592,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 @@ -2764,7 +2771,7 @@ def closing(self, cancelable=False):
return False

for plugin in (self.widgetlist + self.thirdparty_plugins):
# New API
# # New API
try:
plugin.close_window()
if not plugin.on_close(cancelable):
Expand Down Expand Up @@ -3258,9 +3265,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 @@ -4035,7 +4042,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 @@ -4044,8 +4051,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 @@ -2278,11 +2281,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
12 changes: 12 additions & 0 deletions spyder/plugins/console/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- 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
Loading

0 comments on commit 1cccca0

Please sign in to comment.