| | |
| | | from langconv import *
|
| | | import sys
|
| | | import os
|
| | | import ConfigParser
|
| | | import time
|
| | | import traceback
|
| | |
|
| | | print(sys.version)
|
| | | print(sys.version_info)
|
| | |
| | | line.encode('utf-8')
|
| | | return line
|
| | |
|
| | | # æ ¹æ®æå®codeå转æ¢
|
| | | def convertByCode(line, code):
|
| | | line = Converter(code).convert(line)
|
| | | line.encode('utf-8')
|
| | | return line
|
| | |
|
| | | def printlog(msg):
|
| | | print msg.decode('utf-8').encode('gbk')
|
| | | return
|
| | |
|
| | |
|
| | | def main():
|
| | | print 'Number of arguments:', len(sys.argv), 'arguments.'
|
| | | # å
ç´ 0为èªèº«æ¨¡ååï¼ä¸å¤çï¼ä»å
ç´ 1å¼å§å¤ç
|
| | | reload(sys)
|
| | | sys.setdefaultencoding("utf-8")
|
| | | argv = sys.argv[1:]
|
| | | if not argv:
|
| | | printlog("没æé
ç½®è¿è¡çæ¬åºæä»¶é
ç½®åæ°! chscht.py çæ¬åºæä»¶åé
ç½®")
|
| | | return
|
| | | zh_version = argv[0]
|
| | |
|
| | | cfg = ConfigParser.ConfigParser()
|
| | | cfg.read("config/config.ini")
|
| | | path = cfg.get("config", "path")
|
| | | resultByTime = int(cfg.get("config", "resultByTime"))
|
| | | parseFileList = eval(cfg.get("config", "parseFileList"))
|
| | | ignoreFileList = eval(cfg.get("config", "ignoreFileList"))
|
| | | zh_hantFileName = cfg.get("config", zh_version)
|
| | | print "----------------------------"
|
| | | printlog("转åçè·¯å¾: %s" % path)
|
| | | printlog("转å对åºè¡¨: %s" % zh_hantFileName)
|
| | | |
| | | if resultByTime:
|
| | | curTimeStr = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time()))
|
| | | writePath = r"result/%s/%s" % (zh_version, curTimeStr)
|
| | | else:
|
| | | writePath = r"result/%s" % (zh_version)
|
| | | |
| | | if not os.path.exists(writePath):
|
| | | os.makedirs(writePath)
|
| | | else:
|
| | | # æä»¶å¤¹åå¨ï¼å 餿ææä»¶ï¼é²æ¢åå²å¯¼åºçå¤ä½æä»¶æ®ç
|
| | | for root, _, files in os.walk(writePath):
|
| | | for name in files:
|
| | | os.remove(os.path.join(root, name))
|
| | | print "----------------------------"
|
| | | |
| | | # è¯»åæ³¨å转æ¢è¯åº
|
| | | mapFile = open(r"config/%s" % zh_hantFileName, "r")
|
| | | lines = mapFile.readlines()
|
| | | mapFile.close()
|
| | | mapping = {}
|
| | | for line in lines:
|
| | | lineInfo = line.split("\t")
|
| | | mapping[lineInfo[0]] = lineInfo[1].replace("\r", "").replace("\n", "")
|
| | | registery(zh_version, mapping)
|
| | | |
| | | # å¼å§è½¬æ¢
|
| | | ignoreFiles = []
|
| | | totalCount = 0
|
| | | exportCount = 0
|
| | | for filePath in sys.argv[1:]:
|
| | | fileBaseName = os.path.basename(filePath) # æä»¶åï¼å«æ©å±å
|
| | | # fileExtension = os.path.splitext(filePath)[-1] # æ©å±å
|
| | | # fileName = fileBaseName.replace(fileExtension, "") # ä¸å«æ©å±å
|
| | | fw = open(r"ret\%s" % fileBaseName, "w")
|
| | | |
| | | fr = open(filePath, "r")
|
| | | lines = fr.readlines()
|
| | | for line in lines:
|
| | | fw.write(chs_to_cht(line))
|
| | | for parent, _, filenames in os.walk(path):
|
| | | for filename in filenames:
|
| | | fullPath = os.path.join(parent, filename)
|
| | | if not fullPath.endswith(".txt"):
|
| | | continue
|
| | |
|
| | | fr.close()
|
| | | fw.close()
|
| | | exportCount += 1
|
| | | printlog("è½¬æ¢æå: %s" % filePath)
|
| | | |
| | | totalCount += 1
|
| | | if parseFileList and filename not in parseFileList:
|
| | | continue
|
| | | if ignoreFileList:
|
| | | if filename in ignoreFileList:
|
| | | ignoreFiles.append(filename)
|
| | | continue
|
| | | isIgnore = False
|
| | | for ignoreStr in ignoreFileList:
|
| | | if filename.startswith(ignoreStr):
|
| | | ignoreFiles.append(filename)
|
| | | isIgnore = True
|
| | | break
|
| | | |
| | | if isIgnore:
|
| | | continue
|
| | | |
| | | fw = open(r"%s\%s" % (writePath, filename), "w")
|
| | | fr = open(fullPath, "r")
|
| | | lines = fr.readlines()
|
| | | for line in lines:
|
| | | try:
|
| | | fw.write(convertByCode(line, zh_version))
|
| | | except:
|
| | | printlog("é误æä»¶: %s" % filename)
|
| | | print traceback.format_exc()
|
| | | return
|
| | | |
| | | fr.close()
|
| | | fw.close()
|
| | | exportCount += 1
|
| | | printlog("è½¬æ¢æå: %s" % fullPath)
|
| | | |
| | | print "----------------------------"
|
| | | printlog("è½¬æ¢æä»¶æ°: %s" % exportCount)
|
| | | printlog("转æ¢ç»ææä»¶å¤¹ 'ret' æä»¶å¤¹ä¸!")
|
| | | printlog("忽ç¥çæä»¶: %s %s" % (len(ignoreFiles), ignoreFiles))
|
| | | printlog("è½¬æ¢æä»¶æ°: %s / %s" % (exportCount, totalCount))
|
| | | printlog("导åºå°ç®å½: %s" % writePath)
|
| | | print "----------------------------"
|
| | | return
|
| | |
|