diff --git a/.gitignore b/.gitignore index 4e246356e..5e84483c3 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ docs/source/*.j2 docs/source/example.html docs/source/mytemplate/* docs/source/config_options.rst +.venv # Eclipse pollutes the filesystem .project diff --git a/nbconvert/exporters/base.py b/nbconvert/exporters/base.py index cd67601f4..a44884550 100644 --- a/nbconvert/exporters/base.py +++ b/nbconvert/exporters/base.py @@ -4,8 +4,12 @@ # Distributed under the terms of the Modified BSD License. import os +import sys -import entrypoints +if sys.version_info < (3, 10): + from importlib_metadata import entry_points +else: + from importlib.metadata import entry_points from nbformat import NotebookNode from traitlets.config import get_config from traitlets.log import get_logger @@ -97,20 +101,14 @@ def get_exporter(name, config=get_config()): # noqa name = "notebook" try: - exporter = entrypoints.get_single("nbconvert.exporters", name).load() + exporters = entry_points(group="nbconvert.exporters") + exporter = [e for e in exporters if e.name == name or e.name == name.lower()][0].load() if getattr(exporter(config=config), "enabled", True): return exporter else: raise ExporterDisabledError('Exporter "%s" disabled in configuration' % (name)) - except entrypoints.NoSuchEntryPoint: - try: - exporter = entrypoints.get_single("nbconvert.exporters", name.lower()).load() - if getattr(exporter(config=config), "enabled", True): - return exporter - else: - raise ExporterDisabledError('Exporter "%s" disabled in configuration' % (name)) - except entrypoints.NoSuchEntryPoint: - pass + except IndexError: + pass if "." in name: try: @@ -136,7 +134,7 @@ def get_export_names(config=get_config()): # noqa Exporters can be found in external packages by registering them as an nbconvert.exporter entrypoint. """ - exporters = sorted(entrypoints.get_group_named("nbconvert.exporters")) + exporters = sorted(e.name for e in entry_points(group="nbconvert.exporters")) if os.environ.get("NBCONVERT_DISABLE_CONFIG_EXPORTERS"): get_logger().info( "Config exporter loading disabled, no additional exporters will be automatically included." diff --git a/nbconvert/exporters/script.py b/nbconvert/exporters/script.py index 96a55e41a..3661bddd5 100644 --- a/nbconvert/exporters/script.py +++ b/nbconvert/exporters/script.py @@ -2,8 +2,12 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. +import sys -import entrypoints +if sys.version_info < (3, 10): + from importlib_metadata import entry_points +else: + from importlib.metadata import entry_points from traitlets import Dict, default from .base import get_exporter @@ -32,8 +36,9 @@ def _get_language_exporter(self, lang_name): """ if lang_name not in self._lang_exporters: try: - Exporter = entrypoints.get_single("nbconvert.exporters.script", lang_name).load() - except entrypoints.NoSuchEntryPoint: + exporters = entry_points(group="nbconvert.exporters.script") + Exporter = [e for e in exporters if e.name == lang_name][0].load() + except (KeyError, IndexError): self._lang_exporters[lang_name] = None else: # TODO: passing config is wrong, but changing this revealed more complicated issues diff --git a/pyproject.toml b/pyproject.toml index 7a1b26fbf..773bdced9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,7 +23,7 @@ dependencies = [ "beautifulsoup4", "bleach", "defusedxml", - "entrypoints>=0.2.2", + "importlib_metadata>=3.6;python_version<\"3.10\"", "jinja2>=3.0", "jupyter_core>=4.7", "jupyterlab_pygments",