from pathlib import Path from typing import Optional from jinja2 import Environment, PackageLoader, select_autoescape from ee.tools import mk_parents class Message(object): def __init__(self, object: Optional[str] = None, text: Optional[str] = None): self.object = object self.text = text def subsection_filter(s: str): return "{}\n{}".format(s, "~" * len(s)) def save_report(package: str, template_name: str, out_file: Path, **kwargs): env = Environment( loader=PackageLoader(package, "templates"), autoescape=select_autoescape(["html", "xml"]), ) env.filters["subsection"] = subsection_filter mk_parents(out_file) with out_file.open("w") as f: template = env.get_template(template_name) f.write(template.render(**kwargs))