__author__ = 'ywh'
#!/usr/bin/python3
import re
import datetime
import xlrd
from xlrd import xldate_as_tuple
def read_cell(filename, index, row, col):
rbook = xlrd.open_workbook(filename)
sheet = rbook.sheet_by_index(index)
ctype = sheet.cell(row, col).ctype # 表格的数据类型
cell = sheet.cell_value(row, col)
if ctype == 2 and cell % 1 == 0: # 如果是整形+
cell = int(cell)
cell = str(cell)
elif ctype == 3:
# 转成datetime对象
date = datetime(*xldate_as_tuple(cell, 0))
cell = date.strftime('%Y/%d/%m %H:%M:%S')
elif ctype == 4:
cell = True if cell == 1 else False
return cell
def read_sourcecode_excel(filename, index):
rbook = xlrd.open_workbook(filename)
sheet = rbook.sheet_by_index(index)
stypeCode = None
cla = None
scs = None
oty = None
resultDic = {}
rows = sheet.nrows
cols = sheet.ncols
for i in range(rows-1):
for j in range(cols):
if j ==0:
stypeCode = read_cell(filename, 0, i+1, j)
if j == 2:
cla = read_cell(filename, 0, i+1, j)
if j == 3:
scs = read_cell(filename, 0, i+1, j)
if j == 4:
oty = read_cell(filename, 0, i+1, j)
if stypeCode != None and cla != None and scs!= None and oty != None:
resultDicKey = cla + "," + scs + "," + oty
#当2个同样的resultDicKey,存多个stypeCode
#eg: JZ_SOURCE_004222 新增某新类 3 3 5 新增某新类特征信息
# JZ_SOURCE_0040 硬件特征信息代码表 3 3 5 硬件特征信息
# 3,3,5:JZ_SOURCE_004222:JZ_SOURCE_0040
if resultDicKey in resultDic and resultDicKey != ',,':
resultDic[resultDicKey] = resultDic[resultDicKey] + ":" + stypeCode
else:
resultDic[resultDicKey] = stypeCode
return resultDic
def read_sourcecode_app_excel(filename, index):
rbook = xlrd.open_workbook(filename)
sheet = rbook.sheet_by_index(index)
stypeCode = None
stypeCode2 = ''
appName = None
dic = {}
resultDic = {}
listDic = []
rows = sheet.nrows
cols = sheet.ncols
for i in range(rows-1):
for j in range(cols):
if j ==0:
stypeCode = read_cell(filename, 1, i+1, j)
if j ==1:
appCode = read_cell(filename, 1, i+1, j)
if j == 2:
appName = read_cell(filename, 1, i+1, j)
appName = appName.strip()
if stypeCode != None and appName != None and appCode != None:
if stypeCode != stypeCode2:
if stypeCode2 != '':
resultDic[stypeCode2] = listDic
listDic = []
stypeCode2 = stypeCode
dicListKey = stypeCode2
dic[appName] = appCode
else:
dic[appName] = appCode
listDic.append(dic)
dic = {}
resultDic[dicListKey] = listDic
return resultDic
def writeResult(item, siteValueName):
flag = False
actionPattern = re.compile(r'.*=\d*,')
subclassActionValueList = actionPattern.findall(item)
for actionItem in subclassActionValueList:
if re.search('--.*=\d*,', actionItem):
#print("此action已注释")
continue
else:
#print("此action未注释")
actionKey = actionItem[0:actionItem.index('=')]
actionKey = actionKey.strip()
actionValue = actionItem[actionItem.index('=')+1:actionItem.index(',')]
actionDic[actionKey] = actionValue
sourceCodekey = classValue + ","+ subclassValue + "," + actionValue
#sourceCodeDic是从“类型对照表”读取出来的stypeCode
sourceCode1 = ''
if sourceCodekey in sourceCodeDic:
sourceCode = sourceCodeDic[sourceCodekey]
if ':' in sourceCode:
count = sourceCode.count(':')
startIndex = 0
leftSourceCode = sourceCode
for i in range (count + 1):
if i != count:
endIndex = leftSourceCode.index(':')
sourceCode1 = leftSourceCode[startIndex:endIndex]
leftSourceCode = leftSourceCode[endIndex + 1:]
elif ':' not in leftSourceCode:
sourceCode1 = leftSourceCode
if sourceCode1 in sourceCodeDicAppDic:
dicList = sourceCodeDicAppDic[sourceCode1]
for dic in dicList:
siteName = None
if siteValueName in dic:
siteName = siteValueName
flag = True
resultFile.write(dic[siteName] + "," + siteValueName + "," + classValue + "," + subclassValue + "," + siteValueCode + "," + actionValue + "," + sourceCode1 + '\n')
elif siteValueName.upper() in dic:
siteName = siteValueName.upper()
flag = True
resultFile.write(dic[siteName] + "," + siteValueName + "," + classValue + "," + subclassValue + "," + siteValueCode + "," + actionValue + "," + sourceCode1 + '\n')
elif siteValueName.lower() in dic:
flag = True
siteName = siteValueName.lower()
resultFile.write(dic[siteName] + "," + siteValueName + "," + classValue + "," + subclassValue + "," + siteValueCode + "," + actionValue + "," + sourceCode1 + '\n')
if flag == False:
resultFileWithoutCode.write(siteValueName + "," + classValue + "," + subclassValue + "," + siteValueCode + "," + actionValue + "," + sourceCode1 + '\n')
continue
else:
resultFileWithoutStypeCode.write(siteValueName + "," + classValue + "," + subclassValue + "," + siteValueCode + "," + actionValue + '\n')
elif sourceCode in sourceCodeDicAppDic:
dicList = sourceCodeDicAppDic[sourceCode]
for dic in dicList:
siteName = None
if siteValueName in dic:
siteName = siteValueName
flag = True
resultFile.write(dic[siteName] + "," + siteValueName + "," + classValue + "," + subclassValue + "," + siteValueCode + "," + actionValue + "," + sourceCode + '\n')
elif siteValueName.upper() in dic:
siteName = siteValueName.upper()
flag = True
resultFile.write(dic[siteName] + "," + siteValueName + "," + classValue + "," + subclassValue + "," + siteValueCode + "," + actionValue + "," + sourceCode + '\n')
elif siteValueName.lower() in dic:
flag = True
siteName = siteValueName.lower()
resultFile.write(dic[siteName] + "," + siteValueName + "," + classValue + "," + subclassValue + "," + siteValueCode + "," + actionValue + "," + sourceCode + '\n')
if flag == False:
resultFileWithoutCode.write(siteValueName + "," + classValue + "," + subclassValue + "," + siteValueCode + "," + actionValue + "," + sourceCode + '\n')
continue
else:
resultFileWithoutStypeCode.write(siteValueName + "," + classValue + "," + subclassValue + "," + siteValueCode + "," + actionValue + '\n')
def find_class(webClassList, className):
isClassExit = webClassList.find("_G[\"" + className + "\"]")
if isClassExit != -1 :
#"_G\\[\"class\"\\]={[\s\S]*}"最长匹配
#"_G\\[\"class\"\\]={[\s\S]*?}"最短匹配
#返回匹配到的下标
#classValueList = re.search("_G\\[\"class\"\\]={[\s\S]*?}", webClassList).span()
classValueString = re.search("_G\\[\"" + className + "\"\\]={[\s\S]*?}", webClassList).group(0)
pattern = re.compile(r'.*=\d*,.*\n')
classValueList = pattern.findall(classValueString)
return classValueList
def find_site(webClassList, className, subclassName):
siteName = className + "_" + subclassName + "_" + "site"
#print("siteName: ", siteName)
isSiteExit = webClassList.find("_G[\""+ siteName +"\"]")
if isSiteExit == -1 :
siteName = subclassName + "_" + "site"
isSiteExit = webClassList.find("_G[\""+ siteName +"\"]")
if isSiteExit != -1 :
siteValueString = re.search("_G\\[\""+ siteName +"\"\\]={[\s\S]*?}", webClassList).group(0)
pattern = re.compile(r'.*=\d*,.*\n')
siteValueList = pattern.findall(siteValueString)
return siteValueList
def find_oty(webClassList):
isActionExit = webClassList.find("_G[\"action\"]")
if isActionExit != -1 :
actionValueString = re.search("_G\\[\"action\"\\]={[\s\S]*?}", webClassList).group(0)
pattern = re.compile(r'\s*--[^=]*\n[\s\S]*?\n\s*\n')
actionValueList = pattern.findall(actionValueString)
return actionValueList
starttime = datetime.datetime.now()
try:
resultFileWithoutStypeCode = open("JZ_SOURCEWithoutStypeCode.sit","w+", encoding="utf-8")
resultFileWithoutStypeCode.write("codeName" + "," + "CLA" + "," + "SCS" + "," + "SIT" + "," + "OTY" + '\n')
resultFileWithoutCode = open("JZ_SOURCEWithoutCode.sit", "w+", encoding="utf-8")
resultFileWithoutCode.write("codeName" + "," + "CLA" + "," + "SCS" + "," + "SIT" + "," + "OTY" + "," + "stypeCode" + '\n')
resultFile = open("JZ_SOURCE.sit","w+", encoding="utf-8")
resultFile.write("code" + "," + "codeName" + "," + "CLA" + "," + "SCS" + "," + "SIT" + "," + "OTY" + "," + "stypeCode" +'\n')
webClassFile = open("web_class.aps",encoding='utf-8',errors='ignore')
webClassList = webClassFile.read()
rbook = None
sourceCodeDic = read_sourcecode_excel("stypeCode.xlsx", 0)
sourceCodeDicAppDic = read_sourcecode_app_excel("stypeCode.xlsx", 1)
#step1:找Class
#大类映射
classDic = {}
#大小类映射
classSubclassDic = {}
#小类SITE映射
subclassSitDic = {}
#小类映射
subclassDic = {}
#site映射
siteDic = {}
#action映射
actionDic = {}
#resultDic映射
#resultDic = {}
classValueList = find_class(webClassList, "class")
if classValueList != None:
for item in classValueList:
if re.search('--.*=\d*,', item):
#print("此CLASS已注释")
continue
else:
#print("此CLASS未注释")
classKey = item[0:item.index('=')]
classKey = classKey.strip()
classValue = item[item.index('=')+1:item.index(',')]
classDic[classKey] = classValue
#step2:找subclass
className = classKey.lower()
subclassValueList = find_class(webClassList, className)
if subclassValueList != None:
for item in subclassValueList:
if re.search('--.*=\d*,', item):
#print("此SUBCLASS已注释")
continue
else:
#print("此SUBCLASS未注释")
subclassKey = item[0:item.index('=')]
subclassKey = subclassKey.strip()
subclassValue = item[item.index('=')+1:item.index(',')]
subclassDic[subclassKey] = subclassValue
#step3:找site
subclassName = subclassKey.lower()
siteValueList = find_site(webClassList, className, subclassName)
if siteValueList != None:
for item in siteValueList:
if re.search('--.*=\d*,', item):
#print("此SITE已注释")
continue
else:
#print("此SITE未注释")
siteKey = item[0:item.index('=')]
siteKey = siteKey.strip()
siteValueCode = item[item.index('=')+1:item.index(',')]
if re.search('--.*\n', item) != None:
siteValueName = item[item.index('--')+2:item.index('\n')]
siteValueName = siteValueName.strip()
siteDic[siteKey] = siteValueName
#step:找OTY
actionValueList = find_oty(webClassList)
if actionValueList != None:
for item in actionValueList:
#截取出subclass列表,eg:--comment/blog/weibo/bbs/sns
item = item.strip()
subclassList = item[item.index('--')+2:item.index('\n')]
subclassList = subclassList.lower()
#判断加什么OTY
actionKey = className + "_" + subclassName
if actionKey in subclassList:
writeResult(item, siteValueName)
elif subclassName in subclassList:
writeResult(item, siteValueName)
elif className in subclassList:
writeResult(item, siteValueName)
#common action:LOGIN/LOGOUT/REGISTER每类都加
elif "common" in subclassList:
writeResult(item, siteValueName)
subclassSitDic[subclassKey] = siteDic
classSubclassDic[classKey] = subclassDic
finally:
if webClassFile:
webClassFile.close()
if resultFile:
resultFile.close()
if rbook:
rbook.close()
if resultFileWithoutStypeCode:
resultFileWithoutStypeCode.close()
if resultFileWithoutCode:
resultFileWithoutCode.close()
endtime = datetime.datetime.now()
print(endtime-starttime)
根据几个文件形成目标映射文件
最新推荐文章于 2021-01-31 11:30:36 发布