1 |
#!/usr/bin/python |
2 |
|
3 |
# direct inclusion of the README file |
4 |
|
5 |
include(README.dot2term) |
6 |
|
7 |
# J Templon NIKHEF 2010. |
8 |
|
9 |
import getopt, sys, os |
10 |
import re |
11 |
|
12 |
def usage(): |
13 |
print "Usage: dot2term [-r root_template ] file.dot" |
14 |
print "Converts <file.dot> from dot format to udg format readable by uDrawGraph" |
15 |
print "option -r means to display only that part of the include tree stemming from the"+\ |
16 |
" specified template" |
17 |
|
18 |
# note : in order to avoid having to 'install' this program, I have |
19 |
# included the relevant pieces of treedefs.py and dvtree.py in this file. Don't edit here, |
20 |
# edit the originals. |
21 |
# BEGIN code included via m4 macro processor |
22 |
include(treedefs.py) |
23 |
include(dvtree.py) |
24 |
# END code included via m4 macro processor |
25 |
|
26 |
dprefix = " " |
27 |
|
28 |
def main(): |
29 |
try: |
30 |
opts, args = getopt.getopt(sys.argv[1:], "hr:", ["help","root="]) |
31 |
except getopt.GetoptError: |
32 |
usage() |
33 |
sys.exit(2) |
34 |
|
35 |
root = None # default to printing entire graph |
36 |
|
37 |
for o, a in opts: |
38 |
if o in ("-h", "--help"): |
39 |
usage() |
40 |
sys.exit() |
41 |
if o in ("-r", "--root"): |
42 |
root = a |
43 |
|
44 |
if args[0][-4:] == '.dot' : |
45 |
outfile=args[0][:-4]+'.udg' |
46 |
else: |
47 |
outfile = args[0] + '.udg' |
48 |
|
49 |
dotlines = open(args[0]).readlines() |
50 |
if dotlines[0].find('digraph') < 0 : |
51 |
print "Expected digraph as first line, found", dotlines[0] |
52 |
print "Bailing out" |
53 |
sys.exit(1) |
54 |
li = dotlines[0].find('"') + 1 |
55 |
ri = dotlines[0].rfind('"') |
56 |
nodename=dotlines[0][li:ri] |
57 |
shape='ellipse' |
58 |
tnode = Node(nodename,shape) |
59 |
Ltreenode.append(tnode) |
60 |
Dnode[tnode.name] = tnode |
61 |
|
62 |
for dl in dotlines[1:]: |
63 |
l = dl[:-1].strip() # get rid of trailing newline and any preceding whitespace |
64 |
if l.find('node') == 0: |
65 |
li=l.find('shape=')+6 |
66 |
ri=l.rfind(']') |
67 |
shape=l[li:ri] |
68 |
elif l.find(' -> ') > 0: # edge definition |
69 |
fields=l.split('"') # nodes are between quotes, so split on that |
70 |
par = fields[1] |
71 |
child = fields[3] |
72 |
if par not in Dnode.keys(): |
73 |
tnode = Node(par, shape) |
74 |
Dnode[tnode.name] = tnode |
75 |
pn = tnode |
76 |
else: |
77 |
pn = Dnode[par] |
78 |
if not pn.shape : pn.shape = shape |
79 |
if child not in Dnode.keys(): |
80 |
tnode = Node(child) |
81 |
Dnode[tnode.name] = tnode |
82 |
cn = tnode |
83 |
else: |
84 |
cn = Dnode[child] |
85 |
if not pn.children: pn.children = [] |
86 |
pn.children.append(cn) |
87 |
elif l.find('}') == 0: |
88 |
break |
89 |
else: |
90 |
print 'found some incomprehensible line', l |
91 |
sys.exit(1) |
92 |
|
93 |
|
94 |
dvFmt = Formathandler(dvnode, dvref, dvb4child, dvb4next, |
95 |
dvafterchild, dvafterchildren, |
96 |
filename=outfile) |
97 |
|
98 |
init_traverse() # initialize data structures for tree traversal |
99 |
|
100 |
if not root: |
101 |
n = Ltreenode[0] # the root of the entire graph |
102 |
else: |
103 |
if root not in Dnode.keys(): |
104 |
print 'template ' + root + ' not found in call graph, bailing out' |
105 |
sys.exit(2) |
106 |
else: |
107 |
n = Dnode[root] |
108 |
|
109 |
dvFmt.of.write('[\n') # open the term representation |
110 |
fmt_applyunique(n, dvFmt) # traverse the tree, outputting daVinci file |
111 |
dvFmt.of.write(']\n') # close the term representation |
112 |
|
113 |
main() |