|
30 | 30 | Mapping, |
31 | 31 | MutableSequence, |
32 | 32 | ) |
| 33 | +from functools import lru_cache |
33 | 34 | from os.path import relpath |
34 | 35 | from textwrap import dedent |
35 | 36 | from tokenize import COMMENT, NAME, OP, STRING, generate_tokens |
36 | 37 | from typing import TYPE_CHECKING, Any |
37 | 38 |
|
| 39 | +from babel.messages._compat import find_entrypoints |
38 | 40 | from babel.util import parse_encoding, parse_future_flags, pathmatch |
39 | 41 |
|
40 | 42 | if TYPE_CHECKING: |
@@ -363,6 +365,14 @@ def _match_messages_against_spec(lineno: int, messages: list[str|None], comments |
363 | 365 | return lineno, translatable, comments, context |
364 | 366 |
|
365 | 367 |
|
| 368 | +@lru_cache(maxsize=None) |
| 369 | +def _find_extractor(name: str): |
| 370 | + for ep_name, load in find_entrypoints(GROUP_NAME): |
| 371 | + if ep_name == name: |
| 372 | + return load() |
| 373 | + return None |
| 374 | + |
| 375 | + |
366 | 376 | def extract( |
367 | 377 | method: _ExtractionMethod, |
368 | 378 | fileobj: _FileObj, |
@@ -421,25 +431,11 @@ def extract( |
421 | 431 | module, attrname = method.split(':', 1) |
422 | 432 | func = getattr(__import__(module, {}, {}, [attrname]), attrname) |
423 | 433 | else: |
424 | | - try: |
425 | | - from pkg_resources import working_set |
426 | | - except ImportError: |
427 | | - pass |
428 | | - else: |
429 | | - for entry_point in working_set.iter_entry_points(GROUP_NAME, |
430 | | - method): |
431 | | - func = entry_point.load(require=True) |
432 | | - break |
| 434 | + func = _find_extractor(method) |
433 | 435 | if func is None: |
434 | | - # if pkg_resources is not available or no usable egg-info was found |
435 | | - # (see #230), we resort to looking up the builtin extractors |
436 | | - # directly |
437 | | - builtin = { |
438 | | - 'ignore': extract_nothing, |
439 | | - 'python': extract_python, |
440 | | - 'javascript': extract_javascript, |
441 | | - } |
442 | | - func = builtin.get(method) |
| 436 | + # if no named entry point was found, |
| 437 | + # we resort to looking up a builtin extractor |
| 438 | + func = _BUILTIN_EXTRACTORS.get(method) |
443 | 439 |
|
444 | 440 | if func is None: |
445 | 441 | raise ValueError(f"Unknown extraction method {method!r}") |
@@ -838,3 +834,10 @@ def parse_template_string( |
838 | 834 | lineno += len(line_re.findall(expression_contents)) |
839 | 835 | expression_contents = '' |
840 | 836 | prev_character = character |
| 837 | + |
| 838 | + |
| 839 | +_BUILTIN_EXTRACTORS = { |
| 840 | + 'ignore': extract_nothing, |
| 841 | + 'python': extract_python, |
| 842 | + 'javascript': extract_javascript, |
| 843 | +} |
0 commit comments