57 рядки
1.3 KiB
Python
57 рядки
1.3 KiB
Python
import json
|
|
|
|
from anytree import Node, RenderTree
|
|
from anytree.exporter import DictExporter
|
|
|
|
|
|
def print_nodes():
|
|
for i, node in nodes.items():
|
|
print(f" {i}.) {node.result}")
|
|
|
|
|
|
nodes = {}
|
|
i = 0
|
|
node = "start"
|
|
while node:
|
|
print_nodes()
|
|
|
|
node = input("Add node: ")
|
|
|
|
if node:
|
|
needs = input("List needs (comma-separated): ")
|
|
description = input("Detailed description: ")
|
|
|
|
if needs:
|
|
result = input("Result of node (e.g. chopped carrots): ")
|
|
new_node = Node(node, description=description, result=result)
|
|
|
|
for e in [int(key.strip()) for key in needs.split(",")]:
|
|
nodes.pop(e).parent = new_node
|
|
else:
|
|
new_node = Node(node, description=description, result=node)
|
|
|
|
nodes[i] = new_node
|
|
i += 1
|
|
|
|
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:
|
|
sub_recipes = []
|
|
exporter = DictExporter()
|
|
for key in root_keys:
|
|
sub_recipes.append(exporter.export(nodes[key]))
|
|
|
|
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))
|
|
|