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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
#
# Author: Evan Patterson
import builtins
from code import compile_command, InteractiveInterpreter
from io import StringIO
import sys
from time import time
import warnings
from pyface.qt import QtCore, QtGui
from pygments.lexers import PythonLexer
from traits.api import Event, provides
from traits.util.clean_strings import python_name
from .code_editor.pygments_highlighter import PygmentsHighlighter
from .console.api import (
BracketMatcher,
CallTipWidget,
CompletionLexer,
HistoryConsoleWidget,
)
from pyface.i_python_shell import IPythonShell, MPythonShell
from pyface.key_pressed_event import KeyPressedEvent
from .layout_widget import LayoutWidget
@provides(IPythonShell)
class PythonShell(MPythonShell, LayoutWidget):
""" The toolkit specific implementation of a PythonShell. See the
IPythonShell interface for the API documentation.
"""
# 'IPythonShell' interface ---------------------------------------------
command_executed = Event()
key_pressed = Event(KeyPressedEvent)
# --------------------------------------------------------------------------
# 'object' interface
# --------------------------------------------------------------------------
# FIXME v3: Either make this API consistent with other Widget sub-classes
# or make it a sub-class of HasTraits.
def __init__(self, parent=None, **traits):
create = traits.pop("create", None)
super().__init__(parent=parent, **traits)
if create:
# Create the toolkit-specific control that represents the widget.
self.create()
warnings.warn(
"automatic widget creation is deprecated and will be removed "
"in a future Pyface version, code should not pass the create "
"parameter and should instead call create() explicitly",
DeprecationWarning,
stacklevel=2,
)
elif create is not None:
warnings.warn(
"setting create=False is no longer required",
DeprecationWarning,
stacklevel=2,
)
# --------------------------------------------------------------------------
# 'IPythonShell' interface
# --------------------------------------------------------------------------
def interpreter(self):
return self.control.interpreter
def execute_command(self, command, hidden=True):
self.control.execute(command, hidden=hidden)
def execute_file(self, path, hidden=True):
self.control.execute_file(path, hidden=hidden)
def get_history(self):
""" Return the current command history and index.
Returns
-------
history : list of str
The list of commands in the new history.
history_index : int from 0 to len(history)
The current item in the command history navigation.
"""
return self.control._history, self.control._history_index
def set_history(self, history, history_index):
""" Replace the current command history and index with new ones.
Parameters
----------
history : list of str
The list of commands in the new history.
history_index : int
The current item in the command history navigation.
"""
if not 0 <= history_index <= len(history):
history_index = len(history)
self.control._set_history(history, history_index)
# --------------------------------------------------------------------------
# 'IWidget' interface.
# --------------------------------------------------------------------------
def _create_control(self, parent):
return PyfacePythonWidget(self, parent)
def _add_event_listeners(self):
super()._add_event_listeners()
# Connect signals for events.
self.control.executed.connect(self._on_command_executed)
self._event_filter.signal.connect(self._on_obj_drop)
def _remove_event_listeners(self):
if self.control is not None:
# Disconnect signals for events.
self.control.executed.disconnect(self._on_command_executed)
self._event_filter.signal.disconnect(self._on_obj_drop)
self.control._remove_event_listeners()
super()._remove_event_listeners()
def __event_filter_default(self):
return _DropEventEmitter(self.control)
# --------------------------------------------------------------------------
# 'Private' interface.
# --------------------------------------------------------------------------
def _on_obj_drop(self, obj):
""" Handle dropped objects and add to interpreter local namespace. """
# If we can't create a valid Python identifier for the name of an
# object we use this instead.
name = "dragged"
if (
hasattr(obj, "name")
and isinstance(obj.name, str)
and len(obj.name) > 0
):
py_name = python_name(obj.name)
# Make sure that the name is actually a valid Python identifier.
try:
if eval(py_name, {py_name: True}):
name = py_name
except Exception:
pass
self.control.interpreter.locals[name] = obj
self.control.execute(name)
self.control._control.setFocus()
class PythonWidget(HistoryConsoleWidget):
""" A basic in-process Python interpreter.
"""
# Emitted when a command has been executed in the interpeter.
executed = QtCore.Signal()
# --------------------------------------------------------------------------
# 'object' interface
# --------------------------------------------------------------------------
def __init__(self, parent=None):
super().__init__(parent)
# PythonWidget attributes.
self.locals = dict(__name__="__console__", __doc__=None)
self.interpreter = InteractiveInterpreter(self.locals)
# PythonWidget protected attributes.
self._buffer = StringIO()
self._bracket_matcher = BracketMatcher(self._control)
self._call_tip_widget = CallTipWidget(self._control)
self._completion_lexer = CompletionLexer(PythonLexer())
self._hidden = False
self._highlighter = PythonWidgetHighlighter(self)
self._last_refresh_time = 0
# file-like object attributes.
self.encoding = sys.stdin.encoding
# Configure the ConsoleWidget.
self.tab_width = 4
self._set_continuation_prompt("... ")
# Configure the CallTipWidget.
self._call_tip_widget.setFont(self.font)
self.font_changed.connect(self._call_tip_widget.setFont)
# Connect signal handlers.
document = self._control.document()
document.contentsChange.connect(self._document_contents_change)
# Display the banner and initial prompt.
self.reset()
def _remove_event_listeners(self):
self.font_changed.disconnect(self._call_tip_widget.setFont)
document = self._control.document()
document.contentsChange.disconnect(self._document_contents_change)
self._bracket_matcher._remove_event_listeners()
super()._remove_event_listeners()
# --------------------------------------------------------------------------
# file-like object interface
# --------------------------------------------------------------------------
def flush(self):
""" Flush the buffer by writing its contents to the screen.
"""
self._buffer.seek(0)
text = self._buffer.getvalue()
self._buffer.close()
self._buffer = StringIO()
self._append_plain_text(text)
self._control.moveCursor(QtGui.QTextCursor.MoveOperation.End)
def readline(self, prompt=None):
""" Read and return one line of input from the user.
"""
return self._readline(prompt)
def write(self, text, refresh=True):
""" Write text to the buffer, possibly flushing it if 'refresh' is set.
"""
if not self._hidden:
self._buffer.write(text)
if refresh:
current_time = time()
if current_time - self._last_refresh_time > 0.05:
self.flush()
self._last_refresh_time = current_time
def writelines(self, lines, refresh=True):
""" Write a list of lines to the buffer.
"""
for line in lines:
self.write(line, refresh=refresh)
# ---------------------------------------------------------------------------
# 'ConsoleWidget' abstract interface
# ---------------------------------------------------------------------------
def _is_complete(self, source, interactive):
""" Returns whether 'source' can be completely processed and a new
prompt created. When triggered by an Enter/Return key press,
'interactive' is True; otherwise, it is False.
"""
if interactive:
lines = source.splitlines()
if len(lines) == 1:
try:
return compile_command(source) is not None
except:
# We'll let the interpeter handle the error.
return True
else:
return lines[-1].strip() == ""
else:
return True
def _execute(self, source, hidden):
""" Execute 'source'. If 'hidden', do not show any output.
See parent class :meth:`execute` docstring for full details.
"""
# Save the current std* and point them here
old_stdin = sys.stdin
old_stdout = sys.stdout
old_stderr = sys.stderr
sys.stdin = sys.stdout = sys.stderr = self
# Run the source code in the interpeter
self._hidden = hidden
try:
self.interpreter.runsource(source)
finally:
self._hidden = False
# Restore std* unless the executed changed them
if sys.stdin is self:
sys.stdin = old_stdin
if sys.stdout is self:
sys.stdout = old_stdout
if sys.stderr is self:
sys.stderr = old_stderr
self.executed.emit()
self._show_interpreter_prompt()
def _prompt_started_hook(self):
""" Called immediately after a new prompt is displayed.
"""
if not self._reading:
self._highlighter.highlighting_on = True
def _prompt_finished_hook(self):
""" Called immediately after a prompt is finished, i.e. when some input
will be processed and a new prompt displayed.
"""
if not self._reading:
self._highlighter.highlighting_on = False
def _tab_pressed(self):
""" Called when the tab key is pressed. Returns whether to continue
processing the event.
"""
# Perform tab completion if:
# 1) The cursor is in the input buffer.
# 2) There is a non-whitespace character before the cursor.
text = self._get_input_buffer_cursor_line()
if text is None:
return False
complete = bool(text[: self._get_input_buffer_cursor_column()].strip())
if complete:
self._complete()
return not complete
# ---------------------------------------------------------------------------
# 'ConsoleWidget' protected interface
# ---------------------------------------------------------------------------
def _event_filter_console_keypress(self, event):
""" Reimplemented for smart backspace.
"""
if (
event.key() == QtCore.Qt.Key.Key_Backspace
and not event.modifiers() & QtCore.Qt.KeyboardModifier.AltModifier
):
# Smart backspace: remove four characters in one backspace if:
# 1) everything left of the cursor is whitespace
# 2) the four characters immediately left of the cursor are spaces
col = self._get_input_buffer_cursor_column()
cursor = self._control.textCursor()
if col > 3 and not cursor.hasSelection():
text = self._get_input_buffer_cursor_line()[:col]
if text.endswith(" ") and not text.strip():
cursor.movePosition(
QtGui.QTextCursor.MoveOperation.Left, QtGui.QTextCursor.MoveMode.KeepAnchor, 4
)
cursor.removeSelectedText()
return True
return super()._event_filter_console_keypress(event)
def _insert_continuation_prompt(self, cursor):
""" Reimplemented for auto-indentation.
"""
super()._insert_continuation_prompt(cursor)
source = self.input_buffer
space = 0
for c in source.splitlines()[-1]:
if c == "\t":
space += 4
elif c == " ":
space += 1
else:
break
if source.rstrip().endswith(":"):
space += 4
cursor.insertText(" " * space)
# ---------------------------------------------------------------------------
# 'PythonWidget' public interface
# ---------------------------------------------------------------------------
def execute_file(self, path, hidden=False):
""" Attempts to execute file with 'path'. If 'hidden', no output is
shown.
"""
self.execute("exec(open(%s).read())" % repr(path), hidden=hidden)
def reset(self):
""" Resets the widget to its initial state. Similar to ``clear``, but
also re-writes the banner.
"""
self._reading = False
self._highlighter.highlighting_on = False
self._control.clear()
self._append_plain_text(self._get_banner())
self._show_interpreter_prompt()
# ---------------------------------------------------------------------------
# 'PythonWidget' protected interface
# ---------------------------------------------------------------------------
def _call_tip(self):
""" Shows a call tip, if appropriate, at the current cursor location.
"""
# Decide if it makes sense to show a call tip
cursor = self._get_cursor()
cursor.movePosition(QtGui.QTextCursor.MoveOperation.Left)
if cursor.document().characterAt(cursor.position()) != "(":
return False
context = self._get_context(cursor)
if not context:
return False
# Look up the context and show a tip for it
symbol, leftover = self._get_symbol_from_context(context)
doc = getattr(symbol, "__doc__", None)
if doc is not None and not leftover:
self._call_tip_widget.show_call_info(doc=doc)
return True
return False
def _complete(self):
""" Performs completion at the current cursor location.
"""
context = self._get_context()
if context:
symbol, leftover = self._get_symbol_from_context(context)
if len(leftover) == 1:
leftover = leftover[0]
if symbol is None:
names = list(self.interpreter.locals.keys())
names += list(builtins.__dict__.keys())
else:
names = dir(symbol)
completions = [n for n in names if n.startswith(leftover)]
if completions:
cursor = self._get_cursor()
cursor.movePosition(
QtGui.QTextCursor.MoveOperation.Left, n=len(context[-1])
)
self._complete_with_items(cursor, completions)
def _get_banner(self):
""" Gets a banner to display at the beginning of a session.
"""
banner = (
'Python %s on %s\nType "help", "copyright", "credits" or '
'"license" for more information.'
)
return banner % (sys.version, sys.platform)
def _get_context(self, cursor=None):
""" Gets the context for the specified cursor (or the current cursor
if none is specified).
"""
if cursor is None:
cursor = self._get_cursor()
cursor.movePosition(
QtGui.QTextCursor.MoveOperation.StartOfBlock, QtGui.QTextCursor.MoveMode.KeepAnchor
)
text = cursor.selection().toPlainText()
return self._completion_lexer.get_context(text)
def _get_symbol_from_context(self, context):
""" Find a python object in the interpeter namespace from a context (a
list of names).
"""
context = list(map(str, context))
if len(context) == 0:
return None, context
base_symbol_string = context[0]
symbol = self.interpreter.locals.get(base_symbol_string, None)
if symbol is None:
symbol = builtins.__dict__.get(base_symbol_string, None)
if symbol is None:
return None, context
context = context[1:]
for i, name in enumerate(context):
new_symbol = getattr(symbol, name, None)
if new_symbol is None:
return symbol, context[i:]
else:
symbol = new_symbol
return symbol, []
def _show_interpreter_prompt(self):
""" Shows a prompt for the interpreter.
"""
self.flush()
self._show_prompt(">>> ")
# Signal handlers ----------------------------------------------------
def _document_contents_change(self, position, removed, added):
""" Called whenever the document's content changes. Display a call tip
if appropriate.
"""
# Calculate where the cursor should be *after* the change:
position += added
if position == self._get_cursor().position():
self._call_tip()
# -------------------------------------------------------------------------------
# 'PythonWidgetHighlighter' class:
# -------------------------------------------------------------------------------
class PythonWidgetHighlighter(PygmentsHighlighter):
""" A PygmentsHighlighter that can be turned on and off and that ignores
prompts.
"""
def __init__(self, python_widget):
super().__init__(python_widget._control.document())
self._current_offset = 0
self._python_widget = python_widget
self.highlighting_on = False
def highlightBlock(self, string):
""" Highlight a block of text. Reimplemented to highlight selectively.
"""
if not self.highlighting_on:
return
# The input to this function is a unicode string that may contain
# paragraph break characters, non-breaking spaces, etc. Here we acquire
# the string as plain text so we can compare it.
current_block = self.currentBlock()
string = self._python_widget._get_block_plain_text(current_block)
# Decide whether to check for the regular or continuation prompt.
if current_block.contains(self._python_widget._prompt_pos):
prompt = self._python_widget._prompt
else:
prompt = self._python_widget._continuation_prompt
# Don't highlight the part of the string that contains the prompt.
if string.startswith(prompt):
self._current_offset = len(prompt)
string = string[len(prompt):]
else:
self._current_offset = 0
super().highlightBlock(string)
def rehighlightBlock(self, block):
""" Reimplemented to temporarily enable highlighting if disabled.
"""
old = self.highlighting_on
self.highlighting_on = True
super().rehighlightBlock(block)
self.highlighting_on = old
def setFormat(self, start, count, format):
""" Reimplemented to highlight selectively.
"""
start += self._current_offset
super().setFormat(start, count, format)
# -------------------------------------------------------------------------------
# 'PyfacePythonWidget' class:
# -------------------------------------------------------------------------------
class PyfacePythonWidget(PythonWidget):
""" A PythonWidget customized to support the IPythonShell interface.
"""
# --------------------------------------------------------------------------
# 'object' interface
# --------------------------------------------------------------------------
def __init__(self, pyface_widget, *args, **kw):
""" Reimplemented to store a reference to the Pyface widget which
contains this control.
"""
self._pyface_widget = pyface_widget
super().__init__(*args, **kw)
# ---------------------------------------------------------------------------
# 'QWidget' interface
# ---------------------------------------------------------------------------
def keyPressEvent(self, event):
""" Reimplemented to generate Pyface key press events.
"""
# Pyface doesn't seem to be Str aware. Only keep the key code if it
# corresponds to a single Latin1 character.
kstr = event.text()
try:
kcode = ord(str(kstr))
except:
kcode = 0
mods = event.modifiers()
self._pyface_widget.key_pressed = KeyPressedEvent(
alt_down=((mods & QtCore.Qt.KeyboardModifier.AltModifier) == QtCore.Qt.KeyboardModifier.AltModifier),
control_down=(
(mods & QtCore.Qt.KeyboardModifier.ControlModifier) == QtCore.Qt.KeyboardModifier.ControlModifier
),
shift_down=(
(mods & QtCore.Qt.KeyboardModifier.ShiftModifier) == QtCore.Qt.KeyboardModifier.ShiftModifier
),
key_code=kcode,
event=event,
)
super().keyPressEvent(event)
class _DropEventEmitter(QtCore.QObject):
""" Handle object drops on widget. """
signal = QtCore.Signal(object)
def __init__(self, widget):
QtCore.QObject.__init__(self, widget)
self.widget = widget
widget.setAcceptDrops(True)
widget.installEventFilter(self)
def eventFilter(self, source, event):
""" Handle drop events on widget. """
typ = event.type()
if typ == QtCore.QEvent.Type.DragEnter:
if hasattr(event.mimeData(), "instance"):
# It is pymimedata and has instance data
obj = event.mimeData().instance()
if obj is not None:
event.accept()
return True
elif typ == QtCore.QEvent.Type.Drop:
if hasattr(event.mimeData(), "instance"):
# It is pymimedata and has instance data
obj = event.mimeData().instance()
if obj is not None:
self.signal.emit(obj)
event.accept()
return True
return QtCore.QObject.eventFilter(self, source, event)
|