|
1 |
| -import pkg_resources |
2 |
| -from pybtex.plugin import _FakeEntryPoint |
3 |
| -from typing import Type, Any, Dict |
| 1 | +import sys |
| 2 | +if sys.version_info >= (3, 10): |
| 3 | + from importlib.metadata import entry_points, EntryPoint |
| 4 | +else: |
| 5 | + from importlib_metadata import entry_points, EntryPoint |
| 6 | +from typing import Type, Any, Dict, List |
| 7 | + |
| 8 | +_runtime_plugins: Dict[str, Dict[str, Type]] = { |
| 9 | + 'sphinxcontrib.bibtex.style.referencing': {}} |
| 10 | + |
| 11 | + |
| 12 | +# wrapper to work around missing type annotations for entry_points function |
| 13 | +def _entry_points(group: str, name: str) -> List[EntryPoint]: |
| 14 | + return entry_points(group=group, name=name) # type: ignore |
4 | 15 |
|
5 | 16 |
|
6 | 17 | def find_plugin(group: str, name: str) -> Type[Any]:
|
7 |
| - """Load a sphinxcontrib-bibtex plugin.""" |
8 |
| - dist = pkg_resources.get_distribution('sphinxcontrib-bibtex') |
9 |
| - if group not in dist.get_entry_map(): |
| 18 | + """Load a sphinxcontrib-bibtex plugin, either from the runtime store, |
| 19 | + or from the entry points. |
| 20 | + """ |
| 21 | + global _runtime_plugins |
| 22 | + if group not in _runtime_plugins: |
10 | 23 | raise ImportError(f"plugin group {group} not found")
|
11 |
| - for entry_point in pkg_resources.iter_entry_points(group, name): |
12 |
| - return entry_point.load() |
| 24 | + try: |
| 25 | + return _runtime_plugins[group][name] |
| 26 | + except KeyError: |
| 27 | + for entry_point in _entry_points(group=group, name=name): |
| 28 | + return entry_point.load() |
13 | 29 | raise ImportError(f"plugin {group}.{name} not found")
|
14 | 30 |
|
15 | 31 |
|
16 | 32 | def register_plugin(group: str, name: str, klass: Type[Any],
|
17 | 33 | force: bool = False) -> bool:
|
18 |
| - """Register a sphinxcontrib-bibtex plugin at runtime.""" |
19 |
| - dist = pkg_resources.get_distribution('sphinxcontrib-bibtex') |
20 |
| - entry_map: Dict[str, Dict[str, pkg_resources.EntryPoint]] \ |
21 |
| - = dist.get_entry_map() |
| 34 | + """Register a sphinxcontrib-bibtex plugin into the runtime store.""" |
| 35 | + global _runtime_plugins |
| 36 | + if group not in _runtime_plugins: |
| 37 | + raise ImportError(f"plugin group {group} not found") |
| 38 | + eps: List[Any] |
22 | 39 | try:
|
23 |
| - entry_points = entry_map[group] |
| 40 | + eps = [_runtime_plugins[group][name]] |
24 | 41 | except KeyError:
|
25 |
| - raise ImportError(f"plugin group {group} not found") |
26 |
| - if name not in entry_points or force: |
27 |
| - entry_points[name] = _FakeEntryPoint(name, klass) |
| 42 | + eps = _entry_points(group=group, name=name) |
| 43 | + if not eps or force: |
| 44 | + _runtime_plugins[group][name] = klass |
28 | 45 | return True
|
29 | 46 | else:
|
30 | 47 | return False
|
0 commit comments