From 6b31462d2c38d5c3c71f7feea0e6ebfb3a283004 Mon Sep 17 00:00:00 2001 From: Emi Vasilek Date: Mon, 6 Nov 2023 07:24:36 +0100 Subject: [PATCH] parametrize input/output directory, move out/ --- recipes.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/recipes.py b/recipes.py index 293264a..b3a1233 100644 --- a/recipes.py +++ b/recipes.py @@ -285,22 +285,22 @@ class Builder: return yaml.safe_load(txt) def rendertemplate( - self, templatepath: str, format: str, file: str, args: Any + 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"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) self.outfiles.add(file) - def load(self) -> int: + def load(self, dir: str) -> int: issues: List[str] = [] - unitsdct = self.load_file("recipes/units.yaml") + unitsdct = self.load_file(dir+"/units.yaml") unitsschema = self.load_file("schemas/units.json") jsonschema.validate(instance=unitsdct, schema=unitsschema) issues += self.ctx.units.load(unitsdct) @@ -309,7 +309,7 @@ class Builder: print("ERROR in units.yaml:", issue) return 1 - ingredientsdct = self.load_file("recipes/ingredients.yaml") + ingredientsdct = self.load_file(dir+"/ingredients.yaml") ingredientsschema = self.load_file("schemas/ingredients.json") jsonschema.validate(instance=ingredientsdct, schema=ingredientsschema) issues += self.ctx.ingredients.load(ingredientsdct) @@ -320,10 +320,10 @@ class Builder: return 0 - def run(self) -> int: + def run(self, dir: str) -> int: issues = [] files = [] - for _, _, filesx in os.walk("recipes/recipes"): + for _, _, filesx in os.walk(dir+"/recipes"): files = filesx files.sort() @@ -334,7 +334,7 @@ class Builder: print(f"unknown extension of {file}") continue recipe = Recipe(self.ctx) - recipedct = self.load_file("recipes/recipes/" + file) + recipedct = self.load_file(dir+"/recipes/" + file) jsonschema.validate(instance=recipedct, schema=recipeschema) issues += recipe.load(recipedct) recipe.srcpath = file @@ -346,30 +346,30 @@ class Builder: print("ERROR", issue) 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: self.rendertemplate( - "recipe.html", "html", recipe.outpath, {"recipe": recipe} + "recipe.html", "html", recipe.outpath, dir, {"recipe": recipe} ) return 0 - def finish(self) -> int: + def finish(self, dir: str) -> int: files = set() - for _, _, filesx in os.walk("out/html"): + 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"out/html/{file}") + os.remove(f"{dir}/out/html/{file}") return 0 - def main(self) -> int: + def build(self, path: str) -> int: fcs = [self.load, self.run, self.finish] for func in fcs: try: - ret = func() + ret = func(path) if ret != 0: return ret except jsonschema.exceptions.ValidationError as e: @@ -380,4 +380,4 @@ class Builder: if __name__ == "__main__": builder = Builder() - sys.exit(builder.main()) + sys.exit(builder.build("recipes")) \ No newline at end of file