add generate-units and generate-ingredients subcommands
This commit is contained in:
parent
e1f25dbb14
commit
3ae3dfe922
2 changed files with 99 additions and 3 deletions
|
@ -1,4 +1,5 @@
|
|||
from typing import Any, List, Set
|
||||
import sys
|
||||
from typing import Any, Callable, Dict, List, Set, TypeVar
|
||||
import os
|
||||
import json
|
||||
|
||||
|
@ -9,6 +10,7 @@ import jsonschema.exceptions
|
|||
|
||||
import comfyrecipes.parsing as parsing
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
class Builder:
|
||||
def __init__(self) -> None:
|
||||
|
@ -132,6 +134,79 @@ class Builder:
|
|||
)
|
||||
return 0
|
||||
|
||||
def generate_units(self, dir: str) -> None:
|
||||
def collect_unitnames(rec: parsing.Recipe) -> List[str]:
|
||||
results: List[str] = []
|
||||
for ing in rec.ingredients:
|
||||
results.append(ing.unit.name)
|
||||
return results
|
||||
unitnamelists = self.foreach_recipe(dir, collect_unitnames)
|
||||
unitnamesset: Set[str] = set()
|
||||
for unitnamelst in unitnamelists:
|
||||
for unitname in unitnamelst:
|
||||
unitnamesset.add(unitname)
|
||||
unitnames = list(unitnamesset)
|
||||
unitnames.sort()
|
||||
|
||||
units: List[Dict[str, str]] = []
|
||||
for name in unitnames:
|
||||
units.append({"name": name})
|
||||
|
||||
file = f"{dir}/units.yaml"
|
||||
with open(file, "w") as f:
|
||||
f.write(yaml.dump(units))
|
||||
print("found units written to", file)
|
||||
|
||||
def generate_ingredients(self, dir: str) -> None:
|
||||
def collect_ingnames(rec: parsing.Recipe) -> List[str]:
|
||||
results: List[str] = []
|
||||
for ing in rec.ingredients:
|
||||
results.append(ing.name)
|
||||
return results
|
||||
ingredientnamelists = self.foreach_recipe(dir, collect_ingnames)
|
||||
ingredientnamesset: Set[str] = set()
|
||||
for ingredientnamelst in ingredientnamelists:
|
||||
for ingredientname in ingredientnamelst:
|
||||
ingredientnamesset.add(ingredientname)
|
||||
ingredientnames = list(ingredientnamesset)
|
||||
ingredientnames.sort()
|
||||
|
||||
ingredients: List[Dict[str, str]] = []
|
||||
for name in ingredientnames:
|
||||
ingredients.append({"name": name})
|
||||
|
||||
file = f"{dir}/ingredients.yaml"
|
||||
with open(file, "w") as f:
|
||||
f.write(yaml.dump(ingredients))
|
||||
print("found ingredients written to", file)
|
||||
|
||||
def foreach_recipe(self, dir: str, func: Callable[[parsing.Recipe], T]) -> List[T]:
|
||||
files = []
|
||||
for _, _, filesx in os.walk(dir + "/recipes"):
|
||||
files = filesx
|
||||
files.sort()
|
||||
|
||||
def foreach_subrecipe(recipe: parsing.Recipe) -> List[T]:
|
||||
results: List[T] = []
|
||||
for rec in [recipe] + recipe.subrecipes:
|
||||
results.append(func(rec))
|
||||
for subrec in recipe.subrecipes:
|
||||
for subsubrec in subrec.subrecipes:
|
||||
results += foreach_subrecipe(subsubrec)
|
||||
return results
|
||||
|
||||
results: List[T] = []
|
||||
for file in files:
|
||||
if not file.endswith(".yaml"):
|
||||
print(f"unknown extension of {file}")
|
||||
continue
|
||||
recipedct = self.load_file(dir + "/recipes/" + file)
|
||||
recipe = parsing.Recipe.from_dict(self.ctx, recipedct)
|
||||
if self.ctx.issues.check() != 0:
|
||||
continue
|
||||
results += foreach_subrecipe(recipe)
|
||||
return results
|
||||
|
||||
def finish(self, dir: str) -> int:
|
||||
files = set()
|
||||
for _, _, filesx in os.walk(f"{dir}/out/html"):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue