diff --git a/stevedore/extension.py b/stevedore/extension.py index 9329dd4..321d8b7 100644 --- a/stevedore/extension.py +++ b/stevedore/extension.py @@ -63,6 +63,29 @@ class Extension(object): match = self.entry_point.pattern.match(self.entry_point.value) return match.group('module') + @property + def extras(self): + """The 'extras' settings for the plugin.""" + # NOTE: The underlying package returned re.Match objects until this was + # fixed in importlib-metadata 4.11.3. This was fixed in Python 3.10 and + # backported to Python 3.9.11. For older versions without this fix, + # translate the re.Match objects to the matched strings, which seem + # more useful. + extras = [] + for extra in self.entry_point.extras: + if isinstance(extra, str): + # We were previously returning the whole string including + # backets. We need to continue doing so to preserve API + # compatibility. + extras.append(f'[{extra}]') + else: + # Python 3.6 returns _sre.SRE_Match objects. Later + # versions of python return re.Match objects. Both types + # have a 'string' attribute containing the text that + # matched the pattern. + extras.append(getattr(extra, 'string', extra)) + return extras + @property def attr(self): """The attribute of the module to be loaded.""" diff --git a/stevedore/tests/test_extension.py b/stevedore/tests/test_extension.py index b7d06fc..e75391c 100644 --- a/stevedore/tests/test_extension.py +++ b/stevedore/tests/test_extension.py @@ -251,6 +251,10 @@ class TestExtensionProperties(utils.TestCase): self.assertEqual('module.name', self.ext1.module_name) self.assertEqual('module', self.ext2.module_name) + def test_extras(self): + self.assertEqual(['[extra]'], self.ext1.extras) + self.assertEqual([], self.ext2.extras) + def test_attr(self): self.assertEqual('attribute.name', self.ext1.attr) self.assertEqual('attribute', self.ext2.attr)