diff tincan.py @ 34:0fb455b46e5f draft header-includes

Add cache.
author David Barts <n5jrn@me.com>
date Mon, 27 May 2019 20:09:55 -0700
parents cc975bf7a3fa
children 41da0b3d2156
line wrap: on
line diff
--- a/tincan.py	Mon May 27 18:14:14 2019 -0700
+++ b/tincan.py	Mon May 27 20:09:55 2019 -0700
@@ -350,6 +350,18 @@
         if not self.fname.endswith(_IEXTEN):
             raise ValueError("file does not end in {0}".format(_IEXTEN))
 
+# Using a cache is likely to help efficiency a lot, since many pages
+# will typically #include the same standard stuff.
+_tcache = {}
+def _get_template(name, direct):
+    aname = os.path.abspath(os.path.join(direct, name))
+    if aname not in _tcache:
+        tmpl = ChameleonTemplate(name=name, lookup=[direct])
+        tmpl.prepare()
+        assert aname == tmpl.filename
+        _tcache[aname] = tmpl
+    return _tcache[aname]
+
 # R o u t e s
 #
 # Represents a route in TinCan. Our launcher creates these on-the-fly based
@@ -483,8 +495,7 @@
             else:
                 fdir = os.path.join(self._fsroot, *self._subdir)
             try:
-                tmpl = ChameleonTemplate(name=include.fname, lookup=[fdir])
-                tmpl.prepare()
+                tmpl = _get_template(include.fname, fdir)
             except Exception as e:
                 raise TinCanError("{0}: bad #include: {1!s}".format(self._urlpath, e)) from e
             self._includes[include.vname] = tmpl.tpl