import configparser
import re, os
import openpyxl,csv
import glob
from openpyxl.styles import PatternFill
STR_SPACE1 = " "
DATA_BYTE1 = 8
wb = None
ws = None
byteRow = ""
memoRow = ""
config = {}
def init_config():
print("init_config call")
global config
con = configparser.ConfigParser()
con.read("FunctionTest.ini")
for sec in con.sections():
tmp = {}
key: object
for (key, val) in con.items(sec):
tmp[key] = val
config[sec] = tmp
def _reduce(xls_arr):
new_xls = []
for line in xls_arr:
if len(new_xls) == 0:
new_xls.append(line)
else:
find = 0
for item in new_xls:
if item["DataPos"] == line["DataPos"] and item["DataLength"] == line["DataLength"] and item["CANID"] == line["CANID"]:
find = 1
break
if find == 0:
new_xls.append(line)
return new_xls
# 1.1 open xlsx
def s1_generate_xls_array(xls, sheet, MsgLabel, CANID, DataPos, DataLength, debug):
xls_data = []
last_msg_label = ""
last_can_id = ""
global wb, ws
wb = openpyxl.load_workbook(xls)
ws = wb[sheet]
steps = {}
stepstart = 9
stepend = 0
if sheet == "1Trip動作確認(HV)" or sheet == "1Trip動作確認" or sheet == "1Trip動作確認(ISG)" or sheet == "CAN出力データ_HV_CSA" or sheet == "CAN出力データ_ISG_CSA":
stepend = 38
elif sheet == "CAN出力データ_HV_ICS" or sheet == "CAN出力データ_ISG_ICS":
stepend = 23
elif sheet == "MUTE_クリソナ検知表示":
stepend = 56
elif sheet == "ソナー音量のカスタマイズ対応" or sheet == "MUTE_RCDブザー吹鳴あり":
stepend = 23
for row_num, row in enumerate(ws.iter_rows(), start=1):
print("%s %s %s" % (xls, sheet, row_num))
data_pos = ""
data_len = ""
msg_label = ""
can_id = ""
step1expectval = ""
for (col_num, cell) in enumerate(row, start=1):
idx = str(col_num)
print ( str(row_num) +" "+ str(col_num) +" " + str( cell.value))
if (MsgLabel == idx):
msg_label = str(cell.value)
elif (CANID == idx):
can_id = str(cell.value)
elif (DataPos == idx):
data_pos = str(cell.value)
elif (DataLength == idx):
data_len = str(cell.value)
elif (int(idx) >= int(stepstart) and int(idx) <= int(stepend) ):
if ("12" == idx):
step1expectval = str(cell.value)
ddd=str(cell.value)
if -1 != str(cell.value).find("step"):
steps[str(cell.value)] = ws.cell(row_num + 2, col_num ).value #row_num + 1
###########################
#temp = ws.cell(row_num + 1, col_num ).fill.start_color.rgb
#print( "66 14 = " + str(ws.cell(66, 14 ).fill.start_color.rgb))
###########################
print("---")
if msg_label and msg_label.upper() != "NONE":
last_msg_label = msg_label
if can_id and can_id.upper() != "NONE":
last_can_id = can_id
if can_id == 119:
print()
if data_pos and data_len:
line = _check_xls_data(last_msg_label, last_can_id, msg_label, can_id, data_pos, data_len, step1expectval)
if line:
line["row"] = row_num
xls_data.append(line)
xls_data = _reduce(xls_data)
if int(debug) > 0:
# print("write %s %s" % (xls, sheet))
f = openpyxl.Workbook()
table = f.create_sheet(sheet, 0)
table.cell(row=1, column=1, value="msg_label")
table.cell(row=1, column=2, value="can_id")
table.cell(row=1, column=3, value="data_pos")
table.cell(row=1, column=4, value="data_length")
for (line_num, lines) in enumerate(xls_data, start=2):
# print("write line %s" % str(line_num))
table.cell(row=line_num, column=1, value=lines["MsgLabel"])
table.cell(row=line_num, column=2, value=lines["CANID"])
table.cell(row=line_num, column=3, value=lines["DataPos"])
table.cell(row=line_num, column=4, value=lines["DataLength"])
# f.save(os.path.splitext(xls)[0] + "_lines.xlsx")
f.save(sheet + "_lines.xlsx")
return xls_data,steps
def _check_xls_data(last_msg_label, last_can_id, msg_label, can_id, data_pos, data_len, step1expectval):
lines = {}
if not data_pos:
return ""
if not data_len:
return ""
if "-" == step1expectval or "ー" == step1expectval or "‐" == step1expectval or "-" == step1expectval or "―" == step1expectval:
return ""
if re.match("[1-3]?[0-9]\.?[0-8]?$", data_pos) and re.match("[\d][\d]?$", data_len):
lines["DataPos"] = data_pos
lines["DataLength"] = data_len
if msg_label.upper() == "NONE":
msg_label = last_msg_label
if can_id.upper() == "NONE":
can_id = last_can_id
lines["MsgLabel"] = msg_label.upper().strip()
lines["CANID"] = can_id.upper().strip()
return lines
else:
return ""
# 2.1 open log file <LOOP>
# 2.2 search need line from log
# 2.3 generate log array
def s2_generate_log_array():
file_array = {}
for sec, items in config.items():
if not re.match("log", sec, re.IGNORECASE):
continue
debug = items.get("debug", "")
# sheet = items.get("sheetname", "")
for fname in glob.glob("**/*.asc", recursive=True):
farr = []
print("1/3 analyse %s" % fname)
with open(fname, 'r', encoding="utf-8", errors="ignore") as f:
while True:
line = f.readline()
if not line:
break
else:
match1 = re.match(
"\s*([\d]+\.[\d]{6})\sCANFD\s+[\d]+\s+[a-zA-Z]{2}\s+([a-zA-Z0-9]+)\s+[a-zA-Z0-9]+\s+[\d]\s[\d]\s[a-zA-Z0-9]\s+[\d]{1,2}((\s[a-zA-Z0-9]{2})+)\s",
line)
match2 = re.match(
"\s*([\d]+\.[\d]{6})\s+[\d]+\s+([\w]+)\s+[\w]+\s+d\s[\d]\s([0-9A-F\s]+)\s",
line)
if match1:
farr.append([item.upper().strip() for item in list(match1.groups())])
if match2:
farr.append([item.upper().strip() for item in list(match2.groups())])
file_array[fname] = farr
if int(debug) > 0:
f = openpyxl.Workbook()
count = 0
for (fname, arr) in file_array.items():
print("1/3 write " + fname)
table = f.create_sheet("log_sheet" + str(count))
table.cell(row=1, column=1, value=fname)
for (line_num, line) in enumerate(arr, start=2):
# print("write " + str(line_num))
for (col_num, cell) in enumerate(line, start=1):
table.cell(row=line_num, column=col_num, value=cell)
count = count + 1
f.save("_log.xlsx")
return file_array
def jionLogMsgDataBin(msgdata, bytestart, bitpos, bitlength):
msgdatabin = bin(int("1" + str(msgdata).replace(STR_SPACE1, ""), 16))[3:]
bit_end = bytestart * DATA_BYTE1 - bitpos
bit_start = bit_end - bitlength
log_data_value = int(msgdatabin[bit_start:bit_end], 2)
return log_data_value
def _append_array(array, item):
arraysize = len(array)
if 0 == arraysize:
array.append(item)
else:
#jionLogMsgDataBin(array[arraysize - 1]["data"])
if not (
array[arraysize - 1]["CANID"] == item["CANID"] and
array[arraysize - 1]["dataval"] == item["dataval"] and
array[arraysize - 1]["DataPos"] == item["DataPos"] and
array[arraysize - 1]["DataLength"] == item["DataLength"]):
array.append(item)
#print(array)
# 3.3 get whole data 0 0 8 8 08 00 00 00 00 c0 00 47
def s3_get_whole_data(xls_data,steps, file_array, debug):
# 3.1 get line from line array
allstepsdata = {}
_len = len(xls_data)
for (cn, xls_lines) in enumerate(xls_data):
print("%d/%d anaylse" % (cn, _len))
msg_label = xls_lines["MsgLabel"]
can_id = xls_lines["CANID"]
# 3.2 match log from log array
stepdata = {}
for (fname, arr) in file_array.items():
whole_data = {}
for (line_num, line) in enumerate(arr):
# if len(line) < 5:
# continue
if line[1].zfill(3) == can_id:
# print("data " + line[4])
temp = xls_lines.get("data", "")
if not xls_lines.get("data", ""):
xls_lines["data"] = ""
if not xls_lines.get("data", ""):
xls_lines["timestamp"] = line[0]
xls_lines["data"] = line[2]
# new_xls.append(xls_lines)
xls_lines = s3_get_bytes(xls_lines)
dataval = jionLogMsgDataBin(xls_lines["data"], int(xls_lines["DataPos"].split(".")[0]),
int(xls_lines["DataPos"].split(".")[1]), int(xls_lines["DataLength"]))
xls_lines["dataval"] = dataval
if whole_data.get(msg_label, ""):
whole_data[msg_label].append(xls_lines)
else:
whole_data[msg_label] = []
#open(msg_label + ".csv", "w")
whole_data[msg_label].append(xls_lines)
else:
#if xls_lines.get("dataval", "") != dataval:
new_lines = {}
new_lines["MsgLabel"] = msg_label
new_lines["CANID"] = can_id
new_lines["data"] = line[2]
new_lines["timestamp"] = line[0]
new_lines["DataPos"] = xls_lines["DataPos"]
new_lines["DataLength"] = xls_lines["DataLength"]
# new_xls.append(new_lines)
new_lines = s3_get_bytes(new_lines)
dataval = jionLogMsgDataBin( new_lines["data"], int(new_lines["DataPos"].split(".")[0]),
int(new_lines["DataPos"].split(".")[1]), int(new_lines["DataLength"]))
new_lines["dataval"] = dataval
if whole_data.get(msg_label, ""):
_append_array(whole_data[msg_label], new_lines)
# whole_data[msg_label].append(new_lines)
else:
whole_data[msg_label] = []
# whole_data[msg_label].append(new_lines)
_append_array(whole_data[msg_label], new_lines)
print("")
stepdata[fname] = whole_data
allstepsdata[xls_lines["MsgLabel"]+"("+xls_lines["DataPos"]+")"] = stepdata
return allstepsdata
'''
def _write_log(whole_data):
for (label, arr) in whole_data.items():
with open(label + ".csv", "a+") as f:
for new_lines in arr:
f.write("%s,%s,%s,%s,%s,%s,%s\n" % (new_lines["timestamp"],
#new_lines["MsgLabel"], new_lines["CANID"],
new_lines["CANID"],
new_lines["DataPos"],
new_lines["DataLength"],
new_lines.get("data", ""),
new_lines.get("l8", ""),
str(new_lines.get("bytes", ""))))
'''
def _expand_str(data):
new = "0x" + data.replace(" ", "")
size = int(len(new)/2 - 1)#2->3
s16 = int(new, 16)
s64 = str(bin(s16))[2:].zfill(size*8)
l8 = []
for i in range(size):
l8.append(s64[0 + i * 8:8 + i * 8])
return l8
def _analyse_pos(pos):
match = re.match("([\d]+)\.([\d]+)", pos)
if (match):
return (int(match.group(1)), int(match.group(2)))
else:
return (int(pos), -1)
def _get_bytes(l8, begin, end, leng):
front = []
if 1 < begin:
[front.append(l8[idx]) for idx in range(begin - 1)]
else:
front = []
if end == -1:
front.append(l8[begin - 1][:8])
else:
front.append(l8[begin - 1][:8 - end])
whole = "".join(front)
return whole[len(whole) - leng:]
# 3.4 get bytes 08 00 00 00 00 c0 00 47
# 3.5 put to line array
def s3_get_bytes(xls_lines):
msg_label = xls_lines["MsgLabel"]
can_id = xls_lines["CANID"]
pos = xls_lines["DataPos"]
data_length = xls_lines["DataLength"]
data = xls_lines.get("data", "")
if not data:
xls_lines["bytes"] = ""
else:
l8 = _expand_str(data)
xls_lines["l8"] = "-".join(l8)
(begin, end) = _analyse_pos(pos)
strTmp = _get_bytes(l8, begin, end, int(data_length))
xls_lines["bytes"] = str(int(strTmp,2))# print("bytes " + xls_lines["bytes"])
return xls_lines
# 4.1 check result
# 4.2 output result
def s4_check_output(sheet, steps, xls_data, data,xls):
lenval = len(xls_data)
steps1=steps.keys()
#stepm=[]
for i in steps1:
#stepm.append(i)
for ii in memoRow:
del memoRow[0]
for (cn, xls_lines) in enumerate(xls_data):
print("%d/%d output" % (cn, lenval))
msg_label = xls_lines["MsgLabel"]+"("+xls_lines["DataPos"]+")"
msg_label1 = xls_lines["MsgLabel"] #读取msg
can_id = xls_lines["CANID"]
pos = xls_lines["DataPos"]
data_length = xls_lines["DataLength"]
msgdict = data.get(msg_label, [])
msgdict1 = msgdict.get(i,[]) #
msgdict2 = msgdict1.get(msg_label1,[])#
mbytes = []
if len(msgdict):
for new_lines in msgdict2:
if ( # new_lines["MsgLabel"] == msg_label and
new_lines["CANID"] == can_id and
new_lines["DataPos"] == pos and
new_lines["DataLength"] == data_length):
#if not new_lines.get("bytes", "") in mbytes:
mbytes.append(new_lines.get("bytes", ""))
if len(mbytes):
#ws[str(byteRow) + str(xls_lines.get("row"))] = mbytes[0]
ws[str(ii) + str(xls_lines.get("row"))] = "→".join(mbytes[0:])
break
wb.save(sheet + "_result.xlsx")
wb.save(xls)
def getbasicsteplines(actstr, startlineinde,linesdata):
stepdata = []
nextstartindex = 0
nextstartindex1=0
index = startlineinde
global SELECT1
for sec, items in config.items():
if not re.match("OUT", sec, re.IGNORECASE):
continue
SELECT1 = items.get("select", "")
shiftno = int(actstr.split(",")[0])
actflg = actstr.split(",")[1]
#actno = int(actstr.split(",")[2])
while index < len(linesdata):
tempstr = linesdata[index]
#PTCURSFTcan,PTCURSFTDPX,PTCURSFTDPY,PTCURSFTDL
if tempstr[1].strip().zfill(3).upper() == PTCURSFTcan:
tempshift = jionLogMsgDataBin(tempstr[2], PTCURSFTDPX, PTCURSFTDPY, PTCURSFTDL)
if(SELECT1=="PTCU" or SELECT1=="BPRND"):
if tempshift == 0:
print(1)
else:
if shiftno != tempshift:
break
if(SELECT1=="SFTP" or SELECT1=="XPRND" or SELECT1=="JICSXPRND" or SELECT1=="JCSAXPRND"):
if shiftno != tempshift:
break
if actflg == "125" :
if tempstr[1].strip().zfill(3).upper() == "1A0":
tempshift = jionLogMsgDataBin(tempstr[2], 3, 2, 11)
if tempshift == 125:
index = nextstartindex
break
else:
nextstartindex = index
stepdata.append(tempstr)
index = index + 1
return stepdata ,index
def getmixsteplines(actstr, startlineinde,linesdata):
stepdata = []
index = startlineinde
nextstartindex = 0
csr_mutestr = ""
global SELECT1
for sec, items in config.items():
if not re.match("OUT", sec, re.IGNORECASE):
continue
SELECT1 = items.get("select", "")
shiftno = int(actstr.split(",")[0])
actflg = actstr.split(",")[1]
actno = int(actstr.split(",")[2])
#startFlag=False
#endFlag= False
while index < len(linesdata):
tempstr = linesdata[index]
if tempstr[1].strip().zfill(3).upper() == PTCURSFTcan:
tempshift = jionLogMsgDataBin(tempstr[2], PTCURSFTDPX, PTCURSFTDPY, PTCURSFTDL)
if(SELECT1=="XPRND" or SELECT1=="BPRND"):
if tempshift == 0:
print(1)
else:
if shiftno != tempshift:
break
if(SELECT1=="SFTP" or SELECT1=="XPRND" or SELECT1=="JICSXPRND" or SELECT1=="JCSAXPRND"):
if shiftno != tempshift:
break
if actflg == "1" and actno == 0:
if tempstr[1].strip().zfill(3).upper() == "51D":
tempshift = jionLogMsgDataBin(tempstr[2], 1, 7, 1)
if tempshift == 1:
index = nextstartindex
break
else:
nextstartindex = index
if actflg == "1" and actno != 0:
if tempstr[1].strip().zfill(3).upper() == "51D":
tempshift = jionLogMsgDataBin(tempstr[2],1, 7, 1)
if tempshift == 1:
print("1")
if len(csr_mutestr) == 0:
csr_mutestr = csr_mutestr + str(tempshift)
else:
if int (csr_mutestr[len(csr_mutestr) - 1]) != tempshift:
csr_mutestr = csr_mutestr + str(tempshift)
else:
print("0")
nextstartindex = index
if len(csr_mutestr) == 0:
csr_mutestr = csr_mutestr + str(tempshift)
else:
if int (csr_mutestr[len(csr_mutestr) - 1]) != tempshift:
csr_mutestr = csr_mutestr + str(tempshift)
if "0101" == csr_mutestr:
index = nextstartindex
break
stepdata.append(tempstr)
index = index + 1
return stepdata ,index
def getmixsteplines1(actstr, startlineinde,linesdata):
stepdata = []
index = startlineinde
nextstartindex = 0
csr_mutestr = ""
global SELECT1
for sec, items in config.items():
if not re.match("OUT", sec, re.IGNORECASE):
continue
SELECT1 = items.get("select", "")
shiftno = int(actstr.split(",")[0])
actflg = actstr.split(",")[1]
actno = int(actstr.split(",")[2])
#startFlag=False
#endFlag= False
while index < len(linesdata):
tempstr = linesdata[index]
if tempstr[1].strip().zfill(3).upper() == PTCURSFTcan:
tempshift = jionLogMsgDataBin(tempstr[2], PTCURSFTDPX, PTCURSFTDPY, PTCURSFTDL)
if(SELECT1=="XPRND" or SELECT1=="BPRND"):
if tempshift == 0:
print(1)
else:
if shiftno != tempshift:
break
if(SELECT1=="SFTP" or SELECT1=="XPRND" or SELECT1=="JICSXPRND" or SELECT1=="JCSAXPRND"):
if shiftno != tempshift:
break
if actflg == "0" and actno == 1:
if tempstr[1].strip().zfill(3).upper() == "51D":
tempshift = jionLogMsgDataBin(tempstr[2], 2, 3, 12)
if tempshift == 193:
index = nextstartindex
break
else:
nextstartindex = index
if actflg == "193" and actno == 1:
if tempstr[1].strip().zfill(3).upper() == "390":
tempshift = jionLogMsgDataBin(tempstr[2],1, 4, 4)
if tempshift == 0:
index = nextstartindex
break
else:
nextstartindex = index
stepdata.append(tempstr)
index = index + 1
return stepdata ,index
def file_array_steps(file_array, steps):
allsteps_filearray = {}
stepssize = len(steps)
#farrallsteps = {}
startlineinde = 0
#stepdata = []
for stepindex in steps:
stepdata = []
if 10 == stepssize:
print("1Trip動作確認(HV) or 1Trip動作確認(ISG)")
print("1Trip動作確認")
#linecnt = len(file_array.values)
for linekey in file_array:
linesdata = file_array[linekey]
if stepindex == "step1" or stepindex == "step2" :
if stepindex == "step1" :
stepdata, cur_lineindex =getbasicsteplines(ONETRIP_BASIC_ACT[stepindex], startlineinde, linesdata)
allsteps_filearray[stepindex] = stepdata
startlineinde = cur_lineindex
else:
stepdata= allsteps_filearray["step1"]
allsteps_filearray[stepindex] = stepdata
startlineinde =startlineinde
else:
stepdata, cur_lineindex =getbasicsteplines(ONETRIP_BASIC_ACT[stepindex], startlineinde, linesdata)
allsteps_filearray[stepindex] = stepdata
startlineinde =cur_lineindex
elif 16 == stepssize:
print("MUTE_クリソナ検知表示")
for linekey in file_array:
linesdata = file_array[linekey]
stepdata, cur_lineindex =getmixsteplines(ONETRIP_MUTE_CRISINA[stepindex], startlineinde, linesdata)
allsteps_filearray[stepindex] = stepdata
startlineinde =cur_lineindex
elif 5 == stepssize:
print("ソナー音量のカスタマイズ対応")
for linekey in file_array:
linesdata = file_array[linekey]
if stepindex == "step1" or stepindex == "step2" :
if stepindex == "step1" :
stepdata, cur_lineindex =getmixsteplines1(ONETRIP_BASIC_ACT[stepindex], startlineinde, linesdata)
allsteps_filearray[stepindex] = stepdata
startlineinde = cur_lineindex
else:
stepdata= allsteps_filearray["step1"]
allsteps_filearray[stepindex] = stepdata
startlineinde =startlineinde
else:
stepdata, cur_lineindex =getmixsteplines1(ONETRIP_BASIC_ACT[stepindex], startlineinde, linesdata)
allsteps_filearray[stepindex] = stepdata
startlineinde =cur_lineindex
allsteps_filearray[stepindex] = stepdata
return allsteps_filearray
def main():
xls = ""
sheet = ""
global byteRow, memoRow,PTCURSFTcan,PTCURSFTDPX,DISselect,PTCURSFTDPY,PTCURSFTDL,SELECT,SELECT1,ONETRIP_BASIC_ACT,ONETRIP_MUTE_CRISINA,ONETRIP_MUTE_CRISINA1,ONETRIP_MUTE_RCD
print("1/3 analyse log")
file_array = s2_generate_log_array()
for sec, items in config.items():
if not re.match("OUT", sec, re.IGNORECASE):
continue
xls = items.get("xls", "")
sheet = items.get("sheetname", "")
msgLabel = items.get("msglabel", "")
canid = items.get("canid", "")
dataPos = items.get("datapos", "")
dataLength = items.get("datalength", "")
debug = items.get("debug", "")
byteRow = items.get("bytes", "")
memoRow1 = items.get("memo", "")
memoRow = memoRow1.split()
PTCURSFTcan = items.get("ptcursftc", "")
PTCURSFTDPX1 = items.get("ptcursftdpxx", "")
PTCURSFTDPX = int(PTCURSFTDPX1)
PTCURSFTDPY1 = items.get("ptcursftdpyy", "")
PTCURSFTDPY = int(PTCURSFTDPY1)
#Disselect = items.get("disselect", "")
#DISselect = int(Disselect)
PTCURSFTDL1 = items.get("ptcursftdll", "")
PTCURSFTDL = int(PTCURSFTDL1)
SELECT1 = items.get("select", "")
if SELECT1=="XPRND" :
ONETRIP_BASIC_ACT = {"step1":"0,0,0","step2":"0,0,0","step3":"8,125,0","step4":"8,0,0","step5":"4,0,0",
"step6":"2,0,0","step7":"4,0,0","step8":"8,0,0","step9":"0,0,0","step10":"0,0,0"}
ONETRIP_MUTE_CRISINA = {"step1":"0,0,0", "step2":"8,1,0", "step3":"8,1,1", "step4":"8,1,2", "step5":"8,1,3",
"step6":"8,1,4", "step7":"4,1,0", "step8":"4,1,1", "step9":"2,1,0", "step10":"2,1,1",
"step11":"4,1,0", "step12":"4,1,1", "step13":"8,1,0", "step14":"8,1,1", "step15":"0,0,0",
"step16":"0,0,0"}
ONETRIP_MUTE_CRISINA1 = {"step1":"2,0,1", "step2":"2,0,1", "step3":"2,193,1", "step4":"2,193,0", "step5":"2,0,0"}
xls_data = []
steps = {}
if not sheet:
continue
# 1.2 search need line from xlsx
# 1.3 generate line array
if xls and sheet and msgLabel and canid and dataPos and dataLength:
print("2/3 analyse %s sheet %s" % (xls, sheet))
xls_data, steps = s1_generate_xls_array(xls, sheet, msgLabel, canid, dataPos, dataLength, debug)
print("3/3 get data from log")
allsteps_filearray = file_array_steps(file_array, steps)
#data = s3_get_whole_data(xls_data, steps, file_array, debug)
data = s3_get_whole_data(xls_data, steps, allsteps_filearray, debug)
#_write_log(data)
# s4_arr = s3_get_bytes(s3_arr)
s4_check_output(sheet, steps , xls_data, data,xls)
else:
print("config file error")
if __name__ == '__main__':
init_config()
main()
最新发布