# HG changeset patch # User Paul Boddie # Date 1221521280 -7200 # Node ID bf9474f36f3431338a3c066c36b91df7a3d345e8 # Parent b6d5759c931a85604ba5bca82ac0ba65fa72e241 Added a simple graph function producing Graphviz output. diff -r b6d5759c931a -r bf9474f36f34 micropython/__init__.py --- a/micropython/__init__.py Mon Sep 15 01:34:37 2008 +0200 +++ b/micropython/__init__.py Tue Sep 16 01:28:00 2008 +0200 @@ -37,7 +37,7 @@ import micropython.ast import micropython.inspect import micropython.table -import os +import os, sys try: set except NameError: @@ -304,6 +304,67 @@ 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):