Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/polystore/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
4 changes: 4 additions & 0 deletions src/polystore/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions src/polystore/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions src/polystore/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 24 additions & 3 deletions tests/test_disk_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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:
Expand Down
Loading