From 28fd5cba5e819fa989375ae6431492f81832fa84 Mon Sep 17 00:00:00 2001 From: Nicholas Kalar Date: Wed, 17 Dec 2025 23:16:29 -0500 Subject: [PATCH] Updated pathing --- src/FileHandler.py | 47 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/src/FileHandler.py b/src/FileHandler.py index 2e83bd3..db36fe7 100644 --- a/src/FileHandler.py +++ b/src/FileHandler.py @@ -1,20 +1,38 @@ -import os +from pathlib import Path import json -from dotenv import load_dotenv +working_directory = Path(__file__).parent -load_dotenv() +try: + with open(str(working_directory / "settings.json"), "rt") as settings_file: + settings = json.load(settings_file) + checklist_file_path = settings.get("checklist_storage_path", str(working_directory / "json")) + "/checklists.json" + ingredients_file_path = settings.get("meal_storage_path", str(working_directory / "json")) + "/ingredients.json" +except Exception as e: + print(e) + with open(str(working_directory / "settings.json"), "w") as settings_file: + settings = { + "meal_storage_path": str(working_directory / "json"), + "checklist_storage_path": str(working_directory / "json") + } + json.dump(settings, settings_file, indent=4) + checklist_file_path = settings["checklist_storage_path"] + "/checklists.json" + ingredients_file_path = settings["meal_storage_path"] + "/ingredients.json" -checklist_file_path = os.getenv("FILE_PATH") + "/checklist.md" if os.getenv("FILE_PATH") else "./checklist.md" - -ingredients_file_path = "./src/json/ingredients.json" - -def read_json() -> dict: +def read_ingredient_json() -> dict: try: with open(ingredients_file_path, "rt") as file: return json.load(file) except: - print("Could not find or read file.") + print("Could not find or read ingredients file.") + return {} + +def read_json_file(filename: str) -> dict: + try: + with open(filename, "rt") as file: + return json.load(file) + except: + print(f"Could not find or read file.\n{filename=}") return {} def update_json(meals: dict) -> None: @@ -24,8 +42,15 @@ def update_json(meals: dict) -> None: except Exception as e: print(f"Unable to write data to file. {e}") +def update_file(filename: str, content: dict) -> None: + try: + with open(filename, "w") as file: + json.dump(content, file, indent=4) + except Exception as e: + print(f"Unable to write data to file. {e}") + def get_selected_meals(meals: dict) -> dict: - meal_json = read_json() + meal_json = read_ingredient_json() selected_meals = {} if meal_json: for meal in meals: @@ -57,7 +82,7 @@ def write_checklist(data: dict) -> None: if __name__ == "__main__": - meals = read_json() + meals = read_ingredient_json() if meals: combined_ingredients = combine_ingredients(meals) write_checklist(combined_ingredients) \ No newline at end of file