comfy-recipes/comfyrecipes/builder.py
2023-11-19 00:18:12 +01:00

157 lines
5.1 KiB
Python

from typing import Any, List, Set
import os
import json
import yaml
import jinja2
import jsonschema
import jsonschema.exceptions
import comfyrecipes.parsing as parsing
class Builder:
def __init__(self) -> None:
self.jinjaenv = jinja2.Environment(
loader=jinja2.FileSystemLoader(f"{os.path.dirname(__file__)}/templates"),
autoescape=jinja2.select_autoescape(),
)
def numprint(input: int) -> str:
out = str(input)
if out.endswith(".0"):
return out.split(".", maxsplit=1)[0]
return out
def amountprint(input: int) -> str:
out = numprint(input)
if out == "0.5":
return "1/2"
if out == "0.25":
return "1/4"
if out == "0.75":
return "3/4"
return out
self.jinjaenv.filters["numprint"] = numprint
self.jinjaenv.filters["amountprint"] = amountprint
self.ctx = parsing.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}")
with open(file, encoding="utf-8") as f:
txt = f.read()
if file.endswith(".json"):
return json.loads(txt)
return yaml.safe_load(txt)
def load_pkgfile(self, file: str) -> Any:
return self.load_file(f"{os.path.dirname(__file__)}/{file}")
def rendertemplate(
self, templatepath: str, format: str, file: str, dir: str, args: Any
) -> None:
template = self.jinjaenv.get_template(templatepath)
print(f"rendering {file}")
outstr = template.render(args)
os.makedirs(f"{dir}/out/{format}", exist_ok=True)
with open(f"{dir}/out/{format}/{file}", "w", encoding="utf-8") as f:
f.write(outstr)
self.outfiles.add(file)
def load(self, dir: str) -> int:
if os.path.isfile(dir + "/settings.yaml"):
settingsdct = self.load_file(dir + "/settings.yaml")
settingsschema = self.load_pkgfile("schemas/settings.json")
self.ctx.load_settings(settingsdct, settingsschema)
retcode = self.ctx.issues.check()
if retcode != 0:
return 1
if os.path.isfile(dir + "/units.yaml"):
unitsdct = self.load_file(dir + "/units.yaml")
unitsschema = self.load_pkgfile("schemas/units.json")
self.ctx.load_units(unitsdct, unitsschema)
retcode = self.ctx.issues.check()
if retcode != 0:
return 1
if os.path.isfile(dir + "/ingredients.yaml"):
ingredientsdct = self.load_file(dir + "/ingredients.yaml")
ingredientsschema = self.load_pkgfile("schemas/ingredients.json")
self.ctx.load_ingredients(ingredientsdct, ingredientsschema)
retcode = self.ctx.issues.check()
if retcode != 0:
return 1
return 0
def run(self, dir: str) -> int:
files = []
for _, _, filesx in os.walk(dir + "/recipes"):
files = filesx
files.sort()
recipes: List[parsing.Recipe] = []
recipeschema = self.load_pkgfile("schemas/recipe.json")
for file in files:
if not file.endswith(".yaml"):
print(f"unknown extension of {file}")
continue
recipedct = self.load_file(dir + "/recipes/" + file)
jsonschema.validate(instance=recipedct, schema=recipeschema)
recipe = parsing.Recipe.from_dict(self.ctx, recipedct)
recipe.srcpath = file
recipe.outpath = file[:-5] + ".html"
if self.ctx.issues.check() != 0:
continue
recipes.append(recipe)
retcode = self.ctx.issues.check()
if retcode != 0:
return 1
self.rendertemplate(
templatepath="index.html",
format="html",
file="index.html",
dir=dir,
args={"recipes": recipes},
)
for recipe in recipes:
self.rendertemplate(
templatepath="recipe.html",
format="html",
file=recipe.outpath,
dir=dir,
args={"recipe": recipe},
)
return 0
def finish(self, dir: str) -> int:
files = set()
for _, _, filesx in os.walk(f"{dir}/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"{dir}/out/html/{file}")
return 0
def build(self, path: str) -> int:
fcs = [self.load, self.run, self.finish]
for func in fcs:
try:
ret = func(path)
if ret != 0:
return ret
except jsonschema.exceptions.ValidationError as e:
print("ERROR:", e)
return 1
return 0