1.1 --- a/micropython/graph.py Sun Jun 19 02:07:58 2011 +0200
1.2 +++ b/micropython/graph.py Sun Jun 26 14:15:57 2011 +0200
1.3 @@ -21,28 +21,34 @@
1.4
1.5 from micropython.common import TableError
1.6 from micropython.data import Class, Function
1.7 -import sys
1.8 +import sys, os
1.9
1.10 -def get_graph(program, out=None):
1.11 +def get_graph(program, out=None, filename=None):
1.12
1.13 """
1.14 Using the importer maintained by the given 'program', write out the graph
1.15 definition for consumption by the Graphviz tools such as the dot program.
1.16 +
1.17 If the optional 'out' parameter is set to a stream, that stream will be
1.18 used to write out the graph definition; otherwise, standard output will be
1.19 used.
1.20
1.21 + If the optional 'filename' is specified, the leafname minus extension will
1.22 + be used as the name of the graph.
1.23 +
1.24 A command line like the following can be used to produce viewable graph
1.25 files:
1.26
1.27 dot -T svg -o output.svg output.dot
1.28 """
1.29
1.30 + filename = filename and os.path.splitext(os.path.split(filename)[-1])[0] or "program"
1.31 +
1.32 importer = program.get_importer()
1.33 objtable = program.get_object_table()
1.34
1.35 out = out or sys.stdout
1.36 - print >>out, 'digraph G {'
1.37 + print >>out, 'digraph %s {' % filename
1.38 print >>out, ' ratio=auto;'
1.39 print >>out, ' center=true;'
1.40 print >>out, ' rankdir=LR;'
2.1 --- a/test.py Sun Jun 19 02:07:58 2011 +0200
2.2 +++ b/test.py Sun Jun 26 14:15:57 2011 +0200
2.3 @@ -79,7 +79,7 @@
2.4 print "Generating graph as", graph
2.5 f = open(graph, "w")
2.6 try:
2.7 - get_graph(p, f)
2.8 + get_graph(p, f, filename)
2.9 finally:
2.10 f.close()
2.11