Adding support for sub-recipes.

This commit is contained in:
gamerdonkey 2025-09-10 18:17:38 +00:00
parent 38a181652a
commit 290eea8ec2
2 changed files with 58 additions and 34 deletions

View File

@ -1,17 +1,19 @@
import json
from anytree import Node, RenderTree
from anytree.exporter import JsonExporter
from anytree.exporter import DictExporter
def print_ingredients():
for i, node in ingredients.items():
def print_nodes():
for i, node in nodes.items():
print(f" {i}.) {node.result}")
ingredients = {}
nodes = {}
i = 0
node = "start"
while node:
print_ingredients()
print_nodes()
node = input("Add node: ")
@ -24,21 +26,31 @@ while node:
new_node = Node(node, description=description, result=result)
for e in [int(key.strip()) for key in needs.split(",")]:
ingredients.pop(e).parent = new_node
nodes.pop(e).parent = new_node
else:
new_node = Node(node, description=description, result=node)
ingredients[i] = new_node
nodes[i] = new_node
i += 1
for pre, _, node in RenderTree(new_node):
root_keys = nodes.keys()
for key in root_keys:
for pre, _, node in RenderTree(nodes[key]):
print(f"{pre}{node.name}")
name = input("Recipe name: ")
description = input("Recipe description: ")
if name:
name = name.replace(" ", "_")
sub_recipes = []
exporter = DictExporter()
for key in root_keys:
sub_recipes.append(exporter.export(nodes[key]))
with open(f"{name}.json", 'w') as f:
JsonExporter(indent=2).write(new_node, f)
recipe = {"name": name, "description": description, "sub_recipes": sub_recipes}
filename = name.replace(" ", "_").lower()
with open(f"{filename}.json", 'w') as f:
f.write(json.dumps(recipe))

View File

@ -1,5 +1,7 @@
import json
from anytree import LevelOrderIter, LevelOrderGroupIter
from anytree.importer import JsonImporter
from anytree.importer import DictImporter
from sys import argv
@ -37,19 +39,20 @@ if len(argv) < 2:
exit()
with open(argv[1], 'r') as f:
root = JsonImporter().read(f)
recipe = json.loads(f.read())
output_rows = []
recipe_name = recipe.get("name")
recipe_desc = recipe.get("description")
importer = DictImporter()
sub_recipes = [importer.import_(data) for data in recipe["sub_recipes"]]
for i in range(len(root.leaves)):
output_rows.append([])
build_rows_depth_first(root, output_rows)
print("<h2>Ingredients</h2>")
print(f"<h1>{recipe_name}</h1>")
print(f"<p>{recipe_desc}</p>")
print("<h2>Needs</h2>")
print("<ul>")
for ingredient in root.leaves:
for sub_recipe in sub_recipes:
for ingredient in sub_recipe.leaves:
print(f" <li>{ingredient.name}</li>")
print("</ul>")
@ -57,14 +60,23 @@ print("</ul>")
print("<h2>Steps</h2>")
print("<ol>")
tasks = [node for node in LevelOrderIter(root) if not node.is_leaf]
for sub_recipe in sub_recipes:
tasks = [node for node in LevelOrderIter(sub_recipe) if not node.is_leaf]
for task in reversed(tasks):
print(f"<li>{task.description}</li>")
print(f" <li>{task.description if task.description else task.name}</li>")
print("</ol>")
for sub_recipe in sub_recipes:
print("<table cellspacing='0'>")
output_rows = []
for i in range(len(sub_recipe.leaves)):
output_rows.append([])
build_rows_depth_first(sub_recipe, output_rows)
for row in output_rows:
print("<tr>")
for cell in row: