-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate from pkg_resources to importlib.metadata (#299)
- Loading branch information
1 parent
9e9014c
commit 051973a
Showing
4 changed files
with
46 additions
and
22 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,47 @@ | ||
import pkg_resources | ||
from pybtex.plugin import _FakeEntryPoint | ||
from typing import Type, Any, Dict | ||
import sys | ||
if sys.version_info >= (3, 10): | ||
from importlib.metadata import entry_points, EntryPoint | ||
else: | ||
from importlib_metadata import entry_points, EntryPoint | ||
from typing import Type, Any, Dict, List | ||
|
||
_runtime_plugins: Dict[str, Dict[str, Type]] = { | ||
'sphinxcontrib.bibtex.style.referencing': {}} | ||
|
||
|
||
# wrapper to work around missing type annotations for entry_points function | ||
def _entry_points(group: str, name: str) -> List[EntryPoint]: | ||
return entry_points(group=group, name=name) # type: ignore | ||
|
||
|
||
def find_plugin(group: str, name: str) -> Type[Any]: | ||
"""Load a sphinxcontrib-bibtex plugin.""" | ||
dist = pkg_resources.get_distribution('sphinxcontrib-bibtex') | ||
if group not in dist.get_entry_map(): | ||
"""Load a sphinxcontrib-bibtex plugin, either from the runtime store, | ||
or from the entry points. | ||
""" | ||
global _runtime_plugins | ||
if group not in _runtime_plugins: | ||
raise ImportError(f"plugin group {group} not found") | ||
for entry_point in pkg_resources.iter_entry_points(group, name): | ||
return entry_point.load() | ||
try: | ||
return _runtime_plugins[group][name] | ||
except KeyError: | ||
for entry_point in _entry_points(group=group, name=name): | ||
return entry_point.load() | ||
raise ImportError(f"plugin {group}.{name} not found") | ||
|
||
|
||
def register_plugin(group: str, name: str, klass: Type[Any], | ||
force: bool = False) -> bool: | ||
"""Register a sphinxcontrib-bibtex plugin at runtime.""" | ||
dist = pkg_resources.get_distribution('sphinxcontrib-bibtex') | ||
entry_map: Dict[str, Dict[str, pkg_resources.EntryPoint]] \ | ||
= dist.get_entry_map() | ||
"""Register a sphinxcontrib-bibtex plugin into the runtime store.""" | ||
global _runtime_plugins | ||
if group not in _runtime_plugins: | ||
raise ImportError(f"plugin group {group} not found") | ||
eps: List[Any] | ||
try: | ||
entry_points = entry_map[group] | ||
eps = [_runtime_plugins[group][name]] | ||
except KeyError: | ||
raise ImportError(f"plugin group {group} not found") | ||
if name not in entry_points or force: | ||
entry_points[name] = _FakeEntryPoint(name, klass) | ||
eps = _entry_points(group=group, name=name) | ||
if not eps or force: | ||
_runtime_plugins[group][name] = klass | ||
return True | ||
else: | ||
return False |
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