Allow jinja to load from file path or handle

main
kaiyou 6 years ago committed by Alexander Graf
parent 0370b26f3e
commit 351b05b92d
No known key found for this signature in database
GPG Key ID: B8A9DC143E075629

@ -3,13 +3,22 @@ import importlib
def jinja(source, environ, destination=None): def jinja(source, environ, destination=None):
""" Render a Jinja configuration file """ Render a Jinja configuration file, supports file handle or path
""" """
with open(source, "r") as template: close_source = close_destination = False
result = jinja2.Template(template.read()).render(environ) if type(source) is str:
source = open(source, "r")
close_source = True
if type(destination) is str:
destination = open(destination, "w")
close_destination = True
result = jinja2.Template(source.read()).render(environ)
if close_source:
source.close()
if destination is not None: if destination is not None:
with open(destination, "w") as handle: destination.write(result)
handle.write(result) if close_destination:
destination.close()
return result return result
@ -28,7 +37,7 @@ def merge(*objects):
for obj in objects for key in obj.keys() for obj in objects for key in obj.keys()
} }
elif mode is list: elif mode is list:
return sum(objects) return sum(objects, [])
else: else:
raise ValueError("Cannot merge objects of type {}: {}".format( raise ValueError("Cannot merge objects of type {}: {}".format(
mode, objects)) mode, objects))

Loading…
Cancel
Save