From 66cc47d94c5328c9df6c66658a04331454eb2c54 Mon Sep 17 00:00:00 2001 From: michaelpacer Date: Sun, 22 Jan 2017 15:07:30 -0800 Subject: [PATCH 1/5] add test to avoid nbsphinx issue with template extensions --- nbconvert/exporters/templateexporter.py | 31 ++++++++++++++++++- .../exporters/tests/test_templateexporter.py | 13 +++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/nbconvert/exporters/templateexporter.py b/nbconvert/exporters/templateexporter.py index 58714ae66..f6acf01f0 100644 --- a/nbconvert/exporters/templateexporter.py +++ b/nbconvert/exporters/templateexporter.py @@ -71,12 +71,13 @@ def __init__(self, loader, extension): def get_source(self, environment, template): + try: return self.loader.get_source(environment, template) except TemplateNotFound: if template.endswith(self.extension): raise TemplateNotFound(template) - return self.loader.get_source(environment, template+self.extension) + return self.loader.get_source(environment, template + self.extension) def list_templates(self): return self.loader.list_templates() @@ -217,6 +218,34 @@ def _load_template(self): # template by name with extension added, then try loading the template # as if the name is explicitly specified. template_file = self.template_file + + if not self.template_file.endswith(self.template_extension): + try: + self.log.debug("Attempting to load template %s", template_file) + self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) + return self.environment.get_template(template_file) + except TemplateNotFound: + try: + template_file = self.template_file + self.template_extension + self.log.debug("Attempting to load template %s", template_file) + self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) + return self.environment.get_template(template_file) + except TemplateNotFound: + raise TemplateNotFound(template_file) + else: + try: + self.log.debug("Attempting to load template %s", template_file) + self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) + return self.environment.get_template(template_file) + except TemplateNotFound: + try: + template_file = self.template_file[:-len(self.template_extension)] + self.log.debug("Attempting to load template %s", template_file) + self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) + return self.environment.get_template(template_file) + except TemplateNotFound: + raise TemplateNotFound(template_file) + self.log.debug("Attempting to load template %s", template_file) self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) return self.environment.get_template(template_file) diff --git a/nbconvert/exporters/tests/test_templateexporter.py b/nbconvert/exporters/tests/test_templateexporter.py index 5ac5a828c..b372ec332 100644 --- a/nbconvert/exporters/tests/test_templateexporter.py +++ b/nbconvert/exporters/tests/test_templateexporter.py @@ -123,7 +123,6 @@ def test_in_memory_template(self): # creates a class that uses this template with the template_file argument # converts an empty notebook using this mechanism my_loader = DictLoader({'my_template': "{%- extends 'rst.tpl' -%}"}) - class MyExporter(TemplateExporter): template_file = 'my_template' @@ -131,6 +130,18 @@ class MyExporter(TemplateExporter): nb = v4.new_notebook() out, resources = exporter.from_notebook_node(nb) + def test_in_memory_template_extensions(self): + # Loads in an in memory template using jinja2.DictLoader + # creates a class that uses this template with the template_file argument + # converts an empty notebook using this mechanism + my_loader = DictLoader({'my_template.tpl': "{%- extends 'rst.tpl' -%}"}) + + class MyExporter(TemplateExporter): + template_file = 'my_template' + + exporter = MyExporter(extra_loaders=[my_loader]) + nb = v4.new_notebook() + out, resources = exporter.from_notebook_node(nb) def test_fail_to_find_template_file(self): # Create exporter with invalid template file, check that it doesn't From 9a3005093082c705ddac0c0d07bb54bf9d964b47 Mon Sep 17 00:00:00 2001 From: michaelpacer Date: Sun, 22 Jan 2017 15:42:23 -0800 Subject: [PATCH 2/5] clean up logic and execution --- nbconvert/exporters/templateexporter.py | 38 ++++++++++++------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/nbconvert/exporters/templateexporter.py b/nbconvert/exporters/templateexporter.py index f6acf01f0..38077eea5 100644 --- a/nbconvert/exporters/templateexporter.py +++ b/nbconvert/exporters/templateexporter.py @@ -219,29 +219,20 @@ def _load_template(self): # as if the name is explicitly specified. template_file = self.template_file - if not self.template_file.endswith(self.template_extension): - try: - self.log.debug("Attempting to load template %s", template_file) - self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) - return self.environment.get_template(template_file) - except TemplateNotFound: + try: + _log_template_loading(template_file) + return self.environment.get_template(template_file) + except TemplateNotFound: + if self.template_file.endswith(self.template_extension): try: - template_file = self.template_file + self.template_extension - self.log.debug("Attempting to load template %s", template_file) - self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) - return self.environment.get_template(template_file) + template_file = self.template_file[:-len(self.template_extension)] + _log_template_loading(template_file) except TemplateNotFound: raise TemplateNotFound(template_file) - else: - try: - self.log.debug("Attempting to load template %s", template_file) - self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) - return self.environment.get_template(template_file) - except TemplateNotFound: - try: - template_file = self.template_file[:-len(self.template_extension)] - self.log.debug("Attempting to load template %s", template_file) - self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) + else: + try: + template_file = self.template_file + self.template_extension + _log_template_loading(template_file) return self.environment.get_template(template_file) except TemplateNotFound: raise TemplateNotFound(template_file) @@ -250,6 +241,13 @@ def _load_template(self): self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) return self.environment.get_template(template_file) + def _log_template_loading(template_file): + """ abstract away some of the loggging for finding templates + """ + self.log.debug("Attempting to load template %s", template_file) + self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) + pass + def from_notebook_node(self, nb, resources=None, **kw): """ Convert a notebook from a notebook node instance. From 5120882d23cacc00b76dece7a1a2ec7094e465c6 Mon Sep 17 00:00:00 2001 From: michaelpacer Date: Sun, 22 Jan 2017 15:52:33 -0800 Subject: [PATCH 3/5] Include self, reposition private logging function --- nbconvert/exporters/templateexporter.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/nbconvert/exporters/templateexporter.py b/nbconvert/exporters/templateexporter.py index 38077eea5..283300f4c 100644 --- a/nbconvert/exporters/templateexporter.py +++ b/nbconvert/exporters/templateexporter.py @@ -204,6 +204,12 @@ def __init__(self, config=None, **kw): self.observe(self._invalidate_template_cache, list(self.traits(affects_template=True))) + def _log_template_loading(self,template_file): + """ abstract away some of the loggging for finding templates + """ + self.log.debug("Attempting to load template %s", template_file) + self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) + pass def _load_template(self): """Load the Jinja template object from the template file @@ -220,33 +226,24 @@ def _load_template(self): template_file = self.template_file try: - _log_template_loading(template_file) + self._log_template_loading(template_file) return self.environment.get_template(template_file) except TemplateNotFound: if self.template_file.endswith(self.template_extension): try: template_file = self.template_file[:-len(self.template_extension)] - _log_template_loading(template_file) + self._log_template_loading(template_file) + return self.environment.get_template(template_file) except TemplateNotFound: raise TemplateNotFound(template_file) else: try: template_file = self.template_file + self.template_extension - _log_template_loading(template_file) + self._log_template_loading(template_file) return self.environment.get_template(template_file) except TemplateNotFound: raise TemplateNotFound(template_file) - - self.log.debug("Attempting to load template %s", template_file) - self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) - return self.environment.get_template(template_file) - def _log_template_loading(template_file): - """ abstract away some of the loggging for finding templates - """ - self.log.debug("Attempting to load template %s", template_file) - self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) - pass def from_notebook_node(self, nb, resources=None, **kw): """ From 7fc2ef431e6daa110785f1020cd108b9788270f4 Mon Sep 17 00:00:00 2001 From: michaelpacer Date: Sun, 22 Jan 2017 19:39:10 -0800 Subject: [PATCH 4/5] Improve test coverage --- nbconvert/exporters/tests/test_templateexporter.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/nbconvert/exporters/tests/test_templateexporter.py b/nbconvert/exporters/tests/test_templateexporter.py index b372ec332..927b251df 100644 --- a/nbconvert/exporters/tests/test_templateexporter.py +++ b/nbconvert/exporters/tests/test_templateexporter.py @@ -155,8 +155,12 @@ def test_fail_to_find_template_file(self): with pytest.raises(TemplateNotFound): out, resources = exporter.from_notebook_node(nb) - - + template_2 = 'does_not_exist' + exporter = TemplateExporter(template_file=template_2) + assert template_2 not in exporter.environment.list_templates(extensions=['tpl']) + nb = v4.new_notebook() + with pytest.raises(TemplateNotFound): + out, resources = exporter.from_notebook_node(nb) def _make_exporter(self, config=None): # Create the exporter instance, make sure to set a template name since From c780c8b04a7bd9168ef7ee6ba07448c854a649f6 Mon Sep 17 00:00:00 2001 From: michaelpacer Date: Mon, 23 Jan 2017 13:58:33 -0800 Subject: [PATCH 5/5] Remove pass --- nbconvert/exporters/templateexporter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nbconvert/exporters/templateexporter.py b/nbconvert/exporters/templateexporter.py index 283300f4c..12ec521d8 100644 --- a/nbconvert/exporters/templateexporter.py +++ b/nbconvert/exporters/templateexporter.py @@ -209,7 +209,6 @@ def _log_template_loading(self,template_file): """ self.log.debug("Attempting to load template %s", template_file) self.log.debug(" template_path: %s", os.pathsep.join(self.template_path)) - pass def _load_template(self): """Load the Jinja template object from the template file