beetbox / mediafile

elegant audio file tagging
http://mediafile.readthedocs.io/
MIT License
100 stars 26 forks source link

Replace deprecated `imghdr` package with `filetype` #75

Closed geigerzaehler closed 2 weeks ago

geigerzaehler commented 8 months ago

The imghdr package from the Python standard library is deprecated and will be removed in Python 3.13. The deprecated PEP suggests packages filetype, puremagic, python-magic as replacements. I chose filetype because it has a larger user-base than puremagic and does not have a C dependency, like python-magic.

jackwilsdon commented 1 month ago

It seems like there are quite a few changes to the return values of image_mime_type and image_extension as a part of switching to filetype (images from https://github.com/python/cpython/tree/v3.12.7/Lib/test/imghdrdata):

File Before After Match
imghdrdata/python.exr image/x-exr (exr) :heavy_multiplication_x:
imghdrdata/python.png image/png (png) image/png (png) :heavy_check_mark:
imghdrdata/python.xbm image/x-xbitmap (xbm) :heavy_multiplication_x:
imghdrdata/python.sgi image/x-rgb (rgb) :heavy_multiplication_x:
imghdrdata/python.ras image/x-rast (rast) :heavy_multiplication_x:
imghdrdata/python.jpg image/jpeg (jpg) image/jpeg (jpg) :heavy_check_mark:
imghdrdata/python.pgm image/x-portable-graymap (pgm) :heavy_multiplication_x:
imghdrdata/python.webp image/x-webp (webp) image/webp (webp) :heavy_multiplication_x:
imghdrdata/python.gif image/gif (gif) image/gif (gif) :heavy_check_mark:
imghdrdata/python.bmp image/bmp (bmp) image/bmp (bmp) :heavy_check_mark:
imghdrdata/python.pbm image/x-portable-bitmap (pbm) :heavy_multiplication_x:
imghdrdata/python.tiff image/tiff (tiff) image/tiff (tif) :heavy_multiplication_x:
imghdrdata/python-raw.jpg image/jpeg (jpg) image/jpeg (jpg) :heavy_check_mark:
imghdrdata/python.ppm image/x-portable-pixmap (ppm) :heavy_multiplication_x:

The majority of unrecognized types are ones that are less likely to be used, but it seems like the file extension for tif has changed which isn't ideal. Maybe this needs releasing as a breaking change?

theol0403 commented 3 weeks ago

For those looking to run beets in python3.13, the following command is a good workaround until this gets merged:

pip install git+https://github.com/geigerzaehler/mediafile/@replace-imghdr

gbcox commented 2 weeks ago

This has been patched in Fedora 41.

jackwilsdon commented 2 weeks ago

I've added a change to preserve the tiff extension for TIFF files, resulting in this matchup:

File Before After Match
imghdrdata/python.exr image/x-exr (exr) :heavy_multiplication_x:
imghdrdata/python.png image/png (png) image/png (png) :heavy_check_mark:
imghdrdata/python.xbm image/x-xbitmap (xbm) :heavy_multiplication_x:
imghdrdata/python.sgi image/x-rgb (rgb) :heavy_multiplication_x:
imghdrdata/python.ras image/x-rast (rast) :heavy_multiplication_x:
imghdrdata/python.jpg image/jpeg (jpg) image/jpeg (jpg) :heavy_check_mark:
imghdrdata/python.pgm image/x-portable-graymap (pgm) :heavy_multiplication_x:
imghdrdata/python.webp image/x-webp (webp) image/webp (webp) :heavy_multiplication_x:
imghdrdata/python.gif image/gif (gif) image/gif (gif) :heavy_check_mark:
imghdrdata/python.bmp image/bmp (bmp) image/bmp (bmp) :heavy_check_mark:
imghdrdata/python.pbm image/x-portable-bitmap (pbm) :heavy_multiplication_x:
imghdrdata/python.tiff image/tiff (tiff) image/tiff (tiff) :heavy_check_mark:
imghdrdata/python-raw.jpg image/jpeg (jpg) image/jpeg (jpg) :heavy_check_mark:
imghdrdata/python.ppm image/x-portable-pixmap (ppm) :heavy_multiplication_x:

I think this is good enough - the only mismatch is image/x-webp which isn't the correct MIME type for WebP anyway, and the formats we've lost support for aren't normally seen in media files to begin with.

jackwilsdon commented 2 weeks ago

Thanks @geigerzaehler!