Add an ExtensionManager.items() method

The ExtensionManager.items() method is analogous to the Mapping.items()
method.

(This also refactors the _extensions_by_name generation logic out of
__getitem__.)

Change-Id: Iad3a58aec7908eb3a829892bbea567705ab2dd97
This commit is contained in:
Daniel Watkins 2017-08-10 12:01:07 -04:00
parent 937aa18317
commit b8e1c16cf5
2 changed files with 23 additions and 6 deletions

View File

@ -153,7 +153,16 @@ class ExtensionManager(object):
def _init_plugins(self, extensions):
self.extensions = extensions
self._extensions_by_name = None
self._extensions_by_name_cache = None
@property
def _extensions_by_name(self):
if self._extensions_by_name_cache is None:
d = {}
for e in self.extensions:
d[e.name] = e
self._extensions_by_name_cache = d
return self._extensions_by_name_cache
ENTRY_POINT_CACHE = {}
@ -291,6 +300,14 @@ class ExtensionManager(object):
LOG.error('error calling %r: %s', e.name, err)
LOG.exception(err)
def items(self):
"""
Return an iterator of tuples of the form (name, extension).
This is analogous to the Mapping.items() method.
"""
return self._extensions_by_name.items()
def __iter__(self):
"""Produce iterator for the manager.
@ -306,11 +323,6 @@ class ExtensionManager(object):
produces the :class:`Extension` instance with the
specified name.
"""
if self._extensions_by_name is None:
d = {}
for e in self.extensions:
d[e.name] = e
self._extensions_by_name = d
return self._extensions_by_name[name]
def __contains__(self, name):

View File

@ -198,6 +198,11 @@ class TestCallback(utils.TestCase):
result = em.map_method('get_args_and_data', 42)
self.assertEqual(set(r[2] for r in result), set([42]))
def test_items(self):
em = extension.ExtensionManager('stevedore.test.extension')
expected_output = set([(name, em[name]) for name in ALL_NAMES])
self.assertEqual(expected_output, set(em.items()))
class TestLoadRequirementsNewSetuptools(utils.TestCase):
# setuptools 11.3 and later