From 4d079a8c2089c267a73605b5dafd831aa7a7ac74 Mon Sep 17 00:00:00 2001 From: Valay Dave Date: Thu, 19 Jun 2025 16:33:00 -0700 Subject: [PATCH 1/2] Vendor imghdr module --- metaflow/_vendor/imghdr.LICENSE | 48 +++++ metaflow/_vendor/imghdr/__init__.py | 180 ++++++++++++++++++ .../card_modules/convert_to_native_type.py | 7 +- 3 files changed, 233 insertions(+), 2 deletions(-) create mode 100644 metaflow/_vendor/imghdr.LICENSE create mode 100644 metaflow/_vendor/imghdr/__init__.py diff --git a/metaflow/_vendor/imghdr.LICENSE b/metaflow/_vendor/imghdr.LICENSE new file mode 100644 index 00000000000..6a0baeded1c --- /dev/null +++ b/metaflow/_vendor/imghdr.LICENSE @@ -0,0 +1,48 @@ +Copyright © 2001-2023 Python Software Foundation; All Rights Reserved + +This code originally taken from the Python 3.11.3 distribution +and it is therefore now released under the following Python-style +license: + +1. This LICENSE AGREEMENT is between the Python Software Foundation ("PSF"), and + the Individual or Organization ("Licensee") accessing and + otherwise using nntplib software in source or binary form and + its associated documentation. + +2. Subject to the terms and conditions of this License Agreement, PSF hereby + grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce, + analyze, test, perform and/or display publicly, prepare derivative works, + distribute, and otherwise use nntplib alone or in any derivative + version, provided, however, that PSF's License Agreement and PSF's notice of + copyright, i.e., "Copyright © 2001-2023 Python Software Foundation; All Rights + Reserved" are retained in nntplib alone or in any derivative version + prepared by Licensee. + +3. In the event Licensee prepares a derivative work that is based on or + incorporates nntplib or any part thereof, and wants to make the + derivative work available to others as provided herein, then Licensee hereby + agrees to include in any such work a brief summary of the + changes made to nntplib. + +4. PSF is making nntplib available to Licensee on an "AS IS" basis. + PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF + EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND DISCLAIMS ANY REPRESENTATION OR + WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE + USE OF NNTPLIB WILL NOT INFRINGE ANY THIRD PARTY RIGHTS. + +5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF NNTPLIB + FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF + MODIFYING, DISTRIBUTING, OR OTHERWISE USING NNTPLIB, OR ANY DERIVATIVE + THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. + +6. This License Agreement will automatically terminate upon a material breach of + its terms and conditions. + +7. Nothing in this License Agreement shall be deemed to create any relationship + of agency, partnership, or joint venture between PSF and Licensee. This License + Agreement does not grant permission to use PSF trademarks or trade name in a + trademark sense to endorse or promote products or services of Licensee, or any + third party. + +8. By copying, installing or otherwise using nntplib, Licensee agrees + to be bound by the terms and conditions of this License Agreement. diff --git a/metaflow/_vendor/imghdr/__init__.py b/metaflow/_vendor/imghdr/__init__.py new file mode 100644 index 00000000000..33868883470 --- /dev/null +++ b/metaflow/_vendor/imghdr/__init__.py @@ -0,0 +1,180 @@ +"""Recognize image file formats based on their first few bytes.""" + +from os import PathLike +import warnings + +__all__ = ["what"] + + +warnings._deprecated(__name__, remove=(3, 13)) + + +#-------------------------# +# Recognize image headers # +#-------------------------# + +def what(file, h=None): + """Return the type of image contained in a file or byte stream.""" + f = None + try: + if h is None: + if isinstance(file, (str, PathLike)): + f = open(file, 'rb') + h = f.read(32) + else: + location = file.tell() + h = file.read(32) + file.seek(location) + for tf in tests: + res = tf(h, f) + if res: + return res + finally: + if f: f.close() + return None + + +#---------------------------------# +# Subroutines per image file type # +#---------------------------------# + +tests = [] + +def test_jpeg(h, f): + """Test for JPEG data with JFIF or Exif markers; and raw JPEG.""" + if h[6:10] in (b'JFIF', b'Exif'): + return 'jpeg' + elif h[:4] == b'\xff\xd8\xff\xdb': + return 'jpeg' + +tests.append(test_jpeg) + +def test_png(h, f): + """Verify if the image is a PNG.""" + if h.startswith(b'\211PNG\r\n\032\n'): + return 'png' + +tests.append(test_png) + +def test_gif(h, f): + """Verify if the image is a GIF ('87 or '89 variants).""" + if h[:6] in (b'GIF87a', b'GIF89a'): + return 'gif' + +tests.append(test_gif) + +def test_tiff(h, f): + """Verify if the image is a TIFF (can be in Motorola or Intel byte order).""" + if h[:2] in (b'MM', b'II'): + return 'tiff' + +tests.append(test_tiff) + +def test_rgb(h, f): + """test for the SGI image library.""" + if h.startswith(b'\001\332'): + return 'rgb' + +tests.append(test_rgb) + +def test_pbm(h, f): + """Verify if the image is a PBM (portable bitmap).""" + if len(h) >= 3 and \ + h[0] == ord(b'P') and h[1] in b'14' and h[2] in b' \t\n\r': + return 'pbm' + +tests.append(test_pbm) + +def test_pgm(h, f): + """Verify if the image is a PGM (portable graymap).""" + if len(h) >= 3 and \ + h[0] == ord(b'P') and h[1] in b'25' and h[2] in b' \t\n\r': + return 'pgm' + +tests.append(test_pgm) + +def test_ppm(h, f): + """Verify if the image is a PPM (portable pixmap).""" + if len(h) >= 3 and \ + h[0] == ord(b'P') and h[1] in b'36' and h[2] in b' \t\n\r': + return 'ppm' + +tests.append(test_ppm) + +def test_rast(h, f): + """test for the Sun raster file.""" + if h.startswith(b'\x59\xA6\x6A\x95'): + return 'rast' + +tests.append(test_rast) + +def test_xbm(h, f): + """Verify if the image is a X bitmap (X10 or X11).""" + if h.startswith(b'#define '): + return 'xbm' + +tests.append(test_xbm) + +def test_bmp(h, f): + """Verify if the image is a BMP file.""" + if h.startswith(b'BM'): + return 'bmp' + +tests.append(test_bmp) + +def test_webp(h, f): + """Verify if the image is a WebP.""" + if h.startswith(b'RIFF') and h[8:12] == b'WEBP': + return 'webp' + +tests.append(test_webp) + +def test_exr(h, f): + """verify is the image ia a OpenEXR fileOpenEXR.""" + if h.startswith(b'\x76\x2f\x31\x01'): + return 'exr' + +tests.append(test_exr) + +#--------------------# +# Small test program # +#--------------------# + +def test(): + import sys + recursive = 0 + if sys.argv[1:] and sys.argv[1] == '-r': + del sys.argv[1:2] + recursive = 1 + try: + if sys.argv[1:]: + testall(sys.argv[1:], recursive, 1) + else: + testall(['.'], recursive, 1) + except KeyboardInterrupt: + sys.stderr.write('\n[Interrupted]\n') + sys.exit(1) + +def testall(list, recursive, toplevel): + import sys + import os + for filename in list: + if os.path.isdir(filename): + print(filename + '/:', end=' ') + if recursive or toplevel: + print('recursing down:') + import glob + names = glob.glob(os.path.join(glob.escape(filename), '*')) + testall(names, recursive, 0) + else: + print('*** directory (use -r) ***') + else: + print(filename + ':', end=' ') + sys.stdout.flush() + try: + print(what(filename)) + except OSError: + print('*** not found ***') + +if __name__ == '__main__': + test() diff --git a/metaflow/plugins/cards/card_modules/convert_to_native_type.py b/metaflow/plugins/cards/card_modules/convert_to_native_type.py index 9c5e80ade5d..43f8a5f824e 100644 --- a/metaflow/plugins/cards/card_modules/convert_to_native_type.py +++ b/metaflow/plugins/cards/card_modules/convert_to_native_type.py @@ -143,7 +143,10 @@ def parse_image(self, data_object): obj_type_name = self._get_object_type(data_object) if obj_type_name == "bytes": # Works for python 3.1+ - import imghdr + # Python 3.13 removes the standard ``imghdr`` module. Metaflow + # vendors a copy so we can keep using ``what`` to detect image + # formats irrespective of the Python version. + from metaflow._vendor import imghdr resp = imghdr.what(None, h=data_object) # Only accept types supported on the web @@ -157,7 +160,7 @@ def _extract_type_infered_object(self, data_object): obj_type_name = self._get_object_type(data_object) if obj_type_name == "bytes": # Works for python 3.1+ - import imghdr + from metaflow._vendor import imghdr resp = imghdr.what(None, h=data_object) # Only accept types supported on the web From 665bf749342b40e1fd335e074409aa9c9274892d Mon Sep 17 00:00:00 2001 From: Valay Dave Date: Thu, 19 Jun 2025 16:43:44 -0700 Subject: [PATCH 2/2] Add image card using vendored imghdr --- metaflow/_vendor/imghdr.LICENSE | 20 +++++++++---------- metaflow/plugins/__init__.py | 2 ++ .../plugins/cards/card_modules/test_cards.py | 16 +++++++++++++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/metaflow/_vendor/imghdr.LICENSE b/metaflow/_vendor/imghdr.LICENSE index 6a0baeded1c..c046fc7d756 100644 --- a/metaflow/_vendor/imghdr.LICENSE +++ b/metaflow/_vendor/imghdr.LICENSE @@ -6,33 +6,33 @@ license: 1. This LICENSE AGREEMENT is between the Python Software Foundation ("PSF"), and the Individual or Organization ("Licensee") accessing and - otherwise using nntplib software in source or binary form and + otherwise using imghdr software in source or binary form and its associated documentation. 2. Subject to the terms and conditions of this License Agreement, PSF hereby grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce, analyze, test, perform and/or display publicly, prepare derivative works, - distribute, and otherwise use nntplib alone or in any derivative + distribute, and otherwise use imghdr alone or in any derivative version, provided, however, that PSF's License Agreement and PSF's notice of copyright, i.e., "Copyright © 2001-2023 Python Software Foundation; All Rights - Reserved" are retained in nntplib alone or in any derivative version + Reserved" are retained in imghdr alone or in any derivative version prepared by Licensee. 3. In the event Licensee prepares a derivative work that is based on or - incorporates nntplib or any part thereof, and wants to make the + incorporates imghdr or any part thereof, and wants to make the derivative work available to others as provided herein, then Licensee hereby agrees to include in any such work a brief summary of the - changes made to nntplib. + changes made to imghdr. -4. PSF is making nntplib available to Licensee on an "AS IS" basis. +4. PSF is making imghdr available to Licensee on an "AS IS" basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE - USE OF NNTPLIB WILL NOT INFRINGE ANY THIRD PARTY RIGHTS. + USE OF IMGHDR WILL NOT INFRINGE ANY THIRD PARTY RIGHTS. -5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF NNTPLIB +5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF IMGHDR FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF - MODIFYING, DISTRIBUTING, OR OTHERWISE USING NNTPLIB, OR ANY DERIVATIVE + MODIFYING, DISTRIBUTING, OR OTHERWISE USING IMGHDR, OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. 6. This License Agreement will automatically terminate upon a material breach of @@ -44,5 +44,5 @@ license: trademark sense to endorse or promote products or services of Licensee, or any third party. -8. By copying, installing or otherwise using nntplib, Licensee agrees +8. By copying, installing or otherwise using imghdr, Licensee agrees to be bound by the terms and conditions of this License Agreement. diff --git a/metaflow/plugins/__init__.py b/metaflow/plugins/__init__.py index c10886b1708..2ffa363dfc5 100644 --- a/metaflow/plugins/__init__.py +++ b/metaflow/plugins/__init__.py @@ -231,6 +231,7 @@ def get_runner_cli_path(): TestTimeoutCard, TestRefreshCard, TestRefreshComponentCard, + TestImageCard, ) CARDS = [ @@ -249,5 +250,6 @@ def get_runner_cli_path(): DefaultCardJSON, TestRefreshCard, TestRefreshComponentCard, + TestImageCard, ] merge_lists(CARDS, MF_EXTERNAL_CARDS, "type") diff --git a/metaflow/plugins/cards/card_modules/test_cards.py b/metaflow/plugins/cards/card_modules/test_cards.py index c15230b2324..b63ac0929d7 100644 --- a/metaflow/plugins/cards/card_modules/test_cards.py +++ b/metaflow/plugins/cards/card_modules/test_cards.py @@ -213,3 +213,19 @@ def reload_content_token(self, task, data): if task.finished: return "final" return "runtime-%s" % _component_values_to_hash(data["components"]) + + +class TestImageCard(MetaflowCard): + """Card that renders a tiny PNG using ``TaskToDict.parse_image``.""" + + type = "test_image_card" + + def render(self, task): + from .convert_to_native_type import TaskToDict + import base64 + + png_bytes = base64.b64decode( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGNgYGBgAAAABQABRDE8UwAAAABJRU5ErkJggg==" + ) + img_src = TaskToDict().parse_image(png_bytes) + return f""