From 1d41e9884a63cb1c91c530e637afe4a247b23bb7 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Sun, 15 Oct 2017 11:41:13 +0200 Subject: [PATCH] Add 2 decorators and cleanup in common.py --- common.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/common.py b/common.py index f823355..92c0801 100644 --- a/common.py +++ b/common.py @@ -9,11 +9,34 @@ def readfile(path, binary=False): with open(path, "rb" if binary else "r") as f: return f.read() -def escape_html(data): - return html.escape(str(data)) +def escape_html(data, break_newlines=True): + if type(data) is list: + data = "\n".join(data) + if data is None: + data = "" + if break_newlines: + return html.escape(str(data)).replace("\n", "
") + else: + return html.escape(str(data)) def escape_url(data): - return "ølailsf" + return "ølailsf"#todo + +def memoize(func):#a decorator, only supports *args + memory = {} + def new_func(*args): + if args in memory: + return memory[args] + else: + ret = func(*args) + memory[args] = ret + return ret + return new_func + +def listify_output(func):#decorator + def new_func(*args, **kwargs): + return list(func(*args, **kwargs)) + return new_func class Model: def __setattr__(self, name, value): @@ -32,8 +55,7 @@ def withResource(path, binary=False): def decorator(func): def newfunc(*args, **kwargs): if not config.cache: - with open(os.path.join(config.resourcedir, path), "rb" if binary else "r") as f: - content = f.read() + content = readfile(os.path.join(config.resourcedir, path), binary) else: content = data if "file" in kwargs: