import collect class reporter(object): """ base class for reporters reporting in a test-driven manner, walking through the module trees, sorted on containment and name """ # the event handlers, override these in subclasses def module(self, typedesc): msg = 'module %s' % (typedesc.name,) print '=' * len(msg) print msg print '=' * len(msg) print if typedesc.doc: print typedesc.doc print def cls(self, typedesc): msg = 'class %s' % (typedesc.basename,) print msg print '=' * len(msg) print if typedesc.doc: print typedesc.doc print def method(self, typedesc, info): self._callable(typedesc, info) def function(self, typedesc, info): self._callable(typedesc, info) def attribute(self, typedesc): msg = 'attribute %s=%r' % (typedesc.basename, typedesc.value) if len(msg) > 79: msg = '%s...' % (msg[:76],) print msg print # reporter-specific methods def _callable(self, typedesc, info): msg = '%03d def %s(%s)' % ( typedesc.lineno, typedesc.basename, ', '.join(typedesc.args)) print msg print '-' * len(msg) print if typedesc.doc: print typedesc.doc #else: # print '' print if info: for cd in info.calldata: flines = open(cd.filename).readlines() print 'called from %s' % (typedesc.filename.split('/')[-1],) print 'with args:', ', '.join([ '%s=%r' % (k, v) for (k, v) in cd.args]) print '::' print print ' %03d %s' % (cd.lineno, flines[cd.lineno - 1][:-1]) print def report(coll, tracer, reporter): raw = coll.collect() tree = coll.get_tree(raw) for item in tree: if isinstance(item, collect.Module): reporter.module(item) elif isinstance(item, collect.Class): reporter.cls(item) elif isinstance(item, collect.Method): reporter.method(item, tracer.get((item.filename, item.lineno))) elif isinstance(item, collect.Function): reporter.function(item, tracer.get((item.filename, item.lineno))) elif isinstance(item, collect.Attribute): reporter.attribute(item)