1
#!/usr/bin/env python2.4
2
from subprocess import Popen, PIPE
3
from urllib import urlencode
4
from xml.sax.saxutils import escape
10
RSVG_OUTPUT_TYPES = ('png', 'jpg')
11
DOT_OUTPUT_TYPES = ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png',
14
class NoDot(Exception):
16
Exception.__init__(self, "Can't find dot!")
18
class NoRsvg(Exception):
20
Exception.__init__(self, "Can't find rsvg!")
23
def __init__(self, name, color=None, label=None, rev_id=None,
24
cluster=None, node_style=None):
30
if node_style is None:
32
self.cluster = cluster
39
if self.color is not None:
40
attributes.append('fillcolor="%s"' % self.color)
41
style.append('filled')
42
style.extend(self.node_style)
44
attributes.append('style="%s"' % ",".join(style))
47
attributes.append('label="%s"' % label)
48
attributes.append('shape="box"')
49
if self.href is not None:
50
attributes.append('href="%s"' % self.href)
51
if len(attributes) > 0:
52
return '%s[%s]' % (self.name, " ".join(attributes))
58
def __init__(self, start, end, label=None):
64
def dot(self, do_weight=False):
66
if self.label is not None:
67
attributes.append(('label', self.label))
70
if self.start.cluster == self.end.cluster:
72
elif self.start.rank is None:
74
elif self.end.rank is None:
76
attributes.append(('weight', weight))
77
if len(attributes) > 0:
79
for key, value in attributes:
80
atlist.append("%s=\"%s\"" % (key, value))
85
return "%s->%s%s;" % (self.start.name, self.end.name, op)
87
def make_edge(relation):
88
if hasattr(relation, 'start') and hasattr(relation, 'end'):
90
return Edge(relation[0], relation[1])
92
def dot_output(relations, ranking="forced"):
97
edges = [make_edge(f) for f in relations]
98
def rel_appropriate(start, end, cluster):
100
return (start.cluster is None and end.cluster is None) or \
101
start.cluster != end.cluster
103
return start.cluster==cluster and end.cluster==cluster
106
if edge.start.cluster is not None:
107
clusters.add(edge.start.cluster)
108
if edge.end.cluster is not None:
109
clusters.add(edge.end.cluster)
110
clusters = list(clusters)
111
clusters.append(None)
112
for index, cluster in enumerate(clusters):
113
if cluster is not None and ranking == "cluster":
114
yield "subgraph cluster_%s\n" % index
116
yield ' label="%s"\n' % cluster
118
if edge.start.name not in defined and edge.start.cluster == cluster:
119
defined[edge.start.name] = edge.start
120
my_def = edge.start.define()
121
if my_def is not None:
122
yield " %s\n" % my_def
123
if edge.end.name not in defined and edge.end.cluster == cluster:
124
defined[edge.end.name] = edge.end
125
my_def = edge.end.define()
126
if my_def is not None:
127
yield " %s;\n" % my_def
128
if rel_appropriate(edge.start, edge.end, cluster):
129
yield " %s\n" % edge.dot(do_weight=ranking=="forced")
130
if cluster is not None and ranking == "cluster":
133
if ranking == "forced":
135
for node in defined.itervalues():
136
if node.rank not in ranks:
137
ranks[node.rank] = set()
138
ranks[node.rank].add(node.name)
139
sorted_ranks = [n for n in ranks.iteritems()]
142
for rank, nodes in sorted_ranks:
145
yield 'rank%d[style="invis"];\n' % rank
146
if last_rank is not None:
147
yield 'rank%d -> rank%d[style="invis"];\n' % (last_rank, rank)
149
for rank, nodes in ranks.iteritems():
152
node_text = "; ".join('"%s"' % n for n in nodes)
153
yield ' {rank = same; "rank%d"; %s}\n' % (rank, node_text)
156
def invoke_dot_aa(input, out_file, file_type='png'):
158
Produce antialiased Dot output, invoking rsvg on an intermediate file.
159
rsvg only supports png, jpeg and .ico files."""
160
tempdir = tempfile.mkdtemp()
162
temp_file = os.path.join(tempdir, 'temp.svg')
163
invoke_dot(input, temp_file, 'svg')
164
cmdline = ['rsvg', temp_file, out_file]
166
rsvg_proc = Popen(cmdline)
168
if e.errno == errno.ENOENT:
170
status = rsvg_proc.wait()
172
shutil.rmtree(tempdir)
175
def invoke_dot(input, out_file=None, file_type='svg', antialias=None,
176
fontname="Helvetica", fontsize=11):
177
cmdline = ['dot', '-T%s' % file_type, '-Nfontname=%s' % fontname,
178
'-Efontname=%s' % fontname, '-Nfontsize=%d' % fontsize,
179
'-Efontsize=%d' % fontsize]
180
if out_file is not None:
181
cmdline.extend(('-o', out_file))
183
dot_proc = Popen(cmdline, stdin=PIPE)
185
if e.errno == errno.ENOENT:
190
dot_proc.stdin.write(line)
191
dot_proc.stdin.close()
192
return dot_proc.wait()