Bopytex/bopytex/worker/generate.py

33 lines
796 B
Python
Raw Normal View History

2022-04-10 13:50:58 +00:00
from jinja2.environment import Template
2022-04-13 10:26:04 +00:00
from bopytex.message import Message
2022-04-10 13:50:58 +00:00
def generate(args, deps, output):
env = args["options"]["jinja2"]["environment"]
2022-04-10 13:50:58 +00:00
template = env.get_template(deps[0])
2022-04-13 10:26:04 +00:00
variables = {
2022-07-28 07:39:51 +00:00
"options": args["options"],
"subject": args["subject"],
}
try:
args["options"]["direct_access"]
except KeyError:
pass
else:
2022-07-28 07:39:51 +00:00
for (k, v) in args["options"]["direct_access"].items():
if k not in ["options", "subject"]:
variables[k] = v
2022-04-13 10:26:04 +00:00
try:
with open(output, "w") as out:
fed = template.render(variables)
out.write(fed)
2022-04-13 10:26:04 +00:00
return Message(0, [f"GENERATE - {deps[0]} to {output}"], [])
except Exception as e:
return Message(1, [], [e])