讲一下代码:
import logging
from configparser import ConfigParser
import openpyxl
from time import strftime
import sys
from os import path, walk
import xml.etree.ElementTree as ET
# 修改logging打印等级
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s -%(message)s')
class IspCheck():
"""
isp参数变动后快速筛选测试项
"""
# todo 补充机型名信息
def __init__(self):
device_info = self.start_test_get_config()
self.device_name = device_info['DEVICE_NAME']
self.old_isp_cfg_path = device_info['old_isp_cfg_path']
self.new_isp_cfg_path = device_info['new_isp_cfg_path']
self.py_path = device_info['py_path']
# 读取配置文件,返回配置信息+py路径信息字典
def start_test_get_config(self):
logging.debug('加载配置文件.........')
element_dict = {}
if hasattr(sys, 'frozen'):
py_path = path.dirname(sys.executable)
else:
py_path = path.dirname(__file__)
# 添加py文件或exe文件所在路径
element_dict["py_path"] = py_path
# 读取配置文件
xml_path = path.join(py_path, 'isp_check_config.xml')
info_tree = ET.parse(xml_path)
root = info_tree.getroot() # 加载根节点
element_name = root.iter() # 遍历根节点所有后代
for e in element_name:
element_dict[e.tag] = e.text
logging.debug('加载配置文件结束......')
return element_dict
def confirm_test_example(self, sheet, param_list):
"""
根据参数的单元格位置,在测试项一列中找到对应的测试项
:param sheet: 工作表对象
:param param_list: [section, option, old_param, new_param, row]格式的参数列表
:return:
"""
row = param_list[4]
while True:
data_area = "E" + str(row)
data_value = sheet[data_area].value
if data_value:
if "【ISO±1倍】" in data_value:
param_ISO = self.find_param_ISO(sheet, param_list[4])
test_example = '【' + param_ISO + '±1倍】\n' + data_value
return test_example
elif "【lv±1】" in data_value: # todo 根据测试项中的特殊值进一步判断测试项
param_LV = self.find_param_LV(param_list[2], param_list[3])
test_example = '【' + param_LV + '±1】\n' + data_value
return test_example
else:
return data_value
else:
row -= 1
def confirm_param_parent(self, sheet, row, parent_param):
"""
根据参数的单元格位置,向上搜索判断其父节点为所需的节点
:param sheet: 工作表对象【参考模块】
:param row: 参数所在单元格的行数
:param parent_param: 参数对应的父节点
:return: 是所需的父节点,返回True,否则返回False
"""
while True:
data_area = "A" + str(row - 1)
data_value = sheet[data_area].value
section = "[" + parent_param + "]"
if "[" in data_value:
if data_value == section:
return True
else:
return False
else:
row -= 1
def find_param_ISO(self, sheet, row):
"""
判断具体测试的ISO项
:param sheet: 工作表对象【模板参考表】
:param row: 参数所在的单元格行数
:return:返回参数对应的ISO项
"""
while True:
data_area = "D" + str(row)
data_value = sheet[data_area].value
if data_value:
# print(data_value) # 调试打印
return data_value
else:
row -= 1
def find_param_LV(self, old_lv_param, new_lv_param): # todo
"""
:param old_lv_param: 带lv等级的旧参数
:param new_lv_param: 带lv等级的新参数
:return: 返回有差异的lv等级
"""
diff_lv_str = "lv"
old_lv_param_list = old_lv_param.split(',')
new_lv_param_list = new_lv_param.split(',')
for lv_n in range(20):
if old_lv_param_list[lv_n] != new_lv_param_list[lv_n]:
diff_lv_str = diff_lv_str + "_" + str(lv_n)
return diff_lv_str
def find_diff_param(self, isp_old_path, isp_new_path):
"""
对比查询新旧ISP不同参数
:param isp_old_path: 旧isp地址
:param isp_new_path: 新isp地址
:return: 以[section, option, old_param, new_param]格式返回不同参数信息
"""
diff_param_list = []
old_config_parser = ConfigParser()
old_config_parser.read(isp_old_path)
new_config_parser = ConfigParser()
new_config_parser.read(isp_new_path)
old_sections = old_config_parser.sections()
for section in old_sections:
old_options = old_config_parser.options(section)
for option in old_options:
old_param = old_config_parser.get(section, option)
new_param = new_config_parser.get(section, option)
if old_param == new_param:
pass
else:
diff_param_list.append([section, option, old_param, new_param])
return diff_param_list
# todo 加入isp_name,用于定位sheet表,以便区分线性、HDR、夜视的测试项
def find_param_in_test(self, diff_param_list, isp_name):
"""
根据不同参数信息,查找到该参数的行数,添加到参数列表中
:param diff_param_list: 在find_diff_param返回的不同参数列表[section, option, old_param, new_param]
:return: 添加row信息的不同参数列表
"""
workbook = openpyxl.load_workbook(r"VIGI ISP.xlsx")
sheet = workbook[isp_name]
for param_list in diff_param_list:
for row in sheet.iter_rows(min_col=1, max_col=1): # 遍历第一列的所有行
for cell in row:
# 找到参数一致的行,再确认其父节点是否正常
if cell.value == param_list[1]: #todo 后续可优化逻辑,先找父节点,再向下找参数
if self.confirm_param_parent(sheet, cell.row, param_list[0]):
param_list.append(cell.row)
test_example = self.confirm_test_example(sheet, param_list)
param_list.append(test_example)
logging.debug(f"修改参数:{param_list[0]}-->{param_list[1]}")
return diff_param_list
def write_diff_param(self, workbook, isp_name, diff_param_test_list):
"""
不同参数信息写入新表
:param isp_name: isp文件的名称,如day_isp,night_isp
:param diff_param_list: 在find_param_in_test返回的不同参数列表
:return: 在VIGI ISP.xlsx写入新表
"""
sheet_diff_param = workbook.create_sheet(isp_name)
sheet_diff_param.cell(row=1, column=1, value="参数块")
sheet_diff_param.cell(row=1, column=2, value="参数名")
sheet_diff_param.cell(row=1, column=3, value="旧参数")
sheet_diff_param.cell(row=1, column=4, value="新参数")
sheet_diff_param.cell(row=1, column=5, value="位置")
sheet_diff_param.cell(row=1, column=6, value="测试项")
row = 2
for param in diff_param_test_list:
sheet_diff_param.cell(row=row, column=1, value=param[0])
sheet_diff_param.cell(row=row, column=2, value=param[1])
sheet_diff_param.cell(row=row, column=3, value=param[2])
sheet_diff_param.cell(row=row, column=4, value=param[3])
# 在模板中不存在的参数,写入”人工处理“
if len(param) == 6:
sheet_diff_param.cell(row=row, column=5, value=param[4])
sheet_diff_param.cell(row=row, column=6, value=param[5])
else:
sheet_diff_param.cell(row=row, column=5, value="人工处理")
sheet_diff_param.cell(row=row, column=6, value="人工处理")
row += 1
def get_isp_path(self, isp_cfg_path):
isp_path_list = []
paths = walk(isp_cfg_path)
for pat, dir_lst, file_lst in paths:
for fil_name in file_lst:
if '.cfg' in fil_name:
isp_path_list.append(path.join(pat, fil_name))
return isp_path_list
def run(self):
old_isp_path_list = self.get_isp_path(self.old_isp_cfg_path)
new_isp_path_list = self.get_isp_path(self.new_isp_cfg_path)
workbook = openpyxl.Workbook()
for i in range(len(new_isp_path_list)):
isp_name = path.basename(new_isp_path_list[i])
logging.info(f"查询{isp_name}的测试项")
diff_param_list = ISP.find_diff_param(old_isp_path_list[i], new_isp_path_list[i])
diff_param_test_list = ISP.find_param_in_test(diff_param_list, isp_name)
ISP.write_diff_param(workbook, isp_name, diff_param_test_list)
test_xlsx_path = self.device_name + strftime("_%Y%m%d") + ".xlsx"
workbook.save(test_xlsx_path)
if __name__ == "__main__":
# todo 加入开始接口
ISP = IspCheck()
ISP.run()
input("【enter】结束运行")