-
Notifications
You must be signed in to change notification settings - Fork 1
/
dot.py
102 lines (79 loc) · 2.88 KB
/
dot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
Helpers for dot graph file generation (.gv). For a class to implement graph output,
it must simply subclass DotGraph, DotSubgraph and DotNode, and overwrite the
abstract dot_subgraphs(), dot_roots() and dot_edge_sets() methods.
Author: André Rösti
"""
def label_escape(instr):
if not isinstance(instr, str):
return instr
return instr.replace("<", "\\<").replace(">", "\\>")
class DotGraph:
def dot_subgraphs(self):
return []
def dot_repr(self):
i = 0
subgraphs = []
for j, subgraph in enumerate(self.dot_subgraphs()):
i, subgraph_code = subgraph.dot_subgraph_repr(j, i)
subgraphs.append(subgraph_code)
graph = """digraph G {{
{0:s}
}}""".format("\n".join(subgraphs))
return graph
class DotSubgraph(DotGraph):
def dot_roots(self):
return []
def dot_label(self):
return ""
def dot_subgraph_repr(self, j=0, i=0):
defs = []
links = []
for root in self.dot_roots():
i, these_defs, these_links = root.dot_repr(i)
defs.extend(these_defs)
links.extend(these_links)
subgraph_label = self.dot_label()
subgraph_code = """subgraph cluster_{0:d} {{
{1:s}
{2:s}
{3:s}
}}""".format(j, "\n".join(defs), "\n".join(links),
"label=\"{}\"".format(subgraph_label) if subgraph_label else "")
return i, subgraph_code
class DotNode:
def dot_edge_sets(self):
return []
def dot_label(self):
return ""
def dot_repr(self, i=0):
to_discover = {self}
to_draw = {} # dict node -> graph_id
while to_discover:
node = to_discover.pop()
identifier = "A_{}".format(i)
to_draw[node] = identifier
i += 1
for edge_set in node.dot_edge_sets():
for child in edge_set.children:
if child in to_draw:
continue # avoid infinite loops on cycles
to_discover.add(child)
defs = []
links = []
for node, identifier in to_draw.items():
defs.append('{0:s} [shape=record, label="{1}"];'
.format(identifier, node.dot_label()))
edge_sets = node.dot_edge_sets()
for edge_set in edge_sets:
for child in edge_set.children:
child_identifier = to_draw[child]
links.append('{0:s} -> {1:s} [{2:s}];'
.format(identifier, child_identifier, edge_set.get_prop_str()))
return i, defs, links
class DotEdgeSet:
def __init__(self, children, **props):
self.children = children
self.props = props
def get_prop_str(self):
return ", ".join('{}="{}"'.format(k, v) for k, v in self.props.items())