From 7e48d7ba39acb808ce1e72f8293b067da38ce47e Mon Sep 17 00:00:00 2001 From: "U-IBM194-P16TCDD\\liuyug" Date: Sat, 12 Mar 2016 16:06:33 +0800 Subject: [PATCH] missing __main__, add it --- charset/__main__.py | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 charset/__main__.py diff --git a/charset/__main__.py b/charset/__main__.py new file mode 100644 index 0000000..4d4f4b2 --- /dev/null +++ b/charset/__main__.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python +# -*- encoding:utf-8 -*- + +import sys +import argparse +from collections import OrderedDict + +from charset import Unicode, UTF8, GB18030, GBK, GB2312 + + +CHARSET_CLASS = OrderedDict(( + ('gb2312', GB2312), + ('gbk', GBK), + ('gb18030', GB18030), + ('utf8', UTF8), + ('unicode', Unicode), +)) + + +def main(): + parser = argparse.ArgumentParser( + description='CHARSET Tool for %s' % ','.join(CHARSET_CLASS.keys()), + epilog='System encoding: %s' % sys.getfilesystemencoding(), + ) + parser.add_argument( + '--encoding', + choices=CHARSET_CLASS.keys(), + required=True, + help='Use charset', + ) + parser.add_argument( + '--panel', + help='charset panel' + ) + parser.add_argument( + '-P', + action='store_true', + help='list panel' + ) + parser.add_argument( + '--char', + action='store_true', + help='output character for Unicode code' + ) + parser.add_argument( + '--code', + action='store_true', + help='output Unicode code for character' + ) + parser.add_argument( + 'target', + nargs='?', + help='input char or output file' + ) + + args = parser.parse_args() + + in_encoding = out_encoding = sys.getfilesystemencoding() + + charset = CHARSET_CLASS[args.encoding]() + + if args.P: + for k, v in charset.define.items(): + p = k[5:] + print('Panel %s: %s' % (p, ' - '.join(v['desc']))) + elif args.panel: + html = charset.as_html(panels=args.panel.split(',')) + if args.target: + with open(args.target, 'w') as f: + f.write(html.encode('utf-8')) + else: + print(html.encode(out_encoding)) + elif args.code: + chars = args.target.decode(in_encoding) + print('%s code:' % args.encoding.upper()) + print(charset.codes(chars)) + elif args.char: + if '-' in args.target: + codes = args.target.split('-') + elif ',' in args.target: + codes = args.target.split(',') + else: + codes = [args.target] + print('%s char:' % args.encoding.upper()) + print(''.join(charset.chars(codes))) + else: + parser.print_help() + +if __name__ == '__main__': + main()