From d8785afb47bbf29e6649876bfae8d75e08204e5e Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Sat, 3 Oct 2026 21:51:24 -0400 Subject: [PATCH] Read exact stored text through existing source backends Closes #25. Expose DataSource.load_text through FileManager; disk preserves exact UTF-8 bytes and Zarr retains its existing disk-passthrough owner. Text-valued memory storage derives the default source behavior, while structured values refuse it. Required by OpenHCSDev/openhcs#394 to reuse saved analysis tables instead of rendering them again. --- src/polystore/base.py | 7 +++++++ src/polystore/disk.py | 4 ++++ src/polystore/filemanager.py | 7 +++++++ src/polystore/zarr.py | 8 ++++++++ tests/test_disk_backend.py | 27 ++++++++++++++++++++++++--- 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/polystore/base.py b/src/polystore/base.py index b32c515..921c13f 100644 --- a/src/polystore/base.py +++ b/src/polystore/base.py @@ -432,6 +432,13 @@ def sample( sample_origin_yx=request.origin_yx, ) + def load_text(self, file_path: Union[str, Path]) -> str: + """Read a stored text value without decoding its document format.""" + value = self.load(file_path) + if not isinstance(value, str): + raise TypeError(f"Stored value at {file_path!s} is not text.") + return value + @abstractmethod def load(self, file_path: Union[str, Path], **kwargs) -> Any: """ diff --git a/src/polystore/disk.py b/src/polystore/disk.py index 5165451..f2eba70 100644 --- a/src/polystore/disk.py +++ b/src/polystore/disk.py @@ -344,6 +344,10 @@ def _csv_reader(self, path): reader = csv.DictReader(f) return list(reader) + def load_text(self, file_path: str | Path) -> str: + """Read UTF-8 text exactly, preserving saved CSV formatting and newlines.""" + return Path(file_path).read_bytes().decode(DISK_TEXT_ENCODING) + def load(self, file_path: str | Path, **kwargs) -> Any: """ Load data from disk based on explicit content type. diff --git a/src/polystore/filemanager.py b/src/polystore/filemanager.py index 3edfda4..26cd111 100644 --- a/src/polystore/filemanager.py +++ b/src/polystore/filemanager.py @@ -251,6 +251,13 @@ def image_serialization_preserves_values( raise StorageResolutionError(f"Backend {backend!r} is not a DataSink.") return sink.image_serialization_preserves_values(authored_dtype, stored_dtype) + def load_text(self, file_path: str | Path, backend: str) -> str: + """Read exact stored text through the declared source backend.""" + source = self._get_backend(backend) + if not isinstance(source, DataSource): + raise StorageResolutionError(f"Backend {backend!r} is not a DataSource.") + return source.load_text(file_path) + def load(self, file_path: str | Path, backend: str, **kwargs) -> Any: """ Load data from a file using the specified backend. diff --git a/src/polystore/zarr.py b/src/polystore/zarr.py index 3d1a6e3..1ce93d2 100644 --- a/src/polystore/zarr.py +++ b/src/polystore/zarr.py @@ -651,6 +651,14 @@ def _ensure_plate_metadata( fmt=self.output_format, ) + def load_text(self, file_path: str | Path) -> str: + """Keep disk-passthrough text with its existing storage owner.""" + if self._is_disk_passthrough_path(file_path): + from .backend_registry import get_backend_instance + + return get_backend_instance(Backend.DISK.value).load_text(file_path) + return super().load_text(file_path) + def load(self, file_path: str | Path, **kwargs) -> Any: """ Load a single file from zarr store. diff --git a/tests/test_disk_backend.py b/tests/test_disk_backend.py index 618c357..acb609a 100644 --- a/tests/test_disk_backend.py +++ b/tests/test_disk_backend.py @@ -81,10 +81,15 @@ def physical_source_path(self, backend_address, *, base_path): ) +@pytest.mark.parametrize("backend", ["disk", "zarr", "memory"]) def test_preformatted_text_is_written_as_exact_utf8_bytes( tmp_path: Path, monkeypatch, + backend: str, ) -> None: + from polystore import MemoryBackend + from polystore.zarr import ZarrStorageBackend + content = "well,count\r\nA01,2\r\n" output_path = tmp_path / "summary.csv" @@ -93,9 +98,25 @@ def reject_platform_text_translation(*args, **kwargs): raise AssertionError("text persistence must bypass platform newline translation") monkeypatch.setattr(Path, "write_text", reject_platform_text_translation) - DiskBackend().save(content, output_path) - - assert output_path.read_bytes() == content.encode(disk_module.DISK_TEXT_ENCODING) + manager = FileManager({ + "disk": DiskBackend(), "zarr": ZarrStorageBackend(), "memory": MemoryBackend(), + }) + manager.ensure_directory(tmp_path, backend) + manager.save(content, output_path, backend) + assert manager.load_text(output_path, backend) == content + if backend != "memory": + assert output_path.read_bytes() == content.encode(disk_module.DISK_TEXT_ENCODING) + + +def test_text_read_refuses_structured_values(tmp_path): + from polystore import MemoryBackend + + manager = FileManager({"memory": MemoryBackend()}) + manager.ensure_directory(tmp_path, "memory") + path = tmp_path / "values.csv" + manager.save([{"count": 2}], path, "memory") + with pytest.raises(TypeError, match="is not text"): + manager.load_text(path, "memory") def test_tiff_save_normalizes_external_array_payload(tmp_path, monkeypatch) -> None: