Debian Codegraph @

envisage/composite_plugin_manager.py

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