diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 97b473c..22c0a48 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -4,6 +4,7 @@ language: python entry: optimize-avif types: [avif] + additional_dependencies: [".[avif]"] - id: optimize-jpg name: optimize jpgs @@ -25,6 +26,7 @@ language: python entry: optimize-svg types: [svg] + additional_dependencies: [".[svg]"] - id: optimize-webp name: optimize webps diff --git a/Makefile b/Makefile index 9494e66..72ddec3 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ $(VENV)/$(MARKER): $(VENV) ## venv : Initialize virtual environment with dependencies. .PHONY: venv venv: $(VENV)/$(MARKER) - $(VENV)/pip install -q .[tests] + $(VENV)/pip install -q .[tests,svg,avif] .PHONY: all diff --git a/pre_commit_images/optimize_avif.py b/pre_commit_images/optimize_avif.py index b4efe77..151f871 100755 --- a/pre_commit_images/optimize_avif.py +++ b/pre_commit_images/optimize_avif.py @@ -7,11 +7,16 @@ from typing import IO from typing import Optional -import pillow_avif # noqa: F401 from PIL import Image from .optimizer import _optimize_images +try: + import pillow_avif # noqa: F401 +except ImportError: + warnings.warn('Missing `pillow_avif` dependency, install optional "[avif]" dependency group') + sys.exit(1) + def main(argv: Optional[Sequence[str]] = None) -> int: parser = argparse.ArgumentParser() diff --git a/pre_commit_images/optimize_svg.py b/pre_commit_images/optimize_svg.py index a8b5bce..befa828 100755 --- a/pre_commit_images/optimize_svg.py +++ b/pre_commit_images/optimize_svg.py @@ -1,15 +1,20 @@ #!/usr/bin/env python3 import argparse +import pathlib import sys +import warnings from collections.abc import Sequence -from pathlib import Path from typing import IO from typing import Optional -from scour import scour - from .optimizer import _optimize_images +try: + from scour import scour +except ImportError: + warnings.warn('Missing `scour` dependency, install optional "[svg]" dependency group') + sys.exit(1) + def main(argv: Optional[Sequence[str]] = None) -> int: parser = argparse.ArgumentParser() @@ -23,7 +28,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int: ) args = parser.parse_args(argv) - def optimize(source: Path, target: IO[bytes]) -> None: + def optimize(source: pathlib.Path, target: IO[bytes]) -> None: data = source.read_text(encoding="utf-8") options = { "enable_viewboxing": True, diff --git a/pyproject.toml b/pyproject.toml index 0c0e4c4..e897149 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,11 +22,15 @@ classifiers = [ requires-python = ">=3.9" dependencies = [ "pillow==10.0.1", - "pillow-avif-plugin==1.4.1", - "scour==0.38.2", ] [project.optional-dependencies] +avif = [ + "pillow-avif-plugin==1.4.1", +] +svg = [ + "scour==0.38.2", +] dev = [ "pre-commit", "ruff", diff --git a/tox.ini b/tox.ini index 28bb51f..6d52342 100644 --- a/tox.ini +++ b/tox.ini @@ -3,7 +3,7 @@ envlist = py39,py310,py311,py312,pre-commit isolated_build = True [testenv] -extras = tests +extras = tests,svg,avif commands = pytest {posargs:tests} @@ -13,6 +13,6 @@ deps = pre-commit commands = pre-commit run --all-files [testenv:static] -extras = tests +extras = tests,svg,avif commands = pytest --ignore tests