make ingredients&units.yaml optional
This commit is contained in:
parent
dace7ff637
commit
af3454bd0d
1 changed files with 75 additions and 21 deletions
96
recipes.py
96
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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue