| 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 |
""" An extension registry that uses plugins as extension providers. """ |
| 11 |
|
| 12 |
|
| 13 |
# Enthought library imports. |
| 14 |
from traits.api import Instance, observe, on_trait_change |
| 15 |
|
| 16 |
# Local imports. |
| 17 |
from .i_plugin_manager import IPluginManager |
| 18 |
from .provider_extension_registry import ProviderExtensionRegistry |
| 19 |
|
| 20 |
|
| 21 |
class PluginExtensionRegistry(ProviderExtensionRegistry): |
| 22 |
"""An extension registry that uses plugins as extension providers. |
| 23 |
|
| 24 |
The application's plugins are used as the registries providers so adding |
| 25 |
or removing a plugin affects the extension points and extensions etc. |
| 26 |
|
| 27 |
""" |
| 28 |
|
| 29 |
#### 'PluginExtensionRegistry' interface ################################## |
| 30 |
|
| 31 |
#: The plugin manager that has the plugins we are after! |
| 32 |
plugin_manager = Instance(IPluginManager) |
| 33 |
|
| 34 |
########################################################################### |
| 35 |
# 'PluginExtensionRegistry' interface. |
| 36 |
########################################################################### |
| 37 |
|
| 38 |
#### Trait change handlers ################################################ |
| 39 |
|
| 40 |
@observe("plugin_manager") |
| 41 |
def _update_providers(self, event): |
| 42 |
"""Static trait change handler.""" |
| 43 |
old, new = event.old, event.new |
| 44 |
|
| 45 |
# In practise I can't see why you would ever want (or need) to change |
| 46 |
# the registry's plugin manager on the fly, but hey... Hence, 'old' |
| 47 |
# will probably always be 'None'! |
| 48 |
if old is not None: |
| 49 |
for plugin in old: |
| 50 |
self.remove_provider(plugin) |
| 51 |
|
| 52 |
if new is not None: |
| 53 |
for plugin in new: |
| 54 |
self.add_provider(plugin) |
| 55 |
|
| 56 |
@on_trait_change("plugin_manager:plugin_added") |
| 57 |
def _on_plugin_added(self, obj, trait_name, old, event): |
| 58 |
"""Dynamic trait change handler.""" |
| 59 |
|
| 60 |
self.add_provider(event.plugin) |
| 61 |
|
| 62 |
@on_trait_change("plugin_manager:plugin_removed") |
| 63 |
def _on_plugin_removed(self, obj, trait_name, old, event): |
| 64 |
"""Dynamic trait change handler.""" |
| 65 |
|
| 66 |
self.remove_provider(event.plugin) |