| 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 |
""" |
| 11 |
Module defining a simple Python shell Envisage tasks plugin. |
| 12 |
|
| 13 |
This plugin provides a task with a simple Python shell. This shouldn't be |
| 14 |
confused with a more full-featured shell, such as those provided by IPython. |
| 15 |
""" |
| 16 |
|
| 17 |
# Standard library imports. |
| 18 |
import logging |
| 19 |
|
| 20 |
from pyface.tasks.contrib.python_shell import PythonShellTask |
| 21 |
|
| 22 |
# Enthought library imports. |
| 23 |
from traits.api import Dict, Instance, List, Property, Str |
| 24 |
|
| 25 |
from envisage.api import ExtensionPoint, IExtensionRegistry, Plugin |
| 26 |
from envisage.ui.tasks.api import TaskFactory |
| 27 |
|
| 28 |
logger = logging.getLogger() |
| 29 |
|
| 30 |
BINDINGS = "envisage.plugins.python_shell.bindings" |
| 31 |
COMMANDS = "envisage.plugins.python_shell.commands" |
| 32 |
|
| 33 |
|
| 34 |
class EnvisagePythonShellTask(PythonShellTask): |
| 35 |
"""Subclass of PythonShellTask that gets its bindings and commands from |
| 36 |
an Envisage ExtensionPoint |
| 37 |
""" |
| 38 |
|
| 39 |
id = "envisage.plugins.tasks.python_shell_task" |
| 40 |
|
| 41 |
# ExtensionPointUser interface |
| 42 |
extension_registry = Property(Instance(IExtensionRegistry)) |
| 43 |
|
| 44 |
# The list of bindings for the shell |
| 45 |
bindings = ExtensionPoint(id=BINDINGS) |
| 46 |
|
| 47 |
# The list of commands to run on shell startup |
| 48 |
commands = ExtensionPoint(id=COMMANDS) |
| 49 |
|
| 50 |
# property getter/setters |
| 51 |
|
| 52 |
def _get_extension_registry(self): |
| 53 |
if self.window is not None: |
| 54 |
return self.window.application |
| 55 |
return None |
| 56 |
|
| 57 |
|
| 58 |
class PythonShellPlugin(Plugin): |
| 59 |
"""A tasks plugin to display a simple Python shell to the user.""" |
| 60 |
|
| 61 |
# Extension point IDs. |
| 62 |
BINDINGS = BINDINGS |
| 63 |
COMMANDS = COMMANDS |
| 64 |
TASKS = "envisage.ui.tasks.tasks" |
| 65 |
|
| 66 |
#### 'IPlugin' interface ################################################## |
| 67 |
|
| 68 |
# The plugin's unique identifier. |
| 69 |
id = "envisage.plugins.tasks.python_shell_plugin" |
| 70 |
|
| 71 |
# The plugin's name (suitable for displaying to the user). |
| 72 |
name = "Python Shell" |
| 73 |
|
| 74 |
#### Extension points exposed by this plugin ############################## |
| 75 |
|
| 76 |
bindings = ExtensionPoint( |
| 77 |
List(Dict), |
| 78 |
id=BINDINGS, |
| 79 |
desc=""" |
| 80 |
This extension point allows you to contribute name/value pairs that |
| 81 |
will be bound when the interactive Python shell is started. |
| 82 |
|
| 83 |
e.g. Each item in the list is a dictionary of name/value pairs:: |
| 84 |
|
| 85 |
{'x' : 10, 'y' : ['a', 'b', 'c']} |
| 86 |
""", |
| 87 |
) |
| 88 |
|
| 89 |
commands = ExtensionPoint( |
| 90 |
List(Str), |
| 91 |
id=COMMANDS, |
| 92 |
desc=""" |
| 93 |
This extension point allows you to contribute commands that are |
| 94 |
executed when the interactive Python shell is started. |
| 95 |
|
| 96 |
e.g. Each item in the list is a string of arbitrary Python code:: |
| 97 |
|
| 98 |
'import os, sys' |
| 99 |
'from traits.api import *' |
| 100 |
|
| 101 |
Yes, I know this is insecure but it follows the usual Python rule of |
| 102 |
'we are all consenting adults'. |
| 103 |
""", |
| 104 |
) |
| 105 |
|
| 106 |
#### Contributions to extension points made by this plugin ################ |
| 107 |
|
| 108 |
# Bindings. |
| 109 |
contributed_bindings = List(contributes_to=BINDINGS) |
| 110 |
tasks = List(contributes_to=TASKS) |
| 111 |
|
| 112 |
########################################################################### |
| 113 |
# Protected interface. |
| 114 |
########################################################################### |
| 115 |
|
| 116 |
def start(self): |
| 117 |
logger.debug("started python shell plugin") |
| 118 |
|
| 119 |
def _contributed_bindings_default(self): |
| 120 |
""" |
| 121 |
By default, expose the Envisage application object to the namespace |
| 122 |
""" |
| 123 |
return [{"application": self.application}] |
| 124 |
|
| 125 |
def _tasks_default(self): |
| 126 |
return [ |
| 127 |
TaskFactory( |
| 128 |
id="envisage.plugins.tasks.python_shell_task", |
| 129 |
name="Python Shell", |
| 130 |
factory=EnvisagePythonShellTask, |
| 131 |
), |
| 132 |
] |