Skip to content

Commit f41e9c1

Browse files
committed
Support à la carte dependency extras
Move runtime dependencies into optional extras (zim, image, video, download, filesystem, html, rewriting, i18n, all), keep beartype as the only base dependency, and raise actionable ImportErrors when an extra is missing. Resolve cross-feature imports at call time so each extra stands alone. Add metadata/import tests, a CI matrix, docs, and changelog entries. Fixes #244
1 parent 0f8b295 commit f41e9c1

31 files changed

Lines changed: 691 additions & 147 deletions

‎.github/workflows/QA.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
- name: Install dependencies (and project)
6363
run: |
6464
pip install -U pip
65-
pip install -e .[qa,scripts,test]
65+
pip install -e .[all,qa,scripts,test]
6666
6767
- name: Check Linting
6868
run: inv check-lint

‎.github/workflows/Tests.yaml‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,80 @@ jobs:
4040
name: rules
4141
retention-days: 1
4242

43+
check-extras-errors:
44+
runs-on: ubuntu-24.04
45+
46+
steps:
47+
- name: Checkout repo
48+
uses: actions/checkout@v7
49+
50+
- name: Set up Python
51+
uses: actions/setup-python@v7
52+
with:
53+
python-version-file: pyproject.toml
54+
architecture: x64
55+
56+
- name: Install minimal dependencies (and project)
57+
run: |
58+
pip install -U pip
59+
pip install -e .[test]
60+
61+
- name: Check optional dependency errors
62+
run: pytest -q tests/test_extras.py
63+
64+
check-extras:
65+
runs-on: ubuntu-24.04
66+
needs: generate-rules
67+
strategy:
68+
fail-fast: false
69+
matrix:
70+
include:
71+
- extra: none
72+
module: zimscraperlib
73+
- extra: zim
74+
module: zimscraperlib.zim
75+
- extra: image
76+
module: zimscraperlib.image
77+
- extra: video
78+
module: zimscraperlib.video
79+
- extra: download
80+
module: zimscraperlib.download
81+
- extra: filesystem
82+
module: zimscraperlib.filesystem
83+
- extra: html
84+
module: zimscraperlib.html
85+
- extra: rewriting
86+
module: zimscraperlib.rewriting.html
87+
- extra: i18n
88+
module: zimscraperlib.i18n
89+
90+
steps:
91+
- name: Checkout repo
92+
uses: actions/checkout@v7
93+
94+
- name: Restore rules artifact
95+
uses: actions/download-artifact@v8
96+
with:
97+
name: rules
98+
99+
- name: Set up Python
100+
uses: actions/setup-python@v7
101+
with:
102+
python-version-file: pyproject.toml
103+
architecture: x64
104+
105+
- name: Install dependencies (and project)
106+
run: |
107+
pip install -U pip
108+
if [ "${{ matrix.extra }}" = "none" ]; then
109+
pip install -e .
110+
else
111+
pip install -e ".[${{ matrix.extra }}]"
112+
fi
113+
114+
- name: Import module using only that extra
115+
run: python -c "import importlib; importlib.import_module('${{ matrix.module }}')"
116+
43117
run-python-tests:
44118
runs-on: ubuntu-24.04
45119
needs: generate-rules
@@ -65,7 +139,7 @@ jobs:
65139
- name: Install dependencies (and project)
66140
run: |
67141
pip install -U pip
68-
pip install -e .[test,scripts]
142+
pip install -e .[all,test,scripts]
69143
70144
- name: Run the tests
71145
run: inv coverage --args "--runslow --runinstalled -vvv"

‎CHANGELOG.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Optional dependency groups `zim`, `image`, `video`, `download`, `filesystem`, `html`, `rewriting`, `i18n` and `all`; the base install now only requires `beartype` (#244)
13+
- Missing optional dependencies now raise an `ImportError` naming the extra to install (#244)
14+
15+
### Changed
16+
17+
- Cross-feature imports are now resolved where they are used, so each extra only needs its own dependencies (#244)
18+
19+
### Fixed
20+
21+
- The `rewriting` extra now declares `tinycss2` explicitly instead of relying on it being pulled in through `CairoSVG` (#244)
22+
1023
## [5.6.0] - 2026-11-20
1124

1225
### Added

‎README.md‎

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,49 @@ See documentation at [Read the Docs](https://python-scraperlib.readthedocs.io/)
3434
3535
# Dependencies
3636

37-
Most dependencies are installed automatically by pip (from PyPI by default). The following system packages may be required depending on which features you use:
37+
The base install only pulls in `beartype`. Install the extras for the features you use:
3838

39-
- **libmagic** — required for file type detection (used in most scrapers)
40-
- **wget** — required only for `zimscraperlib.download` functions
41-
- **FFmpeg** — required only for video processing functions
42-
- **gifsicle** (>=1.92) — required only for GIF optimization
43-
- **libcairo** — required only for SVG-to-PNG conversion
44-
- **libzim** — auto-installed via PyPI, not available on Windows
45-
- **Pillow** — auto-installed via PyPI; pre-built wheels are used by default and no system image libraries are needed. Only if you need to build Pillow from source should you install additional system libraries — see [Pillow's build documentation](https://pillow.readthedocs.io/en/latest/installation/building-from-source.html) for details.
46-
> **Note:** To run the full test suite, all system dependencies listed above must be installed.
39+
```sh
40+
python -m pip install 'zimscraperlib[zim,image]'
41+
python -m pip install 'zimscraperlib[all]'
42+
```
43+
44+
Importing a module whose extra is missing raises an `ImportError` naming the extra to install.
45+
46+
| Extra | Python dependencies for | System dependencies |
47+
| --- | --- | --- |
48+
| `zim` | `zimscraperlib.zim` | none (`libzim` ships wheels, except on unsupported platforms) |
49+
| `image` | `zimscraperlib.image` | libcairo for SVG conversion, gifsicle (>=1.92) for GIF optimization |
50+
| `video` | `zimscraperlib.video` | FFmpeg |
51+
| `download` | `zimscraperlib.download` | none; wget is only needed if your scraper calls it itself |
52+
| `filesystem` | `zimscraperlib.filesystem` and MIME-type detection | libmagic |
53+
| `html` | `zimscraperlib.html` | none |
54+
| `rewriting` | `zimscraperlib.rewriting` | none |
55+
| `i18n` | `zimscraperlib.i18n` | none |
56+
| `all` | every runtime extra | every system dependency above |
57+
58+
Pillow uses pre-built wheels by default; no system image libraries are needed unless you build it from source (see [Pillow's build documentation](https://pillow.readthedocs.io/en/latest/installation/building-from-source.html)).
59+
60+
> **Note:** To run the full test suite, install `all` and all system dependencies listed above.
61+
62+
To install every system dependency listed above:
4763

4864
## macOS
4965

5066
```sh
51-
brew install libmagic wget ffmpeg gifsicle cairo
67+
brew install libmagic ffmpeg gifsicle cairo
5268
```
5369

5470
## Linux
5571

5672
```sh
57-
sudo apt install libmagic1 wget ffmpeg gifsicle libcairo2
73+
sudo apt install libmagic1 ffmpeg gifsicle libcairo2
5874
```
5975

6076
## Alpine
6177

6278
```sh
63-
apk add ffmpeg gifsicle libmagic wget cairo
79+
apk add ffmpeg gifsicle libmagic cairo
6480
```
6581

6682
# Contribution

‎pyproject.toml‎

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,75 @@ requires-python = ">=3.14,<3.15"
1010
description = "Collection of python tools to re-use common code across scrapers"
1111
readme = "README.md"
1212
dependencies = [
13-
"iso639-lang>=2.4.0,<3.0",
14-
"requests>=2.25.1,<3.0",
15-
"colorthief==0.2.1",
16-
"python-resize-image>=1.1.19,<1.2",
17-
"Babel>=2.9,<3.0",
18-
"python-magic>=0.4.3,<0.5",
13+
"beartype>=0.19,<0.24",
14+
]
15+
dynamic = ["authors", "classifiers", "keywords", "license", "version", "urls"]
16+
17+
[tool.hatch.metadata.hooks.openzim-metadata]
18+
kind = "scraper"
19+
additional-classifiers = [
20+
"Development Status :: 5 - Production/Stable",
21+
"Intended Audience :: Developers",
22+
]
23+
24+
[tool.hatch.build.hooks.openzim-build]
25+
26+
[project.optional-dependencies]
27+
zim = [
1928
"libzim>=3.8.0,<4.0",
20-
"beautifulsoup4>=4.9.3,<5.0",
21-
"lxml>=4.6.3,<7.0",
22-
"optimize-images>=1.3.6,<3.0",
29+
"xxhash>=2.0,<5.0",
2330
# regex has no upper-bound due to "date-based" release numbers, no semver, so their
2431
# promise is that they will never (or always) break the API, and the API is very
2532
# limited and we use only a very small subset of it.
2633
"regex>=2020.7.14",
2734
# pymupdf-lite is the official build of pymupdf without its layout engine, and
2835
# without the heavy dependencies (onnxruntime, numpy, ...) it brings since 2.0
2936
"pymupdf-lite>=2.0,<3.0",
37+
"types-xxhash>=2.0,<4.0",
38+
]
39+
image = [
40+
"pillow>=7.0.0,<14.0",
41+
"colorthief==0.2.1",
42+
# piexif is pinned because of its irregular release management
43+
"piexif==1.1.3",
44+
"optimize-images>=1.3.6,<3.0",
45+
"python-resize-image>=1.1.19,<1.2",
3046
"CairoSVG>=2.2.0,<3.0",
31-
"beartype>=0.19,<0.24",
47+
]
48+
video = []
49+
download = [
50+
"requests>=2.25.1,<3.0",
51+
"urllib3>=1.26.5,<2.9.0",
3252
# youtube-dl should be updated as frequently as possible
3353
"yt-dlp",
34-
"pillow>=7.0.0,<14.0",
35-
"urllib3>=1.26.5,<2.9.0",
36-
"piexif==1.1.3", # this dep is a nightmare in terms of release management, better pinned just like in optimize-images anyway
54+
]
55+
filesystem = [
56+
"python-magic>=0.4.3,<0.5",
57+
]
58+
html = [
59+
"beautifulsoup4>=4.9.3,<5.0",
60+
"lxml>=4.6.3,<7.0",
61+
]
62+
rewriting = [
63+
"beautifulsoup4>=4.9.3,<5.0",
64+
"lxml>=4.6.3,<7.0",
3765
"idna>=2.5,<4.0",
38-
"xxhash>=2.0,<5.0",
66+
"tinycss2>=1.2,<2.0",
3967
# Parsing JavaScript well enough to know which names a script declares at its
4068
# top level (see rewriting/js_ast.py). tree-sitter rather than a pure-Python
4169
# parser because the input is whatever the live web served: esprima is ES2017
4270
# and refuses optional chaining, class fields and `for await`, all ordinary in
4371
# shipped code, and a parse failure here silently restores the bug this fixes.
4472
"tree-sitter>=0.23,<1.0",
4573
"tree-sitter-javascript>=0.23,<1.0",
46-
"types-xxhash>=2.0,<4.0",
4774
]
48-
dynamic = ["authors", "classifiers", "keywords", "license", "version", "urls"]
49-
50-
[tool.hatch.metadata.hooks.openzim-metadata]
51-
kind = "scraper"
52-
additional-classifiers = [
53-
"Development Status :: 5 - Production/Stable",
54-
"Intended Audience :: Developers",
75+
i18n = [
76+
"Babel>=2.9,<3.0",
77+
"iso639-lang>=2.4.0,<3.0",
78+
]
79+
all = [
80+
"zimscraperlib[zim,image,video,download,filesystem,html,rewriting,i18n]",
5581
]
56-
57-
[tool.hatch.build.hooks.openzim-build]
58-
59-
[project.optional-dependencies]
6082
scripts = [
6183
"invoke==3.0.3",
6284
# jinja2 is required to generate JS and Python rules at build time
@@ -90,10 +112,7 @@ docs = [
90112
dev = [
91113
"ipython==9.17.1",
92114
"pre-commit==4.7.0",
93-
"zimscraperlib[scripts]",
94-
"zimscraperlib[qa]",
95-
"zimscraperlib[test]",
96-
"zimscraperlib[docs]",
115+
"zimscraperlib[all,scripts,qa,test,docs]",
97116
]
98117

99118
[project.scripts]
@@ -126,7 +145,7 @@ artifacts = [
126145
features = ["dev"]
127146

128147
[tool.hatch.envs.test]
129-
features = ["scripts", "test"]
148+
features = ["scripts", "test", "all"]
130149

131150
[tool.hatch.envs.test.scripts]
132151
run = "inv test --args '{args}'"
@@ -138,7 +157,7 @@ html = "inv coverage --html --args '{args}'"
138157
[tool.hatch.envs.qa]
139158
template = "qa"
140159
skip-install = false
141-
features = ["scripts", "qa", "test"]
160+
features = ["scripts", "qa", "test", "all"]
142161

143162
[tool.hatch.envs.qa.scripts]
144163
check-lint = "inv check-lint --args '{args}'"
@@ -150,7 +169,7 @@ fix-lint = "inv fix-lint --args '{args}'"
150169
fix-all = "inv fix-all --args '{args}'"
151170

152171
[tool.hatch.envs.docs]
153-
features = ["scripts", "docs"]
172+
features = ["scripts", "docs", "all"]
154173

155174
[tool.hatch.envs.docs.scripts]
156175
serve = "inv docs-serve --args '{args}'"

‎src/zimscraperlib/_extras.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from typing import NoReturn
2+
3+
4+
def missing_extra(extra: str, error: ModuleNotFoundError) -> NoReturn:
5+
name = error.name or "an optional dependency"
6+
raise ImportError(
7+
f"Missing dependency '{name}' required by the '{extra}' extra. "
8+
f"Install it with `python -m pip install 'zimscraperlib[{extra}]'` "
9+
f"or `python -m pip install 'zimscraperlib[all]'`."
10+
) from error

‎src/zimscraperlib/download.py‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
from typing import Any, ClassVar
77
from urllib.parse import urlparse
88

9-
import requests
10-
import requests.adapters
11-
import requests.structures
12-
import urllib3.util
13-
import yt_dlp as youtube_dl # pyright: ignore[reportMissingTypeStubs]
9+
from zimscraperlib._extras import missing_extra
10+
11+
try:
12+
import requests
13+
import requests.adapters
14+
import requests.structures
15+
import urllib3.util
16+
import yt_dlp as youtube_dl # pyright: ignore[reportMissingTypeStubs]
17+
except ModuleNotFoundError as exc:
18+
raise missing_extra("download", exc) from exc
1419

1520
from zimscraperlib import logger
1621
from zimscraperlib.constants import (

‎src/zimscraperlib/filesystem.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
from tempfile import TemporaryDirectory
88
from typing import Any
99

10-
import magic
10+
from zimscraperlib._extras import missing_extra
11+
12+
try:
13+
import magic
14+
except ModuleNotFoundError as exc:
15+
raise missing_extra("filesystem", exc) from exc
1116

1217
# override some MIME-types found by libmagic to different ones
1318
MIME_OVERRIDES = {

‎src/zimscraperlib/html.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import pathlib
44
from typing import BinaryIO, TextIO
55

6-
from bs4 import BeautifulSoup, element
6+
from zimscraperlib._extras import missing_extra
7+
8+
try:
9+
from bs4 import BeautifulSoup, element
10+
except ModuleNotFoundError as exc:
11+
raise missing_extra("html", exc) from exc
712

813
from zimscraperlib.types import ARTICLE_MIME
914

‎src/zimscraperlib/i18n.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import re
22

3-
import babel
4-
import iso639 # pyright: ignore[reportMissingTypeStubs]
5-
import iso639.exceptions # pyright: ignore[reportMissingTypeStubs]
3+
from zimscraperlib._extras import missing_extra
4+
5+
try:
6+
import babel
7+
import iso639 # pyright: ignore[reportMissingTypeStubs]
8+
import iso639.exceptions # pyright: ignore[reportMissingTypeStubs]
9+
except ModuleNotFoundError as exc:
10+
raise missing_extra("i18n", exc) from exc
611

712
ISO_LEVELS = ["1", "2b", "2t", "3", "5"]
813

0 commit comments

Comments
 (0)