#!/usr/bin/env python

import sys, os, string
import pprint

alpha2digit0 = {
    "abc": "2",
    "def": "3",
    "ghi": "4",
    "jkl": "5",
    "mno": "6",
    "prs": "7",
    "tuv": "8",
    "wxy": "9"}

alpha2digit = {
    'a': "2",
    'b': "2",
    'c': "2",
    'd': "3",
    'e': "3",
    'f': "3",
    'g': "4",
    'h': "4",
    'i': "4",
    'j': "5",
    'k': "5",
    'l': "5",
    'm': "6",
    'n': "6",
    'o': "6",
    'p': "7",
    'r': "7",
    's': "7",
    't': "8",
    'u': "8",
    'v': "8",
    'w': "9",
    'x': "9",
    'y': "9"}

def flatten_map(map, name):
    skeys = map.keys()
    skeys.sort()
    map2 = {}
    for key in skeys:
        val = map[key]
        for ch in key:
            map2[ch] = val

    print name, "= ",
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(map2)
    

def main(argv):
    import getopt
    opt_string = "f:"
    opts, args = getopt.getopt(argv[1:], opt_string)
    for o, v in opts:
        if o == '-f':
            flatten_map(alpha2digit0, v)
            continue

    for number in args:
        print "translating:", number, ": ",
        for ch in number:
            if ch.isalpha():
                sys.stdout.write(alpha2digit.get(ch,
                                                 "<%s is not on keypad>" % ch))
            elif ch.isdigit():
                sys.stdout.write(ch)
            else:
                # We may want to change the default action.
                sys.stdout.write(ch)
        print
            
if __name__ == "__main__":
    main(sys.argv)

