changeset 32:3382799f3905 draft header-includes

Refinements.
author David Barts <n5jrn@me.com>
date Mon, 27 May 2019 18:05:42 -0700
parents 443a0001d841
children cc975bf7a3fa
files tincan.py
diffstat 1 files changed, 9 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/tincan.py	Mon May 27 15:17:25 2019 -0700
+++ b/tincan.py	Mon May 27 18:05:42 2019 -0700
@@ -410,7 +410,6 @@
         self._origin = self._urlpath
         self._subdir = subdir
         self._seen = set()
-        self._tclass = launcher.tclass
         self._app = launcher.app
         self._save_includes = launcher.debug
 
@@ -468,20 +467,13 @@
                 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 = self._tclass(source=tfile.body)
-            try:
-                includes = TemplateHeader(tfile.header).include
-            except TemplateHeaderError as e:
-                raise TinCanError("{0}: {1!s}".format(self._fspath, e)) from e
-            ibase = rtpath[:-1]
+            self._body = ChameleonTemplate(source=tfile.body)
         else:
-            self._body = self._tclass(source=self._template.body)
-            includes = self._header.include
-            ibase = self._subdir
+            self._body = ChameleonTemplate(source=self._template.body)
         self._body.prepare()
         # Process includes
         self._includes = {}
-        for include in includes:
+        for include in self._header.include:
             try:
                 include = _IncludedFile(include)
             except ValueError as e:
@@ -489,11 +481,10 @@
             if include.in_lib:
                 fdir = os.path.join(self._fsroot, _WINF, "tlib")
             else:
-                fdir = os.path.join(self._fsroot, *ibase)
+                fdir = os.path.join(self._fsroot, *self._subdir)
             try:
-                tmpl = self._tclass(name=include.fname, lookup=[fdir])
+                tmpl = ChameleonTemplate(name=include.fname, lookup=[fdir])
                 tmpl.prepare()
-                # tmpl.render()  # is this needed?
             except Exception as e:
                 raise TinCanError("{0}: bad #include: {1!s}".format(self._urlpath, e)) from e
             self._includes[include.vname] = tmpl.tpl
@@ -522,7 +513,7 @@
             raise TinCanError("{0}: bad #errors line".format(self._urlpath)) from e
         if not errors:
             errors = range(_ERRMIN, _ERRMAX+1)
-        route = _TinCanErrorRoute(self._tclass(source=self._template.body),
+        route = _TinCanErrorRoute(ChameleonTemplate(source=self._template.body),
             self._includes, self._class)
         for error in errors:
             if error < _ERRMIN or error > _ERRMAX:
@@ -678,13 +669,12 @@
     """
     Helper class for launching webapps.
     """
-    def __init__(self, fsroot, urlroot, tclass, logger):
+    def __init__(self, fsroot, urlroot, logger):
         """
         Lightweight constructor. The real action happens in .launch() below.
         """
         self.fsroot = fsroot
         self.urlroot = urlroot
-        self.tclass = tclass
         self.logger = logger
         self.app = None
         self.errors = 0
@@ -749,14 +739,14 @@
     sys.stderr.write(message)
     sys.stderr.write('\n')
 
-def launch(fsroot=None, urlroot='/', tclass=ChameleonTemplate, logger=_logger, debug=False):
+def launch(fsroot=None, urlroot='/', logger=_logger, debug=False):
     """
     Launch and return a TinCan webapp. Does not run the app; it is the
     caller's responsibility to call app.run()
     """
     if fsroot is None:
         fsroot = os.getcwd()
-    launcher = _Launcher(fsroot, urlroot, tclass, logger)
+    launcher = _Launcher(fsroot, urlroot, logger)
     launcher.debug = debug
     launcher.launch()
     return launcher.app, launcher.errors