added format_advanced jinja filter to template module
This commit is contained in:
parent
bd7710e1b5
commit
dccfce6718
1 changed files with 17 additions and 1 deletions
18
template
18
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)
|
||||
|
|
Loading…
Reference in a new issue