import os
class trans_table:
def __init__(self):
# 0 1 2 3 4 5 6
self.target_v = ['Suite name', 'Test name', 'Test number', 'Bin_s_num', 'Bin_h_num', 'Bin_type', 'Bin_overon']
self.qaandft = ['Lsl_typ', 'Usl_typ', 'Lsl', 'Usl', 'Units']
self.file_head_file_name = ''
self.file_head = ['#LimitTable,,,,,,,,,,,,,,,,,,,,,\n',
'#Block,LimitTableBlock,'+self.file_head_file_name+',,,,,,,,,,,,,,,,,,,\n',
'SetNames,,,,,,,Default,Default,,,,,,,,,,,,,\n',
'#Title,FlowName,TestInstance,TestName,TestNumber,LowCompsign,HighCompsign,LowLimit,HighLimit,'
'Scale,Units,Format,SPassBin,SFailBin,HPassBin,HFailBin,Result,ActionPass,ActionFail,Assume,'
'Sites,Comment\n']
self.line_head = 'LimitItem,'
self.line_tail = ',,,,,\n'
self.compsign = {'GE': '>=', 'GT': '>', 'LE': '<=', 'LT': '<', 'NA': 'NA', '': ''}
self.overon = {'no': 'FailStop', 'yes': 'Fail', '': '', 'good': 'Pass'}
self.flow = {}
self.ltm_flag = False
self.xml_head = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n'
self.ltf_head = '<limittablefile version="1.1">\n <limitpairs>\n <limitpair>Default</limitpair>\n </limitpairs>\n <limittable>\n'
self.ltf_tail = ' </limittable>\n</limittablefile>'
self.ltm_head = '<limittablemaster version="1.1">\n'
self.ltm_tail = '</limittablemaster>'
self.ltf_list = []
def reset(self):
self.flow = {}
self.ltf_list = []
def set_newfile_head(self, stmp):
self.file_head_file_name = stmp.strip('.csv')+'.ltf'
self.file_head = ['#LimitTable,,,,,,,,,,,,,,,,,,,,,\n',
'#Block,LimitTableBlock,' + self.file_head_file_name + ',,,,,,,,,,,,,,,,,,,\n',
'SetNames,,,,,,,Default,Default,,,,,,,,,,,,,\n',
'#Title,FlowName,TestInstance,TestName,TestNumber,LowCompsign,HighCompsign,LowLimit,HighLimit,'
'Scale,Units,Format,SPassBin,SFailBin,HPassBin,HFailBin,Result,ActionPass,ActionFail,Assume,'
'Sites,Comment\n']
def read_file(self, fpath, inpath, outpath):
fpath = fpath.replace('\\', '/')
inpath = inpath.replace('\\', '/')
outpath = outpath.replace('\\', '/')
l_index = 0
head = []
mode = []
data_main = []
data_qa = []
data_ft = []
with open(fpath) as f:
for line in f.readlines():
l_index += 1
l = line.strip().split(',')
for ii in range(len(l)):
l[ii] = l[ii].strip('\"')
if l_index == 1:
head = l
if head[:3] != ['Suite name', 'Test name', 'Test number']:
print('Error file:' + fpath)
return
else:
print('Reading file:' + fpath)
elif l_index == 2:
mode = l
elif l_index > 2:
tmp_data_main = [''] * len(self.target_v)
tmp_data_qa = [''] * len(self.qaandft)
tmp_data_ft = [''] * len(self.qaandft)
for il in range(len(l)):
if l[il] == '':
continue
if head[il] in self.target_v:
tmp_data_main[self.target_v.index(head[il])] = l[il]
elif head[il] in self.qaandft:
# phoneix sys only allow 6 number after .
if '.' in l[il]:
tmpli = l[il].split('.')
if len(tmpli) == 2 and tmpli[1].isdigit() and len(tmpli[1]) > 6:
if int(tmpli[1][6]) > 4:
tmptail = str(int(tmpli[1][:6])+1)
while len(tmptail) < 6:
tmptail = '0'+tmptail
l[il] = tmpli[0] + '.' + tmptail
else:
l[il] = tmpli[0] + '.' + tmpli[1][:6]
if 'FT' in mode[il]:
tmp_data_ft[self.qaandft.index(head[il])] = l[il]
elif 'QA' in mode[il]:
tmp_data_qa[self.qaandft.index(head[il])] = l[il]
data_main.append(tmp_data_main)
data_ft.append(tmp_data_ft)
data_qa.append(tmp_data_qa)
if self.ltm_flag:
self.xml_write(data_main, data_ft, data_qa, fpath, inpath, outpath)
else:
self.file_write(data_main, data_ft, data_qa, fpath, inpath, outpath)
def xml_write(self, main, ft, qa, fpath, inpath, outpath):
print(fpath, inpath)
tmnp_fpath = fpath.split('/')
tmpqa = outpath
f = open(tmpqa +'/'+ tmnp_fpath[-1][:-3]+'ltf', 'w')
print('Writing file:' + tmpqa + tmnp_fpath[-1][:-3]+'ltf')
f.writelines(self.xml_head)
f.writelines(self.ltf_head)
for tmpi in range(len(main)):
s_tmp = self.xml_convert(main[tmpi], ft[tmpi])
if s_tmp:
f.writelines(s_tmp)
f.writelines(self.ltf_tail)
f.close()
self.ltf_list.append(tmnp_fpath[-1][:-3]+'ltf')
def xml_convert(self, main, detail):
tmp = self.convert(main, detail)
tmp_l = tmp.split(',')
re = ''
if tmp_l[2]:
re = ' <row comment=\"\" flowname=\"'+tmp_l[1]+'\" testinstance=\"'+tmp_l[2]+'\" testname=\"'+tmp_l[3]+'\" testnumber=\"'+tmp_l[4]+'">\n'+' <action/>\n <bin hfailbin=\"'+tmp_l[15]+'\" hpassbin=\"'+tmp_l[14]+'\" result=\"'+tmp_l[16]+'\" sfailbin=\"'+tmp_l[13]+'\" spassbin=\"'+tmp_l[12]+'\"/>\n <limit highcompsign=\"'+tmp_l[6]+'\" lowcompsign=\"'+tmp_l[5]+'\" units=\"'+tmp_l[10]+'\">\n <pair highlimit=\"'+tmp_l[8]+'\" lowlimit=\"'+tmp_l[7]+'\" name=\"Default\"/>\n </limit>\n </row>\n'
return re
def write_ltm(self, outpath):
f = open(outpath+'/tmp.ltm', 'w')
f.writelines(self.xml_head)
f.writelines(self.ltm_head)
for l in self.ltf_list:
f.write(' <limittablefile path=\"'+l+'\"/>\n')
f.writelines(self.ltm_tail)
f.close()
def file_write(self, main, ft, qa, fpath, inpath, outpath):
tmnp_fpath = fpath.split(inpath, 1)[1].split('/')
tmpqa = outpath+'/QA'+'/'.join(tmnp_fpath[:-1])
tmpft = outpath+'/FT'+'/'.join(tmnp_fpath[:-1])
if not os.path.exists(tmpqa):
os.makedirs(tmpqa)
if not os.path.exists(tmpft):
os.makedirs(tmpft)
qaf = open(tmpqa+'/QA_cc_'+tmnp_fpath[-1], 'w')
print('Writing file:'+tmpqa+'/FT_cc_'+tmnp_fpath[-1])
print('Writing file:'+tmpft+'/FT_cc_'+tmnp_fpath[-1])
self.set_newfile_head(tmnp_fpath[-1])
qaf.writelines(self.file_head)
ftf = open(tmpft+'/FT_cc_'+tmnp_fpath[-1], 'w')
ftf.writelines(self.file_head)
for tmpi in range(len(main)):
qaf.write(self.convert(main[tmpi], qa[tmpi]))
ftf.write(self.convert(main[tmpi], ft[tmpi]))
qaf.close()
ftf.close()
def convert(self, main, detail):
# 0 1 2 3 4 5 6
# main = ['Suite name', 'Test name', 'Test number', 'Bin_s_num', 'Bin_h_num', 'Bin_type', 'Bin_overon']
if not main[0]:
return ',,,,,,,,,,,,,,,,,,,,\n'
re = main[0]+','+main[1]+','+main[2]+','
re += self.compsign[detail[0]]+','+self.compsign[detail[1]]+','+detail[2]+','+detail[3]+',1,'+detail[4]+',,'
if main[5] == 'bad':
re += ','+main[3]+',,'+main[4]+','+self.overon[main[6]]
else:
print(main)
re += main[3] + ',,' + main[4] + ',,' + self.overon[main[5]]
tmp_head = self.line_head
if self.flow.get(main[0]):
tmp_head += self.flow[main[0]]+','
else:
tmp_head += 'mainflow,'
return tmp_head+re+self.line_tail
def read_flow(self, flow_fname):
flow_fname = flow_fname.replace('\\', '/')
if not flow_fname:
return
tmp = flow_fname.split('/')[-1]
flow_path = flow_fname[:len(flow_fname)-len(tmp)]
flow_name = tmp.split('.')[0]
with open(flow_fname) as f_name:
for line in f_name.readlines():
l = line.split()
if l:
if l[0] == '<row':
opcode = line.split('opcode=\"')[-1].split('\"')[0]
testname = line.split('operand=\"')[-1].split('\"')[0]
if opcode == 'Test':
if self.flow.get(testname) and self.flow[testname] != flow_name:
self.flow[testname] += '|'+flow_name
else:
self.flow[testname] = flow_name
elif opcode == 'Call':
self.read_flow(flow_path+testname)
def execute(self, inpath, mfh, outpath, flow_f, ltm_flag = False):
# if not mfh:
# file_list = find_file(inpath)
# else:
# file_list = an_find_file(mfh)
self.reset()
self.ltm_flag = ltm_flag
for fff in flow_f:
self.read_flow(fff)
if not mfh:
file_list = find_file(inpath)
for files in file_list:
if 'FT_cc_' not in files and 'QA_cc_' not in files:
self.read_file(files, inpath, outpath)
else:
file_list = an_find_file(mfh)
for files in file_list:
if 'FT_cc_' not in files and 'QA_cc_' not in files:
self.read_file(files, inpath, outpath)
if self.ltm_flag:
self.write_ltm(outpath)
def find_file(path):
l = []
for ospath, dirs, files in os.walk(path):
for file in files:
if os.path.splitext(file)[1] == '.csv':
l.append(os.path.join(ospath, file))
for dir in dirs:
l += find_file(os.path.join(ospath, dir))
return l
def an_find_file(mfh):
re = []
mfh = mfh.replace('\\', '/')
tmp = mfh.split('/')
path = '/'.join(tmp[:-1])
f_mfh = tmp[-1]
with open(mfh) as f:
for line in f.readlines():
l = line.split('#')[0].split()
if len(l) >2 and l[0] == 'testerfile' and l[1] == ':':
re.append(path+'/'+l[2])
return re
'''if __name__ == '__main__':
path = './'
table_f = find_file(path)
tt = trans_table()
print(table_f)
tt.execute(table_f, path, path)
input('press any key to end!')'''
优化上述代码的格式,命名,调用
最新发布