From d3b782f1a108995701f69167d2a9a8ddf7cc3387 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sun, 28 Apr 2019 08:53:11 +1000 Subject: [PATCH] graph: add a command to graph just one item --- graph.py | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/graph.py b/graph.py index 2b3a021..dabb5d0 100755 --- a/graph.py +++ b/graph.py @@ -12,6 +12,25 @@ def graph_all(recipes: dict) -> None: 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() @@ -28,6 +47,23 @@ def load_recipes(recipe_root: str) -> dict: if __name__ == '__main__': - root = os.path.dirname(__file__) - recipe_root = os.path.join(root, 'data', 'recipes') - graph_all(load_recipes(recipe_root)) + 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()