From 77c3ed7cf5e3aed60f5465ce174da79c3c954579 Mon Sep 17 00:00:00 2001 From: Denys Savchenko <56398430+dsavchenko@users.noreply.github.com> Date: Tue, 19 Nov 2024 10:53:35 +0100 Subject: [PATCH] Get rid of deprecated `imghdr` (#298) * test up to python3.13 in action * imghdr->puremagic * install self in action * no gw * check if image * properly call puremagic * also install in test-live * restrict astropy version * typo * revert astropy restriction * puremagic in requirements.txt * puremagic in doc reqs --- .github/workflows/python-package-live.yml | 1 + .github/workflows/python-package.yml | 3 ++- doc/requirements.txt | 2 +- oda_api/data_products.py | 21 +++++++++++++++++---- pyproject.toml | 5 +++-- requirements.txt | 1 + 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/.github/workflows/python-package-live.yml b/.github/workflows/python-package-live.yml index 91504dd0..2e2474f9 100644 --- a/.github/workflows/python-package-live.yml +++ b/.github/workflows/python-package-live.yml @@ -40,6 +40,7 @@ jobs: python -m pip install -r doc/requirements.txt python -m ipykernel install --prefix $HOME/.local/share/jupyter/kernels --name python3 if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + pip install .[test,extra-test,ontology] - name: Test with pytest run: | diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 48b1668f..359e711d 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ['3.8', '3.9', '3.10', '3.11'] + python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13'] steps: - uses: actions/checkout@v4 @@ -31,6 +31,7 @@ jobs: python -m pip install types-simplejson types-requests types-click python -m pip install oda-knowledge-base[rdf,cwl] if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + pip install .[test,extra-test,ontology] curl -o tests/oda-ontology.ttl https://raw.githubusercontent.com/oda-hub/ontology/main/ontology.ttl - name: Lint with flake8 diff --git a/doc/requirements.txt b/doc/requirements.txt index 8542e837..5ef8a4f0 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -18,4 +18,4 @@ pyjwt==2.4.0 astroquery==0.4.4 rdflib - +puremagic diff --git a/oda_api/data_products.py b/oda_api/data_products.py index af146823..ed789ea9 100644 --- a/oda_api/data_products.py +++ b/oda_api/data_products.py @@ -41,7 +41,7 @@ from sys import path_importer_cache, version_info from io import StringIO, BytesIO -import imghdr +import puremagic import os import logging from matplotlib import image as mpimg @@ -849,9 +849,22 @@ def __init__(self, binary_data, name=None, metadata={}, file_path=None, write_on self.write_file(file_path) else: self.file_path = None - byte_stream = BytesIO(binary_data) - tp = imghdr.what(byte_stream) - if tp is None: + tp = puremagic.what(None, h=binary_data) + if tp not in [ # the same as was in imghdr + 'rgb', + 'gif', + 'pbm', + 'pgm', + 'ppm', + 'tiff', + 'rast', + 'xbm', + 'jpeg', + 'bmp', + 'png', + 'webp', + 'exr', + ]: raise ValueError('Provided data is not an image') self.img_type = tp diff --git a/pyproject.toml b/pyproject.toml index b4baef48..1650d5f6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,8 @@ dependencies = [ "astroquery", "scipy", "rdflib", - "black" + "black", + "puremagic" ] [project.optional-dependencies] @@ -53,4 +54,4 @@ ontology = [ [project.scripts] oda-api = "oda_api.cli:main" -[tool.setuptools_scm] \ No newline at end of file +[tool.setuptools_scm] diff --git a/requirements.txt b/requirements.txt index bed92c6c..20fe7cfa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,4 @@ astroquery simplejson sentry_sdk rdflib +puremagic