Skip to content

proposal - standardizing directories and template paths #259

Open

Description

@sqlalchemy-bot

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.

  1. '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']

  2. 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>}

Activity

  1. sqlalchemy-bot commented on Nov 23, 2016

    @sqlalchemy-bot
    Author

    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.

  2. sqlalchemy-bot commented on Nov 23, 2016

    @sqlalchemy-bot
    Author

    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.

  3. sqlalchemy-bot commented on Nov 23, 2016

    @sqlalchemy-bot
    Author

    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.

  4. sqlalchemy-bot commented on Nov 23, 2016

    @sqlalchemy-bot
    Author

    jvanasco (@jvanasco) wrote:

    gotcha. thanks.

  5. added this to the 1.3 milestone on Dec 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions