-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage_recipes.py
157 lines (138 loc) · 5.07 KB
/
manage_recipes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import json
import sys
import subprocess
import os
FILE_PATH = "recipes.json"
def load_data():
try:
with open(FILE_PATH, "r") as file:
data = json.load(file)
return data
except FileNotFoundError:
return {"recipes": []}
def save_data(data):
with open(FILE_PATH, "w") as file:
json.dump(data, file, indent=4)
def edit_text(name):
while True:
if sys.platform == "nt":
subprocess.call(["hecto.exe", name])
else:
subprocess.call(["nano", "-R", "-m", "-n", name])
try:
with open(name, "r") as f:
text = f.read()
assert text
break
except Exception as e:
text = ""
break
return text
def list_recipes(data, parent_name=None):
for i, recipe in enumerate(data["recipes"], start=1):
if parent_name is None:
print(f"{i}. {recipe['name']} - {recipe['desc']}")
else:
print(f"{parent_name} -> {i}. {recipe['name']} - {recipe['desc']}")
def create_recipe(data):
name = input("Enter the name of the recipe: ")
desc = input("Enter the description: ")
is_subrecipe = False #input("Is it a sub-recipe? (yes/no): ").lower() == "yes"
if is_subrecipe:
parent_index = int(input("Enter the index of the parent recipe: ")) - 1
parent_recipe = data["recipes"][parent_index]
parent_recipe.setdefault("subrecipes", []).append({"name": name, "desc": desc})
save_data(data)
print("Sub-recipe added successfully.")
else:
print("Enter the ingredients, one per line, then save and exit.")
input("Press Enter to start.")
text=edit_text('name')
ingredients=text.splitlines()
print("Enter the instructions, then save and exit.")
input("Press Enter to start.")
instructions=edit_text('name')
recipe = {
"name": name,
"desc": desc,
"ingredients": [ing.strip() for ing in ingredients],
"instructions": instructions
}
data["recipes"].append(recipe)
save_data(data)
print("Recipe added successfully.")
def read_recipe(data, index):
try:
recipe = data["recipes"][index]
print(f"Name: {recipe['name']}")
print(f"Description: {recipe['desc']}")
print("Ingredients:")
for ingredient in recipe.get("ingredients", []):
print(f"- {ingredient}")
print(f"Instructions: {recipe['instructions']}")
if "subrecipes" in recipe:
print("Sub-recipes:")
list_recipes({"recipes": recipe["subrecipes"]}, parent_name=recipe["name"])
except IndexError:
print("Recipe not found.")
def update_recipe(data, index):
try:
recipe = data["recipes"][index]
print(f"Editing {recipe['name']}:")
name = input("Enter the name of the recipe: ")
recipe["name"] = name if name else recipe["name"]
desc = input(f"Enter the description (Current: {recipe['desc']}): ")
recipe["desc"] = desc if desc else recipe["desc"]
if "ingredients" in recipe:
print("Enter the ingredients, one per line, then save and exit.")
input("Press Enter to start.")
text=edit_text('name')
recipe["ingredients"]=text.splitlines()
if "instructions" in recipe:
print("Enter the instructions, then save and exit.")
input("Press Enter to start.")
recipe["instructions"]=edit_text('name')
save_data(data)
print("Recipe updated successfully.")
except IndexError:
print("Recipe not found.")
def delete_recipe(data, index):
try:
recipe = data["recipes"][index]
confirmation = input(f"Are you sure you want to delete {recipe['name']}? (yes/no): ")
if confirmation.lower() == "yes":
del data["recipes"][index]
save_data(data)
print("Recipe deleted successfully.")
else:
print("Deletion canceled.")
except IndexError:
print("Recipe not found.")
if __name__ == "__main__":
data = load_data()
while True:
print("\nRecipe Manager\n")
print("1. List recipes")
print("2. Create a recipe")
print("3. Read a recipe")
print("4. Update a recipe")
print("5. Delete a recipe")
print("6. Exit")
choice = input("\nEnter your choice: ")
if choice == "1":
list_recipes(data)
elif choice == "2":
create_recipe(data)
elif choice == "3":
index = int(input("Enter the index of the recipe: ")) - 1
read_recipe(data, index)
elif choice == "4":
index = int(input("Enter the index of the recipe: ")) - 1
update_recipe(data, index)
elif choice == "5":
index = int(input("Enter the index of the recipe: ")) - 1
delete_recipe(data, index)
elif choice == "6":
break
else:
print("Invalid choice. Please try again.")