support or in ingredients - ingredient alternatives
This commit is contained in:
parent
87884d9ab3
commit
3d3f0b0012
2 changed files with 31 additions and 7 deletions
23
recipes.py
23
recipes.py
|
@ -137,12 +137,16 @@ class PriceDB:
|
|||
|
||||
|
||||
class IngredientInstance:
|
||||
def __init__(self, ctx: Context) -> None:
|
||||
def __init__(
|
||||
self, ctx: Context, defaultamount: float = 1, defaultunit: Optional[Unit] = None
|
||||
) -> None:
|
||||
self.ctx = ctx
|
||||
self.defaultamount = float(defaultamount)
|
||||
self.defaultunit: Optional[Unit] = defaultunit
|
||||
|
||||
def load(self, dct: Dict[str, Any]) -> List[str]:
|
||||
issues = []
|
||||
issues += assert_dict(dct, ["name"], ["amount", "unit", "note"])
|
||||
issues += assert_dict(dct, ["name"], ["amount", "unit", "note", "or"])
|
||||
|
||||
assert_type(dct, "name", str)
|
||||
self.name = dct["name"]
|
||||
|
@ -152,7 +156,7 @@ class IngredientInstance:
|
|||
except RuntimeError as e:
|
||||
issues.append(str(e))
|
||||
|
||||
self.amount = 1.0
|
||||
self.amount = self.defaultamount
|
||||
if "amount" in dct:
|
||||
if isinstance(dct["amount"], float):
|
||||
self.amount = dct["amount"]
|
||||
|
@ -161,19 +165,28 @@ class IngredientInstance:
|
|||
else:
|
||||
raise RuntimeError(f"{dct['amount']} has to be int or float")
|
||||
|
||||
self.unit = None
|
||||
self.unit = self.defaultunit
|
||||
if "unit" in dct:
|
||||
assert_type(dct, "unit", str)
|
||||
try:
|
||||
self.unit = self.ctx.units.get(dct["unit"])
|
||||
except RuntimeError as e:
|
||||
issues.append(str(e))
|
||||
|
||||
self.note = ""
|
||||
if "note" in dct:
|
||||
assert_type(dct, "note", str)
|
||||
self.note = dct["note"]
|
||||
|
||||
self.alternatives = []
|
||||
if "or" in dct:
|
||||
assert_list(dct["or"])
|
||||
for ingdct in dct["or"]:
|
||||
ingredient = IngredientInstance(
|
||||
self.ctx, defaultamount=self.amount, defaultunit=self.unit
|
||||
)
|
||||
ingredient.load(ingdct)
|
||||
self.alternatives.append(ingredient)
|
||||
|
||||
return issues
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue