boa-constructor /

# Name:
# Purpose:     Displays a Cyclops report in HTML format.
#              Pretty much a copy of code from Cyclops by Tim Peters
# Author:      of changes, Riaan Booysen
# Created:     2000/05/20
# RCS-ID:      $Id$
# Copyright:   of changes (c) 1999 - 2004 Riaan Booysen
# Licence:     Dual GPL & Python

from ExternalLib import Cyclops

clGreen = '#228822'
clRed = '#882222'

def replaceLT(str):
    return '<font color="#000060" size="-1"><b>'+str.replace('<', '&lt;')+'</b></font>'

import repr
_repr = repr
del repr

class _CyclopsHTMLRepr(_repr.Repr):

    def __init__(self):
        # Boost the limit on types we don't know about; else it's too
        # easy to get a useless repr string when adding new types via
        # CycleFinder.chase_type.
        # Perhaps better to expose this class too, but-- sheesh --
        # this module is complicated enough!
        self.maxstring = self.maxother = 40

    # override base dictionary formatter; the code is almost the same,
    # we just leave the dict order alone

    def repr_dictionary(self, x, level):
        n = len(x)
        if n == 0:
            return replaceLT('{}')
        if level <= 0:
            return replaceLT('{...}')
        s = ''
        for k, v in x.items()[:min(n, self.maxdict)]:
            if s:
                s = s + ', '
            s = s + self.repr1(k, level-1)
            s = s + ': ' + self.repr1(v, level-1)
        if n > self.maxdict:
            s = s + ', ...'
        return '{' + s + '}'

    # don't chop instance, class or instance method reprs

    def repr_instance(self, x, level):
            return replaceLT(`x`)
            # Bugs in x.__repr__() can cause arbitrary
            # exceptions -- then make up something
            return replaceLT('<' + x.__class__.__name__ + ' instance at ' + \
                   hex(id(x))[2:] + '>')

    def repr_class(self, x, level):
        return replaceLT(`x`)

    repr_instance_method = repr_class

_quickrepr = _CyclopsHTMLRepr().repr

#def find_declarer(cls, attr):
#    while len(cls.__bases__):
#        if obj.__dict__.has_key(attr):
#            return obj

def find_declarer(cls, attr, found = 0):
#    print 'find_declarer', cls, attr, found
    if found:
        return found, cls
        for base in cls.__bases__:
            if base.__dict__.has_key(attr):
                return 1, base
                found, basecls = find_declarer(base, attr, 0)
    return found, cls

##print 'test find_declarer'
##print find_declarer(_CyclopsHTMLRepr, 'repr1')
##class A:
##    def __init__(self):
##        self.a = 10
##class B(A): pass
##a = A()
##b = B()
##print find_declarer(b.__class__, 'a')
indent = '&nbsp;&nbsp;&nbsp;&nbsp;'

class CycleFinderHTML(Cyclops.CycleFinder):
    def __init__(self):
        Cyclops.CycleFinder.__init__(self) = []
        self.header = ['<h1>Cyclops report</h1>']#<h3> %s</h3><br>'%path.basename(self.model.filename)]

    def _add_section(self, name, text, docs = ''):
        if docs is None:
            docs = ''
        elif docs:
            docs = docs + '<br><br>'
        self.header.append('<a href="#%s">%s</a><br>'%(name, text))'<a NAME="%s"><h3>%s:</h3></a>'%(name, text))

    def _print_separator(self):'<hr>')

    def _print_cycle(self, slice):
        n = len(slice)
        assert n >= 2'<b>%d-element cycle</b><br>' % (n-1))
        for i in xrange(n):
            obj = slice[i][0]
            if i < n-1:
                if type(obj).__name__ == 'instance method':
                    attrib = obj.im_func.func_name

                    found, attrib_decl = find_declarer(obj.im_class, attrib)
#                    print 'XXXXX', found, attrib_decl, attrib
          '%s<a href="attrlink://%s.%s">this%s</a>-><br>' \
                      % (indent, str(attrib_decl), attrib, attrib))
                    index = slice[i+1][1]
                    attrib = self.tag_dispatcher[type(obj)](obj, index)

                    if attrib[0] == '.' and hasattr(obj, "__class__"):
                        found, attrib_decl = find_declarer(obj.__class__, attrib[1:])
              '%s<a href="attrlink://%s%s">this%s</a>-><br>' \
                          % (indent, str(attrib_decl), attrib, attrib))
              'this' + \
                          attrib+ '-><br>')

    def show_obj(self, obj):
        """obj -> print short description of obj to sdtout.

        This is of the form

        <address> rc:<refcount> <typename>
            repr: <shortrepr>

            hex address of obj
            If find_cycles() has been run and obj is in the root set
            or was found in a cycle, this is the number of references
            outstanding less the number held internally by
            CycleFinder.  In most cases, this is what the true
            refcount would be had you not used CycleFinder at all.
            You can screw that up, e.g. by installing a cycle filter
            that holds on to references to one or more cycle elements.
            If find_cycles() has not been run, or has but obj wasn't
            found in a cycle and isn't in the root set, <refcount> is
            type(obj), as a string.  If obj.__class__ exists, also
            prints the class name.
            repr(obj), but using a variant of the std module
            that limits the number of characters displayed.

        objid = id(obj)
        rc = self.id2rc.get(objid, "?")
#        print type(obj).__name__, `type(obj).__name__`'<font size="-1" color="#700060">%s</font> rc: %d <b>%s</b>'%(hex(objid), rc, type(obj).__name__))
        if hasattr(obj, "__class__"):
  '<a href="classlink://%s">%s</a>'%(str(obj.__class__), str(obj.__class__)))'<br>')'&nbsp;&nbsp;&nbsp;&nbsp;%s<br>'%_quickrepr(obj))

    def add_stat_line(self, desc, value):
        if type(value) == type([]):
            lns = []
            for itm in value:
  '<tr><td>%s</td>%s</tr>'%(desc, ' '.join(lns)))
  '<tr><td>%s</td><td><b>%s</b></td></tr>' % (desc, str(value)))

    def stats_list(self):
        if len(self.cycles):
            cf = '<font size="+1" color="#FF0000">%d</font>' % len(self.cycles)
            cf = '<font size="+1" color="#00FF00">%d</font>' % len(self.cycles)
        return [('Objects in root set', len(self.roots)),
                ('Distinct structured objects reachable', len(self.seenids)),
                ('Distinct structured objects in cycles', len(self.cycleobjs)),
                ('Cycles found', cf),
                ('Cycles filtered out', self.ncyclesignored),
                ('Strongly connected components', len(self.sccno2objs)),
                ('Arcs examined', self.narcs)]

    def show_stats(self, stats_list, name = 'Stats', title = 'Statistics'):
        """Print statistics for the last run of find_cycles."""
        self._add_section(name, title)'<table>')
        for desc, value in stats_list:
            self.add_stat_line(desc, value)'</table>')

#    def show_iterated_stats(self):

    def show_cycles(self):
        self._add_section('AllCycles', 'All cycles')
        n = len(self.cycles)'<ul>')
        for i in xrange(n):
#            if i < n-1:
#                self._print_separator()

    def show_cycleobjs(self, compare=Cyclops.typename_address_cmp):
        """Each distinct object find_cycles found in a
        cycle is displayed.  The set of objects found in cycles is
        first sorted by the optional "compare" function.  By default,
        objects are sorted using their type name as the primary key
        and their storage address (id) as the secondary key; among
        objects of instance type, sorts by the instances' class names;
        among objects of class type, sorts by the classes' names.

        self._add_section('CycleObjs', 'Objects involved in cycles', self.show_cycleobjs.__doc__)
        objs = self.cycleobjs.values()
        for obj in objs:

    def show_sccs(self, compare=Cyclops.typename_address_cmp):
        """Shows the objects in cycles partitioned into
        strongly connected components (that is, the largest groupings
        possible such that each object in an SCC is reachable from every
        other object in that SCC).  Within each SCC, objects are sorted
        as for show_cycleobjs.

        self._add_section('SCC', 'Cycle objects partitioned into maximal SCCs', self.show_sccs.__doc__)
        sccs = self.sccno2objs.values()
        n = len(sccs)'<ul>')
        for i in xrange(n):
  '<li><b>SCC %d of %d</b><br>' %(i+1, n))
            objs = sccs[i]
            for obj in objs:

    def show_arcs(self, compare=None):
        """Briefly, each arc in a
        cycle is categorized by the type of the source node, the kind
        of arc (how we got from the source to the destination), and
        the type of the destination node.  Each line of output
        consists of those three pieces of info preceded by the count
        of arcs of that kind.  By default, the rows are sorted first
        by column 2 (source node type), then by columns 3 and 4.

        self._add_section('ArcTypes', 'Arc types involved in cycles', self.show_arcs.__doc__)
        items = self.arctypes.items()
        if compare:
        for triple, count in items:
  '%6d %-20s %-20s -> %-20s<br>' % ((count,) + triple))

    def show_chased_types(self):
        ct = self.get_chased_types()
        self.header.append('<h3>Installed chasers</h3>')
        if len(ct):
            for ch in ct:

    def iterate_til_steady_state(self, show_objs = 0, summary = 1):
        self._add_section('CycleCycles', 'Purge root set')
        cc = 0'Non-cyclic root set objects:')
        stats = []'<ul>')
            while 1:
#                print "*" * 70
#                print "non-cyclic root set objects:"
                sawitalready = {}
                numsurvivors = numdead = 0
                class_count = {}
                name = 'DeadRootSet%d'%cc
                desc = 'Iteration %d'%cc
                self.header.append('<a href="#%s">%s</a><br>'%(name, desc))
      '<li><a NAME="%s"><b>%s</b></a></li><br>'%(name, desc))
                for rc, cyclic, x in self.get_rootset():
                    if not sawitalready.has_key(id(x)):
                        sawitalready[id(x)] = 1
                        if rc == 0:
                            numdead = numdead + 1
                            if show_objs:
                      '<font color="%s">DEAD </font>'%clGreen)
                        elif not cyclic:
                            numsurvivors = numsurvivors + 1
                            if show_objs: self.show_obj(x)
                            if hasattr(x, '__class__'):
                                cn = x.__class__.__name__
                                if class_count.has_key(cn):
                                    class_count[cn][id(x)] = 0
                                    class_count[cn] = {id(x):0}

                x = None
                desc = '<br><b><font color="%s">%d</font> dead; <font color="%s">%d</font> non-cycle & alive</b><br>' % (clGreen, numdead, clRed, numsurvivors)

                desc = '<br><b>Summary of instance count:</b><br>'
                clss = class_count.keys()
                for cls in clss:
                    desc = '%s: %d<br>' % (cls, len(class_count[cls]))

                sts = self.stats_list()+[('Dead', numdead), ('Non-cycle & alive', numsurvivors)]
                for idx in range(len(sts)):
                    if len(stats) < idx + 1:
                        stats.append((sts[idx][0], [sts[idx][1]]))

                if numdead == 0:

                cc = cc + 1

            self.show_stats(stats, 'StatsFin', 'Purge root set statistics')

    def get_page(self):
        return '\n'.join(self.header)+'\n'.join(