fix(documents): read cells beyond declared XLSX dimensions
This commit is contained in:
1 parent
24033152c7
commit
9dc0abae4f
2 files changed
+21
-1
No files matched your search
@@ -253,6 +253,8 @@ def _open_xlsx_line_source(path: Path) -> DocumentLineSource:
|
|||||||
try:
|
try:
|
||||||
for sheet_name in workbook.sheetnames:
|
for sheet_name in workbook.sheetnames:
|
||||||
worksheet = workbook[sheet_name]
|
worksheet = workbook[sheet_name]
|
||||||
|
# Producer-supplied dimensions can omit cells that are present in the XML.
|
||||||
|
worksheet.reset_dimensions()
|
||||||
wrote_header = False
|
wrote_header = False
|
||||||
for row_index, row in enumerate(worksheet.iter_rows(values_only=True), 1):
|
for row_index, row in enumerate(worksheet.iter_rows(values_only=True), 1):
|
||||||
row_text = "\t".join(
|
row_text = "\t".join(
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import threading
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from types import SimpleNamespace
|
from types import SimpleNamespace
|
||||||
from unittest.mock import AsyncMock, MagicMock
|
from unittest.mock import AsyncMock, MagicMock
|
||||||
|
from zipfile import ZipFile
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -26,6 +27,7 @@ from nanobot.security.workspace_access import (
|
|||||||
default_workspace_scope,
|
default_workspace_scope,
|
||||||
reset_workspace_scope,
|
reset_workspace_scope,
|
||||||
)
|
)
|
||||||
|
from nanobot.utils.document import extract_text
|
||||||
from nanobot.utils.llm_runtime import LLMRuntime
|
from nanobot.utils.llm_runtime import LLMRuntime
|
||||||
|
|
||||||
|
|
||||||
@@ -335,9 +337,13 @@ async def test_grep_defaults_to_match_context(tmp_path: Path) -> None:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_grep_searches_xlsx_with_sheet_cell_locator(tmp_path: Path) -> None:
|
@pytest.mark.parametrize("declared_dimension", ["A1:B2", "A1:A1", "A1:A2", "A1:B1"])
|
||||||
|
async def test_grep_searches_xlsx_with_sheet_cell_locator(
|
||||||
|
tmp_path: Path, declared_dimension: str, monkeypatch: pytest.MonkeyPatch,
|
||||||
|
) -> None:
|
||||||
from openpyxl import Workbook
|
from openpyxl import Workbook
|
||||||
|
|
||||||
|
monkeypatch.setattr("nanobot.config.loader._current_config_path", tmp_path / "config.json")
|
||||||
workbook_path = tmp_path / "people.xlsx"
|
workbook_path = tmp_path / "people.xlsx"
|
||||||
workbook = Workbook()
|
workbook = Workbook()
|
||||||
sheet = workbook.active
|
sheet = workbook.active
|
||||||
@@ -347,6 +353,18 @@ async def test_grep_searches_xlsx_with_sheet_cell_locator(tmp_path: Path) -> Non
|
|||||||
workbook.save(workbook_path)
|
workbook.save(workbook_path)
|
||||||
workbook.close()
|
workbook.close()
|
||||||
|
|
||||||
|
# Some XLSX producers report a smaller used range than their actual cells.
|
||||||
|
with ZipFile(workbook_path) as archive:
|
||||||
|
entries = {item.filename: archive.read(item) for item in archive.infolist()}
|
||||||
|
entries["xl/worksheets/sheet1.xml"] = entries["xl/worksheets/sheet1.xml"].replace(
|
||||||
|
b'<dimension ref="A1:B2"', f'<dimension ref="{declared_dimension}"'.encode(),
|
||||||
|
)
|
||||||
|
with ZipFile(workbook_path, "w") as archive:
|
||||||
|
for name, data in entries.items():
|
||||||
|
archive.writestr(name, data)
|
||||||
|
|
||||||
|
assert extract_text(workbook_path) == "--- Sheet: People ---\nName\tRole\nAda\tEngineer"
|
||||||
|
|
||||||
tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
|
tool = GrepTool(workspace=tmp_path, allowed_dir=tmp_path)
|
||||||
result = await tool.execute(
|
result = await tool.execute(
|
||||||
pattern="Engineer",
|
pattern="Engineer",
|
||||||
|
|||||||
Reference in new issue
Block a user