Skip to content

Commit 63fa668

Browse files
committed
Lint Kub-eidagrams script
1 parent a903b07 commit 63fa668

1 file changed

Lines changed: 46 additions & 15 deletions

File tree

bin/kube-diagrams

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import importlib
99
import json
1010
import os
1111
from pathlib import Path
12-
import pygraphviz
1312
import traceback
1413
from pprint import pprint
1514
import subprocess
1615
import sys
16+
import pygraphviz
1717
import yaml
1818
import diagrams
1919
from diagrams import Edge, Cluster
@@ -36,7 +36,17 @@ KUBEDIAGRAMS_URL = \
3636
# Conversion of dot files to Mermaid files
3737
#
3838

39-
def convert_dot_attributes_to_mermaid(command: str, name: str, attributes: dict, attr: str, ident: int, stream):
39+
def convert_dot_attributes_to_mermaid(
40+
keyword: str,
41+
name: str,
42+
attributes: dict,
43+
attr: str,
44+
ident: int,
45+
stream
46+
):
47+
"""
48+
Convert dot attributes to Mermaid attributes.
49+
"""
4050
mappings = []
4151
if attr is not None:
4252
mappings.append(attr)
@@ -57,15 +67,28 @@ def convert_dot_attributes_to_mermaid(command: str, name: str, attributes: dict,
5767
if "dashed" in av:
5868
mappings.append("stroke-dasharray:7 7")
5969
if len(mappings) > 0:
60-
print(" "*ident, command, " ", name, " ", ",".join(mappings), sep="", file=stream)
61-
62-
def convert_dot_subgraph_to_mermaid(graph: pygraphviz.AGraph, already_created_nodes: set, ident: int, stream):
70+
print(" "*ident, keyword, " ", name, " ", ",".join(mappings), sep="", file=stream)
71+
72+
def convert_dot_subgraph_to_mermaid(
73+
graph: pygraphviz.AGraph,
74+
already_created_nodes: set,
75+
ident: int,
76+
stream
77+
):
78+
"""
79+
Convert a dot subgraph to a Mermaid subgraph.
80+
"""
6381
for subgraph in graph.subgraphs():
6482
name = subgraph.name.replace(":", "").replace(" ", "_")
65-
print(" "*ident, "subgraph ", name, " [", subgraph.graph_attr["label"], "]", sep="", file=stream)
83+
print(" "*ident, "subgraph ", name, " [", subgraph.graph_attr["label"],
84+
"]", sep="", file=stream)
6685
print(" "*(ident+1), "direction TB", sep="", file=stream)
67-
convert_dot_attributes_to_mermaid("style", name, subgraph.graph_attr, None, ident+1, stream)
68-
convert_dot_subgraph_to_mermaid(subgraph, already_created_nodes, ident+1, stream)
86+
convert_dot_attributes_to_mermaid(
87+
"style", name, subgraph.graph_attr, None, ident+1, stream
88+
)
89+
convert_dot_subgraph_to_mermaid(
90+
subgraph, already_created_nodes, ident+1, stream
91+
)
6992
print(" "*ident, "end", sep="", file=stream)
7093
for nid in graph.nodes():
7194
if nid not in already_created_nodes:
@@ -74,20 +97,26 @@ def convert_dot_subgraph_to_mermaid(graph: pygraphviz.AGraph, already_created_no
7497
url = node.attr["image"].replace(DIAGRAMS_PATH, DIAGRAMS_URL)
7598
url = url.replace(KUBEDIAGRAMS_PATH, KUBEDIAGRAMS_URL)
7699
label = node.attr["label"].replace("\n", "")
77-
print(" "*ident, node.name, "@{ img: \"", url, "\", label: \"", label, "\", h: 120, constraint: \"on\" }", sep="", file=stream)
78-
convert_dot_attributes_to_mermaid("style", node.name, node.attr, "fill:none,stroke:none", ident, stream)
100+
print(" "*ident, node.name, "@{ img: \"", url, "\", label: \"",
101+
label, "\", h: 120, constraint: \"on\" }", sep="", file=stream)
102+
convert_dot_attributes_to_mermaid(
103+
"style", node.name, node.attr, "fill:none,stroke:none", ident, stream
104+
)
79105

80-
def convert_dot_to_mermaid(dot_filename, mermaid_filename):
106+
def convert_dot_to_mermaid(a_dot_filename, a_mermaid_filename):
107+
"""
108+
Convert a dot file to a Mermaid file.
109+
"""
81110
graph = pygraphviz.AGraph()
82-
graph.read(dot_filename)
83-
with open(mermaid_filename, "wt") as stream:
111+
graph.read(a_dot_filename)
112+
with open(a_mermaid_filename, "wt", encoding="utf-8") as stream:
84113
print("flowchart TB", file=stream)
85114
convert_dot_subgraph_to_mermaid(graph, set(), 1, stream)
86115
for eid, edge in enumerate(graph.edges()):
87116
if edge.attr.get("xlabel") != "":
88117
edge_dir = {
89118
"forward": "-- \"{}\" -->",
90-
#TODO "back": "-- \"{}\" --",
119+
#TO DO "back": "-- \"{}\" --",
91120
"both": "<-- \"{}\" -->",
92121
}[edge.attr["dir"]].format(edge.attr["xlabel"])
93122
else:
@@ -98,7 +127,9 @@ def convert_dot_to_mermaid(dot_filename, mermaid_filename):
98127
}[edge.attr["dir"]]
99128
print(" ", edge[0], edge_dir, edge[1], file=stream)
100129
edge_color = edge.attr.get("color") or graph.edge_attr["color"]
101-
convert_dot_attributes_to_mermaid("linkStyle", eid, edge.attr, f"stroke:{edge_color}", 1, stream)
130+
convert_dot_attributes_to_mermaid(
131+
"linkStyle", eid, edge.attr, f"stroke:{edge_color}", 1, stream
132+
)
102133

103134
# According to https://github.com/yaml/pyyaml/issues/89, PyYAML raises
104135
# yaml.constructor.ConstructorError: could not determine a constructor for

0 commit comments

Comments
 (0)