#!/usr/bin/env python
# Requires the python-cdb extension module from <http://pilcrow.madison.wi.us/>
#
# Usage: % list2cdb listfile
# (produces listfile.cdb)
#
# Convert a tab-delimited TMDA list file (as text) into a CDB file.
#
# List files should have an e-mail address in the first column,
# and an optional overriding action in the second.  e.g,
#
# foo@mastaler.com
# bar@mastaler.com bounce

import cdb
import fileinput
import string
import sys

listfile = sys.argv[1]
cdbname = listfile + '.cdb'

cdb = cdb.cdbmake(cdbname, cdbname + '.tmp')
for line in fileinput.input(listfile):
    line = string.strip(line)
    if line == '' or line[0] in '#':
        continue
    else:
        line = string.expandtabs(line)
        line = string.split(line, ' #')[0]
        line = string.strip(line)
    linef = string.split(line)
    key = linef[0]
    key = string.lower(key)
    try:
        value = linef[1]
    except IndexError:
        value = ''
    cdb.add(key, value)

fileinput.close()
cdb.finish()
