Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Optimize implementation of NameDispatchExtensionManager

Create a dictionary mapping names to extensions so the
NameDispatchExtensionManager can just iterate over the
ones that will be invoked instead of all of the loaded
plugins.

Signed-off-by: Doug Hellmann <doug.hellmann@dreamhost.com>
  • Loading branch information...
commit 401a1e404c1c565f8ec3ceff0aba29ccdea7a90f 1 parent a9de3ec
@dhellmann dhellmann authored
Showing with 18 additions and 16 deletions.
  1. +18 −16 stevedore/dispatch.py
View
34 stevedore/dispatch.py
@@ -60,15 +60,8 @@ def func(ext, *args, **kwds):
raise RuntimeError('No %s extensions found' % self.namespace)
response = []
for e in self.extensions:
- try:
- if filter_func(e, *args, **kwds):
- response.append(func(e, *args, **kwds))
- except Exception as err:
- # FIXME: Provide an argument to control
- # whether to ignore exceptions in each
- # plugin or stop processing.
- LOG.error('error calling %r: %s', e.name, err)
- LOG.exception(err)
+ if filter_func(e, *args, **kwds):
+ self._invoke_one_plugin(response.append, func, e, args, kwds)
return response
@@ -94,6 +87,17 @@ class NameDispatchExtensionManager(DispatchExtensionManager):
:type invoke_kwds: dict
"""
+ def __init__(self, namespace, check_func, invoke_on_load=False,
+ invoke_args=(), invoke_kwds={}):
+ super(NameDispatchExtensionManager, self).__init__(
+ namespace=namespace,
+ check_func=check_func,
+ invoke_on_load=invoke_on_load,
+ invoke_args=invoke_args,
+ invoke_kwds=invoke_kwds,
+ )
+ self.by_name = dict((e.name, e) for e in self.extensions)
+
def map(self, names, func, *args, **kwds):
"""Iterate over the extensions invoking func() for any where
the name is in the given list of names.
@@ -114,10 +118,8 @@ def func(ext, *args, **kwds):
:param kwds: Keyword arguments to pass to func()
:returns: List of values returned from func()
"""
- def name_filter(ext, *args, **kwds):
- return ext.name in names
- return super(NameDispatchExtensionManager, self).map(
- name_filter,
- func,
- *args,
- **kwds)
+ response = []
+ for name in names:
+ e = self.by_name[name]
+ self._invoke_one_plugin(response.append, func, e, args, kwds)
+ return response
Please sign in to comment.
Something went wrong with that request. Please try again.