| 1 | # Copyright (c) 2000-2002 LOGILAB S.A. (Paris, FRANCE). |
|---|
| 2 | # http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|---|
| 3 | # |
|---|
| 4 | # This program is free software; you can redistribute it and/or modify it under |
|---|
| 5 | # the terms of the GNU General Public License as published by the Free Software |
|---|
| 6 | # Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 7 | # version. |
|---|
| 8 | # |
|---|
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 12 | # |
|---|
| 13 | # You should have received a copy of the GNU General Public License along with |
|---|
| 14 | # this program; if not, write to the Free Software Foundation, Inc., |
|---|
| 15 | # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 16 | """ |
|---|
| 17 | utilities functions to generate file readable with Georg Sander's vcg |
|---|
| 18 | (Visualization of Compiler Graphs). |
|---|
| 19 | |
|---|
| 20 | You can download vcg at http://rw4.cs.uni-sb.de/~sander/html/gshome.html |
|---|
| 21 | Note that vcg exists as a debian package. |
|---|
| 22 | |
|---|
| 23 | See the documentation of vcg for explanation about the different value that |
|---|
| 24 | maybe used for the functions parameters |
|---|
| 25 | """ |
|---|
| 26 | |
|---|
| 27 | __revision__ = "$Id: vcgutils.py,v 1.6 2003-12-10 08:15:09 syt Exp $" |
|---|
| 28 | |
|---|
| 29 | import string |
|---|
| 30 | |
|---|
| 31 | ATTRS_VAL = { |
|---|
| 32 | 'algos': ('dfs', 'tree', 'minbackward', |
|---|
| 33 | 'left_to_right','right_to_left', |
|---|
| 34 | 'top_to_bottom','bottom_to_top', |
|---|
| 35 | 'maxdepth', 'maxdepthslow', 'mindepth', 'mindepthslow', |
|---|
| 36 | 'mindegree', 'minindegree', 'minoutdegree', |
|---|
| 37 | 'maxdegree','maxindegree', 'maxoutdegree'), |
|---|
| 38 | 'booleans': ('yes', 'no'), |
|---|
| 39 | 'colors': ('black', 'white', 'blue', 'red', 'green', 'yellow', |
|---|
| 40 | 'magenta', 'lightgrey', |
|---|
| 41 | 'cyan', 'darkgrey', 'darkblue', 'darkred', 'darkgreen', |
|---|
| 42 | 'darkyellow', 'darkmagenta', 'darkcyan', 'gold', |
|---|
| 43 | 'lightblue', 'lightred', 'lightgreen', 'lightyellow', |
|---|
| 44 | 'lightmagenta', 'lightcyan', 'lilac', 'turquoise', |
|---|
| 45 | 'aquamarine', 'khaki', 'purple', 'yellowgreen', 'pink', |
|---|
| 46 | 'orange', 'orchid'), |
|---|
| 47 | 'shapes': ('box', 'ellipse', 'rhomb', 'triangle'), |
|---|
| 48 | 'textmodes': ('center', 'left_justify', 'right_justify'), |
|---|
| 49 | 'arrowstyles': ('solid', 'line', 'none'), |
|---|
| 50 | 'linestyles': ('continuous', 'dashed', 'dotted', 'invisible'), |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | # meaning of possible values: |
|---|
| 54 | # O -> string |
|---|
| 55 | # 1 -> int |
|---|
| 56 | # list -> value in list |
|---|
| 57 | GRAPH_ATTRS = { |
|---|
| 58 | 'title' : 0, |
|---|
| 59 | 'label' : 0, |
|---|
| 60 | 'color': ATTRS_VAL['colors'], |
|---|
| 61 | 'textcolor': ATTRS_VAL['colors'], |
|---|
| 62 | 'bordercolor': ATTRS_VAL['colors'], |
|---|
| 63 | 'width': 1, |
|---|
| 64 | 'height': 1, |
|---|
| 65 | 'borderwidth': 1, |
|---|
| 66 | 'textmode': ATTRS_VAL['textmodes'], |
|---|
| 67 | 'shape': ATTRS_VAL['shapes'], |
|---|
| 68 | 'shrink': 1, |
|---|
| 69 | 'stretch': 1, |
|---|
| 70 | 'orientation': ATTRS_VAL['algos'], |
|---|
| 71 | 'vertical_order': 1, |
|---|
| 72 | 'horizontal_order': 1, |
|---|
| 73 | 'xspace': 1, |
|---|
| 74 | 'yspace': 1, |
|---|
| 75 | 'layoutalgorithm' : ATTRS_VAL['algos'], |
|---|
| 76 | 'late_edge_labels' : ATTRS_VAL['booleans'], |
|---|
| 77 | 'display_edge_labels': ATTRS_VAL['booleans'], |
|---|
| 78 | 'dirty_edge_labels' : ATTRS_VAL['booleans'], |
|---|
| 79 | 'finetuning': ATTRS_VAL['booleans'], |
|---|
| 80 | 'manhattan_edges': ATTRS_VAL['booleans'], |
|---|
| 81 | 'smanhattan_edges': ATTRS_VAL['booleans'], |
|---|
| 82 | 'port_sharing': ATTRS_VAL['booleans'], |
|---|
| 83 | 'edges': ATTRS_VAL['booleans'], |
|---|
| 84 | 'nodes': ATTRS_VAL['booleans'], |
|---|
| 85 | 'splines': ATTRS_VAL['booleans'], |
|---|
| 86 | } |
|---|
| 87 | NODE_ATTRS = { |
|---|
| 88 | 'title' : 0, |
|---|
| 89 | 'label' : 0, |
|---|
| 90 | 'color': ATTRS_VAL['colors'], |
|---|
| 91 | 'textcolor': ATTRS_VAL['colors'], |
|---|
| 92 | 'bordercolor': ATTRS_VAL['colors'], |
|---|
| 93 | 'width': 1, |
|---|
| 94 | 'height': 1, |
|---|
| 95 | 'borderwidth': 1, |
|---|
| 96 | 'textmode': ATTRS_VAL['textmodes'], |
|---|
| 97 | 'shape': ATTRS_VAL['shapes'], |
|---|
| 98 | 'shrink': 1, |
|---|
| 99 | 'stretch': 1, |
|---|
| 100 | 'vertical_order': 1, |
|---|
| 101 | 'horizontal_order': 1, |
|---|
| 102 | } |
|---|
| 103 | EDGE_ATTRS = { |
|---|
| 104 | 'sourcename' : 0, |
|---|
| 105 | 'targetname' : 0, |
|---|
| 106 | 'label' : 0, |
|---|
| 107 | 'linestyle' : ATTRS_VAL['linestyles'], |
|---|
| 108 | 'class' : 1, |
|---|
| 109 | 'thickness' : 0, |
|---|
| 110 | 'color': ATTRS_VAL['colors'], |
|---|
| 111 | 'textcolor': ATTRS_VAL['colors'], |
|---|
| 112 | 'arrowcolor': ATTRS_VAL['colors'], |
|---|
| 113 | 'backarrowcolor': ATTRS_VAL['colors'], |
|---|
| 114 | 'arrowsize': 1, |
|---|
| 115 | 'backarrowsize': 1, |
|---|
| 116 | 'arrowstyle': ATTRS_VAL['arrowstyles'], |
|---|
| 117 | 'backarrowstyle': ATTRS_VAL['arrowstyles'], |
|---|
| 118 | 'textmode': ATTRS_VAL['textmodes'], |
|---|
| 119 | 'priority': 1, |
|---|
| 120 | 'anchor': 1, |
|---|
| 121 | 'horizontal_order': 1, |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | # Misc utilities ############################################################### |
|---|
| 126 | |
|---|
| 127 | def latin_to_vcg(st): |
|---|
| 128 | """convert latin characters using vcg escape sequence |
|---|
| 129 | """ |
|---|
| 130 | for char in st: |
|---|
| 131 | if char not in string.ascii_letters: |
|---|
| 132 | try: |
|---|
| 133 | num = ord(char) |
|---|
| 134 | if num >= 192: |
|---|
| 135 | st = st.replace(char, r'\fi%d'%ord(char)) |
|---|
| 136 | except: |
|---|
| 137 | pass |
|---|
| 138 | return st |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | class VCGPrinter: |
|---|
| 142 | """a vcg graph writer |
|---|
| 143 | """ |
|---|
| 144 | |
|---|
| 145 | def __init__(self, output_stream): |
|---|
| 146 | self._stream = output_stream |
|---|
| 147 | self._indent = '' |
|---|
| 148 | |
|---|
| 149 | def open_graph(self, **args): |
|---|
| 150 | """open a vcg graph |
|---|
| 151 | """ |
|---|
| 152 | self._stream.write('%sgraph:{\n'%self._indent) |
|---|
| 153 | self._inc_indent() |
|---|
| 154 | self._write_attributes(GRAPH_ATTRS, **args) |
|---|
| 155 | |
|---|
| 156 | def close_graph(self): |
|---|
| 157 | """close a vcg graph |
|---|
| 158 | """ |
|---|
| 159 | self._dec_indent() |
|---|
| 160 | self._stream.write('%s}\n'%self._indent) |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | def node(self, title, **args): |
|---|
| 164 | """draw a node |
|---|
| 165 | """ |
|---|
| 166 | self._stream.write('%snode: {title:"%s"' % (self._indent, title)) |
|---|
| 167 | self._write_attributes(NODE_ATTRS, **args) |
|---|
| 168 | self._stream.write('}\n') |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | def edge(self, from_node, to_node, edge_type='', **args): |
|---|
| 172 | """draw an edge from a node to another. |
|---|
| 173 | """ |
|---|
| 174 | self._stream.write( |
|---|
| 175 | '%s%sedge: {sourcename:"%s" targetname:"%s"' % ( |
|---|
| 176 | self._indent, edge_type, from_node, to_node)) |
|---|
| 177 | self._write_attributes(EDGE_ATTRS, **args) |
|---|
| 178 | self._stream.write('}\n') |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | # private ################################################################## |
|---|
| 182 | |
|---|
| 183 | def _write_attributes(self, attributes_dict, **args): |
|---|
| 184 | """write graph, node or edge attributes |
|---|
| 185 | """ |
|---|
| 186 | for key, value in args.items(): |
|---|
| 187 | try: |
|---|
| 188 | _type = attributes_dict[key] |
|---|
| 189 | except KeyError: |
|---|
| 190 | raise Exception('''no such attribute %s |
|---|
| 191 | possible attributes are %s''' % (key, attributes_dict.keys())) |
|---|
| 192 | |
|---|
| 193 | if not _type: |
|---|
| 194 | self._stream.write('%s%s:"%s"\n' % (self._indent, key, value)) |
|---|
| 195 | elif _type == 1: |
|---|
| 196 | self._stream.write('%s%s:%s\n' % (self._indent, key, |
|---|
| 197 | int(value))) |
|---|
| 198 | elif value in _type: |
|---|
| 199 | self._stream.write('%s%s:%s\n' % (self._indent, key, value)) |
|---|
| 200 | else: |
|---|
| 201 | raise Exception('''value %s isn\'t correct for attribute %s |
|---|
| 202 | correct values are %s''' % (value, key, _type)) |
|---|
| 203 | |
|---|
| 204 | def _inc_indent(self): |
|---|
| 205 | """increment indentation |
|---|
| 206 | """ |
|---|
| 207 | self._indent = ' %s' % self._indent |
|---|
| 208 | |
|---|
| 209 | def _dec_indent(self): |
|---|
| 210 | """decrement indentation |
|---|
| 211 | """ |
|---|
| 212 | self._indent = self._indent[:-2] |
|---|