|
17 | 17 | import zipfile
|
18 | 18 | from io import BytesIO
|
19 | 19 | from pathlib import Path
|
20 |
| -from typing import TYPE_CHECKING |
| 20 | +from typing import TYPE_CHECKING, Literal |
21 | 21 |
|
22 | 22 | import defusedxml
|
23 | 23 | import numpy as np
|
@@ -352,17 +352,13 @@ def save_tile(tile_path: Path, tile: Image.Image) -> None:
|
352 | 352 | )
|
353 | 353 |
|
354 | 354 | else: # container == "tar":
|
355 |
| - compression2mode = { |
356 |
| - None: "w", |
357 |
| - "gzip": "w:gz", |
358 |
| - "bz2": "w:bz2", |
359 |
| - "lzma": "w:xz", |
360 |
| - } |
361 |
| - if compression not in compression2mode: |
| 355 | + if compression not in [None, "gzip", "bz2", "lzma"]: |
362 | 356 | msg = "Unsupported compression for tar."
|
363 | 357 | raise ValueError(msg)
|
364 | 358 |
|
365 |
| - tar_archive = tarfile.TarFile.open(path, mode=compression2mode[compression]) |
| 359 | + compression_mode = self._compression_mode_to_literal(compression) |
| 360 | + |
| 361 | + tar_archive = tarfile.TarFile.open(str(path), mode=compression_mode) |
366 | 362 |
|
367 | 363 | def save_tile(tile_path: Path, tile: Image.Image) -> None:
|
368 | 364 | """Write the tile to the output zip."""
|
@@ -399,6 +395,19 @@ def __iter__(self: TilePyramidGenerator) -> Iterator:
|
399 | 395 | for x, y in np.ndindex(self.tile_grid_size(level)):
|
400 | 396 | yield self.get_tile(level=level, x=x, y=y)
|
401 | 397 |
|
| 398 | + def _compression_mode_to_literal( |
| 399 | + self: TilePyramidGenerator, mode: str | None |
| 400 | + ) -> Literal["w", "w:gz", "w:bz2", "w:xz"]: |
| 401 | + """Convert compression mode to a literal string.""" |
| 402 | + if mode is None: |
| 403 | + return "w" |
| 404 | + if mode == "gzip": |
| 405 | + return "w:gz" |
| 406 | + if mode == "bz2": |
| 407 | + return "w:bz2" |
| 408 | + # mode is "lzma" |
| 409 | + return "w:xz" |
| 410 | + |
402 | 411 |
|
403 | 412 | class ZoomifyGenerator(TilePyramidGenerator):
|
404 | 413 | r"""Pyramid tile generator with extra Zoomify specific methods.
|
|
0 commit comments