1.1 --- a/micropython/__init__.py Sun Sep 21 21:00:46 2008 +0200
1.2 +++ b/micropython/__init__.py Sun Sep 21 21:17:51 2008 +0200
1.3 @@ -37,7 +37,7 @@
1.4 import micropython.ast
1.5 import micropython.inspect
1.6 import micropython.table
1.7 -import os, sys
1.8 +import os
1.9 try:
1.10 set
1.11 except NameError:
1.12 @@ -304,67 +304,6 @@
1.13
1.14 return self.paramtable
1.15
1.16 - def get_graph(self, out=None, with_builtins=0):
1.17 - out = out or sys.stdout
1.18 - print >>out, 'digraph G {'
1.19 - print >>out, ' ratio=auto;'
1.20 - print >>out, ' center=true;'
1.21 - print >>out, ' rankdir=LR;'
1.22 - for module in self.modules_ordered:
1.23 - if not with_builtins and module.name == "__builtins__":
1.24 - continue
1.25 -
1.26 - print >>out, ' subgraph "%s" {' % module.full_name()
1.27 - print >>out, ' label="%s";' % module.full_name()
1.28 - for obj in module.all_objects:
1.29 -
1.30 - if isinstance(obj, micropython.inspect.Class):
1.31 - print >>out, ' "%s" [' % obj.full_name(),
1.32 - print >>out, 'shape=record,',
1.33 - print >>out, 'label="{<%s> %s|{' % (obj.full_name(), obj.full_name()),
1.34 - first = 1
1.35 - for attr in obj.all_attributes().values():
1.36 - if not first:
1.37 - print >>out, '|',
1.38 - print >>out, '<%s> %s' % (attr.name, attr.name),
1.39 - first = 0
1.40 - print >>out, '}}"];'
1.41 -
1.42 - elif isinstance(obj, micropython.inspect.Function):
1.43 - print >>out, ' "%s" [' % obj.full_name(),
1.44 - print >>out, 'shape=record,',
1.45 - print >>out, 'label="{<%s> %s|{' % (obj.full_name(), obj.full_name()),
1.46 - first = 1
1.47 - for attr in obj.all_locals().values():
1.48 - if not first:
1.49 - print >>out, '|',
1.50 - print >>out, '<%s> %s' % (attr.name, attr.name),
1.51 - first = 0
1.52 - print >>out, '}}"];'
1.53 -
1.54 - print >>out, ' }'
1.55 -
1.56 - for module in self.modules_ordered:
1.57 - if not with_builtins and module.name == "__builtins__":
1.58 - continue
1.59 -
1.60 - print >>out, ' {'
1.61 - for obj in module.all_objects:
1.62 - if isinstance(obj, micropython.inspect.Class):
1.63 - for attr in obj.all_attributes().values():
1.64 - if attr.value is not None:
1.65 - print >>out, ' "%s":%s -> "%s";' % (obj.full_name(), attr.name, attr.value.full_name())
1.66 - elif isinstance(obj, micropython.inspect.Function):
1.67 - for attr in obj.all_locals().values():
1.68 - if attr.value is not None:
1.69 - print >>out, ' "%s":%s -> "%s";' % (obj.full_name(), attr.name, attr.value.full_name())
1.70 - print >>out, ' }'
1.71 -
1.72 - print >>out, "}"
1.73 -
1.74 - def _get_graph_name(self, obj):
1.75 - return obj.full_name().replace(".", "___")
1.76 -
1.77 # Import methods.
1.78
1.79 def find_in_path(self, name):
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/micropython/graph.py Sun Sep 21 21:17:51 2008 +0200
2.3 @@ -0,0 +1,103 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"""
2.7 +A module providing elementary graph support for programs.
2.8 +
2.9 +Copyright (C) 2008 Paul Boddie <paul@boddie.org.uk>
2.10 +
2.11 +This program is free software; you can redistribute it and/or modify it under
2.12 +the terms of the GNU General Public License as published by the Free Software
2.13 +Foundation; either version 3 of the License, or (at your option) any later
2.14 +version.
2.15 +
2.16 +This program is distributed in the hope that it will be useful, but WITHOUT
2.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
2.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
2.19 +details.
2.20 +
2.21 +You should have received a copy of the GNU General Public License along with
2.22 +this program. If not, see <http://www.gnu.org/licenses/>.
2.23 +"""
2.24 +
2.25 +import micropython.inspect
2.26 +import micropython.common
2.27 +import micropython.data
2.28 +import sys
2.29 +
2.30 +def get_graph(importer, out=None, with_builtins=0):
2.31 +
2.32 + """
2.33 + Using the program maintained by the given 'importer', write out the graph
2.34 + definition for consumption by the Graphviz tools such as the dot program.
2.35 + If the optional 'out' parameter is set to a stream, that stream will be
2.36 + used to write out the graph definition; otherwise, standard output will be
2.37 + used. If 'with_builtins' is set to a true value, the module providing the
2.38 + built-in classes and functions will also be represented in the graph.
2.39 + """
2.40 +
2.41 + out = out or sys.stdout
2.42 + print >>out, 'digraph G {'
2.43 + print >>out, ' ratio=auto;'
2.44 + print >>out, ' center=true;'
2.45 + print >>out, ' rankdir=LR;'
2.46 + for module in importer.modules_ordered:
2.47 + if not with_builtins and module.name == "__builtins__":
2.48 + continue
2.49 +
2.50 + print >>out, ' subgraph "%s" {' % module.full_name()
2.51 + print >>out, ' label="%s";' % module.full_name()
2.52 + for obj in module.all_objects:
2.53 +
2.54 + if isinstance(obj, micropython.inspect.Class):
2.55 + print >>out, ' "%s" [' % obj.full_name(),
2.56 + print >>out, 'shape=record,',
2.57 + print >>out, 'label="{<%s> %s|{' % (obj.full_name(), obj.full_name()),
2.58 + first = 1
2.59 + for attr in obj.all_attributes().values():
2.60 + if not first:
2.61 + print >>out, '|',
2.62 + print >>out, '<%s> %s' % (attr.name, attr.name),
2.63 + first = 0
2.64 + print >>out, '}}"];'
2.65 +
2.66 + elif isinstance(obj, micropython.inspect.Function):
2.67 + print >>out, ' "%s" [' % obj.full_name(),
2.68 + print >>out, 'shape=record,',
2.69 + print >>out, 'label="{<%s> %s|{' % (obj.full_name(), obj.full_name()),
2.70 + first = 1
2.71 + for attr in obj.all_locals().values():
2.72 + if not first:
2.73 + print >>out, '|',
2.74 + print >>out, '<%s> %s' % (attr.name, attr.name),
2.75 + first = 0
2.76 + print >>out, '}}"];'
2.77 +
2.78 + print >>out, ' }'
2.79 +
2.80 + for module in importer.modules_ordered:
2.81 + if not with_builtins and module.name == "__builtins__":
2.82 + continue
2.83 +
2.84 + print >>out, ' {'
2.85 + for obj in module.all_objects:
2.86 + if isinstance(obj, micropython.inspect.Class):
2.87 + for attr in obj.all_attributes().values():
2.88 + if attr.value is not None:
2.89 + print >>out, ' "%s":%s -> "%s";' % (obj.full_name(), attr.name, get_name(attr.value))
2.90 + elif isinstance(obj, micropython.inspect.Function):
2.91 + for attr in obj.all_locals().values():
2.92 + if attr.value is not None:
2.93 + print >>out, ' "%s":%s -> "%s";' % (obj.full_name(), attr.name, get_name(attr.value))
2.94 + print >>out, ' }'
2.95 +
2.96 + print >>out, "}"
2.97 +
2.98 +def get_name(obj):
2.99 + if isinstance(obj, micropython.common.Naming):
2.100 + return obj.full_name()
2.101 + elif isinstance(obj, micropython.data.Const):
2.102 + return obj.value_type_name()
2.103 + else:
2.104 + return "<None>"
2.105 +
2.106 +# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/test.py Sun Sep 21 21:00:46 2008 +0200
3.2 +++ b/test.py Sun Sep 21 21:17:51 2008 +0200
3.3 @@ -2,6 +2,7 @@
3.4
3.5 import micropython
3.6 from micropython.rsvp import raw
3.7 +from micropython.graph import get_graph
3.8 import rsvp
3.9 import sys
3.10 import os