From d9a464ee9daa2f1e2c1540f45bbabab79b17e0b0 Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Tue, 27 Mar 2012 08:51:37 -0700 Subject: [PATCH 1/4] added format_advanced jinja filter to template module --- library/template | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/library/template b/library/template index 110d397aa1..68e967ffe9 100755 --- a/library/template +++ b/library/template @@ -19,6 +19,7 @@ import sys import os +import collections import jinja2 import shlex try: @@ -26,6 +27,21 @@ try: except ImportError: import simplejson as json +environment = jinja2.Environment() + +def format_advanced(fmt, data): + # jinja2 filter to use advanced python string formatting + # e.g, {{ "{0} {1} {2}"|format_advanced(['a', 'b', 'c']) }} + # see http://docs.python.org/library/string.html#formatstrings + if isinstance(data, collections.Mapping): + return fmt.format(**data) + elif isinstance(data, collections.Sequence): + return fmt.format(*data) + else: + return data + +environment.filters['format_advanced'] = format_advanced + # =========================================== # convert arguments of form a=b c=d # to a dictionary @@ -91,7 +107,7 @@ if os.path.exists(dest): md5sum = os.popen("md5sum %s" % dest).read().split()[0] # call Jinja2 here and save the new template file -template = jinja2.Template(source) +template = environment.from_string(source) data_out = template.render(data) f = open(dest, "w+") f.write(data_out) From 9d9e3d8c5667bd42f8430709a2d4001bc2ff5d20 Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Tue, 27 Mar 2012 09:10:51 -0700 Subject: [PATCH 2/4] resetting template.. previous commit not needed --- library/template | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/library/template b/library/template index 68e967ffe9..110d397aa1 100755 --- a/library/template +++ b/library/template @@ -19,7 +19,6 @@ import sys import os -import collections import jinja2 import shlex try: @@ -27,21 +26,6 @@ try: except ImportError: import simplejson as json -environment = jinja2.Environment() - -def format_advanced(fmt, data): - # jinja2 filter to use advanced python string formatting - # e.g, {{ "{0} {1} {2}"|format_advanced(['a', 'b', 'c']) }} - # see http://docs.python.org/library/string.html#formatstrings - if isinstance(data, collections.Mapping): - return fmt.format(**data) - elif isinstance(data, collections.Sequence): - return fmt.format(*data) - else: - return data - -environment.filters['format_advanced'] = format_advanced - # =========================================== # convert arguments of form a=b c=d # to a dictionary @@ -107,7 +91,7 @@ if os.path.exists(dest): md5sum = os.popen("md5sum %s" % dest).read().split()[0] # call Jinja2 here and save the new template file -template = environment.from_string(source) +template = jinja2.Template(source) data_out = template.render(data) f = open(dest, "w+") f.write(data_out) From bf0f3eac1f980197bd575689668ac6845a164702 Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Tue, 27 Mar 2012 11:06:31 -0700 Subject: [PATCH 3/4] catch jinja template errors in template module --- library/template | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/library/template b/library/template index 110d397aa1..c6b0c8e3c1 100755 --- a/library/template +++ b/library/template @@ -26,6 +26,8 @@ try: except ImportError: import simplejson as json +environment = jinja2.Environment() + # =========================================== # convert arguments of form a=b c=d # to a dictionary @@ -57,9 +59,17 @@ if not os.path.exists(metadata): sys.exit(1) # raise an error if we can't parse the template metadata +#data = {} try: f = open(metadata) data = json.loads(f.read()) +# Hack by mgw to get nested variables -- use at your own risk +# data_in = json.loads(f.read()) +# for k, v in data_in.items(): +# try: +# data[k] = eval(v) +# except: +# data[k] = v f.close() except: print json.dumps({ @@ -90,16 +100,20 @@ md5sum = None if os.path.exists(dest): md5sum = os.popen("md5sum %s" % dest).read().split()[0] -# call Jinja2 here and save the new template file -template = jinja2.Template(source) -data_out = template.render(data) +try: + # call Jinja2 here and save the new template file + template = environment.from_string(source) + data_out = template.render(data) +except jinja2.TemplateError as e: + print json.dumps({ + "failed": True, + "msg" : e.message + }) + sys.exit(1) f = open(dest, "w+") f.write(data_out) f.close() -# TODO: catch templating errors and do not clobber the file on the -# other end unless things were successful - # record m5sum and return success and whether things have changed md5sum2 = os.popen("md5sum %s" % dest).read().split()[0] From 3046f743d9b3d147da8180ecd60ce58333d40d8a Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Tue, 27 Mar 2012 11:12:33 -0700 Subject: [PATCH 4/4] removed hack --- library/template | 7 ------- 1 file changed, 7 deletions(-) diff --git a/library/template b/library/template index c6b0c8e3c1..03bdb18d57 100755 --- a/library/template +++ b/library/template @@ -63,13 +63,6 @@ if not os.path.exists(metadata): try: f = open(metadata) data = json.loads(f.read()) -# Hack by mgw to get nested variables -- use at your own risk -# data_in = json.loads(f.read()) -# for k, v in data_in.items(): -# try: -# data[k] = eval(v) -# except: -# data[k] = v f.close() except: print json.dumps({