8000 Introduce SelectableGroups, created for the 3.x line to provide forwa… · python/importlib_metadata@e0ec362 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit e0ec362

Browse files
committed
Introduce SelectableGroups, created for the 3.x line to provide forward compatibilty to the new interfaces without sacrificing backward compatibility.
1 parent d6f7c20 commit e0ec362

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

CHANGES.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
v4.0.0
1+
v3.6.0
22
======
33

44
* #284: Introduces new ``EntryPoints`` object, a tuple of
@@ -13,10 +13,18 @@ v4.0.0
1313
- Item access (e.g. ``eps[name]``) retrieves a single
1414
entry point by name.
1515

16-
``entry_points()`` now returns an ``EntryPoints``
17-
object, but provides for backward compatibility with
18-
a ``__getitem__`` accessor by group and a ``get()``
19-
method.
16+
``entry_points()`` now provides a future-compatible
17+
``SelectableGroups`` object that supplies the above interface
18+
but remains a dict for compatibility.
19+
20+
In the future, ``entry_points()`` will return an
21+
``EntryPoints`` object, but provide for backward
22+
compatibility with a deprecated ``__getitem__``
23+
accessor by group and a ``get()`` method.
24+
25+
If passing selection parameters to ``entry_points``, the
26+
future behavior is invoked and an ``EntryPoints`` is the
27+
result.
2028

2129
Construction of entry points using
2230
``dict([EntryPoint, ...])`` is now deprecated and raises

importlib_metadata/__init__.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,37 @@ def _from_text_for(cls, text, dist):
187187
return cls(ep._for(dist) for ep in EntryPoint._from_text(text))
188188

189189

190+
class SelectableGroups(dict):
191+
"""
192+
A backward- and forward-compatible result from
193+
entry_points that fully implements the dict interface.
194+
"""
195+
196+
@classmethod
197+
def load(cls, eps):
198+
by_group = operator.attrgetter('group')
199+
ordered = sorted(eps, key=by_group)
200+
grouped = itertools.groupby(ordered, by_group)
201+
return cls((group, EntryPoints(eps)) for group, eps in grouped)
202+
203+
@property
204+
def groups(self):
205+
return self.keys()
206+
207+
@property
208+
def names(self):
209+
return (ep.name for ep in self._all)
210+
211+
@property
212+
def _all(self):
213+
return itertools.chain.from_iterable(self.values())
214+
215+
def select(self, **params):
216+
if not params:
217+
return self
218+
return EntryPoints(self._all).select(**params)
219+
220+
190221
class LegacyGroupedEntryPoints(EntryPoints):
191222
"""
192223
Compatibility wrapper around EntryPoints to provide
@@ -722,7 +753,7 @@ def entry_points(**params):
722753
eps = itertools.chain.from_iterable(
723754
dist.entry_points for dist in unique(distributions())
724755
)
725-
return LegacyGroupedEntryPoints(eps).select(**params)
756+
return SelectableGroups.load(eps).select(**params)
726757

727758

728759
def files(distribution_name):

0 commit comments

Comments
 (0)
0