Skip to content

Commit dfc9613

Browse files
committed
Add module and attr properties to EntryPoint
1 parent 654dc66 commit dfc9613

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

importlib_metadata/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ def load(self):
9696
attrs = filter(None, (match.group('attr') or '').split('.'))
9797
return functools.reduce(getattr, attrs, module)
9898

99+
@property
100+
def module(self):
101+
match = self.pattern.match(self.value)
102+
return match.group('module')
103+
104+
@property
105+
def attr(self):
106+
match = self.pattern.match(self.value)
107+
return match.group('attr')
108+
99109
@property
100110
def extras(self):
101111
match = self.pattern.match(self.value)

importlib_metadata/docs/using.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ Entry points
7070
The ``entry_points()`` function returns a dictionary of all entry points,
7171
keyed by group. Entry points are represented by ``EntryPoint`` instances;
7272
each ``EntryPoint`` has a ``.name``, ``.group``, and ``.value`` attributes and
73-
a ``.load()`` method to resolve the value::
73+
a ``.load()`` method to resolve the value. There are also ``.module``,
74+
``.attr``, and ``.extras`` attributes for getting the components of the
75+
``.value`` attribute::
7476

7577
>>> eps = entry_points()
7678
>>> list(eps)
@@ -79,6 +81,12 @@ a ``.load()`` method to resolve the value::
7981
>>> wheel = [ep for ep in scripts if ep.name == 'wheel'][0]
8082
>>> wheel
8183
EntryPoint(name='wheel', value='wheel.cli:main', group='console_scripts')
84+
>>> wheel.module
85+
'wheel.cli'
86+
>>> wheel.attr
87+
'main'
88+
>>> wheel.extras
89+
[]
8290
>>> main = wheel.load()
8391
>>> main
8492
<function main at 0x103528488>

importlib_metadata/tests/test_main.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,9 @@ def test_json_dump(self):
250250
"""
251251
with self.assertRaises(Exception):
252252
json.dumps(self.ep)
253+
254+
def test_module(self):
255+
assert self.ep.module == 'value'
256+
257+
def test_attr(self):
258+
assert self.ep.attr is None

0 commit comments

Comments
 (0)