-
Notifications
You must be signed in to change notification settings - Fork 444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow use of importlib.metadata for finding entrypoints #1102
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import sys | ||
from functools import partial | ||
|
||
|
||
def find_entrypoints(group_name: str): | ||
""" | ||
Find entrypoints of a given group using either `importlib.metadata` or the | ||
older `pkg_resources` mechanism. | ||
|
||
Yields tuples of the entrypoint name and a callable function that will | ||
load the actual entrypoint. | ||
""" | ||
if sys.version_info >= (3, 10): | ||
# "Changed in version 3.10: importlib.metadata is no longer provisional." | ||
try: | ||
from importlib.metadata import entry_points | ||
except ImportError: | ||
pass | ||
else: | ||
eps = entry_points(group=group_name) | ||
# Only do this if this implementation of `importlib.metadata` is | ||
# modern enough to not return a dict. | ||
if not isinstance(eps, dict): | ||
for entry_point in eps: | ||
yield (entry_point.name, entry_point.load) | ||
return | ||
|
||
try: | ||
from pkg_resources import working_set | ||
except ImportError: | ||
pass | ||
else: | ||
for entry_point in working_set.iter_entry_points(group_name): | ||
yield (entry_point.name, partial(entry_point.load, require=True)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% trans %}Hello, {{ name }}!{% endtrans %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[jinja2: *.html] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pathlib | ||
|
||
import pytest | ||
|
||
from babel.messages import frontend | ||
|
||
jinja2 = pytest.importorskip("jinja2") | ||
|
||
jinja2_data_path = pathlib.Path(__file__).parent / "jinja2_data" | ||
|
||
|
||
def test_jinja2_interop(monkeypatch, tmp_path): | ||
""" | ||
Test that babel can extract messages from Jinja2 templates. | ||
""" | ||
monkeypatch.chdir(jinja2_data_path) | ||
cli = frontend.CommandLineInterface() | ||
pot_file = tmp_path / "messages.pot" | ||
cli.run(['pybabel', 'extract', '--mapping', 'mapping.cfg', '-o', str(pot_file), '.']) | ||
assert '"Hello, %(name)s!"' in pot_file.read_text() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the try/except block needed if we have an explicit version check?
importlib.metadata
should always be available on 3.10+There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1102 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @podgorniy94 mentioned, I think it's a good thing to double-check, for supporting some more esoteric environments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, there's no need to check the Python version. We simply try
pkg_resources
, and if the library isn't available, then we attemptimportlib.metadata
. To avoid strict version checking for, as you said, esoteric environments where there might be different version labeling :) What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes and no – using
pkg_resources
where it's deprecated will raise a DeprecationWarning, which in some (in e.g. my work-work) environments deprecation warnings are hard errors in tests.