Skip to content

Commit

Permalink
Merge pull request #266 from python/bugfix/bpo-42382
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Dec 13, 2020
2 parents 877d351 + c19a2d0 commit 1816266
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
@@ -1,3 +1,10 @@
v3.3.0
======

* * #265: ``EntryPoint`` objects now expose a ``.dist`` object
referencing the ``Distribution`` when constructed from a
Distribution.

v3.2.0
======

Expand Down
25 changes: 20 additions & 5 deletions importlib_metadata/__init__.py
Expand Up @@ -24,7 +24,7 @@
from importlib import import_module
from importlib.abc import MetaPathFinder
from itertools import starmap
from typing import Any, List, TypeVar, Union
from typing import Any, List, Optional, TypeVar, Union


__all__ = [
Expand Down Expand Up @@ -85,6 +85,8 @@ class EntryPoint(
following the attr, and following any extras.
"""

dist: Optional['Distribution'] = None

def load(self):
"""Load the entry point from its definition. If only a module
is indicated by the value, return that module. Otherwise,
Expand Down Expand Up @@ -112,19 +114,27 @@ def extras(self):

@classmethod
def _from_config(cls, config):
return [
return (
cls(name, value, group)
for group in config.sections()
for name, value in config.items(group)
]
)

@classmethod
def _from_text(cls, text):
config = ConfigParser(delimiters='=')
# case sensitive: https://stackoverflow.com/q/1611799/812183
config.optionxform = str
config.read_string(text)
return EntryPoint._from_config(config)
return cls._from_config(config)

@classmethod
def _from_text_for(cls, text, dist):
return (ep._for(dist) for ep in cls._from_text(text))

def _for(self, dist):
self.dist = dist
return self

def __iter__(self):
"""
Expand Down Expand Up @@ -282,14 +292,19 @@ def metadata(self) -> PackageMetadata:
)
return email.message_from_string(text)

@property
def name(self):
"""Return the 'Name' metadata for the distribution package."""
return self.metadata['Name']

@property
def version(self):
"""Return the 'Version' metadata for the distribution package."""
return self.metadata['Version']

@property
def entry_points(self):
return EntryPoint._from_text(self.read_text('entry_points.txt'))
return list(EntryPoint._from_text_for(self.read_text('entry_points.txt'), self))

@property
def files(self):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_api.py
Expand Up @@ -69,6 +69,13 @@ def test_entry_points(self):
self.assertEqual(ep.value, 'mod:main')
self.assertEqual(ep.extras, [])

def test_entry_points_distribution(self):
entries = dict(entry_points()['entries'])
for entry in ("main", "ns:sub"):
ep = entries[entry]
self.assertIn(ep.dist.name, ('distinfo-pkg', 'egginfo-pkg'))
self.assertEqual(ep.dist.version, "1.0.0")

def test_metadata_for_this_package(self):
md = metadata('egginfo-pkg')
assert md['author'] == 'Steven Ma'
Expand Down

0 comments on commit 1816266

Please sign in to comment.