2019-04-27 18:50:04 +10:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
2019-04-28 09:08:03 +10:00
|
|
|
from typing import Iterable
|
2019-04-28 08:26:43 +10:00
|
|
|
|
2019-04-28 09:08:03 +10:00
|
|
|
class Cookbook(object):
|
|
|
|
recipes: dict
|
2019-04-27 18:50:04 +10:00
|
|
|
|
2019-04-28 09:08:03 +10:00
|
|
|
def __init__(self, root: str):
|
|
|
|
self.recipes = dict()
|
2019-04-27 18:50:04 +10:00
|
|
|
|
2019-04-28 09:08:03 +10:00
|
|
|
for dirname, dirs, files in os.walk(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)
|
|
|
|
|
|
|
|
self.recipes[name] = variations
|
|
|
|
|
|
|
|
def __getitem__(self, item: str):
|
|
|
|
return self.recipes[item]
|
|
|
|
|
|
|
|
def all(self):
|
|
|
|
return self.recipes.keys()
|
|
|
|
|
|
|
|
|
|
|
|
def graph(recipes: dict, targets: Iterable[str]):
|
2019-04-28 08:53:11 +10:00
|
|
|
print("digraph G {")
|
|
|
|
|
|
|
|
seen = set()
|
2019-04-28 09:08:03 +10:00
|
|
|
remain = set(targets)
|
2019-04-28 08:53:11 +10:00
|
|
|
|
|
|
|
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("}")
|
|
|
|
|
|
|
|
|
2019-04-28 09:08:03 +10:00
|
|
|
def graph_all(recipes: dict) -> None:
|
|
|
|
graph(recipes, recipes.all())
|
2019-04-27 18:50:04 +10:00
|
|
|
|
|
|
|
|
2019-04-28 09:08:03 +10:00
|
|
|
def graph_one(recipes: dict, target: str) -> None:
|
|
|
|
graph(recipes, [target])
|
2019-04-28 08:26:43 +10:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-04-28 08:53:11 +10:00
|
|
|
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()
|
|
|
|
|
2019-04-28 09:08:03 +10:00
|
|
|
recipes = Cookbook(args.data)
|
2019-04-28 08:53:11 +10:00
|
|
|
|
|
|
|
if args.target:
|
|
|
|
graph_one(recipes, args.target)
|
|
|
|
else:
|
|
|
|
graph_all(recipes)
|
|
|
|
|
|
|
|
main()
|