Add 2 decorators and cleanup in common.py
This commit is contained in:
@@ -9,11 +9,34 @@ def readfile(path, binary=False):
|
|||||||
with open(path, "rb" if binary else "r") as f:
|
with open(path, "rb" if binary else "r") as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
|
|
||||||
def escape_html(data):
|
def escape_html(data, break_newlines=True):
|
||||||
return html.escape(str(data))
|
if type(data) is list:
|
||||||
|
data = "\n".join(data)
|
||||||
|
if data is None:
|
||||||
|
data = ""
|
||||||
|
if break_newlines:
|
||||||
|
return html.escape(str(data)).replace("\n", "<br>")
|
||||||
|
else:
|
||||||
|
return html.escape(str(data))
|
||||||
|
|
||||||
def escape_url(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:
|
class Model:
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
@@ -32,8 +55,7 @@ def withResource(path, binary=False):
|
|||||||
def decorator(func):
|
def decorator(func):
|
||||||
def newfunc(*args, **kwargs):
|
def newfunc(*args, **kwargs):
|
||||||
if not config.cache:
|
if not config.cache:
|
||||||
with open(os.path.join(config.resourcedir, path), "rb" if binary else "r") as f:
|
content = readfile(os.path.join(config.resourcedir, path), binary)
|
||||||
content = f.read()
|
|
||||||
else:
|
else:
|
||||||
content = data
|
content = data
|
||||||
if "file" in kwargs:
|
if "file" in kwargs:
|
||||||
|
|||||||
Reference in New Issue
Block a user