| 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 base class for extension registry implementation. """ |
| 11 |
|
| 12 |
|
| 13 |
# Standard library imports. |
| 14 |
import logging |
| 15 |
import types |
| 16 |
import weakref |
| 17 |
|
| 18 |
# Enthought library imports. |
| 19 |
from traits.api import Dict, HasTraits, provides |
| 20 |
|
| 21 |
# Local imports. |
| 22 |
from .extension_point_changed_event import ExtensionPointChangedEvent |
| 23 |
from .i_extension_registry import IExtensionRegistry |
| 24 |
from .unknown_extension_point import UnknownExtensionPoint |
| 25 |
|
| 26 |
# Logging. |
| 27 |
logger = logging.getLogger(__name__) |
| 28 |
|
| 29 |
|
| 30 |
def _saferef(listener): |
| 31 |
""" |
| 32 |
Weak reference for a (possibly bound method) listener. |
| 33 |
|
| 34 |
Returns a weakref.WeakMethod reference for bound methods, |
| 35 |
and a regular weakref.ref otherwise. |
| 36 |
|
| 37 |
This means that for example ``_saferef(myobj.mymethod)`` |
| 38 |
returns a reference whose lifetime is connected to the |
| 39 |
lifetime of the object ``myobj``, rather than the lifetime |
| 40 |
of the temporary method ``myobj.mymethod``. |
| 41 |
|
| 42 |
Parameters |
| 43 |
---------- |
| 44 |
listener : callable |
| 45 |
Listener to return a weak reference for. This can be |
| 46 |
either a plain function, a bound method, or some other |
| 47 |
form of callable. |
| 48 |
|
| 49 |
Returns |
| 50 |
------- |
| 51 |
weakref.ref |
| 52 |
A weak reference to the listener. This will be a ``weakref.WeakMethod`` |
| 53 |
object if the listener is an instance of ``types.MethodType``, and a |
| 54 |
plain ``weakref.ref`` otherwise. |
| 55 |
|
| 56 |
""" |
| 57 |
if isinstance(listener, types.MethodType): |
| 58 |
return weakref.WeakMethod(listener) |
| 59 |
else: |
| 60 |
return weakref.ref(listener) |
| 61 |
|
| 62 |
|
| 63 |
@provides(IExtensionRegistry) |
| 64 |
class ExtensionRegistry(HasTraits): |
| 65 |
"""A base class for extension registry implementation.""" |
| 66 |
|
| 67 |
########################################################################### |
| 68 |
# Protected 'ExtensionRegistry' interface. |
| 69 |
########################################################################### |
| 70 |
|
| 71 |
# A dictionary of extensions, keyed by extension point. |
| 72 |
_extensions = Dict |
| 73 |
|
| 74 |
# The extension points that have been added *explicitly*. |
| 75 |
_extension_points = Dict |
| 76 |
|
| 77 |
# Extension listeners. |
| 78 |
# |
| 79 |
# These are called when extensions are added to or removed from an |
| 80 |
# extension point. |
| 81 |
# |
| 82 |
# e.g. Dict(extension_point, [weakref.ref(callable)]) |
| 83 |
# |
| 84 |
# A listener is any Python callable with the following signature:- |
| 85 |
# |
| 86 |
# def listener(extension_registry, extension_point_changed_event): |
| 87 |
# ... |
| 88 |
_listeners = Dict |
| 89 |
|
| 90 |
########################################################################### |
| 91 |
# 'IExtensionRegistry' interface. |
| 92 |
########################################################################### |
| 93 |
|
| 94 |
def add_extension_point_listener(self, listener, extension_point_id=None): |
| 95 |
"""Add a listener for extensions being added or removed.""" |
| 96 |
|
| 97 |
listeners = self._listeners.setdefault(extension_point_id, []) |
| 98 |
listeners.append(_saferef(listener)) |
| 99 |
|
| 100 |
def add_extension_point(self, extension_point): |
| 101 |
"""Add an extension point.""" |
| 102 |
|
| 103 |
self._extension_points[extension_point.id] = extension_point |
| 104 |
logger.debug("extension point <%s> added", extension_point.id) |
| 105 |
|
| 106 |
def get_extensions(self, extension_point_id): |
| 107 |
"""Return the extensions contributed to an extension point.""" |
| 108 |
|
| 109 |
return self._get_extensions(extension_point_id)[:] |
| 110 |
|
| 111 |
def get_extension_point(self, extension_point_id): |
| 112 |
"""Return the extension point with the specified Id.""" |
| 113 |
|
| 114 |
return self._extension_points.get(extension_point_id) |
| 115 |
|
| 116 |
def get_extension_points(self): |
| 117 |
"""Return all extension points.""" |
| 118 |
|
| 119 |
return list(self._extension_points.values()) |
| 120 |
|
| 121 |
def remove_extension_point_listener( |
| 122 |
self, listener, extension_point_id=None |
| 123 |
): |
| 124 |
"""Remove a listener for extensions being added or removed.""" |
| 125 |
|
| 126 |
listeners = self._listeners.setdefault(extension_point_id, []) |
| 127 |
listeners.remove(_saferef(listener)) |
| 128 |
|
| 129 |
def remove_extension_point(self, extension_point_id): |
| 130 |
"""Remove an extension point.""" |
| 131 |
|
| 132 |
self._check_extension_point(extension_point_id) |
| 133 |
|
| 134 |
# Remove the extension point. |
| 135 |
del self._extension_points[extension_point_id] |
| 136 |
|
| 137 |
# Remove any extensions to the extension point. |
| 138 |
if extension_point_id in self._extensions: |
| 139 |
old = self._extensions[extension_point_id] |
| 140 |
del self._extensions[extension_point_id] |
| 141 |
|
| 142 |
else: |
| 143 |
old = [] |
| 144 |
|
| 145 |
refs = self._get_listener_refs(extension_point_id) |
| 146 |
self._call_listeners(refs, extension_point_id, [], old, 0) |
| 147 |
|
| 148 |
logger.debug("extension point <%s> removed", extension_point_id) |
| 149 |
|
| 150 |
def set_extensions(self, extension_point_id, extensions): |
| 151 |
"""Set the extensions contributed to an extension point.""" |
| 152 |
|
| 153 |
self._check_extension_point(extension_point_id) |
| 154 |
|
| 155 |
old = self._get_extensions(extension_point_id) |
| 156 |
self._extensions[extension_point_id] = extensions |
| 157 |
|
| 158 |
refs = self._get_listener_refs(extension_point_id) |
| 159 |
self._call_listeners(refs, extension_point_id, extensions, old, None) |
| 160 |
|
| 161 |
########################################################################### |
| 162 |
# Protected 'ExtensionRegistry' interface. |
| 163 |
########################################################################### |
| 164 |
|
| 165 |
def _call_listeners(self, refs, extension_point_id, added, removed, index): |
| 166 |
"""Call listeners that are listening to an extension point.""" |
| 167 |
|
| 168 |
event = ExtensionPointChangedEvent( |
| 169 |
extension_point_id=extension_point_id, |
| 170 |
added=added, |
| 171 |
removed=removed, |
| 172 |
index=index, |
| 173 |
) |
| 174 |
|
| 175 |
for ref in refs: |
| 176 |
listener = ref() |
| 177 |
if listener is not None: |
| 178 |
listener(self, event) |
| 179 |
|
| 180 |
def _check_extension_point(self, extension_point_id): |
| 181 |
"""Check to see if the extension point exists. |
| 182 |
|
| 183 |
Raise an 'UnknownExtensionPoint' if it does not. |
| 184 |
|
| 185 |
""" |
| 186 |
|
| 187 |
if extension_point_id not in self._extension_points: |
| 188 |
raise UnknownExtensionPoint(extension_point_id) |
| 189 |
|
| 190 |
def _get_extensions(self, extension_point_id): |
| 191 |
"""Return the extensions for the given extension point.""" |
| 192 |
|
| 193 |
return self._extensions.setdefault(extension_point_id, []) |
| 194 |
|
| 195 |
def _get_listener_refs(self, extension_point_id): |
| 196 |
"""Get weak references to all listeners to an extension point. |
| 197 |
|
| 198 |
Returns a list containing the weak references to those listeners that |
| 199 |
are listening to this extension point specifically first, followed by |
| 200 |
those that are listening to any extension point. |
| 201 |
|
| 202 |
""" |
| 203 |
|
| 204 |
refs = [] |
| 205 |
refs.extend(self._listeners.get(extension_point_id, [])) |
| 206 |
refs.extend(self._listeners.get(None, [])) |
| 207 |
|
| 208 |
return refs |