#!/usr/bin/env python
# coding:utf-8

import os
import sys


def convert(path):
    with open(path, "rb") as fr:
        datas = fr.readlines()
    fr.close()
    new_datas = []
    new = ""
    for data in datas:
        if data.count("<source>"):
            temp = data.replace("<source>", "").replace("</source>", "").strip()
            new = "        <translation>{}</translation>\n".format(temp)
        if data.count("type=\"unfinished"):
            new_datas.append(new)
        else:
            new_datas.append(data)
    directory = os.path.dirname(path)
    with open("{}/uos-recovery-tool_en_US.ts".format(directory), "wb") as fw:
        fw.writelines(new_datas)
    fw.close()


if __name__ == '__main__':
    file_path = sys.argv[1]
    if os.path.exists(file_path):
        convert(file_path)
    else:
        print "文件或目录不正确"
