From 7b535027d6b6323aea05d619d13ef4e52c89e5de Mon Sep 17 00:00:00 2001 From: Matthew Seal Date: Fri, 2 Oct 2020 15:07:46 -0700 Subject: [PATCH] Added proper support for backwards compatibility templates (#1432) * Added proper support for backwards compatibility templates * Removed test for old name we don't support anymore --- nbconvert/exporters/templateexporter.py | 26 +++++++++++++++++-- nbconvert/exporters/tests/test_rst.py | 9 ------- .../exporters/tests/test_templateexporter.py | 21 +++++++++++++-- .../compatibility/display_priority.tpl | 2 +- .../templates/compatibility/full.tpl | 4 +-- .../templates/compatibility/python.tpl | 2 -- .../nbconvert/templates/compatibility/rst.tpl | 2 -- 7 files changed, 46 insertions(+), 20 deletions(-) delete mode 100644 share/jupyter/nbconvert/templates/compatibility/python.tpl delete mode 100644 share/jupyter/nbconvert/templates/compatibility/rst.tpl diff --git a/nbconvert/exporters/templateexporter.py b/nbconvert/exporters/templateexporter.py index ab922c0cc..0d540eb18 100644 --- a/nbconvert/exporters/templateexporter.py +++ b/nbconvert/exporters/templateexporter.py @@ -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 = [] @@ -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 diff --git a/nbconvert/exporters/tests/test_rst.py b/nbconvert/exporters/tests/test_rst.py index 6a1fbf503..a8f2a4c1f 100644 --- a/nbconvert/exporters/tests/test_rst.py +++ b/nbconvert/exporters/tests/test_rst.py @@ -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""" diff --git a/nbconvert/exporters/tests/test_templateexporter.py b/nbconvert/exporters/tests/test_templateexporter.py index e43171a82..dd6b6a406 100644 --- a/nbconvert/exporters/tests/test_templateexporter.py +++ b/nbconvert/exporters/tests/test_templateexporter.py @@ -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""" @@ -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): diff --git a/share/jupyter/nbconvert/templates/compatibility/display_priority.tpl b/share/jupyter/nbconvert/templates/compatibility/display_priority.tpl index 2fd7728a8..70cd67c1b 100644 --- a/share/jupyter/nbconvert/templates/compatibility/display_priority.tpl +++ b/share/jupyter/nbconvert/templates/compatibility/display_priority.tpl @@ -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' -%} diff --git a/share/jupyter/nbconvert/templates/compatibility/full.tpl b/share/jupyter/nbconvert/templates/compatibility/full.tpl index b440b8224..863c94b16 100644 --- a/share/jupyter/nbconvert/templates/compatibility/full.tpl +++ b/share/jupyter/nbconvert/templates/compatibility/full.tpl @@ -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' -%} diff --git a/share/jupyter/nbconvert/templates/compatibility/python.tpl b/share/jupyter/nbconvert/templates/compatibility/python.tpl deleted file mode 100644 index a8406dfac..000000000 --- a/share/jupyter/nbconvert/templates/compatibility/python.tpl +++ /dev/null @@ -1,2 +0,0 @@ -{{ resources.deprecated("This template is deprecated, please use python/index.html.j2") }} -{%- extends 'python/index.html.j2' -%} diff --git a/share/jupyter/nbconvert/templates/compatibility/rst.tpl b/share/jupyter/nbconvert/templates/compatibility/rst.tpl deleted file mode 100644 index d1715a9bf..000000000 --- a/share/jupyter/nbconvert/templates/compatibility/rst.tpl +++ /dev/null @@ -1,2 +0,0 @@ -{{ resources.deprecated("This template is deprecated, please use rst/index.rst.j2") }} -{%- extends 'rst/index.rst.j2' -%}