/
pydotfactory.py
179 lines (164 loc) · 6.97 KB
/
pydotfactory.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Software License Agreement (BSD License)
#
# Copyright (c) 2008, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Willow Garage, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from distutils.version import LooseVersion
try:
from urllib.request import quote
except ImportError:
from urllib import quote
import os
import pydot
# Reference implementation for a dotcode factory
class PydotFactory():
def __init__(self):
pass
def escape_label(self, name):
if name in ['graph', 'subgraph', 'node', 'edge']:
ret = '%s_' % name
else:
ret = name
return ret
def escape_name(self, name):
ret = quote(name.strip())
ret = ret.replace('/', '__')
ret = ret.replace('%', '_')
ret = ret.replace('-', '_')
return self.escape_label(ret)
def get_graph(
self, graph_type='digraph', rank='same', simplify=True,
rankdir='TB', ranksep=0.2, compound=True):
# Lucid version of pydot bugs with certain settings, not sure which
# version exactly fixes those
if LooseVersion(pydot.__version__) > LooseVersion('1.0.10'):
graph = pydot.Dot('graphname',
graph_type=graph_type,
rank=rank,
rankdir=rankdir,
simplify=simplify
)
graph.set_ranksep(ranksep)
graph.set_compound(compound)
else:
graph = pydot.Dot('graphname',
graph_type=graph_type,
rank=rank,
rankdir=rankdir)
return graph
def add_node_to_graph(self,
graph,
nodename,
nodelabel=None,
shape='box',
color=None,
url=None,
tooltip=None):
"""
Create a node item for this factory, adds it to the graph.
Node name can vary from label but must always be same for the same node label
"""
if nodename is None or nodename == '':
raise ValueError('Empty Node name')
if nodelabel is None:
nodelabel = nodename
node = pydot.Node(self.escape_name(nodename))
node.set_shape(shape)
node.set_label(self.escape_label(nodelabel))
if tooltip is not None:
node.set_tooltip(tooltip)
elif url is not None:
node.set_tooltip(url)
if url is not None:
node.set_URL(self.escape_name(url))
if color is not None:
node.set_color(color)
graph.add_node(node)
def add_subgraph_to_graph(self,
graph,
subgraphname,
rank='same',
simplify=True,
rankdir='TB',
ranksep=0.2,
compound=True,
color=None,
shape='box',
style='bold',
subgraphlabel=None):
"""
Create a cluster subgraph item for this factory, adds it to the graph.
cluster name can vary from label but must always be same for the same node label.
Most layouters require cluster names to start with cluster.
"""
if subgraphname is None or subgraphname == '':
raise ValueError('Empty subgraph name')
g = pydot.Cluster(self.escape_name(subgraphname),
rank=rank, rankdir=rankdir, simplify=simplify)
if 'set_style' in g.__dict__:
g.set_style(style)
if 'set_shape' in g.__dict__:
g.set_shape(shape)
if LooseVersion(pydot.__version__) > LooseVersion('1.0.10'):
g.set_compound(compound)
g.set_ranksep(ranksep)
subgraphlabel = subgraphname if subgraphlabel is None else subgraphlabel
subgraphlabel = self.escape_label(subgraphlabel)
if subgraphlabel:
g.set_label(subgraphlabel)
if 'set_color' in g.__dict__:
if color is not None:
g.set_color(color)
graph.add_subgraph(g)
return g
def add_edge_to_graph(
self, graph, nodename1, nodename2, label=None, url=None,
simplify=True, style=None, penwidth=1, color=None):
if simplify and LooseVersion(pydot.__version__) < LooseVersion('1.0.10'):
if graph.get_edge(self.escape_name(nodename1), self.escape_name(nodename2)) != []:
return
edge = pydot.Edge(self.escape_name(nodename1), self.escape_name(nodename2))
if label is not None and label != '':
edge.set_label(label)
if url is not None:
edge.set_URL(self.escape_name(url))
if style is not None:
edge.set_style(style)
edge.obj_dict['attributes']['penwidth'] = str(penwidth)
if color is not None:
edge.obj_dict['attributes']['colorR'] = str(color[0])
edge.obj_dict['attributes']['colorG'] = str(color[1])
edge.obj_dict['attributes']['colorB'] = str(color[2])
graph.add_edge(edge)
def create_dot(self, graph):
dot = graph.create_dot()
if type(dot) != str:
dot = dot.decode()
# sadly pydot generates line wraps cutting between numbers
return dot.replace('\\%s' % os.linesep, '').replace('\\\n', '')