Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Namespace contributions.
for bindings in self._bindings:
for name, value in bindings.items():
self.bind(name, value)
for command in self._commands:
try:
self.execute_command(command)
except Exception as e:
logger.exception(
"The command '%s' supplied to the Ipython shell "
"plugin has raised an exception:\n%s" %
(command, traceback.format_exc()))
# Register the view as a service.
self.window.application.register_service(IPythonShell, self)
ns_view = self.window.application.get_service(INamespaceView)
if ns_view is not None:
self.on_trait_change(ns_view._on_names_changed, 'names')
def try_set_focus():
try:
self.shell.control.SetFocus()
except:
# The window may not have been created yet.
pass
def set_focus():
self.window.application.gui.invoke_later(try_set_focus)
GUI.invoke_later(set_focus)
for name, value in bindings.items():
self.bind(name, value)
for command in self._commands:
self.execute_command(command)
# We take note of the starting set of names and types bound in the
# interpreter's namespace so that we can show the user what they have
# added or removed in the namespace view.
self._namespace_types = set(
(name, type(value)) for name, value in self.namespace.items()
)
# Register the view as a service.
app = self.window.application
self._service_id = app.register_service(IPythonShell, self)
return self.shell.control
def run_file(self, path):
""" Called by the server to execute a file.
"""
shell = self.application.get_service(IPythonShell)
shell.execute_file(path, hidden=False)
def get_shell(window):
""" Given an application window, retrieve the ipython shell.
"""
return window.application.get_service(IPythonShell)
def readline(self):
pass
def writelines(self, lines):
for line in lines:
self.write(line)
def flush(self):
pass
def isatty(self):
return 1
@provides(IPythonShell)
class PythonShellView(View):
""" A view containing an interactive Python shell. """
#### 'IView' interface ####################################################
# The part's globally unique identifier.
id = "envisage.plugins.python_shell_view"
# The part's name (displayed to the user).
name = "Python"
# The default position of the view relative to the item specified in the
# 'relative_to' trait.
position = "bottom"
#### 'PythonShellView' interface ##########################################
def create_control(self, parent):
""" Creates the toolkit-specific control that represents the view.
'parent' is the toolkit-specific control that is the view's parent.
"""
self.ui = self.edit_traits(parent=parent, kind="subpanel")
self.shell_view = self.window.application.get_service(IPythonShell)
# 'shell_view' is an instance of the class PythonShellView from the
# module envisage.plugins.python_shell.view.python_shell_view.
return self.ui.control
def run_text(self, text):
""" Called by the server to execute text.
"""
shell = self.application.get_service(IPythonShell)
shell.execute_command(text, hidden=False)
def _get_tree_nodes(self):
""" Property getter. """
shell = self.window.application.get_service(IPythonShell)
# Cater for an un-initialized python shell view
if shell is None:
return NamespaceNode(value={}, readonly=True)
filtered_namespace = dict()
for name in shell.names:
filtered_namespace[name] = shell.lookup(name)
if not self.search_text == '':
filtered_namespace = filter_namespace(filtered_namespace,
self.search_text)
return NamespaceNode(value=filtered_namespace, readonly=True)
def create_control(self, parent):
""" Creates the toolkit-specific control that represents the view.
'parent' is the toolkit-specific control that is the view's parent.
"""
self.ui = self.edit_traits(parent=parent, kind='subpanel')
# Register the view as a service.
self.window.application.register_service(INamespaceView, self)
shell = self.window.application.get_service(IPythonShell)
if shell is not None:
shell.on_trait_change(self._on_names_changed, 'names')
self._on_names_changed(shell.names)
return self.ui.control
# Enthought library imports.
from envisage.api import IExtensionRegistry
from envisage.api import ExtensionPoint
from envisage.plugins.python_shell.api import IPythonShell
from envisage.plugins.ipython_shell.api import INamespaceView
from pyface.workbench.api import View
from pyface.ipython_widget import IPythonWidget
from pyface.api import GUI
from traits.api import Instance, Property, provides, Dict
# Setup a logger for this module.
logger = logging.getLogger(__name__)
@provides(IPythonShell)
class IPythonShellView(View):
""" A view containing an IPython shell. """
#### 'IView' interface ####################################################
# The part's globally unique identifier.
id = 'envisage.plugins.python_shell_view'
# The part's name (displayed to the user).
name = 'IPython'
# The default position of the view relative to the item specified in the
# 'relative_to' trait.
position = 'bottom'
#### 'PythonShellView' interface ##########################################