/
plugin.py
253 lines (208 loc) · 7.38 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
"""
Internal Console Plugin.
"""
# Standard library imports
import logging
import os
# Third party imports
from qtpy.QtCore import QObject, Signal, Slot
from qtpy.QtGui import QIcon
# Local imports
from spyder.api.plugins import Plugins, SpyderDockablePlugin
from spyder.api.plugin_registration.decorators import on_plugin_available
from spyder.api.translations import get_translation
from spyder.plugins.application.plugin import ApplicationActions
from spyder.plugins.console.widgets.main_widget import ConsoleWidget
from spyder.plugins.mainmenu.api import (
ApplicationMenus, FileMenuSections, HelpMenuSections)
# Localization
_ = get_translation('spyder')
# Logging
logger = logging.getLogger(__name__)
class Console(SpyderDockablePlugin):
"""
Console widget
"""
NAME = 'internal_console'
WIDGET_CLASS = ConsoleWidget
OPTIONAL = [Plugins.MainMenu]
CONF_SECTION = NAME
CONF_FILE = False
TABIFY = [Plugins.IPythonConsole, Plugins.History]
# --- Signals
# ------------------------------------------------------------------------
sig_focus_changed = Signal() # TODO: I think this is not being used now?
sig_edit_goto_requested = Signal(str, int, str)
"""
This signal will request to open a file in a given row and column
using a code editor.
Parameters
----------
path: str
Path to file.
row: int
Cursor starting row position.
word: str
Word to select on given row.
"""
sig_refreshed = Signal()
"""This signal is emitted when the interpreter buffer is flushed."""
sig_help_requested = Signal(dict)
"""
This signal is emitted to request help on a given object `name`.
Parameters
----------
help_data: dict
Example `{'name': str, 'ignore_unknown': bool}`.
"""
# --- SpyderDockablePlugin API
# ------------------------------------------------------------------------
def get_name(self):
return _('Internal console')
def get_icon(self):
return QIcon()
def get_description(self):
return _('Internal console running Spyder.')
def on_initialize(self):
widget = self.get_widget()
# Signals
widget.sig_edit_goto_requested.connect(self.sig_edit_goto_requested)
widget.sig_focus_changed.connect(self.sig_focus_changed)
widget.sig_quit_requested.connect(self.sig_quit_requested)
widget.sig_refreshed.connect(self.sig_refreshed)
widget.sig_help_requested.connect(self.sig_help_requested)
# Crash handling
previous_crash = self.get_conf(
'previous_crash',
default='',
section='main',
)
if previous_crash:
error_data = dict(
text=previous_crash,
is_traceback=True,
title="Segmentation fault crash",
label=_("<h3>Spyder crashed during last session</h3>"),
steps=_("Please provide any additional information you "
"might have about the crash."),
)
widget.handle_exception(error_data)
@on_plugin_available(plugin=Plugins.MainMenu)
def on_main_menu_available(self):
widget = self.get_widget()
mainmenu = self.get_plugin(Plugins.MainMenu)
# Actions
mainmenu.add_item_to_application_menu(
widget.quit_action,
menu_id=ApplicationMenus.File,
section=FileMenuSections.Restart)
def update_font(self):
font = self.get_font()
self.get_widget().set_font(font)
def on_close(self, cancelable=False):
self.get_widget().dialog_manager.close_all()
return True
def on_mainwindow_visible(self):
self.set_exit_function(self.main.closing)
# --- API
# ------------------------------------------------------------------------
@Slot()
def report_issue(self):
"""Report an issue with the SpyderErrorDialog."""
self.get_widget().report_issue()
@property
def error_dialog(self):
"""
Error dialog attribute accesor.
"""
return self.get_widget().error_dlg
def close_error_dialog(self):
"""
Close the error dialog if visible.
"""
self.get_widget().close_error_dlg()
def exit_interpreter(self):
"""
Exit the internal console interpreter.
This is equivalent to requesting the main application to quit.
"""
self.get_widget().exit_interpreter()
def execute_lines(self, lines):
"""
Execute the given `lines` of code in the internal console.
"""
self.get_widget().execute_lines(lines)
def get_sys_path(self):
"""
Return the system path of the internal console.
"""
return self.get_widget().get_sys_path()
@Slot(dict)
def handle_exception(self, error_data):
"""
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,
}
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.
"""
self.get_widget().handle_exception(
error_data,
sender=self.sender(),
internal_plugins=self._main._INTERNAL_PLUGINS,
)
def quit(self):
"""
Send the quit request to the main application.
"""
self.sig_quit_requested.emit()
def restore_stds(self):
"""
Restore stdout and stderr when using open file dialogs.
"""
self.get_widget().restore_stds()
def redirect_stds(self):
"""
Redirect stdout and stderr when using open file dialogs.
"""
self.get_widget().redirect_stds()
def set_exit_function(self, func):
"""
Set the callback function to execute when the `exit_interpreter` is
called.
"""
self.get_widget().set_exit_function(func)
def start_interpreter(self, namespace):
"""
Start the internal console interpreter.
Stdin and stdout are now redirected through the internal console.
"""
widget = self.get_widget()
widget.set_conf('namespace', namespace)
widget.start_interpreter(namespace)
def set_namespace_item(self, name, value):
"""
Add an object to the namespace dictionary of the internal console.
"""
self.get_widget().set_namespace_item(name, value)