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 os
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
import jinja2
|
import jinja2
|
||||||
|
|
||||||
|
|
||||||
class Context:
|
class Context:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.units = Units()
|
self.units = Units()
|
||||||
|
@ -36,6 +37,7 @@ class Units:
|
||||||
class Ingredient:
|
class Ingredient:
|
||||||
def __init__(self, ctx: Context) -> None:
|
def __init__(self, ctx: Context) -> None:
|
||||||
self.ctx = ctx
|
self.ctx = ctx
|
||||||
|
|
||||||
def load(self, dct: Dict[str, Any]) -> List[str]:
|
def load(self, dct: Dict[str, Any]) -> List[str]:
|
||||||
issues = []
|
issues = []
|
||||||
issues += assert_dict(dct, ["name"], ["wdid", "pricedb", "aliases"])
|
issues += assert_dict(dct, ["name"], ["wdid", "pricedb", "aliases"])
|
||||||
|
@ -178,6 +180,7 @@ class IngredientInstance:
|
||||||
class RecipePart:
|
class RecipePart:
|
||||||
def __init__(self, ctx: Context) -> None:
|
def __init__(self, ctx: Context) -> None:
|
||||||
self.ctx = ctx
|
self.ctx = ctx
|
||||||
|
|
||||||
def load(self, dct: Dict[str, Any]) -> List[str]:
|
def load(self, dct: Dict[str, Any]) -> List[str]:
|
||||||
issues = []
|
issues = []
|
||||||
issues += assert_dict(dct, ["title", "ingredients", "steps"], [])
|
issues += assert_dict(dct, ["title", "ingredients", "steps"], [])
|
||||||
|
@ -263,6 +266,8 @@ class Builder:
|
||||||
autoescape=jinja2.select_autoescape(),
|
autoescape=jinja2.select_autoescape(),
|
||||||
)
|
)
|
||||||
self.ctx = Context()
|
self.ctx = Context()
|
||||||
|
# list of output files that will be built
|
||||||
|
self.outfiles: Set[str] = set()
|
||||||
|
|
||||||
def load_file(self, file: str) -> Any:
|
def load_file(self, file: str) -> Any:
|
||||||
print(f"loading {file}")
|
print(f"loading {file}")
|
||||||
|
@ -281,6 +286,7 @@ class Builder:
|
||||||
|
|
||||||
with open(f"out/{format}/{file}", "w", encoding="utf-8") as f:
|
with open(f"out/{format}/{file}", "w", encoding="utf-8") as f:
|
||||||
f.write(outstr)
|
f.write(outstr)
|
||||||
|
self.outfiles.add(file)
|
||||||
|
|
||||||
def load(self) -> None:
|
def load(self) -> None:
|
||||||
issues: List[str] = []
|
issues: List[str] = []
|
||||||
|
@ -322,10 +328,27 @@ class Builder:
|
||||||
|
|
||||||
self.rendertemplate("index.html", "html", "index.html", {"recipes": recipes})
|
self.rendertemplate("index.html", "html", "index.html", {"recipes": recipes})
|
||||||
for recipe in 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__":
|
if __name__ == "__main__":
|
||||||
builder = Builder()
|
builder = Builder()
|
||||||
builder.load()
|
builder.main()
|
||||||
builder.run()
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue