From 1504fc60e4b676aeec2ccc3121d7462bc885545f Mon Sep 17 00:00:00 2001 From: donkopotamus Date: Fri, 20 Dec 2013 11:15:33 +1300 Subject: Move calculation of normalised uri to the codepath where it is actually used - this fixes a bug in the mako-render script which refuses to process a filename that is given as a relative path --- mako/template.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mako/template.py b/mako/template.py index 3a7b7f0..bb5d891 100644 --- a/mako/template.py +++ b/mako/template.py @@ -255,16 +255,6 @@ class Template(object): self.module_id = "memory:" + hex(id(self)) self.uri = self.module_id - u_norm = self.uri - if u_norm.startswith("/"): - u_norm = u_norm[1:] - u_norm = os.path.normpath(u_norm) - if u_norm.startswith(".."): - raise exceptions.TemplateLookupException( - "Template uri \"%s\" is invalid - " - "it cannot be relative outside " - "of the root path." % self.uri) - self.input_encoding = input_encoding self.output_encoding = output_encoding self.encoding_errors = encoding_errors @@ -310,6 +300,16 @@ class Template(object): if module_filename is not None: path = module_filename elif module_directory is not None: + u_norm = self.uri + if u_norm.startswith("/"): + u_norm = u_norm[1:] + u_norm = os.path.normpath(u_norm) + if u_norm.startswith(".."): + raise exceptions.TemplateLookupException( + "Template uri \"%s\" is invalid - " + "it cannot be relative outside " + "of the root path." % self.uri) + path = os.path.abspath( os.path.join( os.path.normpath(module_directory), -- cgit v1.2.1 From bfc5f2c97b2f6d1af70e8d098f926fe110d16ea0 Mon Sep 17 00:00:00 2001 From: donkopotamus Date: Tue, 7 Jan 2014 13:58:24 +1300 Subject: Revert "Move calculation of normalised uri to the codepath where it is actually used" This reverts commit 1504fc60e4b676aeec2ccc3121d7462bc885545f. --- mako/template.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mako/template.py b/mako/template.py index bb5d891..3a7b7f0 100644 --- a/mako/template.py +++ b/mako/template.py @@ -255,6 +255,16 @@ class Template(object): self.module_id = "memory:" + hex(id(self)) self.uri = self.module_id + u_norm = self.uri + if u_norm.startswith("/"): + u_norm = u_norm[1:] + u_norm = os.path.normpath(u_norm) + if u_norm.startswith(".."): + raise exceptions.TemplateLookupException( + "Template uri \"%s\" is invalid - " + "it cannot be relative outside " + "of the root path." % self.uri) + self.input_encoding = input_encoding self.output_encoding = output_encoding self.encoding_errors = encoding_errors @@ -300,16 +310,6 @@ class Template(object): if module_filename is not None: path = module_filename elif module_directory is not None: - u_norm = self.uri - if u_norm.startswith("/"): - u_norm = u_norm[1:] - u_norm = os.path.normpath(u_norm) - if u_norm.startswith(".."): - raise exceptions.TemplateLookupException( - "Template uri \"%s\" is invalid - " - "it cannot be relative outside " - "of the root path." % self.uri) - path = os.path.abspath( os.path.join( os.path.normpath(module_directory), -- cgit v1.2.1 From 10e7ab84ed7aec07e7e97f4c9ec7e39d3c778064 Mon Sep 17 00:00:00 2001 From: donkopotamus Date: Tue, 7 Jan 2014 15:03:52 +1300 Subject: Add --template-dir option to mako-render, also allow relative paths on command line --- scripts/mako-render | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) mode change 100644 => 100755 scripts/mako-render diff --git a/scripts/mako-render b/scripts/mako-render old mode 100644 new mode 100755 index a28f3aa..122589f --- a/scripts/mako-render +++ b/scripts/mako-render @@ -1,11 +1,11 @@ #!/usr/bin/env python -def render(data, filename, kw): +def render(data, kw, lookup_dirs): from mako.template import Template from mako.lookup import TemplateLookup - lookup = TemplateLookup(["."]) - return Template(data, filename, lookup=lookup).render(**kw) + lookup = TemplateLookup(lookup_dirs) + return Template(data, lookup=lookup).render(**kw) def varsplit(var): if "=" not in var: @@ -13,7 +13,7 @@ def varsplit(var): return var.split("=", 1) def main(argv=None): - from os.path import isfile + from os.path import isfile, dirname from sys import stdin if argv is None: @@ -25,6 +25,12 @@ def main(argv=None): parser = OptionParser("usage: %prog [FILENAME]") parser.add_option("--var", default=[], action="append", help="variable (can be used multiple times, use name=value)") + parser.add_option("--template-dir", default=[], action="append", + help="Directory to use for template lookup (multiple " + "directories may be provided). If not given then if the " + "template is read from stdin, the value defaults to be " + "the current directory, otherwise it defaults to be the " + "parent directory of the file provided.") opts, args = parser.parse_args(argv[1:]) if len(args) not in (0, 1): @@ -32,15 +38,17 @@ def main(argv=None): if (len(args) == 0) or (args[0] == "-"): fo = stdin + lookup_dirs = opts.template_dir or ["."] else: filename = args[0] if not isfile(filename): raise SystemExit("error: can't find %s" % filename) fo = open(filename) + lookup_dirs = opts.template_dir or [dirname(filename)] kw = dict([varsplit(var) for var in opts.var]) data = fo.read() - print(render(data, filename, kw)) + print(render(data, kw, lookup_dirs=lookup_dirs)) if __name__ == "__main__": main() -- cgit v1.2.1