#!/usr/bin/env python # encoding: utf-8 """ untitled.py Created by Jun Du on 2010-04-06. Copyright (c) 2010 Ruiking Mobile. All rights reserved. """ import sys,os import getopt import itertools import codecs from time import sleep exts = ['txt'] coders = ['utf-8','gb18030','gb2312','utf-16le'] help_message = ''' The help message goes here. ''' def utf8Detect(text): '''Detect if a string is utf-8 encoding''' lastch=0 begin=0 BOM=True BOMchs=(0xEF, 0xBB, 0xBF) good=0 bad=0 for char in text: ch=ord(char) if begin<3: BOM=(BOMchs[begin]==ch) and BOM begin += 1 continue if (begin==4) and (BOM==True): break; if (ch & 0xC0) == 0x80: if (lastch & 0xC0) == 0xC0: good += 1 elif (lastch &0x80) == 0: bad += 1 elif (lastch & 0xC0) == 0xC0: bad += 1 lastch = ch if (((begin == 4) and (BOM == True)) or (good >= bad)): return True else: return False def anyTrue(predicate,sequence): return True in itertools.imap(predicate,sequence) def endsWith(s,*endings): return anyTrue(s.endswith,endings) def filterFiles(folder,exts): for fileName in os.listdir(folder): thePath = folder + '/' + fileName if os.path.isdir(thePath): filterFiles(thePath, exts) elif anyTrue(fileName.endswith,exts): # print thePath text = '' for theCoder in coders: try: theFile = codecs.open(thePath,'r',theCoder) text = theFile.read() if len(text) <> 0: break; except: continue f = file(thePath,'w') f.write(codecs.BOM_UTF8) f.write(text.encode('utf-8')) f.close() class Usage(Exception): def __init__(self, msg): self.msg = msg def main(argv=None): if argv is None: argv = sys.argv try: try: opts, args = getopt.getopt(argv[1:], "ho:v", ["help", "output="]) except getopt.error, msg: raise Usage(msg) # option processing for option, value in opts: if option == "-v": verbose = True if option in ("-h", "--help"): raise Usage(help_message) if option in ("-o", "--output"): output = value except Usage, err: print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg) print >> sys.stderr, "\t for help use --help" return 2 print 'processing...' rootpath = os.path.abspath('.') # print rootpath filterFiles(rootpath,exts) print 'finished.' if __name__ == "__main__": sys.exit(main())