Skip to content

Commit

Permalink
Moved the parse_ignored_ingredients function from BringHandler to…
Browse files Browse the repository at this point in the history
… `main.py`
  • Loading branch information
felixschndr committed Jan 26, 2025
1 parent 2548a58 commit 73ae088
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
17 changes: 0 additions & 17 deletions source/bring_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def __init__(self, loop: asyncio.AbstractEventLoop):

self.bring = loop.run_until_complete(self.initialize())
self.list_uuid = loop.run_until_complete(self.determine_list_uuid())
self.ignored_ingredients = self.parse_ignored_ingredients()

async def initialize(self) -> Bring:
username = EnvironmentVariableGetter.get("BRING_USERNAME")
Expand All @@ -42,22 +41,6 @@ async def determine_list_uuid(self) -> str:
self.log.critical(f'Can not find a list with the name "{list_name}"')
sys.exit(1)

def parse_ignored_ingredients(self) -> list[Ingredient]:
try:
ignored_ingredients_input = EnvironmentVariableGetter.get("IGNORED_INGREDIENTS")
except KeyError:
self.log.info(
'The variable IGNORED_INGREDIENTS is not set. All ingredients will be added. Consider adding something like "Salt,Pepper"'
)
return []

ignored_ingredients = [
Ingredient(name) for name in ignored_ingredients_input.lower().replace(", ", ",").split(",")
]
self.log.info(f"Ignoring ingredients {Ingredient.to_string_list(ignored_ingredients)}")

return ignored_ingredients

async def add_items(self, ingredients: list[Ingredient]) -> None:
items = [ingredient.to_dict() for ingredient in ingredients]
await self.bring.batch_update_list(self.list_uuid, items, BringItemOperation.ADD)
Expand Down
4 changes: 2 additions & 2 deletions source/ingredient.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def _get_note(raw_data: dict) -> str:
return f" ({raw_data['note']})"

@staticmethod
def is_ignored(raw_data: dict, ignored_ingredients: list[str]) -> bool:
return raw_data["food"]["name"] in ignored_ingredients
def is_ignored(raw_data: dict, ignored_ingredients: list[Ingredient]) -> bool:
return raw_data["food"]["name"] in Ingredient.to_string_list(ignored_ingredients)

@staticmethod
def to_string_list(ingredients: list[Ingredient]) -> list[str]:
Expand Down
26 changes: 23 additions & 3 deletions source/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from ingredient import Ingredient
from logger_mixin import LoggerMixin

from source.environment_variable_getter import EnvironmentVariableGetter

load_dotenv()

app = Flask(__name__)
Expand All @@ -30,13 +32,13 @@ def webhook_handler() -> str:
ingredients_to_add = []
ingredients_raw_data = data["content"]["recipe_ingredient"]
for ingredient_raw_data in ingredients_raw_data:
if Ingredient.is_ignored(ingredient_raw_data, bring_handler.ignored_ingredients):
if Ingredient.is_ignored(ingredient_raw_data, ignored_ingredients):
logger.log.debug(f"Ignoring ingredient {ingredient_raw_data}")

try:
ingredients_to_add.append(Ingredient.from_raw_data(ingredient_raw_data))
except ValueError as e:
logger.log.warning(e)
except ValueError:
logger.log.warning(exc_info=True)
continue

logger.log.info(f"Adding ingredients to Bring: {ingredients_to_add}")
Expand All @@ -52,13 +54,31 @@ def status_handler() -> str:
return "OK"


def parse_ignored_ingredients() -> list[Ingredient]:
try:
ignored_ingredients_input = EnvironmentVariableGetter.get("IGNORED_INGREDIENTS")
except KeyError:
logger.log.info(
'The variable IGNORED_INGREDIENTS is not set. All ingredients will be added. Consider adding something like "Salt,Pepper"'
)
return []

ignored_ingredients = [
Ingredient(name) for name in ignored_ingredients_input.lower().replace(", ", ",").split(",")
]
logger.log.info(f"Ignoring ingredients {Ingredient.to_string_list(ignored_ingredients)}")

return ignored_ingredients


if __name__ == "__main__":
logger = LoggerMixin()
logger.log = logging.getLogger("Main")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

bring_handler = BringHandler(loop)
ignored_ingredients = parse_ignored_ingredients()

host = os.getenv("HTTP_HOST", "0.0.0.0")
port = int(os.getenv("HTTP_PORT", 8742))
Expand Down

0 comments on commit 73ae088

Please sign in to comment.