add output file tracking and remove obsolete files
This commit is contained in:
parent
2bb9aed4a3
commit
87884d9ab3
1 changed files with 27 additions and 4 deletions
31
recipes.py
31
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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue