parametrize input/output directory, move out/
This commit is contained in:
parent
7636923b30
commit
6b31462d2c
1 changed files with 17 additions and 17 deletions
34
recipes.py
34
recipes.py
|
@ -285,22 +285,22 @@ class Builder:
|
||||||
return yaml.safe_load(txt)
|
return yaml.safe_load(txt)
|
||||||
|
|
||||||
def rendertemplate(
|
def rendertemplate(
|
||||||
self, templatepath: str, format: str, file: str, args: Any
|
self, templatepath: str, format: str, file: str, dir: str, args: Any
|
||||||
) -> None:
|
) -> None:
|
||||||
template = self.jinjaenv.get_template(templatepath)
|
template = self.jinjaenv.get_template(templatepath)
|
||||||
print(f"rendering {file}")
|
print(f"rendering {file}")
|
||||||
outstr = template.render(args)
|
outstr = template.render(args)
|
||||||
|
|
||||||
os.makedirs(f"out/{format}", exist_ok=True)
|
os.makedirs(f"{dir}/out/{format}", exist_ok=True)
|
||||||
|
|
||||||
with open(f"out/{format}/{file}", "w", encoding="utf-8") as f:
|
with open(f"{dir}/out/{format}/{file}", "w", encoding="utf-8") as f:
|
||||||
f.write(outstr)
|
f.write(outstr)
|
||||||
self.outfiles.add(file)
|
self.outfiles.add(file)
|
||||||
|
|
||||||
def load(self) -> int:
|
def load(self, dir: str) -> int:
|
||||||
issues: List[str] = []
|
issues: List[str] = []
|
||||||
|
|
||||||
unitsdct = self.load_file("recipes/units.yaml")
|
unitsdct = self.load_file(dir+"/units.yaml")
|
||||||
unitsschema = self.load_file("schemas/units.json")
|
unitsschema = self.load_file("schemas/units.json")
|
||||||
jsonschema.validate(instance=unitsdct, schema=unitsschema)
|
jsonschema.validate(instance=unitsdct, schema=unitsschema)
|
||||||
issues += self.ctx.units.load(unitsdct)
|
issues += self.ctx.units.load(unitsdct)
|
||||||
|
@ -309,7 +309,7 @@ class Builder:
|
||||||
print("ERROR in units.yaml:", issue)
|
print("ERROR in units.yaml:", issue)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
ingredientsdct = self.load_file("recipes/ingredients.yaml")
|
ingredientsdct = self.load_file(dir+"/ingredients.yaml")
|
||||||
ingredientsschema = self.load_file("schemas/ingredients.json")
|
ingredientsschema = self.load_file("schemas/ingredients.json")
|
||||||
jsonschema.validate(instance=ingredientsdct, schema=ingredientsschema)
|
jsonschema.validate(instance=ingredientsdct, schema=ingredientsschema)
|
||||||
issues += self.ctx.ingredients.load(ingredientsdct)
|
issues += self.ctx.ingredients.load(ingredientsdct)
|
||||||
|
@ -320,10 +320,10 @@ class Builder:
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def run(self) -> int:
|
def run(self, dir: str) -> int:
|
||||||
issues = []
|
issues = []
|
||||||
files = []
|
files = []
|
||||||
for _, _, filesx in os.walk("recipes/recipes"):
|
for _, _, filesx in os.walk(dir+"/recipes"):
|
||||||
files = filesx
|
files = filesx
|
||||||
files.sort()
|
files.sort()
|
||||||
|
|
||||||
|
@ -334,7 +334,7 @@ class Builder:
|
||||||
print(f"unknown extension of {file}")
|
print(f"unknown extension of {file}")
|
||||||
continue
|
continue
|
||||||
recipe = Recipe(self.ctx)
|
recipe = Recipe(self.ctx)
|
||||||
recipedct = self.load_file("recipes/recipes/" + file)
|
recipedct = self.load_file(dir+"/recipes/" + file)
|
||||||
jsonschema.validate(instance=recipedct, schema=recipeschema)
|
jsonschema.validate(instance=recipedct, schema=recipeschema)
|
||||||
issues += recipe.load(recipedct)
|
issues += recipe.load(recipedct)
|
||||||
recipe.srcpath = file
|
recipe.srcpath = file
|
||||||
|
@ -346,30 +346,30 @@ class Builder:
|
||||||
print("ERROR", issue)
|
print("ERROR", issue)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
self.rendertemplate("index.html", "html", "index.html", {"recipes": recipes})
|
self.rendertemplate("index.html", "html", "index.html", dir, {"recipes": recipes})
|
||||||
for recipe in recipes:
|
for recipe in recipes:
|
||||||
self.rendertemplate(
|
self.rendertemplate(
|
||||||
"recipe.html", "html", recipe.outpath, {"recipe": recipe}
|
"recipe.html", "html", recipe.outpath, dir, {"recipe": recipe}
|
||||||
)
|
)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def finish(self) -> int:
|
def finish(self, dir: str) -> int:
|
||||||
files = set()
|
files = set()
|
||||||
for _, _, filesx in os.walk("out/html"):
|
for _, _, filesx in os.walk(f"{dir}/out/html"):
|
||||||
files = set(filesx)
|
files = set(filesx)
|
||||||
|
|
||||||
# files we did not generate, probably left by a previous run, but not valid anymore
|
# files we did not generate, probably left by a previous run, but not valid anymore
|
||||||
extra_files = files - self.outfiles
|
extra_files = files - self.outfiles
|
||||||
for file in extra_files:
|
for file in extra_files:
|
||||||
print(f"removing obsolete {file}")
|
print(f"removing obsolete {file}")
|
||||||
os.remove(f"out/html/{file}")
|
os.remove(f"{dir}/out/html/{file}")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def main(self) -> int:
|
def build(self, path: str) -> int:
|
||||||
fcs = [self.load, self.run, self.finish]
|
fcs = [self.load, self.run, self.finish]
|
||||||
for func in fcs:
|
for func in fcs:
|
||||||
try:
|
try:
|
||||||
ret = func()
|
ret = func(path)
|
||||||
if ret != 0:
|
if ret != 0:
|
||||||
return ret
|
return ret
|
||||||
except jsonschema.exceptions.ValidationError as e:
|
except jsonschema.exceptions.ValidationError as e:
|
||||||
|
@ -380,4 +380,4 @@ class Builder:
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
builder = Builder()
|
builder = Builder()
|
||||||
sys.exit(builder.main())
|
sys.exit(builder.build("recipes"))
|
Loading…
Add table
Add a link
Reference in a new issue