From 651613c08f7f1bb5a1179daa8c23bbc156e965cc Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Thu, 5 Sep 2024 15:40:22 +0100 Subject: [PATCH] Drop use of deprecated imghdr standard library module which has been removed in the upcoming Python 3.13 . --- lib/galaxy/util/image_util.py | 5 ----- test/unit/util/test_checkers.py | 13 ++++++++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/galaxy/util/image_util.py b/lib/galaxy/util/image_util.py index 3b11a50d2fda..b8c26cf0ff54 100644 --- a/lib/galaxy/util/image_util.py +++ b/lib/galaxy/util/image_util.py @@ -1,6 +1,5 @@ """Provides utilities for working with image files.""" -import imghdr import logging from typing import ( List, @@ -22,11 +21,7 @@ def image_type(filename: str) -> Optional[str]: with Image.open(filename) as im: fmt = im.format except Exception: - # We continue to try with imghdr, so this is a rare case of an - # exception we expect to happen frequently, so we're not logging pass - if not fmt: - fmt = imghdr.what(filename) if fmt: return fmt.upper() else: diff --git a/test/unit/util/test_checkers.py b/test/unit/util/test_checkers.py index 1e30d25656cd..b474defc965a 100644 --- a/test/unit/util/test_checkers.py +++ b/test/unit/util/test_checkers.py @@ -1,6 +1,10 @@ +import os.path import tempfile -from galaxy.util.checkers import check_html +from galaxy.util.checkers import ( + check_html, + check_image, +) def test_check_html(): @@ -17,3 +21,10 @@ def test_check_html(): tmpb.write(b"\x1f\x8b") tmpb.flush() assert not check_html(tmpb.name) + + +def test_check_image(): + for filename, expected in (("1.tiff", True), ("454Score.png", True), ("1.bam", False)): + path = f"test-data/{filename}" + assert os.path.exists(path) + assert check_image(path) is expected