添加数据验证逻辑,抛出自定义异常以处理验证失败情况;更新错误处理逻辑以显示详细错误信息

This commit is contained in:
2026-01-20 23:19:10 +08:00
parent 28e35ea429
commit 96350bb8e2
3 changed files with 41 additions and 8 deletions

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2025 Jeffrey Hsu - JITToolBox
# Copyright (c) 2025-2026 Jeffrey Hsu - JITToolBox
# #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -73,6 +73,13 @@ class ExcelReader:
image = io.BytesIO(self._images[cell]())
return Image.open(image)
class ValidError(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return self.message
def __init__(self, file_path: str, version_check: bool = False,
signal: Callable[[str, str], None] = lambda x, y: print(x)):
super().__init__()
@@ -271,9 +278,15 @@ class ExcelReader:
for i in range(len(self.suggestion_template_list), 5):
self.suggestion_template_list.append(None)
self.validate_data()
if vd_lst := self.validate_data():
raise self.ValidError("\n\n".join(vd_lst))
self.gen_picture()
except self.ValidError as ve:
raise Exception(f"""
数据验证失败:\n\n{str(ve)}
""")
except Exception as e:
error_message = traceback.format_exc()
raise Exception(f"""
@@ -284,9 +297,18 @@ class ExcelReader:
def set_file_path(self, file_path: str):
self.file_path = file_path
def validate_data(self):
def validate_data(self) -> list[str]:
lst: list[str] = []
self.signal("正在验证数据", LOGLEVEL.INFO)
return 0
if len(self.kpi_list) != self.kpi_number:
self.signal("\"课程目标\"\"目标支撑的毕业要求指标点\"数量与期望目标数量不符", LOGLEVEL.ERROR)
lst.append(
f"\"课程目标\"\"目标支撑的毕业要求指标点\"数量与期望目标数量不符请检查Excel表格中的\"课程目标\"\"目标支撑的毕业要求指标点\"列是否填写完整。"
f"期望得到 {self.kpi_number} 个,实际检测到 {len(self.kpi_list)} 个。"
f"如想暂时不填请在Excel表格对应的位置添加一个空格"
)
return lst
def run(self):
self.parse_excel()
@@ -655,7 +677,7 @@ class ExcelReader:
yield "专业负责人/系主任(签字)"
yield ("整改意见:\n"
"\n\n\n"
f"{" "*8}{self.suggestion_template_list[3] if self.suggestion_template_list[3] is not None else '\n\n\n'}\n\n\n")
f"{" " * 8}{self.suggestion_template_list[3] if self.suggestion_template_list[3] is not None else '\n\n\n'}\n\n\n")
yield ""
yield "签字:"
yield ""
@@ -664,7 +686,7 @@ class ExcelReader:
yield "课程负责人(签字)"
yield ("拟整改计划与措施:\n"
"\n\n\n"
f"{" "*8}{self.suggestion_template_list[4] if self.suggestion_template_list[4] is not None else '\n\n\n'}\n\n\n")
f"{" " * 8}{self.suggestion_template_list[4] if self.suggestion_template_list[4] is not None else '\n\n\n'}\n\n\n")
yield ""
yield "签字:"
yield ""