| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
""" A view containing an interactive Python shell. """ |
| 11 |
|
| 12 |
|
| 13 |
# Standard library imports. |
| 14 |
import logging |
| 15 |
import sys |
| 16 |
|
| 17 |
from pyface.api import PythonShell |
| 18 |
from pyface.workbench.api import View |
| 19 |
from traits.api import Any, Dict, Event, Instance, Property, provides, Str |
| 20 |
|
| 21 |
# Enthought library imports. |
| 22 |
from envisage.api import ExtensionPoint, IExtensionRegistry |
| 23 |
from envisage.plugins.python_shell.api import IPythonShell |
| 24 |
|
| 25 |
# Setup a logger for this module. |
| 26 |
logger = logging.getLogger(__name__) |
| 27 |
|
| 28 |
|
| 29 |
class PseudoFile(object): |
| 30 |
"""Simulates a normal File object.""" |
| 31 |
|
| 32 |
def __init__(self, write): |
| 33 |
self.write = write |
| 34 |
|
| 35 |
def readline(self): |
| 36 |
pass |
| 37 |
|
| 38 |
def writelines(self, lines): |
| 39 |
for line in lines: |
| 40 |
self.write(line) |
| 41 |
|
| 42 |
def flush(self): |
| 43 |
pass |
| 44 |
|
| 45 |
def isatty(self): |
| 46 |
return 1 |
| 47 |
|
| 48 |
|
| 49 |
@provides(IPythonShell) |
| 50 |
class PythonShellView(View): |
| 51 |
"""A view containing an interactive Python shell.""" |
| 52 |
|
| 53 |
#### 'IView' interface #################################################### |
| 54 |
|
| 55 |
# The part's globally unique identifier. |
| 56 |
id = "envisage.plugins.python_shell_view" |
| 57 |
|
| 58 |
# The part's name (displayed to the user). |
| 59 |
name = "Python" |
| 60 |
|
| 61 |
# The default position of the view relative to the item specified in the |
| 62 |
# 'relative_to' trait. |
| 63 |
position = "bottom" |
| 64 |
|
| 65 |
#### 'PythonShellView' interface ########################################## |
| 66 |
|
| 67 |
# The interpreter's namespace. |
| 68 |
namespace = Property(Dict(Str, Any)) |
| 69 |
|
| 70 |
# The names bound in the interpreter's namespace. |
| 71 |
names = Property |
| 72 |
|
| 73 |
# Original value for 'sys.stdout': |
| 74 |
original_stdout = Any |
| 75 |
|
| 76 |
# Stdout text is posted to this event |
| 77 |
stdout_text = Event |
| 78 |
|
| 79 |
#### 'IExtensionPointUser' interface ###################################### |
| 80 |
|
| 81 |
# The extension registry that the object's extension points are stored in. |
| 82 |
extension_registry = Property(Instance(IExtensionRegistry)) |
| 83 |
|
| 84 |
#### Private interface #################################################### |
| 85 |
|
| 86 |
# Bindings. |
| 87 |
_bindings = ExtensionPoint(id="envisage.plugins.python_shell.bindings") |
| 88 |
|
| 89 |
# Commands. |
| 90 |
_commands = ExtensionPoint(id="envisage.plugins.python_shell.commands") |
| 91 |
|
| 92 |
########################################################################### |
| 93 |
# 'IExtensionPointUser' interface. |
| 94 |
########################################################################### |
| 95 |
|
| 96 |
def _get_extension_registry(self): |
| 97 |
"""Trait property getter.""" |
| 98 |
|
| 99 |
return self.window.application |
| 100 |
|
| 101 |
########################################################################### |
| 102 |
# 'View' interface. |
| 103 |
########################################################################### |
| 104 |
|
| 105 |
def create_control(self, parent): |
| 106 |
"""Creates the toolkit-specific control that represents the view.""" |
| 107 |
|
| 108 |
self.shell = shell = PythonShell(parent) |
| 109 |
shell.on_trait_change(self._on_key_pressed, "key_pressed") |
| 110 |
shell.on_trait_change(self._on_command_executed, "command_executed") |
| 111 |
|
| 112 |
# Write application standard out to this shell instead of to DOS window |
| 113 |
self.on_trait_change( |
| 114 |
self._on_write_stdout, "stdout_text", dispatch="ui" |
| 115 |
) |
| 116 |
self.original_stdout = sys.stdout |
| 117 |
sys.stdout = PseudoFile(self._write_stdout) |
| 118 |
|
| 119 |
# Namespace contributions. |
| 120 |
for bindings in self._bindings: |
| 121 |
for name, value in bindings.items(): |
| 122 |
self.bind(name, value) |
| 123 |
|
| 124 |
for command in self._commands: |
| 125 |
self.execute_command(command) |
| 126 |
|
| 127 |
# We take note of the starting set of names and types bound in the |
| 128 |
# interpreter's namespace so that we can show the user what they have |
| 129 |
# added or removed in the namespace view. |
| 130 |
self._namespace_types = set( |
| 131 |
(name, type(value)) for name, value in self.namespace.items() |
| 132 |
) |
| 133 |
|
| 134 |
# Register the view as a service. |
| 135 |
app = self.window.application |
| 136 |
self._service_id = app.register_service(IPythonShell, self) |
| 137 |
|
| 138 |
return self.shell.control |
| 139 |
|
| 140 |
def destroy_control(self): |
| 141 |
"""Destroys the toolkit-specific control that represents the view.""" |
| 142 |
|
| 143 |
super().destroy_control() |
| 144 |
|
| 145 |
# Unregister the view as a service. |
| 146 |
self.window.application.unregister_service(self._service_id) |
| 147 |
|
| 148 |
# Remove the sys.stdout handlers. |
| 149 |
self.on_trait_change(self._on_write_stdout, "stdout_text", remove=True) |
| 150 |
|
| 151 |
# Restore the original stdout. |
| 152 |
sys.stdout = self.original_stdout |
| 153 |
|
| 154 |
########################################################################### |
| 155 |
# 'PythonShellView' interface. |
| 156 |
########################################################################### |
| 157 |
|
| 158 |
#### Properties ########################################################### |
| 159 |
|
| 160 |
def _get_namespace(self): |
| 161 |
"""Property getter.""" |
| 162 |
|
| 163 |
return self.shell.interpreter().locals |
| 164 |
|
| 165 |
def _get_names(self): |
| 166 |
"""Property getter.""" |
| 167 |
|
| 168 |
return list(self.shell.interpreter().locals.keys()) |
| 169 |
|
| 170 |
#### Methods ############################################################## |
| 171 |
|
| 172 |
def bind(self, name, value): |
| 173 |
"""Binds a name to a value in the interpreter's namespace.""" |
| 174 |
|
| 175 |
self.shell.bind(name, value) |
| 176 |
|
| 177 |
def execute_command(self, command, hidden=True): |
| 178 |
"""Execute a command in the interpreter.""" |
| 179 |
|
| 180 |
return self.shell.execute_command(command, hidden) |
| 181 |
|
| 182 |
def execute_file(self, path, hidden=True): |
| 183 |
"""Execute a command in the interpreter.""" |
| 184 |
|
| 185 |
return self.shell.execute_file(path, hidden) |
| 186 |
|
| 187 |
def lookup(self, name): |
| 188 |
"""Returns the value bound to a name in the interpreter's namespace.""" |
| 189 |
|
| 190 |
return self.shell.interpreter().locals[name] |
| 191 |
|
| 192 |
########################################################################### |
| 193 |
# Private interface. |
| 194 |
########################################################################### |
| 195 |
|
| 196 |
def _write_stdout(self, text): |
| 197 |
"""Handles text written to stdout.""" |
| 198 |
|
| 199 |
self.stdout_text = text |
| 200 |
|
| 201 |
#### Trait change handlers ################################################ |
| 202 |
|
| 203 |
def _on_command_executed(self, shell): |
| 204 |
"""Dynamic trait change handler.""" |
| 205 |
|
| 206 |
if self.control is not None: |
| 207 |
# Get the set of tuples of names and types in the current |
| 208 |
# namespace. |
| 209 |
namespace_types = set( |
| 210 |
(name, type(value)) for name, value in self.namespace.items() |
| 211 |
) |
| 212 |
# Figure out the changes in the namespace, if any. |
| 213 |
added = namespace_types.difference(self._namespace_types) |
| 214 |
removed = self._namespace_types.difference(namespace_types) |
| 215 |
# Cache the new list, to use for comparison next time. |
| 216 |
self._namespace_types = namespace_types |
| 217 |
# Fire events if there are change. |
| 218 |
if len(added) > 0 or len(removed) > 0: |
| 219 |
self.trait_property_changed("namespace", {}, self.namespace) |
| 220 |
self.trait_property_changed("names", [], self.names) |
| 221 |
|
| 222 |
def _on_key_pressed(self, event): |
| 223 |
"""Dynamic trait change handler.""" |
| 224 |
|
| 225 |
if event.alt_down and event.key_code == 317: |
| 226 |
zoom = self.shell.control.GetZoom() |
| 227 |
if zoom != 20: |
| 228 |
self.shell.control.SetZoom(zoom + 1) |
| 229 |
|
| 230 |
elif event.alt_down and event.key_code == 319: |
| 231 |
zoom = self.shell.control.GetZoom() |
| 232 |
if zoom != -10: |
| 233 |
self.shell.control.SetZoom(zoom - 1) |
| 234 |
|
| 235 |
def _on_write_stdout(self, text): |
| 236 |
"""Dynamic trait change handler.""" |
| 237 |
|
| 238 |
self.shell.control.write(text) |