diff tincan.py @ 57:935f8013f540 draft

Fix lack of error detection and recovery in template parsing.
author David Barts <n5jrn@me.com>
date Fri, 31 May 2019 19:40:06 -0700
parents f177756200be
children e08e24707da1
line wrap: on
line diff
--- a/tincan.py	Fri May 31 18:52:55 2019 -0700
+++ b/tincan.py	Fri May 31 19:40:06 2019 -0700
@@ -8,6 +8,7 @@
 import ast
 import binascii
 from base64 import b16encode, b16decode
+from chameleon import PageTemplate, PageTemplateFile, TemplateError
 import email.utils
 import functools
 import importlib
@@ -196,7 +197,6 @@
 
 class ChameleonTemplate(bottle.BaseTemplate):
     def prepare(self, **options):
-        from chameleon import PageTemplate, PageTemplateFile
         if self.source:
             self.tpl = PageTemplate(self.source, **options)
         else:
@@ -590,9 +590,9 @@
                 raise TinCanError("{0}: invalid #template: {1!s}".format(self._urlpath, e)) from e
             except IndexError as e:
                 raise TinCanError("{0}: invalid #template".format(self._urlpath)) from e
-            self._body = ChameleonTemplate(source=tfile.body, encoding=self._encoding)
+            self._body = self._mktemplate(tfile.body, self.urljoin(rtpath))
         else:
-            self._body = ChameleonTemplate(source=self._template.body, encoding=self._encoding)
+            self._body = self._mktemplate(self._template.body, self._urlpath)
         self._body.prepare()
         # Process loads
         self._loads = {}
@@ -625,6 +625,12 @@
         self.logger.info("adding route: %s (%s)", self._origin, ','.join(methods))
         self._app.route(self._origin, methods, self)
 
+    def _mktemplate(self, source, name):
+        try:
+            return ChameleonTemplate(source=source, encoding=self._encoding)
+        except TemplateError as e:
+            raise TinCanError("{0}: {1!s}".format(name, e)) from e
+
     def _splitpath(self, unsplit):
         return _normpath(self._subdir, unsplit)