Skip to content

Commit

Permalink
Added proper support for backwards compatibility templates (#1432)
Browse files Browse the repository at this point in the history
* Added proper support for backwards compatibility templates

* Removed test for old name we don't support anymore
  • Loading branch information
MSeal authored Oct 2, 2020
1 parent 0f645ba commit 7b53502
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 20 deletions.
26 changes: 24 additions & 2 deletions nbconvert/exporters/templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,14 @@ def _template_paths(self, prune=True, root_dirs=None):

return self.extra_template_paths + additional_paths + paths

@classmethod
def get_compatibility_base_template_conf(cls, name):
# Hard-coded base template confs to use for backwards compatibility for 5.x-only templates
if name == 'display_priority':
return dict(base_template='base')
if name == 'full':
return dict(base_template='classic', mimetypes={"text/html": True})

def get_template_names(self):
# finds a list of template names where each successive template name is the base template
template_names = []
Expand Down Expand Up @@ -575,8 +583,22 @@ def get_template_names(self):
with open(conf_file) as f:
conf = recursive_update(json.load(f), conf)
if not found_at_least_one:
paths = "\n\t".join(root_dirs)
raise ValueError('No template sub-directory with name %r found in the following paths:\n\t%s' % (base_template, paths))
# Check for backwards compatibility template names
for root_dir in root_dirs:
compatibility_file = base_template + '.tpl'
compatibility_path = os.path.join(root_dir, 'nbconvert', 'templates', 'compatibility', compatibility_file)
if os.path.exists(compatibility_path):
found_at_least_one = True
warnings.warn(
f"5.x template name passed '{self.template_name}'. Use 'lab' or 'classic' for new template usage.",
DeprecationWarning)
self.template_file = compatibility_file
conf = self.get_compatibility_base_template_conf(base_template)
self.template_name = conf.get('base_template')
break
if not found_at_least_one:
paths = "\n\t".join(root_dirs)
raise ValueError('No template sub-directory with name %r found in the following paths:\n\t%s' % (base_template, paths))
merged_conf = recursive_update(dict(conf), merged_conf)
base_template = conf.get('base_template')
conf = merged_conf
Expand Down
9 changes: 0 additions & 9 deletions nbconvert/exporters/tests/test_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ def test_export(self):
(output, resources) = RSTExporter().from_filename(self._get_notebook())
assert len(output) > 0

@onlyif_cmds_exist('pandoc')
def test_export_nbformat_template_v5_name(self):
"""
We support the nbconvert v5 filename, but with a deprecation warning.
"""
with pytest.warns(DeprecationWarning):
(output, resources) = RSTExporter(template_file='rst.tpl').from_filename(self._get_notebook())
assert len(output) > 0

@onlyif_cmds_exist('pandoc')
def test_empty_code_cell(self):
"""No empty code cells in rst"""
Expand Down
21 changes: 19 additions & 2 deletions nbconvert/exporters/tests/test_templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ def _file_extension_default(self):
def _template_name_default(self):
return 'python'

output_mimetype = 'text/x-python'

class TestExporter(ExportersTestsBase):
"""Contains test functions for exporter.py"""

Expand Down Expand Up @@ -198,6 +196,25 @@ def test_absolute_template_name_tpl_compatibility(self):
exporter = self._make_exporter(config=config)
assert exporter.template.filename == template
assert os.path.dirname(template) in exporter.template_paths

# Can't use @pytest.mark.parametrize without removing all self.assert calls in all tests... repeating some here
def absolute_template_name_5x_compatibility_test(self, template, mimetype=None):
config = Config()
# We're setting the template_name instead of the template_file
config.TemplateExporter.template_name = template
with pytest.warns(DeprecationWarning):
exporter = self._make_exporter(config=config)
template_dir, template_file = os.path.split(exporter.template.filename)
_, compat_dir = os.path.split(template_dir)
assert compat_dir == 'compatibility'
assert template_file == template + '.tpl'
assert template_dir in exporter.template_paths

def test_absolute_template_name_5x_compatibility_full(self):
self.absolute_template_name_5x_compatibility_test('full', 'text/html')

def test_absolute_template_name_5x_compatibility_display_priority(self):
self.absolute_template_name_5x_compatibility_test('display_priority')

# Can't use @pytest.mark.parametrize without removing all self.assert calls in all tests... repeating some here
def relative_template_test(self, template):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{{ resources.deprecated("This template is deprecated, please use base/display_priority.j2") }}
{%- extends 'base/display_priority.j2' -%}
{%- extends 'display_priority.j2' -%}
4 changes: 2 additions & 2 deletions share/jupyter/nbconvert/templates/compatibility/full.tpl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{{ resources.deprecated("This template is deprecated, please use lab/index.html.j2") }}
{%- extends 'classic/index.html.j2' -%}
{{ resources.deprecated("This template is deprecated, please use classic/index.html.j2") }}
{%- extends 'index.html.j2' -%}
2 changes: 0 additions & 2 deletions share/jupyter/nbconvert/templates/compatibility/python.tpl

This file was deleted.

2 changes: 0 additions & 2 deletions share/jupyter/nbconvert/templates/compatibility/rst.tpl

This file was deleted.

0 comments on commit 7b53502

Please sign in to comment.