|
1 | | -from typing import Optional |
2 | | - |
3 | | -from errors import IgnoredIngredient |
4 | | -from logger_mixin import LoggerMixin |
5 | | - |
6 | | -NO_INGREDIENT_NAME_ERROR = "There is an ingredient with no name, it will be ignored!" |
7 | | - |
8 | | - |
9 | | -class Ingredient(LoggerMixin): |
10 | | - def __init__( |
11 | | - self, |
12 | | - ingredient_input: dict, |
13 | | - ignored_ingredients: list[str], |
14 | | - enable_amount: bool, |
15 | | - mealie_version_after_2: bool, |
16 | | - ): |
17 | | - super().__init__() |
18 | | - |
19 | | - self.ingredient_input = ingredient_input |
20 | | - self.ignored_ingredients = ignored_ingredients |
21 | | - |
22 | | - self.enable_amount = enable_amount |
23 | | - |
24 | | - self.food = None |
25 | | - self.specification = "" |
26 | | - |
27 | | - self.parse_input(mealie_version_after_2) |
28 | | - |
29 | | - def __repr__(self): |
30 | | - if self.specification: |
31 | | - return f"{self.food} ({self.specification})" |
32 | | - return self.food |
33 | | - |
34 | | - def parse_input(self, mealie_version_after_2: bool) -> None: |
35 | | - self.log.debug(f"Parsing {self.ingredient_input}") |
36 | | - |
37 | | - if self.enable_amount: |
38 | | - self._parse_input_with_ingredient_amounts(mealie_version_after_2) |
39 | | - else: |
40 | | - self._parse_input_with_no_ingredient_amounts() |
41 | | - |
42 | | - self.log.debug(f"Parsed ingredient: {self}") |
43 | | - |
44 | | - def _parse_input_with_no_ingredient_amounts(self) -> None: |
45 | | - note = self.ingredient_input["note"] |
46 | | - if not note: |
47 | | - raise ValueError(NO_INGREDIENT_NAME_ERROR) |
48 | | - self.food = note |
49 | | - |
50 | | - def _parse_input_with_ingredient_amounts(self, mealie_version_after_2: bool) -> None: |
51 | | - if not self.ingredient_input["food"]: |
52 | | - # Happens if there is an empty ingredient (i.e., added one ingredient but did not fill it out) |
53 | | - raise ValueError(NO_INGREDIENT_NAME_ERROR) |
54 | | - |
55 | | - food_name = self.ingredient_input["food"]["name"] |
56 | | - if mealie_version_after_2: |
57 | | - food_plural_name = self.ingredient_input["food"]["plural_name"] |
58 | | - else: |
59 | | - food_plural_name = self.ingredient_input["food"]["pluralName"] |
60 | | - quantity_raw = self.ingredient_input["quantity"] or 0 |
61 | | - if int(quantity_raw) == quantity_raw: |
62 | | - quantity = int(quantity_raw) |
63 | | - else: |
64 | | - quantity = quantity_raw |
65 | | - unit = self.ingredient_input["unit"] |
66 | | - note = self.ingredient_input["note"] |
67 | | - |
68 | | - # Ignored check # |
69 | | - if food_name.lower() in self.ignored_ingredients: |
70 | | - raise IgnoredIngredient(f"Found ignored ingredient {food_name}") |
71 | | - |
72 | | - self._set_food(food_name, food_plural_name, quantity) |
73 | | - self._set_quantity(quantity) |
74 | | - self._set_seperator(quantity, unit) |
75 | | - self._set_unit(quantity, unit, mealie_version_after_2) |
76 | | - self._set_note(note) |
77 | | - |
78 | | - def _set_food(self, food_name: str, food_plural_name: str, quantity: int) -> None: |
79 | | - if quantity and quantity > 1 and food_plural_name: |
80 | | - self.food = food_plural_name |
81 | | - return |
82 | | - |
83 | | - self.food = food_name |
84 | | - |
85 | | - def _set_quantity(self, quantity: int) -> None: |
86 | | - if quantity: |
87 | | - self.specification += str(quantity) |
88 | | - |
89 | | - def _set_seperator(self, quantity: int, unit: Optional[dict]) -> None: |
90 | | - if quantity and unit: |
91 | | - self.specification += " " |
92 | | - |
93 | | - def _set_unit(self, quantity: int, unit: Optional[dict], mealie_version_after_2: bool) -> None: |
94 | | - if not unit: |
95 | | - return |
96 | | - |
97 | | - unit_name = unit["name"] |
98 | | - unit_abbreviation = unit["abbreviation"] |
99 | | - if mealie_version_after_2: |
100 | | - unit_plural_name = unit["plural_name"] |
101 | | - else: |
102 | | - unit_plural_name = unit["pluralName"] |
103 | | - if unit_abbreviation: |
104 | | - self.specification += unit_abbreviation |
105 | | - elif unit_plural_name and quantity and quantity > 1: |
106 | | - self.specification += unit_plural_name |
107 | | - elif unit_name: |
108 | | - self.specification += unit_name |
109 | | - |
110 | | - def _set_note(self, note: str) -> None: |
111 | | - if note: |
112 | | - self.specification += f" ({note})" |
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import dataclasses |
| 4 | +import uuid |
| 5 | + |
| 6 | + |
| 7 | +@dataclasses.dataclass |
| 8 | +class Ingredient: |
| 9 | + name: str |
| 10 | + specification: str = None |
| 11 | + |
| 12 | + @staticmethod |
| 13 | + def from_raw_data(raw_data: dict) -> Ingredient: |
| 14 | + return Ingredient(name=Ingredient._get_name(raw_data), specification=Ingredient._get_specification(raw_data)) |
| 15 | + |
| 16 | + @staticmethod |
| 17 | + def _get_name(raw_data: dict) -> str: |
| 18 | + return raw_data["food"]["name"] |
| 19 | + |
| 20 | + @staticmethod |
| 21 | + def _get_specification(raw_data: dict) -> str: |
| 22 | + specification = f"{Ingredient._get_quantity(raw_data)}{Ingredient._get_unit(raw_data)}" |
| 23 | + note = Ingredient._get_note(raw_data) |
| 24 | + if specification == "" and note == "": |
| 25 | + return "" |
| 26 | + if specification == "": |
| 27 | + return note |
| 28 | + if note == "": |
| 29 | + return specification |
| 30 | + return f"{specification} {note}" |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + def _get_quantity(raw_data: dict) -> str: |
| 34 | + quantity_raw = raw_data["quantity"] |
| 35 | + if quantity_raw is None: |
| 36 | + return "" |
| 37 | + quantity = int(quantity_raw) if quantity_raw.is_integer() else quantity_raw |
| 38 | + return str(quantity) |
| 39 | + |
| 40 | + @staticmethod |
| 41 | + def _get_unit(raw_data: dict) -> str: |
| 42 | + unit_raw = raw_data["unit"] |
| 43 | + if unit_raw is None: |
| 44 | + return "" |
| 45 | + quantity_is_not_one = raw_data["quantity"] != 1 |
| 46 | + if quantity_is_not_one: |
| 47 | + if unit_raw["plural_name"]: |
| 48 | + return f" {unit_raw["plural_name"]}" |
| 49 | + if unit_raw["plural_abbreviation"]: |
| 50 | + return unit_raw["plural_abbreviation"] |
| 51 | + |
| 52 | + if unit_raw["name"]: |
| 53 | + return f" {unit_raw["name"]}" |
| 54 | + if unit_raw["abbreviation"]: |
| 55 | + return unit_raw["abbreviation"] |
| 56 | + |
| 57 | + return "" |
| 58 | + |
| 59 | + @staticmethod |
| 60 | + def _get_note(raw_data: dict) -> str: |
| 61 | + if not raw_data["note"]: |
| 62 | + return "" |
| 63 | + |
| 64 | + return f"({raw_data['note']})" |
| 65 | + |
| 66 | + @staticmethod |
| 67 | + def is_ignored(name_of_ingredient: str, ignored_ingredients: list[Ingredient]) -> bool: |
| 68 | + return name_of_ingredient.lower() in [ingredient.name for ingredient in ignored_ingredients] |
| 69 | + |
| 70 | + def to_dict(self) -> dict: |
| 71 | + return {"itemId": self.name, "spec": self.specification, "uuid": str(uuid.uuid4())} |
| 72 | + |
| 73 | + |
| 74 | +@dataclasses.dataclass |
| 75 | +class IngredientWithAmountsDisabled(Ingredient): |
| 76 | + @staticmethod |
| 77 | + def from_raw_data(raw_data: dict) -> Ingredient: |
| 78 | + return IngredientWithAmountsDisabled(name=raw_data["display"]) |
0 commit comments