From 301647704b21bea5463c660515f779de01ce1676 Mon Sep 17 00:00:00 2001 From: Raphael Boidol Date: Sun, 15 Oct 2023 16:16:37 +0200 Subject: [PATCH] feat: reduce install size by setting `avif` and `svg` dependencies optional --- .pre-commit-hooks.yaml | 2 ++ Makefile | 2 +- pre_commit_images/optimize_avif.py | 7 ++++++- pre_commit_images/optimize_svg.py | 13 +++++++++---- pyproject.toml | 8 ++++++-- tox.ini | 4 ++-- 6 files changed, 26 insertions(+), 10 deletions(-) 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 8e287b3..682ec10 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 10c9ff0..731210c 100755 --- a/pre_commit_images/optimize_avif.py +++ b/pre_commit_images/optimize_avif.py @@ -6,11 +6,16 @@ from pathlib import Path from typing import IO -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: Sequence[str] | None = None) -> int: parser = argparse.ArgumentParser() diff --git a/pre_commit_images/optimize_svg.py b/pre_commit_images/optimize_svg.py index d958ebb..61b139f 100755 --- a/pre_commit_images/optimize_svg.py +++ b/pre_commit_images/optimize_svg.py @@ -1,14 +1,19 @@ #!/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 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: Sequence[str] | None = None) -> int: parser = argparse.ArgumentParser() @@ -22,7 +27,7 @@ def main(argv: Sequence[str] | None = 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 22c36c6..6057c3f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,11 +22,15 @@ classifiers = [ requires-python = ">=3.10" dependencies = [ "pillow==10.1.0", - "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 4175c21..6e9db64 100644 --- a/tox.ini +++ b/tox.ini @@ -3,7 +3,7 @@ envlist = 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