fix: Enhance error handling and logging across multiple evaluators
- Added logging for file retrieval and error handling in file.py, improving robustness during file operations. - Implemented checks for file existence and parsing errors in general.py, enhancing reliability in JSON/YAML processing. - Improved table comparison logic in table.py with detailed error logging for sheet loading and cell value reading. - Enhanced metrics evaluation in slides.py with additional checks for paragraph and run counts, ensuring thorough comparison. - Updated utils.py to include file existence checks and detailed error logging during cell value reading.
This commit is contained in:
@@ -36,8 +36,14 @@ def _parse_sheet_idx(sheet_idx: Union[int, str]
|
||||
# function _parse_sheet_idx {{{ #
|
||||
if isinstance(sheet_idx, int):
|
||||
try:
|
||||
index: str = result_sheet_names[sheet_idx]
|
||||
except:
|
||||
if not result_sheet_names or sheet_idx >= len(result_sheet_names):
|
||||
logger.error(f"Sheet index {sheet_idx} out of range. Available sheets: {result_sheet_names}")
|
||||
index = ""
|
||||
else:
|
||||
index: str = result_sheet_names[sheet_idx]
|
||||
logger.debug(f"Sheet index {sheet_idx} resolved to sheet: {index}")
|
||||
except Exception as e:
|
||||
logger.error(f"Error resolving sheet index {sheet_idx}: {e}")
|
||||
index = ""
|
||||
book: BOOK = result
|
||||
elif sheet_idx.startswith("RI"):
|
||||
@@ -114,12 +120,21 @@ def compare_table(result: str, expected: str = None, **options) -> float:
|
||||
"""
|
||||
|
||||
if result is None:
|
||||
logger.error("Result file path is None")
|
||||
return 0.
|
||||
|
||||
# Check if result file exists
|
||||
if not os.path.exists(result):
|
||||
logger.error(f"Result file not found: {result}")
|
||||
return 0.
|
||||
|
||||
try:
|
||||
logger.info(f"Loading result file: {result}")
|
||||
xlworkbookr: Workbook = openpyxl.load_workbook(filename=result)
|
||||
pdworkbookr = pd.ExcelFile(result)
|
||||
except:
|
||||
logger.info(f"Successfully loaded result file with sheets: {pdworkbookr.sheet_names}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to load result file {result}: {e}")
|
||||
return 0.
|
||||
worksheetr_names: List[str] = pdworkbookr.sheet_names
|
||||
|
||||
@@ -432,19 +447,35 @@ def compare_table(result: str, expected: str = None, **options) -> float:
|
||||
# props: dict like {attribute: {"method": str, "ref": anything}}
|
||||
# supported attributes: value & those supported by utils._read_cell_style
|
||||
|
||||
sheet: Worksheet = _load_sheet(*parse_idx(r["sheet_idx"], xlworkbookr, xlworkbooke))
|
||||
if sheet is None:
|
||||
return 0.
|
||||
# data_frame: pd.DataFrame = _load_sheet(*parse_idx(r["sheet_idx"], pdworkbookr, pdworkbooke))
|
||||
cell: Cell = sheet[r["coordinate"]]
|
||||
metric: bool = True
|
||||
for prpt, rule in r["props"].items():
|
||||
if prpt == "value":
|
||||
val = read_cell_value(*parse_idx(r["sheet_idx"], result, expected), r["coordinate"])
|
||||
else:
|
||||
val = _read_cell_style(prpt, cell)
|
||||
try:
|
||||
sheet: Worksheet = _load_sheet(*parse_idx(r["sheet_idx"], xlworkbookr, xlworkbooke))
|
||||
if sheet is None:
|
||||
logger.error(f"Failed to load sheet for sheet_idx: {r['sheet_idx']}")
|
||||
return 0.
|
||||
# data_frame: pd.DataFrame = _load_sheet(*parse_idx(r["sheet_idx"], pdworkbookr, pdworkbooke))
|
||||
cell: Cell = sheet[r["coordinate"]]
|
||||
metric: bool = True
|
||||
for prpt, rule in r["props"].items():
|
||||
if prpt == "value":
|
||||
try:
|
||||
parsed_result = parse_idx(r["sheet_idx"], result, expected)
|
||||
logger.debug(f"parse_idx result: {parsed_result}")
|
||||
val = read_cell_value(*parsed_result, r["coordinate"])
|
||||
logger.debug(f"Cell {r['coordinate']} value: {val}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to read cell value at {r['coordinate']}: {e}")
|
||||
val = None
|
||||
else:
|
||||
try:
|
||||
val = _read_cell_style(prpt, cell)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to read cell style {prpt} at {r['coordinate']}: {e}")
|
||||
val = None
|
||||
|
||||
metric = metric and _match_value_to_rule(val, rule)
|
||||
metric = metric and _match_value_to_rule(val, rule)
|
||||
except Exception as e:
|
||||
logger.error(f"Error in check_cell processing: {e}")
|
||||
return 0.
|
||||
|
||||
logger.debug("Assertion: %s[%s] :%s - %s"
|
||||
, r["sheet_idx"], r["coordinate"]
|
||||
|
||||
Reference in New Issue
Block a user