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

Allow exporters to be enabled or disabled via config #1273

Merged
merged 3 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 32 additions & 7 deletions nbconvert/exporters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import entrypoints

from traitlets.config import get_config
from traitlets.log import get_logger
from traitlets.utils.importstring import import_item

Expand All @@ -32,6 +33,9 @@
class ExporterNameError(NameError):
pass

class ExporterDisabledError(ValueError):
pass

def export(exporter, nb, **kw):
"""
Export a notebook object using specific exporter class.
Expand Down Expand Up @@ -85,26 +89,38 @@ def export(exporter, nb, **kw):
return output, resources


def get_exporter(name):
def get_exporter(name, config=get_config()):
"""Given an exporter name or import path, return a class ready to be instantiated

Raises ValueError if exporter is not found
Raises ExporterName if exporter is not found or ExporterDisabledError if not enabled
"""

if name == 'ipynb':
name = 'notebook'

try:
return entrypoints.get_single('nbconvert.exporters', name).load()
exporter = entrypoints.get_single('nbconvert.exporters', name).load()
if getattr(exporter(config=config), 'enabled', True):
return exporter
else:
raise ExporterDisabledError('Exporter "%s" disabled in configuration' % (name))
except entrypoints.NoSuchEntryPoint:
try:
return entrypoints.get_single('nbconvert.exporters', name.lower()).load()
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

if '.' in name:
try:
return import_item(name)
exporter = import_item(name)
if getattr(exporter(config=config), 'enabled', True):
return exporter
else:
raise ExporterDisabledError('Exporter "%s" disabled in configuration' % (name))
except ImportError:
log = get_logger()
log.error("Error importing %s" % name, exc_info=True)
Expand All @@ -113,10 +129,19 @@ def get_exporter(name):
% (name, ', '.join(get_export_names())))


def get_export_names():
def get_export_names(config=get_config()):
"""Return a list of the currently supported export targets

Exporters can be found in external packages by registering
them as an nbconvert.exporter entrypoint.
"""
return sorted(entrypoints.get_group_named('nbconvert.exporters'))
exporters = sorted(entrypoints.get_group_named('nbconvert.exporters'))
enabled_exporters = []
for exporter_name in exporters:
try:
e = get_exporter(exporter_name)(config=config)
if e.enabled:
enabled_exporters.append(exporter_name)
except ExporterDisabledError:
pass
return enabled_exporters
6 changes: 5 additions & 1 deletion nbconvert/exporters/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from traitlets.config.configurable import LoggingConfigurable
from traitlets.config import Config
from traitlets import HasTraits, Unicode, List, TraitError
from traitlets import Bool, HasTraits, Unicode, List, TraitError
from traitlets.utils.importstring import import_item
from ipython_genutils import text, py3compat

Expand Down Expand Up @@ -52,6 +52,10 @@ class Exporter(LoggingConfigurable):
accompanying resources dict.
"""

enabled = Bool(True,
help = "Disable this exporter (and any exporters inherited from it)."
).tag(config=True)

file_extension = FilenameExtension(
help="Extension of the file that should be written to disk"
).tag(config=True)
Expand Down
14 changes: 13 additions & 1 deletion nbconvert/exporters/tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@

import nbformat
import nbconvert.tests
import pytest

from traitlets.config import Config

from .base import ExportersTestsBase
from ..base import get_exporter, export, ExporterNameError, get_export_names
from ..base import get_exporter, export, ExporterNameError, ExporterDisabledError, get_export_names
from ..exporter import Exporter
from ..python import PythonExporter

Expand All @@ -32,6 +35,15 @@ def test_export_wrong_name(self):
pass


def test_export_disabled(self):
"""
Trying to use a disabled exporter should raise ExporterDisbledError
"""
config = Config({'NotebookExporter': {'enabled': False}})
with pytest.raises(ExporterDisabledError):
get_exporter('notebook', config=config)


def test_export_filename(self):
"""
Can a notebook be exported by filename?
Expand Down
16 changes: 16 additions & 0 deletions nbconvert/exporters/tests/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .base import ExportersTestsBase
from ...preprocessors.base import Preprocessor
from ..exporter import Exporter
from ..base import get_export_names, ExporterDisabledError


#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -57,3 +58,18 @@ def test_preprocessor(self):
exporter = Exporter(config=config)
(notebook, resources) = exporter.from_filename(self._get_notebook())
self.assertEqual(notebook['pizza'], 'cheese')

def test_get_export_names_disable(self):
"""Can we disable a specific importer?"""
config = Config({'Exporter': {'enabled': False}})
export_names = get_export_names()
self.assertFalse('Exporter' in export_names)

def test_get_export_names_disable(self):
"""Can we disable all exporters then enable a single one"""
config = Config({
'Exporter': {'enabled': False},
'NotebookExporter': {'enabled': True}
})
export_names = get_export_names(config=config)
self.assertEqual(export_names, ['notebook'])