#!/usr/bin/env python
from __future__ import division
import bsddb, os.path, sys
from collections import defaultdict
from contextlib import closing

bayes_seen_path = os.path.expanduser('~/.spamassassin/bayes_seen')

try:
    with closing(bsddb.hashopen(bayes_seen_path, 'r')) as bayes_seen:
        stats = defaultdict(int)
        overall = 0
        for value in bayes_seen.values():
            stats[value] += 1
            overall += 1

        # do some proper rounding for accurate display
        percentage_ham = int(round((stats['h'] / overall) * 100, 0))
        percentage_spam = 100 - percentage_ham

        print 'Total number of mails processed: %d' % overall
        print 'Ham mails seen: %d (%d%%) \tSpam mails seen: %d (%d%%)' % (
                stats['h'], percentage_ham,
                stats['s'], percentage_spam)
except bsddb.db.DBNoSuchFileError:
    print >> sys.stderr, 'Bayes DB does not exist, check %s' % bayes_seen_path
