Allow mako-render script to take relative path filenames

#2 Merged at 9e1d236
Deleted repository
patch-fix-mako-render-relative-path (10e7ab84ed7a)
  1. Derek Harland

Currently, using mako-render as follows

$ mako-render ../my-template.mako

will cause an exception

mako.exceptions.TemplateLookupException: Template uri "../my-template.mako" is invalid - it cannot be relative outside of the root path.

This is because mako-render passes both the template source and the template filename to Template (according to the documentation they are mutually exclusive). Thus the Template will never need to try and load the template itself, but will still complain about the path name.

This patch defers the path-name check until if and when it is actually needed.

Comments (6)

  1. Michael Bayer repo owner

    yeah we can't go this route exactly, the validation you see for u_norm there is unconditional. See 5cd508ff. The mako-render script instead should be adjusted. For one, yes i see it's passing "filename" and not "uri", it should probably be producing a correct URI on the outside and passing that (and even an exception message to indicate that "text" and "filename" are in fact exclusive, that would be fine as well). Also I see it's passed a TemplateLookup with "." as the path, perhaps when a ".." path is encountered, this lookup should be adjusted to start where the given template is located. The basic rule is that we can't have any includes or anything that go outside of the root of the TemplateLookup.

  2. Michael Bayer repo owner

    also just an FYI I wanted to do a release of 0.9.1 today, as we have some other fixes I want to get out. This isn't suggesting that you need to hurry, just that this pullreq might not be in this particular release, sorry I haven't reviewed this pullreq sooner.

  3. Derek Harland author

    Apologies in advance ... I'm out of office so no quick access to source, might make some contextual errors below!

    No problem! I wonder if a more general solution might be:

    • allow mako-render to take an optional --template-root; then

    • if passed a filename on the command line

      • construct a TemplateLookup(options.template_root), defaulting the template root to be the directory of the filename (if no --template-root given);

      • and a template that is given the filename only (not the source).

    • if using stdin

      • construct a TemplateLookup(options.template_root), defaulting the template root to be the current directory (if no --template-root given);

      • and a template that is given the source read from stdin.

    This option would be useful to me at least. Can patch as suggested if you agree?

  4. Derek Harland author

    Have now reversed the original patch and implemented something similar to above.

    This patch also removes another bug that previously existed … mako-render wouldn't actually work if reading from stdin (as filename would never be initialized causing a name error)