Adding support for sub-recipes.
This commit is contained in:
parent
38a181652a
commit
290eea8ec2
38
make_tree.py
38
make_tree.py
@ -1,17 +1,19 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
from anytree import Node, RenderTree
|
from anytree import Node, RenderTree
|
||||||
from anytree.exporter import JsonExporter
|
from anytree.exporter import DictExporter
|
||||||
|
|
||||||
|
|
||||||
def print_ingredients():
|
def print_nodes():
|
||||||
for i, node in ingredients.items():
|
for i, node in nodes.items():
|
||||||
print(f" {i}.) {node.result}")
|
print(f" {i}.) {node.result}")
|
||||||
|
|
||||||
|
|
||||||
ingredients = {}
|
nodes = {}
|
||||||
i = 0
|
i = 0
|
||||||
node = "start"
|
node = "start"
|
||||||
while node:
|
while node:
|
||||||
print_ingredients()
|
print_nodes()
|
||||||
|
|
||||||
node = input("Add node: ")
|
node = input("Add node: ")
|
||||||
|
|
||||||
@ -24,21 +26,31 @@ while node:
|
|||||||
new_node = Node(node, description=description, result=result)
|
new_node = Node(node, description=description, result=result)
|
||||||
|
|
||||||
for e in [int(key.strip()) for key in needs.split(",")]:
|
for e in [int(key.strip()) for key in needs.split(",")]:
|
||||||
ingredients.pop(e).parent = new_node
|
nodes.pop(e).parent = new_node
|
||||||
else:
|
else:
|
||||||
new_node = Node(node, description=description, result=node)
|
new_node = Node(node, description=description, result=node)
|
||||||
|
|
||||||
ingredients[i] = new_node
|
nodes[i] = new_node
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
for pre, _, node in RenderTree(new_node):
|
root_keys = nodes.keys()
|
||||||
print(f"{pre}{node.name}")
|
|
||||||
|
for key in root_keys:
|
||||||
|
for pre, _, node in RenderTree(nodes[key]):
|
||||||
|
print(f"{pre}{node.name}")
|
||||||
|
|
||||||
name = input("Recipe name: ")
|
name = input("Recipe name: ")
|
||||||
|
description = input("Recipe description: ")
|
||||||
if name:
|
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:
|
recipe = {"name": name, "description": description, "sub_recipes": sub_recipes}
|
||||||
JsonExporter(indent=2).write(new_node, f)
|
|
||||||
|
filename = name.replace(" ", "_").lower()
|
||||||
|
|
||||||
|
with open(f"{filename}.json", 'w') as f:
|
||||||
|
f.write(json.dumps(recipe))
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
from anytree import LevelOrderIter, LevelOrderGroupIter
|
from anytree import LevelOrderIter, LevelOrderGroupIter
|
||||||
from anytree.importer import JsonImporter
|
from anytree.importer import DictImporter
|
||||||
from sys import argv
|
from sys import argv
|
||||||
|
|
||||||
|
|
||||||
@ -37,38 +39,48 @@ if len(argv) < 2:
|
|||||||
exit()
|
exit()
|
||||||
|
|
||||||
with open(argv[1], 'r') as f:
|
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)):
|
print(f"<h1>{recipe_name}</h1>")
|
||||||
output_rows.append([])
|
print(f"<p>{recipe_desc}</p>")
|
||||||
|
print("<h2>Needs</h2>")
|
||||||
build_rows_depth_first(root, output_rows)
|
|
||||||
|
|
||||||
print("<h2>Ingredients</h2>")
|
|
||||||
print("<ul>")
|
print("<ul>")
|
||||||
|
|
||||||
for ingredient in root.leaves:
|
for sub_recipe in sub_recipes:
|
||||||
print(f" <li>{ingredient.name}</li>")
|
for ingredient in sub_recipe.leaves:
|
||||||
|
print(f" <li>{ingredient.name}</li>")
|
||||||
|
|
||||||
print("</ul>")
|
print("</ul>")
|
||||||
|
|
||||||
print("<h2>Steps</h2>")
|
print("<h2>Steps</h2>")
|
||||||
print("<ol>")
|
print("<ol>")
|
||||||
|
|
||||||
tasks = [node for node in LevelOrderIter(root) if not node.is_leaf]
|
for sub_recipe in sub_recipes:
|
||||||
for task in reversed(tasks):
|
tasks = [node for node in LevelOrderIter(sub_recipe) if not node.is_leaf]
|
||||||
print(f"<li>{task.description}</li>")
|
for task in reversed(tasks):
|
||||||
|
print(f" <li>{task.description if task.description else task.name}</li>")
|
||||||
|
|
||||||
print("</ol>")
|
print("</ol>")
|
||||||
|
|
||||||
print("<table cellspacing='0'>")
|
for sub_recipe in sub_recipes:
|
||||||
|
print("<table cellspacing='0'>")
|
||||||
|
|
||||||
for row in output_rows:
|
output_rows = []
|
||||||
print("<tr>")
|
|
||||||
for cell in row:
|
|
||||||
print(f" {cell}")
|
|
||||||
print("</tr>")
|
|
||||||
|
|
||||||
print("</table>")
|
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:
|
||||||
|
print(f" {cell}")
|
||||||
|
print("</tr>")
|
||||||
|
|
||||||
|
print("</table>")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user