#! /usr/bin/python2
# vim: set fileencoding=utf-8 :

import re
import locale
from argparse import ArgumentParser

_, default_encoding = locale.getdefaultlocale()

wtos_map = { }

# normal mapping ; offseted by  0xfee0
for x in range(0x21, 0x7f):
    wtos_map[unichr( x + 0xfee0)] = unichr(x)

# two un-regular mapping : space and dot.
wtos_map[unichr(0x3000)] = chr(0x20)
wtos_map[unichr(0x3002)] = chr(0x2e)


def wtos(uni_char):
    "transform double-width char to its single-width equivalent"
    try:
        return wtos_map[uni_char]
    except KeyError:
        return uni_char

def wtos_str(uni_str):
    "transform string to its single-width equivalent"
    return "".join( map(wtos, uni_str) )

def wtos_file(name):
    "simple wraper for wtos_str()"
    string = unicode( open(name).read(), default_encoding)
    return wtos_str(string)


if __name__ == '__main__':

    argparser = ArgumentParser( description=
                        "replace ugly double-width characters with their ascii equivalent"
                        )

    argparser.add_argument("files", metavar="FILES", nargs='*',
                            help="text files to be processed"
                           )

    args = argparser.parse_args()

    for filename in args.files:
        print wtos_file(filename)
