From af3454bd0d558dd210a200992689ac8a2477df03 Mon Sep 17 00:00:00 2001 From: Emi Vasilek Date: Fri, 17 Nov 2023 20:15:59 +0100 Subject: [PATCH] make ingredients&units.yaml optional --- recipes.py | 96 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 75 insertions(+), 21 deletions(-) diff --git a/recipes.py b/recipes.py index db53a9c..2b1db35 100644 --- a/recipes.py +++ b/recipes.py @@ -49,12 +49,27 @@ class Issues: class Context: def __init__(self) -> None: - self.units = Units(self) + self.units: AUnits = FakeUnits(self) self.default_unit = Unit(self, {"name": "piece"}) - self.units.units.append(self.default_unit) - self.ingredients = Ingredients(self) + self.ingredients: AIngredients = FakeIngredients(self) self.issues = Issues() + def load_units( + self, unitsdct: List[Dict[str, Any]], unitsschema: Dict[str, Any] + ) -> None: + self.units = Units(self) + self.units.units.append(self.default_unit) + jsonschema.validate(instance=unitsdct, schema=unitsschema) + self.units.load(unitsdct) + self.units.validate() + + def load_ingredients( + self, ingredientsdct: List[Dict[str, Any]], ingredientsschema: Dict[str, Any] + ) -> None: + self.ingredients = Ingredients(self) + jsonschema.validate(instance=ingredientsdct, schema=ingredientsschema) + self.ingredients.load(ingredientsdct) + class Element: def __init__(self, ctx: Context, dct: Dict[str, Any]) -> None: @@ -121,11 +136,33 @@ class Unit(Element): self["conversions"] = conversions -class Units: +class AUnits: def __init__(self, ctx: Context) -> None: self.ctx = ctx self.units: List[Unit] = [] + def load(self, lst: List[Any]) -> None: + pass + + @abstractmethod + def get(self, name: str) -> Optional[Unit]: + ... + + def validate(self) -> None: + pass + + +class FakeUnits(AUnits): + def get(self, name: str) -> Optional[Unit]: + for unit in self.units: + if unit["name"] == name: + return unit + unit = Unit(self.ctx, {"name": name}) + self.units.append(unit) + return unit + + +class Units(AUnits): def load(self, lst: List[Any]) -> None: for unitdct in lst: unit = Unit(self.ctx, unitdct) @@ -234,11 +271,30 @@ class Ingredient(Element): return prices[0] -class Ingredients: +class AIngredients: def __init__(self, ctx: Context) -> None: self.ctx = ctx self.ingredients: List[Ingredient] = [] + def load(self, lst: List[Any]) -> None: + pass + + @abstractmethod + def get(self, name: str) -> Optional[Ingredient]: + ... + + +class FakeIngredients(AIngredients): + def get(self, name: str) -> Optional[Ingredient]: + for ing in self.ingredients: + if ing["name"] == name: + return ing + ing = Ingredient(self.ctx, {"name": name}) + self.ingredients.append(ing) + return ing + + +class Ingredients(AIngredients): def load(self, lst: List[Any]) -> None: for ingdct in lst: ing = Ingredient(self.ctx, ingdct) @@ -399,23 +455,21 @@ class Builder: self.outfiles.add(file) def load(self, dir: str) -> int: - unitsdct = self.load_file(dir + "/units.yaml") - unitsschema = self.load_file("schemas/units.json") - jsonschema.validate(instance=unitsdct, schema=unitsschema) - self.ctx.units.load(unitsdct) - retcode = self.ctx.issues.check() - if retcode != 0: - return 1 - self.ctx.units.validate() - - ingredientsdct = self.load_file(dir + "/ingredients.yaml") - ingredientsschema = self.load_file("schemas/ingredients.json") - jsonschema.validate(instance=ingredientsdct, schema=ingredientsschema) - self.ctx.ingredients.load(ingredientsdct) - retcode = self.ctx.issues.check() - if retcode != 0: - return 1 + if os.path.isfile(dir + "/units.yaml"): + unitsschema = self.load_file("schemas/units.json") + unitsdct = self.load_file(dir + "/units.yaml") + 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_file("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: