proposal - standardizing directories and template paths
Migrated issue, originally created by jvanasco (@jvanasco)
I've been working on some tools to precompile templates before a webserver forks, and have run into a few issues when trying to build this out more reliably.
-
'directories' can be duplicates. I'm not sure this will affect anything down the line, but it might make sense. this is relatively simple to fix.
lookup = TemplateLookup(directories=['templates', 'templates/', 'templates']) print lookup.directories ['templates', 'templates', 'templates']
-
filenames aren't standardized, so the same template can become 2 objects/compiled 2 ways (relative vs absolute). this will require more thought.
lookup = TemplateLookup(directories=['templates']) t1 = lookup.get_template('goodnight_moon.mako') t2 = lookup.get_template('/goodnight_moon.mako') print lookup._collection {'goodnight_moon.mako': <mako.template.Template object at 0x1015df510>, '/goodnight_moon.mako': <mako.template.Template object at 0x1015df850>}
Michael Bayer (@zzzeek) wrote:
i think you mean "canonicalized". you run the paths through os.path.abspath(os.path.realpath()) in order to get the key in the lookup. This is too expensive to do for every include call, so you'd still cache on all three paths, the canonical path is only used when first creating the entry:
{"/canonical/path/to/template.mako": <Template at 0x123>, "template.mako": <Template at 0x123>, "/path/to/template.mako": <Template at 0x123>}
the behavior turns on using a flag "canonical=True" to TemplateLookup.
jvanasco (@jvanasco) wrote:
Yeah - canonical names for templates.
the behavior turns on using a flag "canonical=True" to TemplateLookup
Is that your suggestion for implementing? That doesn't exist right now.
I was thinking about augmenting the .directories with something like:
self.directories_real = [d: os.path.abspath(os.path.realpath(d)) for d in self.directories]
then using that cached value when generating a key.
Michael Bayer (@zzzeek) wrote:
yes, the flag would be added. The cache lookup for an <%include> should still be just one hash lookup though. Just put mulitple entries into the cache, one on the canonical path, one on the requested path.