Skip to content

Commit

Permalink
fix(vega#2672): replace entrypoints with importlib.metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
daylinmorgan committed Oct 1, 2022
1 parent 0217b2e commit bdf280c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
25 changes: 18 additions & 7 deletions altair/utils/plugin_registry.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
from typing import Any, Dict, List, Optional, Generic, TypeVar, cast
from types import TracebackType

import entrypoints
try:
from importlib.metadata import entry_points
except ImportError:
from importlib_metadata import entry_points

from toolz import curry


PluginType = TypeVar("PluginType")


class NoSuchEntryPoint(Exception):
def __init__(self, group, name):
self.group = group
self.name = name

def __str__(self):
return f"No {self.name!r} entry point found in group {self.group!r}"


class PluginEnabler(object):
"""Context manager for enabling plugins
Expand Down Expand Up @@ -108,9 +121,7 @@ def register(self, name: str, value: Optional[PluginType]) -> Optional[PluginTyp
def names(self) -> List[str]:
"""List the names of the registered and entry points plugins."""
exts = list(self._plugins.keys())
more_exts = [
ep.name for ep in entrypoints.get_group_all(self.entry_point_group)
]
more_exts = [ep.name for ep in entry_points(group=self.entry_point_group)]
exts.extend(more_exts)
return sorted(set(exts))

Expand Down Expand Up @@ -139,12 +150,12 @@ def _set_state(self, state: Dict[str, Any]) -> None:
def _enable(self, name: str, **options) -> None:
if name not in self._plugins:
try:
ep = entrypoints.get_single(self.entry_point_group, name)
except entrypoints.NoSuchEntryPoint:
(ep,) = entry_points(group=self.entry_point_group, name=name)
except ValueError:
if name in self.entrypoint_err_messages:
raise ValueError(self.entrypoint_err_messages[name])
else:
raise
raise NoSuchEntryPoint(self.entry_point_group, name)
value = cast(PluginType, ep.load())
self.register(name, value)
self._active_name = name
Expand Down
4 changes: 2 additions & 2 deletions doc/getting_started/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Altair has the following dependencies, all of which are installed automatically
with the above installation commands:

- python 3.6 or newer
- entrypoints_
- importlib_metadata_ (python<3.8)
- jsonschema_
- NumPy_
- Pandas_
Expand Down Expand Up @@ -74,7 +74,7 @@ development version directly from GitHub using:
.. _Zeppelin: https://zeppelin.apache.org/
.. _IPython: https://github.com/ipython/ipython

.. _entrypoints: https://github.com/takluyver/entrypoints
.. _importlib_metadata: https://github.com/python/importlib_metadata
.. _jsonschema: https://github.com/Julian/jsonschema
.. _NumPy: http://www.numpy.org/
.. _Pandas: http://pandas.pydata.org
Expand Down
7 changes: 3 additions & 4 deletions doc/user_guide/display_frontends.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Some of the built-in renderers are:
newer versions of JupyterLab_, nteract_, and `VSCode-Python`_, but does not work
with the `Jupyter Notebook`_, or with tools like nbviewer_ and nbconvert_.

Other renderers can be installed by third-party packages via Python's entrypoints_ system;
Other renderers can be installed by third-party packages via Python's entrypoints system;
see :ref:`renderer-api`.


Expand Down Expand Up @@ -79,7 +79,7 @@ Optionally, for offline rendering in Jupyter Notebook, you can use the notebook

# Optional in Jupyter Notebook: requires an up-to-date vega nbextension.
alt.renderers.enable('notebook')

This renderer is provided by the `ipyvega`_ notebook extension. which can be
installed and enabled either using pip:

Expand Down Expand Up @@ -262,10 +262,9 @@ To register and enable a new renderer::
>>> alt.renderers.register('custom_renderer', custom_renderer)
>>> alt.renderers.enable('custom_renderer')

Renderers can also be registered using the `entrypoints`_ API of Python packages.
Renderers can also be registered using the `entrypoints` API of Python packages.
For an example, see `ipyvega`_.

.. _entrypoints: https://github.com/takluyver/entrypoints
.. _ipyvega: https://github.com/vega/ipyvega/
.. _JupyterLab: http://jupyterlab.readthedocs.io/en/stable/
.. _nteract: https://nteract.io
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
entrypoints
importlib_metadata; python_version < "3.8"
jinja2
jsonschema>=3.0
numpy
Expand Down

0 comments on commit bdf280c

Please sign in to comment.