Skip to content

Commit 89deeef

Browse files
authored
Merge pull request #26 from OpenHCSDev/fix/exact-saved-text-read-20261004
Read exact saved text through source backends
2 parents f0bf513 + d8785af commit 89deeef

5 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎src/polystore/base.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,13 @@ def sample(
432432
sample_origin_yx=request.origin_yx,
433433
)
434434

435+
def load_text(self, file_path: Union[str, Path]) -> str:
436+
"""Read a stored text value without decoding its document format."""
437+
value = self.load(file_path)
438+
if not isinstance(value, str):
439+
raise TypeError(f"Stored value at {file_path!s} is not text.")
440+
return value
441+
435442
@abstractmethod
436443
def load(self, file_path: Union[str, Path], **kwargs) -> Any:
437444
"""

‎src/polystore/disk.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ def _csv_reader(self, path):
344344
reader = csv.DictReader(f)
345345
return list(reader)
346346

347+
def load_text(self, file_path: str | Path) -> str:
348+
"""Read UTF-8 text exactly, preserving saved CSV formatting and newlines."""
349+
return Path(file_path).read_bytes().decode(DISK_TEXT_ENCODING)
350+
347351
def load(self, file_path: str | Path, **kwargs) -> Any:
348352
"""
349353
Load data from disk based on explicit content type.

‎src/polystore/filemanager.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ def image_serialization_preserves_values(
251251
raise StorageResolutionError(f"Backend {backend!r} is not a DataSink.")
252252
return sink.image_serialization_preserves_values(authored_dtype, stored_dtype)
253253

254+
def load_text(self, file_path: str | Path, backend: str) -> str:
255+
"""Read exact stored text through the declared source backend."""
256+
source = self._get_backend(backend)
257+
if not isinstance(source, DataSource):
258+
raise StorageResolutionError(f"Backend {backend!r} is not a DataSource.")
259+
return source.load_text(file_path)
260+
254261
def load(self, file_path: str | Path, backend: str, **kwargs) -> Any:
255262
"""
256263
Load data from a file using the specified backend.

‎src/polystore/zarr.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,14 @@ def _ensure_plate_metadata(
651651
fmt=self.output_format,
652652
)
653653

654+
def load_text(self, file_path: str | Path) -> str:
655+
"""Keep disk-passthrough text with its existing storage owner."""
656+
if self._is_disk_passthrough_path(file_path):
657+
from .backend_registry import get_backend_instance
658+
659+
return get_backend_instance(Backend.DISK.value).load_text(file_path)
660+
return super().load_text(file_path)
661+
654662
def load(self, file_path: str | Path, **kwargs) -> Any:
655663
"""
656664
Load a single file from zarr store.

‎tests/test_disk_backend.py‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,15 @@ def physical_source_path(self, backend_address, *, base_path):
8181
)
8282

8383

84+
@pytest.mark.parametrize("backend", ["disk", "zarr", "memory"])
8485
def test_preformatted_text_is_written_as_exact_utf8_bytes(
8586
tmp_path: Path,
8687
monkeypatch,
88+
backend: str,
8789
) -> None:
90+
from polystore import MemoryBackend
91+
from polystore.zarr import ZarrStorageBackend
92+
8893
content = "well,count\r\nA01,2\r\n"
8994
output_path = tmp_path / "summary.csv"
9095

@@ -93,9 +98,25 @@ def reject_platform_text_translation(*args, **kwargs):
9398
raise AssertionError("text persistence must bypass platform newline translation")
9499

95100
monkeypatch.setattr(Path, "write_text", reject_platform_text_translation)
96-
DiskBackend().save(content, output_path)
97-
98-
assert output_path.read_bytes() == content.encode(disk_module.DISK_TEXT_ENCODING)
101+
manager = FileManager({
102+
"disk": DiskBackend(), "zarr": ZarrStorageBackend(), "memory": MemoryBackend(),
103+
})
104+
manager.ensure_directory(tmp_path, backend)
105+
manager.save(content, output_path, backend)
106+
assert manager.load_text(output_path, backend) == content
107+
if backend != "memory":
108+
assert output_path.read_bytes() == content.encode(disk_module.DISK_TEXT_ENCODING)
109+
110+
111+
def test_text_read_refuses_structured_values(tmp_path):
112+
from polystore import MemoryBackend
113+
114+
manager = FileManager({"memory": MemoryBackend()})
115+
manager.ensure_directory(tmp_path, "memory")
116+
path = tmp_path / "values.csv"
117+
manager.save([{"count": 2}], path, "memory")
118+
with pytest.raises(TypeError, match="is not text"):
119+
manager.load_text(path, "memory")
99120

100121

101122
def test_tiff_save_normalizes_external_array_payload(tmp_path, monkeypatch) -> None:

0 commit comments

Comments
 (0)