Skip to content

Commit aa79af8

Browse files
committed
Move to python-magic for alpine compatibility
python-magic is supporting Alpine Linux since 0.4.24 (hence the minimum version) while file-magic is still not supporting it.
1 parent 56628e1 commit aa79af8

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies = [
1313
"colorthief==0.2.1",
1414
"python-resize-image>=1.1.19,<1.2",
1515
"Babel>=2.9,<3.0",
16-
"file-magic>=0.4.0,<0.5",
16+
"python-magic>=0.4.24,<0.5",
1717
"libzim>=3.4.0,<4.0",
1818
"beautifulsoup4>=4.9.3,<4.10", # upgrade to 4.10 and later to be done
1919
"lxml>=4.6.3,<4.10", # upgrade to 4.10 and later to be done

src/zimscraperlib/filesystem.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ def get_content_mimetype(content: bytes) -> str:
3131
"""MIME Type of content retrieved from magic headers"""
3232

3333
try:
34-
detected_mime = magic.detect_from_content(content).mime_type
34+
detected_mime = magic.from_buffer(content, mime=True)
35+
if isinstance(
36+
detected_mime, bytes
37+
): # pragma: no cover (old python-magic versions where returning bytes)
38+
detected_mime = detected_mime.decode()
3539
except UnicodeDecodeError:
3640
return "application/octet-stream"
3741
return MIME_OVERRIDES.get(detected_mime, detected_mime)

tests/filesystem/test_filesystem.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def test_content_mimetype_fallback(monkeypatch, undecodable_byte_stream):
2828
assert get_content_mimetype(undecodable_byte_stream) == "application/octet-stream"
2929

3030
# mock then so we keep coverage on systems where magic works
31-
def raising_magic(*args): # noqa: ARG001
31+
def raising_magic(*args, **kwargs): # noqa: ARG001
3232
raise UnicodeDecodeError("nocodec", b"", 0, 1, "noreason")
3333

34-
monkeypatch.setattr(magic, "detect_from_content", raising_magic)
34+
monkeypatch.setattr(magic, "from_buffer", raising_magic)
3535
assert get_content_mimetype(undecodable_byte_stream) == "application/octet-stream"
3636

3737

0 commit comments

Comments
 (0)