| 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 simple plugin manager implementation. """ |
| 11 |
|
| 12 |
|
| 13 |
import logging |
| 14 |
import warnings |
| 15 |
from fnmatch import fnmatch |
| 16 |
|
| 17 |
from traits.api import Event, HasTraits, Instance, List, observe, provides, Str |
| 18 |
|
| 19 |
from .i_application import IApplication |
| 20 |
from .i_plugin import IPlugin |
| 21 |
from .i_plugin_manager import IPluginManager |
| 22 |
from .plugin_event import PluginEvent |
| 23 |
|
| 24 |
logger = logging.getLogger(__name__) |
| 25 |
|
| 26 |
|
| 27 |
@provides(IPluginManager) |
| 28 |
class PluginManager(HasTraits): |
| 29 |
"""A simple plugin manager implementation. |
| 30 |
|
| 31 |
This implementation manages an explicit collection of plugin instances, |
| 32 |
e.g:: |
| 33 |
|
| 34 |
plugin_manager = PluginManager(plugins=[MyPlugin(), YourPlugin()]) |
| 35 |
|
| 36 |
Plugins can be added and removed after construction time via the methods |
| 37 |
'add_plugin' and 'remove_plugin'. |
| 38 |
|
| 39 |
""" |
| 40 |
|
| 41 |
#### 'IPluginManager' protocol ############################################ |
| 42 |
|
| 43 |
#: Fired when a plugin has been added to the manager. |
| 44 |
plugin_added = Event(PluginEvent) |
| 45 |
|
| 46 |
#: Fired when a plugin has been removed from the manager. |
| 47 |
plugin_removed = Event(PluginEvent) |
| 48 |
|
| 49 |
#### 'PluginManager' protocol ############################################# |
| 50 |
|
| 51 |
#: The application that the plugin manager is part of. |
| 52 |
application = Instance(IApplication) |
| 53 |
|
| 54 |
@observe("application") |
| 55 |
def _set_new_application_on_all_plugins(self, event): |
| 56 |
"""Static trait change handler.""" |
| 57 |
|
| 58 |
self._update_application_on_plugins([], self._plugins) |
| 59 |
|
| 60 |
#: An optional list of the Ids of the plugins that are to be excluded by |
| 61 |
#: the manager. |
| 62 |
#: |
| 63 |
#: Each item in the list is actually an 'fnmatch' expression. |
| 64 |
exclude = List(Str) |
| 65 |
|
| 66 |
#: An optional list of the Ids of the plugins that are to be included by |
| 67 |
#: the manager (i.e. *only* plugins with Ids in this list will be added to |
| 68 |
#: the manager). |
| 69 |
#: |
| 70 |
#: Each item in the list is actually an 'fnmatch' expression. |
| 71 |
include = List(Str) |
| 72 |
|
| 73 |
#### 'object' protocol #################################################### |
| 74 |
|
| 75 |
def __init__(self, plugins=None, **traits): |
| 76 |
"""Constructor. |
| 77 |
|
| 78 |
We allow the caller to specify an initial list of plugins, but the |
| 79 |
list itself is not part of the public API. To add and remove plugins |
| 80 |
after construction, use the 'add_plugin' and 'remove_plugin' methods |
| 81 |
respectively. The manager is also iterable, so to iterate over the |
| 82 |
plugins use 'for plugin in plugin_manager'. |
| 83 |
|
| 84 |
""" |
| 85 |
if "include" in traits or "exclude" in traits: |
| 86 |
warnings.warn( |
| 87 |
"The 'include' and 'exclude' traits to PluginManager " |
| 88 |
"are deprecated, and will be removed in a future version " |
| 89 |
"of Envisage", |
| 90 |
DeprecationWarning, |
| 91 |
stacklevel=2, |
| 92 |
) |
| 93 |
|
| 94 |
super().__init__(**traits) |
| 95 |
|
| 96 |
if plugins is not None: |
| 97 |
self._plugins = plugins |
| 98 |
|
| 99 |
def __iter__(self): |
| 100 |
"""Return an iterator over the manager's plugins.""" |
| 101 |
|
| 102 |
plugins = [ |
| 103 |
plugin |
| 104 |
for plugin in self._plugins |
| 105 |
if self._include_plugin(plugin.id) |
| 106 |
] |
| 107 |
|
| 108 |
return iter(plugins) |
| 109 |
|
| 110 |
#### 'IPluginManager' protocol ############################################ |
| 111 |
|
| 112 |
def add_plugin(self, plugin): |
| 113 |
"""Add a plugin to the manager.""" |
| 114 |
|
| 115 |
self._plugins.append(plugin) |
| 116 |
self.plugin_added = PluginEvent(plugin=plugin) |
| 117 |
|
| 118 |
def get_plugin(self, plugin_id): |
| 119 |
"""Return the plugin with the specified Id.""" |
| 120 |
|
| 121 |
for plugin in self._plugins: |
| 122 |
if plugin_id == plugin.id: |
| 123 |
if not self._include_plugin(plugin.id): |
| 124 |
plugin = None |
| 125 |
|
| 126 |
break |
| 127 |
|
| 128 |
else: |
| 129 |
plugin = None |
| 130 |
|
| 131 |
return plugin |
| 132 |
|
| 133 |
def remove_plugin(self, plugin): |
| 134 |
"""Remove a plugin from the manager.""" |
| 135 |
|
| 136 |
self._plugins.remove(plugin) |
| 137 |
self.plugin_removed = PluginEvent(plugin=plugin) |
| 138 |
|
| 139 |
def start(self): |
| 140 |
"""Start the plugin manager.""" |
| 141 |
|
| 142 |
for plugin in self._plugins: |
| 143 |
self.start_plugin(plugin) |
| 144 |
|
| 145 |
def start_plugin(self, plugin=None, plugin_id=None): |
| 146 |
"""Start the specified plugin.""" |
| 147 |
|
| 148 |
plugin = plugin or self.get_plugin(plugin_id) |
| 149 |
if plugin is not None: |
| 150 |
logger.debug("plugin %s starting", plugin.id) |
| 151 |
plugin.activator.start_plugin(plugin) |
| 152 |
logger.debug("plugin %s started", plugin.id) |
| 153 |
|
| 154 |
else: |
| 155 |
raise ValueError("no such plugin %s" % plugin_id) |
| 156 |
|
| 157 |
def stop(self): |
| 158 |
"""Stop the plugin manager.""" |
| 159 |
|
| 160 |
# We stop the plugins in the reverse order that they were started. |
| 161 |
stop_order = self._plugins[:] |
| 162 |
stop_order.reverse() |
| 163 |
|
| 164 |
for plugin in stop_order: |
| 165 |
self.stop_plugin(plugin) |
| 166 |
|
| 167 |
def stop_plugin(self, plugin=None, plugin_id=None): |
| 168 |
"""Stop the specified plugin.""" |
| 169 |
|
| 170 |
plugin = plugin or self.get_plugin(plugin_id) |
| 171 |
if plugin is not None: |
| 172 |
logger.debug("plugin %s stopping", plugin.id) |
| 173 |
plugin.activator.stop_plugin(plugin) |
| 174 |
logger.debug("plugin %s stopped", plugin.id) |
| 175 |
|
| 176 |
else: |
| 177 |
raise ValueError("no such plugin %s" % plugin_id) |
| 178 |
|
| 179 |
#### Protected 'PluginManager' ############################################ |
| 180 |
|
| 181 |
# The plugins that the manager manages! |
| 182 |
_plugins = List(IPlugin) |
| 183 |
|
| 184 |
@observe("_plugins") |
| 185 |
def _update_application_on_all_plugins(self, event): |
| 186 |
"""Static trait change handler.""" |
| 187 |
old, new = event.old, event.new |
| 188 |
self._update_application_on_plugins(old, new) |
| 189 |
|
| 190 |
@observe("_plugins:items") |
| 191 |
def _update_application_on_changed_plugins(self, event): |
| 192 |
"""Static trait change handler.""" |
| 193 |
|
| 194 |
self._update_application_on_plugins(event.removed, event.added) |
| 195 |
|
| 196 |
def _include_plugin(self, plugin_id): |
| 197 |
"""Return True if the plugin should be included. |
| 198 |
|
| 199 |
This is just shorthand for:- |
| 200 |
|
| 201 |
if self._is_included(plugin_id) and not self._is_excluded(plugin_id): |
| 202 |
... |
| 203 |
|
| 204 |
""" |
| 205 |
|
| 206 |
return self._is_included(plugin_id) and not self._is_excluded( |
| 207 |
plugin_id |
| 208 |
) |
| 209 |
|
| 210 |
#### Private protocol ##################################################### |
| 211 |
|
| 212 |
def _is_excluded(self, plugin_id): |
| 213 |
"""Return True if the plugin Id is excluded. |
| 214 |
|
| 215 |
If no 'exclude' patterns are specified then this method returns False |
| 216 |
for all plugin Ids. |
| 217 |
|
| 218 |
""" |
| 219 |
|
| 220 |
if len(self.exclude) == 0: |
| 221 |
return False |
| 222 |
|
| 223 |
for pattern in self.exclude: |
| 224 |
if fnmatch(plugin_id, pattern): |
| 225 |
return True |
| 226 |
|
| 227 |
return False |
| 228 |
|
| 229 |
def _is_included(self, plugin_id): |
| 230 |
"""Return True if the plugin Id is included. |
| 231 |
|
| 232 |
If no 'include' patterns are specified then this method returns True |
| 233 |
for all plugin Ids. |
| 234 |
|
| 235 |
""" |
| 236 |
|
| 237 |
if len(self.include) == 0: |
| 238 |
return True |
| 239 |
|
| 240 |
for pattern in self.include: |
| 241 |
if fnmatch(plugin_id, pattern): |
| 242 |
return True |
| 243 |
|
| 244 |
return False |
| 245 |
|
| 246 |
def _update_application_on_plugins(self, removed, added): |
| 247 |
"""Update the 'application' trait of plugins added/removed.""" |
| 248 |
|
| 249 |
for plugin in removed: |
| 250 |
plugin.application = None |
| 251 |
|
| 252 |
for plugin in added: |
| 253 |
plugin.application = self.application |