From 87884d9ab3470a8aac221865375a75a5859067c9 Mon Sep 17 00:00:00 2001 From: Emi Vasilek Date: Sun, 5 Nov 2023 11:19:59 +0100 Subject: [PATCH] add output file tracking and remove obsolete files --- recipes.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/recipes.py b/recipes.py index b03d889..0e1224f 100644 --- a/recipes.py +++ b/recipes.py @@ -1,9 +1,10 @@ -from typing import Dict, List, Any, Optional +from typing import Dict, List, Any, Optional, Set import os import yaml import jinja2 + class Context: def __init__(self) -> None: self.units = Units() @@ -36,6 +37,7 @@ class Units: class Ingredient: def __init__(self, ctx: Context) -> None: self.ctx = ctx + def load(self, dct: Dict[str, Any]) -> List[str]: issues = [] issues += assert_dict(dct, ["name"], ["wdid", "pricedb", "aliases"]) @@ -178,6 +180,7 @@ class IngredientInstance: class RecipePart: def __init__(self, ctx: Context) -> None: self.ctx = ctx + def load(self, dct: Dict[str, Any]) -> List[str]: issues = [] issues += assert_dict(dct, ["title", "ingredients", "steps"], []) @@ -263,6 +266,8 @@ class Builder: autoescape=jinja2.select_autoescape(), ) self.ctx = Context() + # list of output files that will be built + self.outfiles: Set[str] = set() def load_file(self, file: str) -> Any: print(f"loading {file}") @@ -281,6 +286,7 @@ class Builder: with open(f"out/{format}/{file}", "w", encoding="utf-8") as f: f.write(outstr) + self.outfiles.add(file) def load(self) -> None: issues: List[str] = [] @@ -322,10 +328,27 @@ class Builder: self.rendertemplate("index.html", "html", "index.html", {"recipes": recipes}) for recipe in recipes: - self.rendertemplate( "recipe.html", "html", recipe.outpath, {"recipe": recipe}) + self.rendertemplate( + "recipe.html", "html", recipe.outpath, {"recipe": recipe} + ) + + def finish(self) -> None: + files = set() + for _, _, filesx in os.walk("out/html"): + files = set(filesx) + + # files we did not generate, probably left by a previous run, but not valid anymore + extra_files = files - self.outfiles + for file in extra_files: + print(f"removing obsolete {file}") + os.remove(f"out/html/{file}") + + def main(self) -> None: + self.load() + self.run() + self.finish() if __name__ == "__main__": builder = Builder() - builder.load() - builder.run() + builder.main()