From 351b05b92d630a6778bb23fd331ac693e71d2d77 Mon Sep 17 00:00:00 2001 From: kaiyou Date: Mon, 6 May 2019 14:57:18 +0200 Subject: [PATCH] Allow jinja to load from file path or handle --- core/base/libs/socrate/socrate/conf.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/core/base/libs/socrate/socrate/conf.py b/core/base/libs/socrate/socrate/conf.py index 5ccde54d..d792879c 100644 --- a/core/base/libs/socrate/socrate/conf.py +++ b/core/base/libs/socrate/socrate/conf.py @@ -3,13 +3,22 @@ import importlib 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: - result = jinja2.Template(template.read()).render(environ) + close_source = close_destination = False + 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: - with open(destination, "w") as handle: - handle.write(result) + destination.write(result) + if close_destination: + destination.close() return result @@ -28,7 +37,7 @@ def merge(*objects): for obj in objects for key in obj.keys() } elif mode is list: - return sum(objects) + return sum(objects, []) else: raise ValueError("Cannot merge objects of type {}: {}".format( mode, objects))