from pathlib import Path from typing import Optional from jinja2 import Environment, PackageLoader, select_autoescape from ee.part import Part from ee.tools import mk_parents from ee.xml import types class Message(object): def __init__(self, obj: Optional[str] = None, text: Optional[str] = None): self.object = obj self.text = text def section_filter(s: str): return "{}\n{}".format(s, "=" * len(s)) def subsection_filter(s: str): return "{}\n{}".format(s, "-" * len(s)) def subsubsection_filter(s: str): return "{}\n{}".format(s, "~" * len(s)) def subsubsubsection_filter(s: str): return "{}\n{}".format(s, "." * len(s)) def first_ref_filter(part: Part) -> Optional[types.SchematicReference]: return part.get_only_schematic_reference() def first_pn_filter(part: Part) -> Optional[types.PartNumber]: return part.get_only_mpn() def first_spn_filter(part: Part) -> Optional[types.SupplierPartNumber]: return part.get_only_spn() 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 env.filters["subsubsection"] = subsubsection_filter env.filters["subsubsubsection"] = subsubsubsection_filter env.filters["first_ref"] = first_ref_filter env.filters["first_pn"] = first_pn_filter env.filters["first_spn"] = first_spn_filter mk_parents(out_file) with out_file.open("w") as f: template = env.get_template(template_name) f.write(template.render(**kwargs))