from jinja2 import Environment, FileSystemLoader, select_autoescape import shutil import time import os import datetime from sources import CSItem from inventory import load_cheatsheet_inventory, prepare_cheatsheets from config import get_settings from logger import get_worker_thread_logger def build(trigger_list: list[str] | None = None): start_time = time.time() settings = get_settings() inv_raw = load_cheatsheet_inventory(settings.paths.inventory_file) # Clear output directory shutil.rmtree(settings.paths.output, ignore_errors=True) shutil.copytree(settings.paths.static, settings.paths.output) inv, cats, no_cat = prepare_cheatsheets(inv_raw) if not os.path.exists(settings.paths.prod): os.mkdir(settings.paths.prod) env = Environment( loader=FileSystemLoader(settings.paths.templates), autoescape=select_autoescape() ) index = env.get_template("index.html.j2") logger = get_worker_thread_logger() logger.info("Generated cheatsheets:") for i in inv: logger.info(f"- {i}") thisYear = datetime.datetime.now().year print(cats) with open(f"{settings.paths.output}/index.html", "w", encoding="utf-8") as f: f.write(index.render(cats=list( sorted( map(lambda c: (c[0], list(c[1].items())), cats.items()), key=lambda x: x[0] ) ), no_cat=no_cat, thisYear=thisYear)) with open(f"{settings.paths.output}/impressum.html", "w", encoding="utf-8") as f: f.write(env.get_template("impressum.html.j2").render(thisYear=thisYear)) with open(f"{settings.paths.output}/license.html", "w", encoding="utf-8") as f: f.write(env.get_template("license.html.j2").render(thisYear=thisYear)) # Copy to prod logger.info("Copying output to production directory") shutil.copytree(settings.paths.output, settings.paths.prod, dirs_exist_ok=True) logger.info("Done after {:.2f}s".format(time.time() - start_time)) if __name__ == "__main__": build()