70 lines
1.5 KiB
Python
Executable File
70 lines
1.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import json
|
|
import os
|
|
|
|
|
|
def graph_all(recipes: dict) -> None:
|
|
print("digraph G {")
|
|
for result, method in recipes.items():
|
|
for i in method[0]["input"]:
|
|
print(f"{i} -> {result}")
|
|
print("}")
|
|
|
|
|
|
def graph_one(recipes: dict, target: str) -> None:
|
|
print("digraph G {")
|
|
|
|
seen = set()
|
|
remain = set()
|
|
remain.add(target)
|
|
|
|
while remain:
|
|
output = remain.pop()
|
|
|
|
for need in recipes[output][0]['input']:
|
|
print(f"{need} -> {output}")
|
|
if need not in seen:
|
|
remain.add(need)
|
|
|
|
seen.add(output)
|
|
print("}")
|
|
|
|
|
|
def load_recipes(recipe_root: str) -> dict:
|
|
recipes = dict()
|
|
|
|
for dirname, dirs, files in os.walk(recipe_root):
|
|
for f in files:
|
|
path = os.path.join(dirname, f)
|
|
name, _ = os.path.splitext(f)
|
|
with open(path, 'r') as src:
|
|
variations = json.load(src)
|
|
|
|
recipes[name] = variations
|
|
|
|
return recipes
|
|
|
|
|
|
if __name__ == '__main__':
|
|
def main():
|
|
import argparse
|
|
|
|
root = os.path.dirname(__file__)
|
|
recipe_root = os.path.join(root, 'data', 'recipes')
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--data', type=str, default=recipe_root)
|
|
parser.add_argument('--target', type=str, required=False)
|
|
|
|
args = parser.parse_args()
|
|
|
|
recipes = load_recipes(args.data)
|
|
|
|
if args.target:
|
|
graph_one(recipes, args.target)
|
|
else:
|
|
graph_all(recipes)
|
|
|
|
main()
|