Debian Codegraph @

envisage/composite_plugin_manager.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 plugin manager composed of other plugin managers! """
11
12
13 # Standard library imports.
14 import logging
15
16 # Enthought library imports.
17 from traits.api import (
18 Event,
19 HasTraits,
20 Instance,
21 List,
22 observe,
23 on_trait_change,
24 provides,
25 )
26
27 # Local imports.
28 from .i_application import IApplication
29 from .i_plugin import IPlugin
30 from .i_plugin_manager import IPluginManager
31 from .plugin_event import PluginEvent
32 from .plugin_manager import PluginManager
33
34 # Logging.
35 logger = logging.getLogger(__name__)
36
37
38 @provides(IPluginManager)
39 class CompositePluginManager(HasTraits):
40 """A plugin manager composed of other plugin managers!
41
42 e.g::
43
44 plugin_manager = CompositePluginManager(
45 plugin_managers = [
46 EggBasketPluginManager(...),
47 PackagePluginManager(...),
48 ]
49 )
50
51 """
52
53 #### 'IPluginManager' protocol ############################################
54
55 #### Events ####
56
57 # Fired when a plugin has been added to the manager.
58 plugin_added = Event(PluginEvent)
59
60 # Fired when a plugin has been removed from the manager.
61 plugin_removed = Event(PluginEvent)
62
63 #### 'CompositePluginManager' protocol ####################################
64
65 # The application that the plugin manager is part of.
66 application = Instance(IApplication)
67
68 @observe("application")
69 def _update_application(self, event):
70 new = event.new
71 for plugin_manager in self.plugin_managers:
72 plugin_manager.application = new
73
74 # The plugin managers that make up this plugin manager!
75 #
76 # This is currently a list of 'PluginManager's as opposed to, the more
77 # preferable 'IPluginManager' because the interface doesn't currently
78 # have an 'application' trait. Should we move 'application' up to
79 # 'IPluginManager'?
80 plugin_managers = List(PluginManager)
81
82 @on_trait_change("plugin_managers[]")
83 def _update_application_on_plugins(self, obj, trait_named, removed, added):
84 for plugin_manager in removed:
85 plugin_manager.application = self.application
86
87 for plugin_manager in added:
88 plugin_manager.application = self.application
89
90 @on_trait_change("plugin_managers:plugin_added")
91 def _plugin_added(self, obj, trait_name, old, new):
92 self.plugin_added = new
93
94 @on_trait_change("plugin_managers:plugin_removed")
95 def _plugin_removed(self, obj, trait_name, old, new):
96 self.plugin_removed = new
97
98 #### Private protocol #####################################################
99
100 # The plugins that the manager manages!
101 _plugins = List(IPlugin)
102
103 def __plugins_default(self):
104 plugins = []
105 for plugin_manager in self.plugin_managers:
106 for plugin in plugin_manager:
107 plugins.append(plugin)
108
109 return plugins
110
111 #### 'object' protocol ####################################################
112
113 def __iter__(self):
114 """Return an iterator over the manager's plugins."""
115
116 plugins = []
117 for plugin_manager in self.plugin_managers:
118 for plugin in plugin_manager:
119 plugins.append(plugin)
120
121 return iter(plugins)
122
123 #### 'IPluginManager' protocol ############################################
124
125 def add_plugin(self, plugin):
126 """Add a plugin to the manager."""
127
128 raise NotImplementedError
129
130 def get_plugin(self, plugin_id):
131 """Return the plugin with the specified Id."""
132
133 for plugin in self:
134 if plugin_id == plugin.id:
135 break
136
137 else:
138 plugin = None
139
140 return plugin
141
142 def remove_plugin(self, plugin):
143 """Remove a plugin from the manager."""
144
145 raise NotImplementedError
146
147 def start(self):
148 """Start the plugin manager."""
149
150 for plugin in self:
151 self.start_plugin(plugin)
152
153 def start_plugin(self, plugin=None, plugin_id=None):
154 """Start the specified plugin."""
155
156 plugin = plugin or self.get_plugin(plugin_id)
157 if plugin is not None:
158 logger.debug("plugin %s starting", plugin.id)
159 plugin.activator.start_plugin(plugin)
160 logger.debug("plugin %s started", plugin.id)
161
162 else:
163 raise ValueError("no such plugin %s" % plugin_id)
164
165 def stop(self):
166 """Stop the plugin manager."""
167
168 # We stop the plugins in the reverse order that they were started.
169 stop_order = list(iter(self))
170 stop_order.reverse()
171
172 for plugin in stop_order:
173 self.stop_plugin(plugin)
174
175 def stop_plugin(self, plugin=None, plugin_id=None):
176 """Stop the specified plugin."""
177
178 plugin = plugin or self.get_plugin(plugin_id)
179 if plugin is not None:
180 logger.debug("plugin %s stopping", plugin.id)
181 plugin.activator.stop_plugin(plugin)
182 logger.debug("plugin %s stopped", plugin.id)
183
184 else:
185 raise ValueError("no such plugin %s" % plugin_id)