Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/QA.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
generate-rules:
runs-on: ubuntu-24.04
runs-on: ubuntu-26.04

steps:
- name: Checkout repo
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
retention-days: 1

check-python-qa:
runs-on: ubuntu-24.04
runs-on: ubuntu-26.04
needs: generate-rules

steps:
Expand All @@ -62,7 +62,7 @@ jobs:
- name: Install dependencies (and project)
run: |
pip install -U pip
pip install -e .[qa,scripts,test]
pip install -e .[all,qa,scripts,test]

- name: Check Linting
run: inv check-lint
Expand All @@ -74,7 +74,7 @@ jobs:
run: inv check-type

check-javascript-qa:
runs-on: ubuntu-24.04
runs-on: ubuntu-26.04
needs: generate-rules

steps:
Expand Down
82 changes: 77 additions & 5 deletions .github/workflows/Tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
generate-rules:
runs-on: ubuntu-24.04
runs-on: ubuntu-26.04

steps:
- name: Checkout repo
Expand Down Expand Up @@ -40,8 +40,80 @@ jobs:
name: rules
retention-days: 1

check-extras-errors:
runs-on: ubuntu-26.04

steps:
- name: Checkout repo
uses: actions/checkout@v7

- name: Set up Python
uses: actions/setup-python@v7
with:
python-version-file: pyproject.toml

- name: Install test dependencies (and project)
run: |
pip install -U pip
pip install -e '.[test,download]'

- name: Check optional dependency errors
run: pytest -q tests/test_extras.py

check-extras:
runs-on: ubuntu-26.04
needs: generate-rules
strategy:
fail-fast: false
matrix:
include:
- extra: none
module: zimscraperlib
- extra: zim
module: zimscraperlib.zim
- extra: image
module: zimscraperlib.image
- extra: video
module: zimscraperlib.video
- extra: download
module: zimscraperlib.download
- extra: filesystem
module: zimscraperlib.filesystem
- extra: html
module: zimscraperlib.html
- extra: rewriting
module: zimscraperlib.rewriting.html
- extra: i18n
module: zimscraperlib.i18n

steps:
- name: Checkout repo
uses: actions/checkout@v7

- name: Restore rules artifact
uses: actions/download-artifact@v8
with:
name: rules

- name: Set up Python
uses: actions/setup-python@v7
with:
python-version-file: pyproject.toml

- name: Install dependencies (and project)
run: |
pip install -U pip
if [ "${{ matrix.extra }}" = "none" ]; then
pip install -e .
else
pip install -e ".[${{ matrix.extra }}]"
fi

- name: Import module using only that extra
run: python -c "import importlib; importlib.import_module('${{ matrix.module }}')"

run-python-tests:
runs-on: ubuntu-24.04
runs-on: ubuntu-26.04
needs: generate-rules

steps:
Expand All @@ -65,7 +137,7 @@ jobs:
- name: Install dependencies (and project)
run: |
pip install -U pip
pip install -e .[test,scripts]
pip install -e .[all,test,scripts]

- name: Run the tests
run: inv coverage --args "--runslow --runinstalled -vvv"
Expand All @@ -82,7 +154,7 @@ jobs:
python3 -m build --sdist --wheel

run-js-tests:
runs-on: ubuntu-24.04
runs-on: ubuntu-26.04
needs: generate-rules

steps:
Expand Down Expand Up @@ -123,7 +195,7 @@ jobs:
working-directory: javascript

build-docs: # validate that doc can be built
runs-on: ubuntu-24.04
runs-on: ubuntu-26.04
needs: generate-rules

steps:
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Breaking Changes

- Runtime dependencies are now optional extras (`zim`, `image`, `video`, `download`, `filesystem`, `html`, `rewriting`, `i18n`, `all`); a base install only provides `beartype`, so install `zimscraperlib[all]` (or the extras you need) to keep the previous behavior (#244)
- `path_from` moved from `zimscraperlib.filesystem` to the new public `zimscraperlib.paths` module (#244)
- Missing optional dependencies now raise an `ImportError` naming the extra to install (#244)
- Cross-feature imports are now resolved where they are used, so each extra only needs its own dependencies (#244)

### Fixed

- The `rewriting` extra now declares `tinycss2` explicitly instead of relying on it being pulled in through `CairoSVG` (#244)

## [5.6.0] - 2026-11-20

### Added
Expand Down
40 changes: 28 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,49 @@ See documentation at [Read the Docs](https://python-scraperlib.readthedocs.io/)

# Dependencies

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

- **libmagic** — required for file type detection (used in most scrapers)
- **wget** — required only for `zimscraperlib.download` functions
- **FFmpeg** — required only for video processing functions
- **gifsicle** (>=1.92) — required only for GIF optimization
- **libcairo** — required only for SVG-to-PNG conversion
- **libzim** — auto-installed via PyPI, not available on Windows
- **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.
> **Note:** To run the full test suite, all system dependencies listed above must be installed.
```sh
python -m pip install 'zimscraperlib[zim,image]'
python -m pip install 'zimscraperlib[all]'
```

Importing a module whose extra is missing raises an `ImportError` naming the extra to install.

| Extra | Python dependencies for | System dependencies |
| --- | --- | --- |
| `zim` | `zimscraperlib.zim` | none (`libzim` ships wheels, except on unsupported platforms) |
| `image` | `zimscraperlib.image` | libcairo for SVG conversion, gifsicle (>=1.92) for GIF optimization |
| `video` | `zimscraperlib.video` | FFmpeg |
| `download` | `zimscraperlib.download` | none; downloads use `requests`, not `wget` |
| `filesystem` | `zimscraperlib.filesystem` and MIME-type detection | libmagic |
| `html` | `zimscraperlib.html` | none |
| `rewriting` | `zimscraperlib.rewriting` | none |
| `i18n` | `zimscraperlib.i18n` | none |
| `all` | every runtime extra | every system dependency above |

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)).

> **Note:** To run the full test suite, install `all` and all system dependencies listed above.

To install every system dependency listed above:

## macOS

```sh
brew install libmagic wget ffmpeg gifsicle cairo
brew install libmagic ffmpeg gifsicle cairo
```

## Linux

```sh
sudo apt install libmagic1 wget ffmpeg gifsicle libcairo2
sudo apt install libmagic1 ffmpeg gifsicle libcairo2
```

## Alpine

```sh
apk add ffmpeg gifsicle libmagic wget cairo
apk add ffmpeg gifsicle libmagic cairo
```

# Contribution
Expand Down
85 changes: 52 additions & 33 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,75 @@ requires-python = ">=3.14,<3.15"
description = "Collection of python tools to re-use common code across scrapers"
readme = "README.md"
dependencies = [
"iso639-lang>=2.4.0,<3.0",
"requests>=2.25.1,<3.0",
"colorthief==0.2.1",
"python-resize-image>=1.1.19,<1.2",
"Babel>=2.9,<3.0",
"python-magic>=0.4.3,<0.5",
"beartype>=0.19,<0.24",
]
dynamic = ["authors", "classifiers", "keywords", "license", "version", "urls"]

[tool.hatch.metadata.hooks.openzim-metadata]
kind = "scraper"
additional-classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
]

[tool.hatch.build.hooks.openzim-build]

[project.optional-dependencies]
zim = [
"libzim>=3.8.0,<4.0",
"beautifulsoup4>=4.9.3,<5.0",
"lxml>=4.6.3,<7.0",
"optimize-images>=1.3.6,<3.0",
"xxhash>=2.0,<5.0",
# regex has no upper-bound due to "date-based" release numbers, no semver, so their
# promise is that they will never (or always) break the API, and the API is very
# limited and we use only a very small subset of it.
"regex>=2020.7.14",
# pymupdf-lite is the official build of pymupdf without its layout engine, and
# without the heavy dependencies (onnxruntime, numpy, ...) it brings since 2.0
"pymupdf-lite>=2.0,<3.0",
"types-xxhash>=2.0,<4.0",
]
image = [
"pillow>=7.0.0,<14.0",
"colorthief==0.2.1",
# piexif is pinned because of its irregular release management
"piexif==1.1.3",
"optimize-images>=1.3.6,<3.0",
"python-resize-image>=1.1.19,<1.2",
"CairoSVG>=2.2.0,<3.0",
"beartype>=0.19,<0.24",
]
video = []
download = [
"requests>=2.25.1,<3.0",
"urllib3>=1.26.5,<2.9.0",
# youtube-dl should be updated as frequently as possible
"yt-dlp",
"pillow>=7.0.0,<14.0",
"urllib3>=1.26.5,<2.9.0",
"piexif==1.1.3", # this dep is a nightmare in terms of release management, better pinned just like in optimize-images anyway
]
filesystem = [
"python-magic>=0.4.3,<0.5",
]
html = [
"beautifulsoup4>=4.9.3,<5.0",
"lxml>=4.6.3,<7.0",
]
rewriting = [
"beautifulsoup4>=4.9.3,<5.0",
"lxml>=4.6.3,<7.0",
"idna>=2.5,<4.0",
"xxhash>=2.0,<5.0",
"tinycss2>=1.2,<2.0",
# Parsing JavaScript well enough to know which names a script declares at its
# top level (see rewriting/js_ast.py). tree-sitter rather than a pure-Python
# parser because the input is whatever the live web served: esprima is ES2017
# and refuses optional chaining, class fields and `for await`, all ordinary in
# shipped code, and a parse failure here silently restores the bug this fixes.
"tree-sitter>=0.23,<1.0",
"tree-sitter-javascript>=0.23,<1.0",
"types-xxhash>=2.0,<4.0",
]
dynamic = ["authors", "classifiers", "keywords", "license", "version", "urls"]

[tool.hatch.metadata.hooks.openzim-metadata]
kind = "scraper"
additional-classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
i18n = [
"Babel>=2.9,<3.0",
"iso639-lang>=2.4.0,<3.0",
]
all = [
"zimscraperlib[zim,image,video,download,filesystem,html,rewriting,i18n]",
]

[tool.hatch.build.hooks.openzim-build]

[project.optional-dependencies]
scripts = [
"invoke==3.0.3",
# jinja2 is required to generate JS and Python rules at build time
Expand Down Expand Up @@ -90,10 +112,7 @@ docs = [
dev = [
"ipython==9.17.1",
"pre-commit==4.7.0",
"zimscraperlib[scripts]",
"zimscraperlib[qa]",
"zimscraperlib[test]",
"zimscraperlib[docs]",
"zimscraperlib[all,scripts,qa,test,docs]",
]

[project.scripts]
Expand Down Expand Up @@ -126,7 +145,7 @@ artifacts = [
features = ["dev"]

[tool.hatch.envs.test]
features = ["scripts", "test"]
features = ["scripts", "test", "all"]

[tool.hatch.envs.test.scripts]
run = "inv test --args '{args}'"
Expand All @@ -138,7 +157,7 @@ html = "inv coverage --html --args '{args}'"
[tool.hatch.envs.qa]
template = "qa"
skip-install = false
features = ["scripts", "qa", "test"]
features = ["scripts", "qa", "test", "all"]

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

[tool.hatch.envs.docs]
features = ["scripts", "docs"]
features = ["scripts", "docs", "all"]

[tool.hatch.envs.docs.scripts]
serve = "inv docs-serve --args '{args}'"
Expand Down
10 changes: 10 additions & 0 deletions src/zimscraperlib/_extras.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from typing import NoReturn


def missing_extra(extra: str, error: ModuleNotFoundError) -> NoReturn:
name = error.name or "an optional dependency"
raise ImportError(
f"Missing dependency '{name}' required by the '{extra}' extra. "
f"Install it with `python -m pip install 'zimscraperlib[{extra}]'` "
f"or `python -m pip install 'zimscraperlib[all]'`."
) from error
Loading