#!/www/python/bin/python

"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/tools/zodb_census.py $
$Id: zodb_census.py 20912 2003-03-04 16:07:12Z dbinger $

Inspect every object in a ZODB and count how many times each type
occurs.  (Note that each ExtensionClass is a separate type, so
we'll get a class-by-class breakdown from this.)
"""

import sys
from dulcinea.script_util import writenow

object_count = {}                       # maps type name to count

def main(db, default_dbspec):
    if len(sys.argv) > 1:
        dbspec = sys.argv[1]
    else:
        dbspec = default_dbspec

    writenow("opening database...")
    db.open(dbspec)
    writenow("\n")

    for obj in db.iterate():
        typename = type(obj).__name__
        if object_count.has_key(typename):
            object_count[typename] += 1
        else:
            object_count[typename] = 1L

    writenow("census completed\n")

    typenames = object_count.keys()
    typenames.sort()
    print "objects seen by type:"
    for name in typenames:
        print "%-25.25s %10d" % (name, object_count[name])

