genstr.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/python
#-*- coding:utf-8 –*-
 
import os
import sys
import re
import shutil
import xlrd
import xlwt
import getopt
import math
from xlutils.copy import copy
 
 
'''
脚本使用:
    设置strUiPorject ui项目名称,取值如下 "mstar"/"formal"/"haier"/"videocon"/"bbk"/"atv_project"
    删除无用字串:             ./genstr -d
    特殊标记的字串优先排序:    ./genstr -p
    给已整理好优先级高的字串添加strMark..../genstr -a
    读取 优先级字串整理.h 中的字串进行比对添加标记(未整理)./genstr -c
 
脚本功能:
    1、根据strUiPorject设置的UI名称,迭代过滤UI目录的所有源文件和头文件,获取项目使用字串总数,并删除UIL多余字串
    2、对某一种语言做特殊标记strMark,标记的字串会放在翻译的最前面
    3、mstar优先级字串整理在mstarPrimaryString.h,已使用字串整理在UsedString.h中,
    formal优先级字串整理在PrimaryString.h
执行过程:
    1、设置UI项目名称
    2、根据UI名称,配置过滤路径和UIL删除路径(filterPath/strUilPath)
    3、再根据配置的路径执行过滤和删除动作
 
注意:
    SourceCode中,有一些是 TV_IDS_String_ID+Offset方式获取新字串的,这些字串要手动加到脚本,以防误删
        如:TV_IDS_String_GMT_0 字串
'''
 
#=======注意此处设置UI项目================================#
#=="mstar"/"formal"/"haier"/"videocon"/"bbk"/"atv_project"=#
strUiPorject = "mstar"
#========================================================#
 
g_deleteMode = 0
g_priorityMode = 0
g_AddmarkMode = 0
g_CompareMode = 0
 
setStr = set()
tupleStr = ()
strMark = "aaaa"
 
#=======================以下不需要设置=======================#
if "bbk" in strUiPorject:
    filterPath = "aps/application/radisson/formal"
    strUilPath = "aps/application/radisson/formal/UI_Project/TV_UIProject/Languages"
elif "formal" in strUiPorject:
    filterPath = "aps/application/radisson/formal"
    strUilPath = "aps/application/radisson/formal/UI_Project/TV_UIProject_new/Languages"
else:
    filterPath = "aps/application/radisson/%s" % strUiPorject
    strUilPath = "aps/application/radisson/%s/UI_Project/TV_UIProject/Languages" % strUiPorject
gamePath = "aps/game"
 
listPath = [filterPath,gamePath]
 
def filterUsefulString():
    listStrId = []
    for path in listPath:
        for  dirPath, dirNames, fileNames in os.walk(path):
            for  sourceFile in fileNames:
                filePath = dirPath+"/"+sourceFile
                if (re.search(".*\.c.*",sourceFile) or re.search(".*\.h.*",sourceFile)) \
                    and sourceFile != "TV_strid.h":
                    for line in open(filePath,"r"):
                        if "TV_IDS_String" in line:
                            if line.count("TV_IDS_String") > 2:
                                print "\n\nthe number of string are more than 2 in a row \n\n "
                                print sourceFile
                                print "\n"
                                continue
                            if re.search(".*TV_(IDS_String\w*).*TV_(IDS_String\w*).*",line):
                                tupleStr=re.search(".*TV_(IDS_String\w*).*TV_(IDS_String\w*).*",line).groups()
                                for i in range(len(tupleStr)):
                                    setStr.add(tupleStr[i])
                            else:
                                setStr.add(re.search(".*TV_(IDS_String\w*).*",line).group(1))
                        elif "TV_IDS_Game_Menu_OSD_String" in line:
                            setStr.add("IDS_Game_Menu_OSD_String")
 
    print "\n\n程序中共使用 %d 个字串。\n保存在当前目录 UsedString.h-文件中\n\n" % len(setStr)
    for line in setStr:
        listStrId.append(line)
        listStrId.append("\n")
    open("UsedString.h","w").writelines(listStrId)
 
'''
    #读EXCEL到映射表
def excelSetting()
    setElStr = set()
    mapStr = {}
    listFirst = []
    setDiff = set()
    book = xlrd.open_workbook(r'Languages.xls')
    sheet = book.sheet_by_index(0)
 
    listFirst = sheet.row_values(0)
 
    for row in range(sheet.nrows):
        cellStr = str(sheet.cell(row,0).value)
        cellStr.rstrip()
        if cellStr in setStr:
            mapStr[cellStr] = sheet.row_values(row)
 
    #setElStr = set(mapStr.keys())
    #setDiff = setElStr - setStr
 
    #写EXCEL
    wboot = xlwt.Workbook()
    sheet = wboot.add_sheet("Language")
    #操作第一行,抬头
    for col in range(len(listFirst)):
        sheet.write(0,col,listFirst[col])
 
    #其它行
    row = 1
    for (k,v) in mapStr.items():
        for col in range(len(v)):
            sheet.write(row,col,v[col])
        row = row + 1
    wboot.save(r'Language_.xls')
     
         
'''
#处理UIL文件,对比setStr集合,删除无用字串
def deleteString():
    delCount = 0
    lanList = []
    for dirPath,dirNames,fileNames in os.walk(strUilPath):
        for sourceFile in fileNames:
            filePath = dirPath + "/" + sourceFile
            for line in open(filePath,"r"):
                #==============================================#
                #有些字串在code中是以偏移量的方式使用,不能删除
                if "IDS_String_GMT_" in line:
                    lanList.append(line)
                    continue
                elif re.search(".*IDS_String_\d{1,2}\".*",line) or ("IDS_String_LNB" in line):
                    lanList.append(line)
                    continue
                # ==============================================#
 
                if "<String ID=" in line:
                    if re.search("\s*<String ID=\"(\w*)\".*",line).group(1) in setStr:
                        lanList.append(line)
                    else:
                        delCount = delCount+1
                else:
                    lanList.append(line)
            open(filePath,"w").writelines(lanList)
            print(sourceFile + "删除 %s" %delCount)
            lanList = []
            delCount = 0
             
#处理UIL文件,迭代lanFist集合,标记字串放在UIL文件前面
def priorityString():
    lanFist = []
    lanList1 = []
    lanList2 = []
    lanList3 = []
    pat = re.compile(".*\"(IDS_String\w*)\".*")
    for line in open(strUilPath+"/English.uil","r").readlines():
        if strMark in line and pat.search(line):
            lanFist.append(pat.search(line).group(1))
    open("PrimaryString.h", "a").writelines([x + "\n" for x in lanFist])
    print "优先级字串共%d,如下:" %len(lanFist)
    print lanFist
     
    for dirPath, dirNames, fileNames in os.walk(strUilPath):
        for sourceFile in fileNames:
            filePath = dirPath + "/" + sourceFile
            for line in open(filePath, "r"):
                if pat.search(line) and pat.search(line).group(1) in lanFist:
                    line = line.replace(strMark,"")
                    lanList1.append(line)
                elif "IDS_String_spliteLine" in line:
                    lanList3.append(line)
                else:
                    lanList2.append(line)
            if(len(lanList1) and len(lanList2)>=3):
                lanList2 = lanList2[0:2] +lanList1 + lanList3+lanList2[2:]
                open(filePath, "w").writelines(lanList2)
            lanList1 = []
            lanList2 = []
 
#给已整理好优先级高的字串添加strMark
def AddMark():
    StringList = []
    a = 0
    for line in open(strUilPath+"/English.uil","r") :
        if ("<String ID=" in line): 
            if re.search("IDS_String_spliteLine",line):
                a = 1
            elif(a == 0):
                line = line.replace('Value="','Value="'+strMark)
        StringList.append(line)
    open(strUilPath+"/English.uil","w").writelines(StringList)
    print "\n添加StrMark完成\n"
 
#读取 优先级字串整理.h 中的字串进行比对添加标记(未整理)
def CompareAddMark():
    strSet = set()
    strList = []
    for line in open("PrimaryString.h", "r"):
        strSet.add(re.search(".*(IDS_String_.*).*",line).group(1))
    print strSet
    for line in open(strUilPath+"/English.uil","r") :
        if "<String ID=" in line:
            if re.search("\s*<String ID=\"(\w*)\".*", line).group(1) in strSet:
                print "a\n"
                line = line.replace('Value="','Value="'+strMark)
        strList.append(line)
    open(strUilPath+"/English.uil","w").writelines(strList)   
     
def fun_parse_InputParam():
    global g_deleteMode
    global g_priorityMode
    global g_AddmarkMode
    global g_CompareMode
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'dpac')
    except getopt.GetoptError, err:
        #print str(err)
        sys.exit()
 
    for op, value in opts:
        if op == "-d":
            g_deleteMode = 1
        elif op == "-p":
            g_priorityMode = 1
        elif op == "-a":
            g_AddmarkMode = 1
        elif op == "-c":
            g_CompareMode = 1
        else:
            print("unhandled option")
            sys.exit()
 
             
if __name__ == "__main__":
    fun_parse_InputParam()
    if g_deleteMode:
        filterUsefulString()
        deleteString()
    if g_priorityMode:
        priorityString()
    if g_AddmarkMode:
        AddMark()
    if g_CompareMode:
        CompareAddMark()

  

posted @   轻轻的吻  阅读(255)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示