Skip to content

Commit 08ead32

Browse files
committed
Rework following review
1 parent dab6ba2 commit 08ead32

File tree

4 files changed

+9
-47
lines changed

4 files changed

+9
-47
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Added
1515

16-
- Add utility to fetch and prepare ZIM illustration ; make this utility directly accessible from illustration metadata (#254)
16+
- Add utility to fetch and prepare ZIM illustration (#254)
1717

1818
## [5.1.1] - 2025-02-17
1919

src/zimscraperlib/zim/metadata.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import base64
22
import datetime
33
import io
4-
import pathlib
54
from abc import ABC, abstractmethod
65
from collections.abc import Iterable
76
from dataclasses import asdict, dataclass, fields
@@ -19,7 +18,6 @@
1918
RECOMMENDED_MAX_TITLE_LENGTH,
2019
)
2120
from zimscraperlib.i18n import is_valid_iso_639_3
22-
from zimscraperlib.image.illustration import get_zim_illustration
2321
from zimscraperlib.image.probing import is_valid_image
2422
from zimscraperlib.inputs import unique_values
2523
from zimscraperlib.typing import SupportsRead, SupportsSeekableRead
@@ -356,20 +354,12 @@ class IllustrationBasedMetadata(Metadata):
356354
meta_mimetype = "image/png"
357355

358356
def __init__(
359-
self,
360-
value: bytes | io.IOBase | io.BytesIO | str | pathlib.Path,
361-
name: str | None = None,
357+
self, value: bytes | io.IOBase | io.BytesIO, name: str | None = None
362358
) -> None:
363359
super().__init__(value=value, name=name)
364360

365361
# native type is PNG image buffer
366-
def get_cleaned_value(
367-
self, value: bytes | io.IOBase | io.BytesIO | str | pathlib.Path
368-
) -> bytes:
369-
if isinstance(value, str | pathlib.Path):
370-
value = get_zim_illustration(
371-
value, self.illustration_size, self.illustration_size
372-
)
362+
def get_cleaned_value(self, value: bytes | io.IOBase | io.BytesIO) -> bytes:
373363
value = self.get_binary_from(value)
374364
if not is_valid_image(
375365
image=value,
@@ -425,10 +415,7 @@ class IllustrationMetadata(IllustrationBasedMetadata):
425415
illustration_scale: int = 1
426416

427417
def __init__(
428-
self,
429-
value: bytes | io.IOBase | io.BytesIO | str | pathlib.Path,
430-
size: int,
431-
scale: int = 1,
418+
self, value: bytes | io.IOBase | io.BytesIO, size: int, scale: int = 1
432419
) -> None:
433420
self.illustration_scale = scale
434421
self.illustration_size = size

tests/zim/test_metadata.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import pytest
1212
from beartype.roar import BeartypeCallHintParamViolation as InvalidType
13-
from PIL.Image import open as pilopen
1413

1514
from zimscraperlib.zim import metadata
1615

@@ -214,11 +213,6 @@ def test_validate_illustration_invalid_image():
214213
metadata.IllustrationMetadata(b"PN", size=48)
215214

216215

217-
def test_validate_illustration_invalid_image_location():
218-
with pytest.raises(OSError, match="missing.png could not be found"):
219-
metadata.IllustrationMetadata("missing.png", size=48)
220-
221-
222216
def test_validate_illustration_wrong_sizes(png_image2: pathlib.Path):
223217
with open(png_image2, "rb") as fh:
224218
png_data = fh.read()
@@ -228,22 +222,6 @@ def test_validate_illustration_wrong_sizes(png_image2: pathlib.Path):
228222
metadata.IllustrationMetadata(png_data, size=48)
229223

230224

231-
def test_validate_illustration_path_resized(png_image2: pathlib.Path):
232-
with pilopen(
233-
io.BytesIO(metadata.IllustrationMetadata(png_image2, size=32).libzim_value)
234-
) as image:
235-
assert image.size == (32, 32)
236-
237-
238-
def test_validate_illustration_str_resized(png_image2: pathlib.Path):
239-
with pilopen(
240-
io.BytesIO(
241-
metadata.IllustrationMetadata(png_image2.resolve(), size=48).libzim_value
242-
)
243-
) as image:
244-
assert image.size == (48, 48)
245-
246-
247225
def test_blank_metadata():
248226
with pytest.raises(ValueError, match=r"Missing value \(empty not allowed\)"):
249227
metadata.Metadata(name="Blank", value=b"")

tests/zim/test_zim_creator.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import pytest
1616
from libzim.writer import Compression # pyright: ignore[reportMissingModuleSource]
17-
from PIL.Image import open as pilopen
1817

1918
from zimscraperlib.constants import UTF8
2019
from zimscraperlib.download import save_large_file, stream_file
@@ -757,9 +756,11 @@ def test_relax_metadata(
757756
],
758757
)
759758
def test_config_metadata(
760-
tmp_path: pathlib.Path, png_image2: pathlib.Path, tags: str | list[str]
759+
tmp_path: pathlib.Path, png_image: pathlib.Path, tags: str | list[str]
761760
):
762761
fpath = tmp_path / "test_config.zim"
762+
with open(png_image, "rb") as fh:
763+
png_data = fh.read()
763764
creator = Creator(fpath, "").config_metadata(
764765
StandardMetadataList(
765766
Name=NameMetadata("wikipedia_fr_football"),
@@ -780,7 +781,7 @@ def test_config_metadata(
780781
Flavour=FlavourMetadata("nopic"),
781782
Source=SourceMetadata("https://en.wikipedia.org/"),
782783
Scraper=ScraperMetadata("mwoffliner 1.2.3"),
783-
Illustration_48x48_at_1=DefaultIllustrationMetadata(png_image2),
784+
Illustration_48x48_at_1=DefaultIllustrationMetadata(png_data),
784785
),
785786
[CustomTextMetadata("X-TestMetadata", "Test Metadata")],
786787
)
@@ -816,11 +817,7 @@ def test_config_metadata(
816817
assert reader.get_text_metadata("Flavour") == "nopic"
817818
assert reader.get_text_metadata("Source") == "https://en.wikipedia.org/"
818819
assert reader.get_text_metadata("Scraper") == "mwoffliner 1.2.3"
819-
with pilopen(
820-
io.BytesIO(reader.get_metadata("Illustration_48x48@1"))
821-
) as illustration:
822-
assert illustration.size == (48, 48)
823-
assert illustration.format == "PNG"
820+
assert reader.get_metadata("Illustration_48x48@1") == png_data
824821
assert reader.get_text_metadata("X-TestMetadata") == "Test Metadata"
825822

826823

0 commit comments

Comments
 (0)