paul@149 | 1 | #!/usr/bin/env python |
paul@149 | 2 | |
paul@149 | 3 | """ |
paul@149 | 4 | A module providing elementary graph support for programs. |
paul@149 | 5 | |
paul@403 | 6 | Copyright (C) 2008, 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk> |
paul@149 | 7 | |
paul@149 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@149 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@149 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@149 | 11 | version. |
paul@149 | 12 | |
paul@149 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@149 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@149 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@149 | 16 | details. |
paul@149 | 17 | |
paul@149 | 18 | You should have received a copy of the GNU General Public License along with |
paul@149 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@149 | 20 | """ |
paul@149 | 21 | |
paul@403 | 22 | from micropython.data import Class, Function |
paul@555 | 23 | from micropython.errors import TableError |
paul@435 | 24 | import sys, os |
paul@149 | 25 | |
paul@435 | 26 | def get_graph(program, out=None, filename=None): |
paul@149 | 27 | |
paul@149 | 28 | """ |
paul@403 | 29 | Using the importer maintained by the given 'program', write out the graph |
paul@149 | 30 | definition for consumption by the Graphviz tools such as the dot program. |
paul@435 | 31 | |
paul@149 | 32 | If the optional 'out' parameter is set to a stream, that stream will be |
paul@149 | 33 | used to write out the graph definition; otherwise, standard output will be |
paul@403 | 34 | used. |
paul@403 | 35 | |
paul@435 | 36 | If the optional 'filename' is specified, the leafname minus extension will |
paul@435 | 37 | be used as the name of the graph. |
paul@435 | 38 | |
paul@403 | 39 | A command line like the following can be used to produce viewable graph |
paul@403 | 40 | files: |
paul@403 | 41 | |
paul@403 | 42 | dot -T svg -o output.svg output.dot |
paul@149 | 43 | """ |
paul@149 | 44 | |
paul@435 | 45 | filename = filename and os.path.splitext(os.path.split(filename)[-1])[0] or "program" |
paul@435 | 46 | |
paul@403 | 47 | importer = program.get_importer() |
paul@403 | 48 | objtable = program.get_object_table() |
paul@403 | 49 | |
paul@149 | 50 | out = out or sys.stdout |
paul@435 | 51 | print >>out, 'digraph %s {' % filename |
paul@149 | 52 | print >>out, ' ratio=auto;' |
paul@149 | 53 | print >>out, ' center=true;' |
paul@149 | 54 | print >>out, ' rankdir=LR;' |
paul@149 | 55 | |
paul@403 | 56 | for from_name, attributes in importer.inferred_name_references.items(): |
paul@403 | 57 | from_attribute = get_unit(objtable, from_name) |
paul@403 | 58 | if from_attribute: |
paul@403 | 59 | for objname, attrname in attributes: |
paul@403 | 60 | if not have_unit(objtable, objname, attrname): |
paul@403 | 61 | continue |
paul@403 | 62 | colour = get_colour(from_name, objname, attrname, importer) |
paul@403 | 63 | print >>out, ' "%s" -> "%s.%s" [color="%s"];' % (from_name, objname, attrname, colour) |
paul@149 | 64 | |
paul@149 | 65 | print >>out, "}" |
paul@149 | 66 | |
paul@403 | 67 | def get_colour(from_name, objname, attrname, importer): |
paul@403 | 68 | try: |
paul@403 | 69 | if (objname, attrname) in importer.specific_name_references[from_name]: |
paul@403 | 70 | return "red" |
paul@403 | 71 | except KeyError: |
paul@403 | 72 | pass |
paul@403 | 73 | return "black" |
paul@403 | 74 | |
paul@403 | 75 | def get_unit(objtable, attribute): |
paul@403 | 76 | t = attribute.rsplit(".", 1) |
paul@403 | 77 | try: |
paul@403 | 78 | objname, attrname = t |
paul@403 | 79 | if not have_unit(objtable, objname, attrname, Function): |
paul@403 | 80 | return None |
paul@403 | 81 | return attribute |
paul@403 | 82 | except ValueError: |
paul@403 | 83 | return t[0] |
paul@403 | 84 | |
paul@403 | 85 | def have_unit(objtable, objname, attrname, allowed_types=None): |
paul@403 | 86 | allowed_types = allowed_types or (Class, Function) |
paul@403 | 87 | try: |
paul@403 | 88 | attr = objtable.access(objname, attrname) |
paul@403 | 89 | except TableError: |
paul@403 | 90 | #print >>sys.stderr, "%s.%s not found!" % (objname, attrname) |
paul@591 | 91 | return False |
paul@403 | 92 | |
paul@403 | 93 | for value in attr.get_values(): |
paul@403 | 94 | if isinstance(value, allowed_types): |
paul@591 | 95 | return True |
paul@591 | 96 | return False |
paul@149 | 97 | |
paul@149 | 98 | # vim: tabstop=4 expandtab shiftwidth=4 |