Initial commit

This commit is contained in:
2017-09-24 18:07:50 +02:00
commit 7d7642ead6
11 changed files with 316 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
import airspeed, config, os, html
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_url(data):
return "ølailsf"
class Model:
def __setattr__(self, name, value):
if not hasattr(self, name):
raise Exception(f"{self.__class__.__name__} has no attribute {name!r}")
super(Model, self).__setattr__(name, value)
def __repr__(self):
return "Card(%s)" % \
", ".join(f"{i}={getattr(self, i)!r}" for i in dir(self) if "_" not in i)
__str__ = __repr__
#decorators with parameters:
def withResource(path, binary=False):
data = readfile(os.path.join(config.resourcedir, path), binary)
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()
else:
content = data
if "file" in kwargs:
kwargs["file"][os.path.basename(path)] = content
else:
kwargs["file"] = {os.path.basename(path): content}
return func(*args, **kwargs)
return newfunc
return decorator
def withTemplate(path):
template = airspeed.Template(readfile(os.path.join(config.resourcedir, path)))
def decorator(func):
def newfunc(*args, **kwargs):
if not config.cache:
t = airspeed.Template(readfile(os.path.join(config.resourcedir, path)))
else:
t = template
class T:
def merge(self, objects):
objects.update({"escape_html":escape_html, "escape_url":escape_url})
return t.merge(objects)
if "template" in kwargs:
kwargs["template"][os.path.basename(path)] = T()
else:
kwargs["template"] = {os.path.basename(path): T()}
return func(*args, **kwargs)
return newfunc
return decorator