graph: load all recipes into a dedicated object

This commit is contained in:
Danny Robson 2019-04-28 09:08:03 +10:00
parent 4f90ad35ab
commit 44ee8eaeee

View File

@ -3,21 +3,35 @@
import json import json
import os import os
from typing import Iterable
def graph_all(recipes: dict) -> None: class Cookbook(object):
print("digraph G {") recipes: dict
for result, method in recipes.items():
for i in method[0]["input"]: def __init__(self, root: str):
print(f"{i} -> {result}") self.recipes = dict()
print("}")
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_one(recipes: dict, target: str) -> None: def graph(recipes: dict, targets: Iterable[str]):
print("digraph G {") print("digraph G {")
seen = set() seen = set()
remain = set() remain = set(targets)
remain.add(target)
while remain: while remain:
output = remain.pop() output = remain.pop()
@ -31,19 +45,12 @@ def graph_one(recipes: dict, target: str) -> None:
print("}") print("}")
def load_recipes(recipe_root: str) -> dict: def graph_all(recipes: dict) -> None:
recipes = dict() graph(recipes, recipes.all())
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 def graph_one(recipes: dict, target: str) -> None:
graph(recipes, [target])
return recipes
if __name__ == '__main__': if __name__ == '__main__':
@ -59,7 +66,7 @@ if __name__ == '__main__':
args = parser.parse_args() args = parser.parse_args()
recipes = load_recipes(args.data) recipes = Cookbook(args.data)
if args.target: if args.target:
graph_one(recipes, args.target) graph_one(recipes, args.target)