Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
haidaraM committed Feb 13, 2022
1 parent 48597e7 commit af3bf40
Showing 1 changed file with 51 additions and 47 deletions.
98 changes: 51 additions & 47 deletions ansibleplaybookgrapher/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
):
Expand All @@ -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:
"""
Expand All @@ -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,
Expand Down Expand Up @@ -166,7 +166,6 @@ def render_block(
edge_counter: int,
edge: EdgeNode,
color: str,
label_prefix="",
**kwargs,
):
"""
Expand All @@ -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",
Expand All @@ -197,28 +209,17 @@ 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.
for b_counter, task_edge_node in enumerate(
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(
Expand All @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -320,43 +324,43 @@ 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] ",
)

# roles
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] ",
)

# post_tasks
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] ",
)

Expand Down

0 comments on commit af3bf40

Please sign in to comment.