Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_template(self):
t = Template("index.html", path=PATH)
output = t.transform(dict(n=3))
self.assertEqual(output, "0,1,2.\n")
def transform(self, output):
if not isinstance(output, dict):
return output
context = dict(request=request)
context.update(yatl.helpers.__dict__)
context.update(URL=URL)
context.update(output)
context["__vars__"] = output
app_folder = os.path.join(os.environ["PY4WEB_APPS_FOLDER"], request.app_name)
path = self.path or os.path.join(app_folder, "templates")
filename = os.path.join(path, self.filename)
output = yatl.render(
Template.reader(filename),
path=path,
context=context,
delimiters=self.delimiters,
reader=Template.reader,
)
return output
data.update(status="error", message="validation errors", code=401)
elif "errors" in data and not data["errors"]:
del data["errors"]
data["status"] = data.get("status", "success")
data["code"] = data.get("code", 200)
return data
elif path == "logout":
self.session["user"] = None
# Somehow call revoke for active plugin
elif path == "verify_email" and self.db:
if self.verify_email(get_vars.get("token")):
redirect(URL("auth", "email_verified"))
else:
redirect(URL("auth", "token_expired"))
env["path"] = path
return Template("auth.html").transform(env)
def uses(*fixtures_in):
"""Find all fixtures, including dependencies, topologically sorted"""
fixtures = []
reversed_fixtures = []
stack = list(fixtures_in)
while stack:
fixture = stack.pop()
reversed_fixtures.append(fixture)
for other in getattr(fixture, "__prerequisites__", []):
stack.append(other)
for fixture in reversed(reversed_fixtures):
if isinstance(fixture, str):
fixture = Template(fixture)
if not fixture in fixtures:
fixtures.append(fixture)
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
[obj.on_request() for obj in fixtures]
ret = func(*args, **kwargs)
for obj in fixtures:
ret = obj.transform(ret)
[obj.on_success() for obj in fixtures]
return ret
except HTTP:
[obj.on_success() for obj in fixtures]
raise
def reader(filename):
"""Cached file reader, only reads template if it has changed"""
def raw_read():
with open(filename, encoding="utf8") as stream:
return stream.read()
return Template.cache.get(
filename, raw_read, expiration=1, monitor=lambda: os.path.getmtime(filename)
)