From af3bf4066261a1dda82398127ee54ee495edb005 Mon Sep 17 00:00:00 2001 From: Mohamed El Mouctar HAIDARA Date: Sun, 13 Feb 2022 12:56:16 +0100 Subject: [PATCH] refactoring --- ansibleplaybookgrapher/renderer.py | 98 ++++++++++++++++-------------- 1 file changed, 51 insertions(+), 47 deletions(-) diff --git a/ansibleplaybookgrapher/renderer.py b/ansibleplaybookgrapher/renderer.py index f50c748a..1000c6df 100644 --- a/ansibleplaybookgrapher/renderer.py +++ b/ansibleplaybookgrapher/renderer.py @@ -112,9 +112,9 @@ def render(self, output_filename: str, save_dot_file=False, view=False) -> str: def render_node( self, graph: Digraph, + edge_counter: int, edge: EdgeNode, color: str, - node_counter: int, shape: str = "octagon", **kwargs, ): @@ -123,7 +123,7 @@ def render_node( :param graph: The graph to render the node to :param edge: The edge from a node to the Node :param color: The color to apply - :param node_counter: The counter for this node + :param edge_counter: The counter for this node :param shape: the default shape of the node :return: """ @@ -132,12 +132,12 @@ def render_node( node_label_prefix = kwargs.get("node_label_prefix", "") if isinstance(destination_node, BlockNode): - self.render_block(graph, node_counter, edge, color) + self.render_block(graph, edge_counter, edge, color) elif isinstance(destination_node, RoleNode): - self.render_role(graph, node_counter, edge, color) + self.render_role(graph, edge_counter, edge, color) else: # Here we have a TaskNode - edge_label = f"{node_counter} {edge.name}" + edge_label = f"{edge_counter} {edge.name}" # Task node graph.node( destination_node.id, @@ -166,7 +166,6 @@ def render_block( edge_counter: int, edge: EdgeNode, color: str, - label_prefix="", **kwargs, ): """ @@ -177,17 +176,30 @@ def render_block( :param edge: The edge from a node to the BlockNode :param color: The color to apply :param kwargs: - :param label_prefix: A prefix to add to the node label :return: """ # noinspection PyTypeChecker destination_node = edge.destination # type: BlockNode edge_label = f"{edge_counter}" + # Edge from parent to the block node inside the cluster + graph.edge( + edge.source.id, + destination_node.id, + label=edge_label, + color=color, + fontcolor=color, + tooltip=edge_label, + id=edge.id, + labeltooltip=edge_label, + ) + # BlockNode is a special node: a cluster is created instead of a normal node - with graph.subgraph(name=f"cluster_{destination_node.id}") as block_subgraph: + with graph.subgraph( + name=f"cluster_{destination_node.id}" + ) as cluster_block_subgraph: # block node - block_subgraph.node( + cluster_block_subgraph.node( destination_node.id, label=f"[block] {destination_node.name}", shape="box", @@ -197,17 +209,6 @@ def render_block( labeltooltip=destination_node.name, URL=self.get_node_url(destination_node, "file"), ) - # Edge from parent to block node inside the cluster - graph.edge( - edge.source.id, - destination_node.id, - label=edge_label, - color=color, - fontcolor=color, - tooltip=edge_label, - id=edge.id, - labeltooltip=edge_label, - ) # The reverse here is a little hack due to how graphviz render nodes inside a cluster by reversing them. # Don't really know why for the moment neither if there is an attribute to change that. @@ -215,10 +216,10 @@ def render_block( reversed(destination_node.tasks) ): self.render_node( - block_subgraph, + cluster_block_subgraph, + len(destination_node.tasks) - b_counter, task_edge_node, color, - len(destination_node.tasks) - b_counter, ) def render_role( @@ -242,6 +243,18 @@ def render_role( else: # For normal role invocation, we point to the folder url = self.get_node_url(role, "folder") + # from parent to the role node + graph.edge( + edge.source.id, + role.id, + label=role_edge_label, + color=color, + fontcolor=color, + id=edge.id, + tooltip=role_edge_label, + labeltooltip=role_edge_label, + ) + with graph.subgraph(name=role.name, node_attr={}) as role_subgraph: role_subgraph.node( role.id, @@ -251,22 +264,13 @@ def render_role( color=color, URL=url, ) - # from parent to role - graph.edge( - edge.source.id, - role.id, - label=role_edge_label, - color=color, - fontcolor=color, - id=edge.id, - tooltip=role_edge_label, - labeltooltip=role_edge_label, - ) - # role tasks for role_task_counter, role_task_edge in enumerate(role.tasks, 1): self.render_node( - role_subgraph, role_task_edge, color, node_counter=role_task_counter + role_subgraph, + edge_counter=role_task_counter, + edge=role_task_edge, + color=color, ) def _convert_to_graphviz(self): @@ -320,9 +324,9 @@ def _convert_to_graphviz(self): for pre_task_counter, pre_task_edge in enumerate(play.pre_tasks, 1): self.render_node( play_subgraph, - pre_task_edge, - color, - node_counter=pre_task_counter, + edge_counter=pre_task_counter, + edge=pre_task_edge, + color=color, node_label_prefix="[pre_task] ", ) @@ -330,20 +334,20 @@ def _convert_to_graphviz(self): for role_counter, role_edge in enumerate(play.roles, 1): self.render_role( play_subgraph, - role_counter + len(play.pre_tasks), - role_edge, - color, + edge_counter=role_counter + len(play.pre_tasks), + edge=role_edge, + color=color, ) # tasks for task_counter, task_edge in enumerate(play.tasks, 1): self.render_node( play_subgraph, - task_edge, - color, - node_counter=len(play.pre_tasks) + edge_counter=len(play.pre_tasks) + len(play.roles) + task_counter, + edge=task_edge, + color=color, node_label_prefix="[task] ", ) @@ -351,12 +355,12 @@ def _convert_to_graphviz(self): for post_task_counter, post_task_edge in enumerate(play.post_tasks, 1): self.render_node( play_subgraph, - post_task_edge, - color, - node_counter=len(play.pre_tasks) + edge_counter=len(play.pre_tasks) + len(play.roles) + len(play.tasks) + post_task_counter, + edge=post_task_edge, + color=color, node_label_prefix="[post_task] ", )