Added update_json and refactor
This commit is contained in:
@@ -5,9 +5,9 @@ from dotenv import load_dotenv
|
|||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
file_path = os.getenv("file_path")
|
file_path = os.getenv("file_path") if os.getenv("file_path") else "./checklist.md"
|
||||||
|
|
||||||
def read_json() -> dict:
|
def read_json() -> dict | None:
|
||||||
try:
|
try:
|
||||||
with open("./src/json/ingredients.json", "rt") as file:
|
with open("./src/json/ingredients.json", "rt") as file:
|
||||||
return json.load(file)
|
return json.load(file)
|
||||||
@@ -15,15 +15,20 @@ def read_json() -> dict:
|
|||||||
print("Could not find or read file.")
|
print("Could not find or read file.")
|
||||||
|
|
||||||
def update_json(meals: dict):
|
def update_json(meals: dict):
|
||||||
print("TODO: Add update logic")
|
try:
|
||||||
|
with open("./src/json/ingredients.json", "w") as file:
|
||||||
|
json.dump(meals, file, indent=4)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Unable to write data to file. {e}")
|
||||||
|
|
||||||
def get_selected_meals(meals: dict) -> dict:
|
def get_selected_meals(meals: dict) -> dict | None:
|
||||||
meal_json = read_json()
|
meal_json = read_json()
|
||||||
selected_meals = {}
|
selected_meals = {}
|
||||||
for meal in meals:
|
if meal_json:
|
||||||
if meal in meal_json:
|
for meal in meals:
|
||||||
selected_meals[meal] = meal_json[meal]
|
if meal in meal_json:
|
||||||
return selected_meals
|
selected_meals[meal] = meal_json[meal]
|
||||||
|
return selected_meals
|
||||||
|
|
||||||
def combine_ingredients(meals: dict) -> dict:
|
def combine_ingredients(meals: dict) -> dict:
|
||||||
combined_ingredients = {}
|
combined_ingredients = {}
|
||||||
@@ -37,9 +42,6 @@ def combine_ingredients(meals: dict) -> dict:
|
|||||||
return combined_ingredients
|
return combined_ingredients
|
||||||
|
|
||||||
def write_checklist(data: dict) -> None:
|
def write_checklist(data: dict) -> None:
|
||||||
if os.path.exists(file_path):
|
|
||||||
os.remove(file_path)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(file_path, 'w') as checklist:
|
with open(file_path, 'w') as checklist:
|
||||||
for ingredient, details in data.items():
|
for ingredient, details in data.items():
|
||||||
|
|||||||
Reference in New Issue
Block a user