Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests warning about imghdr being deprecated #78

Merged
merged 1 commit into from
Aug 27, 2024
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
37 changes: 25 additions & 12 deletions mopidy_local/storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import hashlib
import imghdr
import logging
import pathlib
import shutil
Expand All @@ -21,7 +20,6 @@
)


# would be nice to have these in imghdr...
def get_image_size_png(data):
return struct.unpack(">ii", data[16:24])

Expand Down Expand Up @@ -58,13 +56,25 @@
return width, height


def test_jpeg(data, file_handle):
# Additional JPEG detection looking for JPEG SOI marker
if data[:2] == b"\xff\xd8":
return "jpeg"
MIN_BYTES_FOR_IMAGE_TYPE = 8


def get_image_type_from_header(header: bytes) -> str:
# original source: https://github.com/sphinx-doc/sphinx/commit/a502e7

if len(header) < MIN_BYTES_FOR_IMAGE_TYPE:
raise ValueError("Unknown image type")

imghdr.tests.append(test_jpeg)
if header.startswith(b"\x89PNG\r\n\x1A\n"):
return "png"

if header.startswith((b"GIF87a", b"GIF89a")):
return "gif"

if header.startswith(b"\xFF\xD8"):
return "jpeg"

raise ValueError("Unknown image type")


class LocalStorageProvider:
Expand Down Expand Up @@ -214,15 +224,18 @@
return images

def _get_or_create_image_file(self, path, data=None):
what = imghdr.what(path, data)
if not what:
raise ValueError("Unknown image type")
if not data:
data_source = path.as_uri()
with open(path, "rb") as f:
data = f.read()
header = f.read(MIN_BYTES_FOR_IMAGE_TYPE)

Check warning on line 229 in mopidy_local/storage.py

View check run for this annotation

Codecov / codecov/patch

mopidy_local/storage.py#L229

Added line #L229 was not covered by tests

what = get_image_type_from_header(header)
data_source = path.as_uri()
data = header + f.read()

Check warning on line 233 in mopidy_local/storage.py

View check run for this annotation

Codecov / codecov/patch

mopidy_local/storage.py#L231-L233

Added lines #L231 - L233 were not covered by tests
else:
header = data[:MIN_BYTES_FOR_IMAGE_TYPE]
what = get_image_type_from_header(header)

Check warning on line 236 in mopidy_local/storage.py

View check run for this annotation

Codecov / codecov/patch

mopidy_local/storage.py#L235-L236

Added lines #L235 - L236 were not covered by tests
data_source = "embedded image"

digest, width, height = hashlib.md5(data).hexdigest(), None, None
try:
if what == "png":
Expand Down
1 change: 0 additions & 1 deletion tests/dummy_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
tests of the core and backends.
"""


import pykka

from mopidy import audio
Expand Down
33 changes: 31 additions & 2 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
from mopidy_local import storage


def test_get_image_type_from_header_png():
data_bytes = b"\x89PNG\r\n\x1A\nffe000104a464"
assert storage.get_image_type_from_header(data_bytes) == "png"


@pytest.mark.parametrize(
"data",
[
pytest.param("474946383761ffe000104a46", id="GIF87a"),
pytest.param("474946383961ffe000104a46", id="GIF89a"),
],
)
def test_get_image_type_from_header_gif(data):
data_bytes = bytes.fromhex(data)
assert storage.get_image_type_from_header(data_bytes) == "gif"


@pytest.mark.parametrize(
"data",
[
Expand All @@ -11,6 +28,18 @@
pytest.param("ffd8ffe1095068747470", id="XMP"),
],
)
def test_jpeg_detection(data):
def test_get_image_type_from_header_jpeg(data):
data_bytes = bytes.fromhex(data)
assert storage.imghdr.what(None, data_bytes) is not None
assert storage.get_image_type_from_header(data_bytes) == "jpeg"


def test_get_image_type_from_header_unknown_header():
data_bytes = b"PIF81affe000104a464"
with pytest.raises(ValueError):
storage.get_image_type_from_header(data_bytes)


def test_get_image_type_from_header_too_short_header():
data_bytes = b"\xFF"
with pytest.raises(ValueError):
storage.get_image_type_from_header(data_bytes)
Loading