Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch from entrypoints to importlib-metadata #1782

Merged
merged 4 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 9 additions & 12 deletions nbconvert/exporters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

import os

import entrypoints
try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one last thing: because the stdlib version was broken, this is most accurate as a check of sys.version, e.g.:

if sys.version_info < (3, 10):
    from importlib_metadata import entry_points
else:
    from importlib.metadata import entry_points

https://github.com/jupyterlab/jupyterlab_server/blob/main/jupyterlab_server/translation_utils.py#L20

import importlib.metadata as importlib_metadata
except ModuleNotFoundError:
import importlib_metadata
from nbformat import NotebookNode
from traitlets.config import get_config
from traitlets.log import get_logger
Expand Down Expand Up @@ -97,20 +100,14 @@ def get_exporter(name, config=get_config()): # noqa
name = "notebook"

try:
exporter = entrypoints.get_single("nbconvert.exporters", name).load()
exporters = importlib_metadata.entry_points()["nbconvert.exporters"]
Copy link
Contributor

@bollwyvl bollwyvl Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this loads every single entry_point, and can take seconds: indeed, i think it throws warnings on newer pythons.

Using entry_points(group="nbconvert.exporters.script") will only load the one requested, but there are some version quirks (see comment on pyproject.toml).

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:
Expand All @@ -136,7 +133,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 = (e.name for e in importlib_metadata.entry_points()["nbconvert.exporters"])
if os.environ.get("NBCONVERT_DISABLE_CONFIG_EXPORTERS"):
get_logger().info(
"Config exporter loading disabled, no additional exporters will be automatically included."
Expand Down
10 changes: 7 additions & 3 deletions nbconvert/exporters/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import entrypoints
try:
import importlib.metadata as importlib_metadata
except ModuleNotFoundError:
import importlib_metadata
from traitlets import Dict, default

from .base import get_exporter
Expand Down Expand Up @@ -32,8 +35,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 = importlib_metadata.entry_points()["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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies = [
"beautifulsoup4",
"bleach",
"defusedxml",
"entrypoints>=0.2.2",
"importlib-metadata;python_version<'3.8'",
"jinja2>=3.0",
"jupyter_core>=4.7",
"jupyterlab_pygments",
Expand Down