58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
import os
|
|
import json
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
file_path = os.getenv("file_path")
|
|
|
|
def read_json() -> dict:
|
|
try:
|
|
with open("./src/json/ingredients.json", "rt") as file:
|
|
return json.load(file)
|
|
except:
|
|
print("Could not find or read file.")
|
|
|
|
def update_json(meals: dict):
|
|
print("TODO: Add update logic")
|
|
|
|
def get_selected_meals(meals: dict) -> dict:
|
|
meal_json = read_json()
|
|
selected_meals = {}
|
|
for meal in meals:
|
|
if meal in meal_json:
|
|
selected_meals[meal] = meal_json[meal]
|
|
return selected_meals
|
|
|
|
def combine_ingredients(meals: dict) -> dict:
|
|
combined_ingredients = {}
|
|
|
|
for meal, ingredients in meals.items():
|
|
for ingredient, detail in ingredients.items():
|
|
if ingredient in combined_ingredients:
|
|
combined_ingredients[ingredient]['quantity'] += detail['quantity']
|
|
else:
|
|
combined_ingredients[ingredient] = detail
|
|
return combined_ingredients
|
|
|
|
def write_checklist(data: dict) -> None:
|
|
if os.path.exists(file_path):
|
|
os.remove(file_path)
|
|
|
|
try:
|
|
with open(file_path, 'w') as checklist:
|
|
for ingredient, details in data.items():
|
|
s = ingredient + " - " + str(details['quantity'])
|
|
if details['units'] != None:
|
|
s += " " + details['units']
|
|
checklist.write(f"- [ ] {s}\n")
|
|
except Exception as e:
|
|
print(f"Unable to write data to file. {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
meals = read_json()
|
|
if meals:
|
|
combined_ingredients = combine_ingredients(meals)
|
|
write_checklist(combined_ingredients) |