# HG changeset patch # User Paul Boddie # Date 1222024671 -7200 # Node ID c1982c40f0cfa132fbde3f45f148f4d5754b6a24 # Parent 89c6daeaa02daa1c3c16de65540b5c49d5605e3c Moved graph definition production to a separate module. diff -r 89c6daeaa02d -r c1982c40f0cf micropython/__init__.py --- a/micropython/__init__.py Sun Sep 21 21:00:46 2008 +0200 +++ b/micropython/__init__.py Sun Sep 21 21:17:51 2008 +0200 @@ -37,7 +37,7 @@ import micropython.ast import micropython.inspect import micropython.table -import os, sys +import os try: set except NameError: @@ -304,67 +304,6 @@ return self.paramtable - def get_graph(self, out=None, with_builtins=0): - out = out or sys.stdout - print >>out, 'digraph G {' - print >>out, ' ratio=auto;' - print >>out, ' center=true;' - print >>out, ' rankdir=LR;' - for module in self.modules_ordered: - if not with_builtins and module.name == "__builtins__": - continue - - print >>out, ' subgraph "%s" {' % module.full_name() - print >>out, ' label="%s";' % module.full_name() - for obj in module.all_objects: - - if isinstance(obj, micropython.inspect.Class): - print >>out, ' "%s" [' % obj.full_name(), - print >>out, 'shape=record,', - print >>out, 'label="{<%s> %s|{' % (obj.full_name(), obj.full_name()), - first = 1 - for attr in obj.all_attributes().values(): - if not first: - print >>out, '|', - print >>out, '<%s> %s' % (attr.name, attr.name), - first = 0 - print >>out, '}}"];' - - elif isinstance(obj, micropython.inspect.Function): - print >>out, ' "%s" [' % obj.full_name(), - print >>out, 'shape=record,', - print >>out, 'label="{<%s> %s|{' % (obj.full_name(), obj.full_name()), - first = 1 - for attr in obj.all_locals().values(): - if not first: - print >>out, '|', - print >>out, '<%s> %s' % (attr.name, attr.name), - first = 0 - print >>out, '}}"];' - - print >>out, ' }' - - for module in self.modules_ordered: - if not with_builtins and module.name == "__builtins__": - continue - - print >>out, ' {' - for obj in module.all_objects: - if isinstance(obj, micropython.inspect.Class): - for attr in obj.all_attributes().values(): - if attr.value is not None: - print >>out, ' "%s":%s -> "%s";' % (obj.full_name(), attr.name, attr.value.full_name()) - elif isinstance(obj, micropython.inspect.Function): - for attr in obj.all_locals().values(): - if attr.value is not None: - print >>out, ' "%s":%s -> "%s";' % (obj.full_name(), attr.name, attr.value.full_name()) - print >>out, ' }' - - print >>out, "}" - - def _get_graph_name(self, obj): - return obj.full_name().replace(".", "___") - # Import methods. def find_in_path(self, name): diff -r 89c6daeaa02d -r c1982c40f0cf micropython/graph.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/micropython/graph.py Sun Sep 21 21:17:51 2008 +0200 @@ -0,0 +1,103 @@ +#!/usr/bin/env python + +""" +A module providing elementary graph support for programs. + +Copyright (C) 2008 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +import micropython.inspect +import micropython.common +import micropython.data +import sys + +def get_graph(importer, out=None, with_builtins=0): + + """ + Using the program maintained by the given 'importer', write out the graph + definition for consumption by the Graphviz tools such as the dot program. + If the optional 'out' parameter is set to a stream, that stream will be + used to write out the graph definition; otherwise, standard output will be + used. If 'with_builtins' is set to a true value, the module providing the + built-in classes and functions will also be represented in the graph. + """ + + out = out or sys.stdout + print >>out, 'digraph G {' + print >>out, ' ratio=auto;' + print >>out, ' center=true;' + print >>out, ' rankdir=LR;' + for module in importer.modules_ordered: + if not with_builtins and module.name == "__builtins__": + continue + + print >>out, ' subgraph "%s" {' % module.full_name() + print >>out, ' label="%s";' % module.full_name() + for obj in module.all_objects: + + if isinstance(obj, micropython.inspect.Class): + print >>out, ' "%s" [' % obj.full_name(), + print >>out, 'shape=record,', + print >>out, 'label="{<%s> %s|{' % (obj.full_name(), obj.full_name()), + first = 1 + for attr in obj.all_attributes().values(): + if not first: + print >>out, '|', + print >>out, '<%s> %s' % (attr.name, attr.name), + first = 0 + print >>out, '}}"];' + + elif isinstance(obj, micropython.inspect.Function): + print >>out, ' "%s" [' % obj.full_name(), + print >>out, 'shape=record,', + print >>out, 'label="{<%s> %s|{' % (obj.full_name(), obj.full_name()), + first = 1 + for attr in obj.all_locals().values(): + if not first: + print >>out, '|', + print >>out, '<%s> %s' % (attr.name, attr.name), + first = 0 + print >>out, '}}"];' + + print >>out, ' }' + + for module in importer.modules_ordered: + if not with_builtins and module.name == "__builtins__": + continue + + print >>out, ' {' + for obj in module.all_objects: + if isinstance(obj, micropython.inspect.Class): + for attr in obj.all_attributes().values(): + if attr.value is not None: + print >>out, ' "%s":%s -> "%s";' % (obj.full_name(), attr.name, get_name(attr.value)) + elif isinstance(obj, micropython.inspect.Function): + for attr in obj.all_locals().values(): + if attr.value is not None: + print >>out, ' "%s":%s -> "%s";' % (obj.full_name(), attr.name, get_name(attr.value)) + print >>out, ' }' + + print >>out, "}" + +def get_name(obj): + if isinstance(obj, micropython.common.Naming): + return obj.full_name() + elif isinstance(obj, micropython.data.Const): + return obj.value_type_name() + else: + return "" + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 89c6daeaa02d -r c1982c40f0cf test.py --- a/test.py Sun Sep 21 21:00:46 2008 +0200 +++ b/test.py Sun Sep 21 21:17:51 2008 +0200 @@ -2,6 +2,7 @@ import micropython from micropython.rsvp import raw +from micropython.graph import get_graph import rsvp import sys import os