Skip to content

Commit

Permalink
Get rid of deprecated imghdr (#298)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dsavchenko authored Nov 19, 2024
1 parent 810d3b1 commit 77c3ed7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/python-package-live.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ pyjwt==2.4.0
astroquery==0.4.4

rdflib

puremagic
21 changes: 17 additions & 4 deletions oda_api/data_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ dependencies = [
"astroquery",
"scipy",
"rdflib",
"black"
"black",
"puremagic"
]

[project.optional-dependencies]
Expand All @@ -53,4 +54,4 @@ ontology = [
[project.scripts]
oda-api = "oda_api.cli:main"

[tool.setuptools_scm]
[tool.setuptools_scm]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ astroquery
simplejson
sentry_sdk
rdflib
puremagic

0 comments on commit 77c3ed7

Please sign in to comment.