From 5441f326dd89cb6f6511c5a3ccae00211d916747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Avil=C3=A9s?= Date: Thu, 27 Jun 2024 15:47:28 +0200 Subject: [PATCH 1/2] Update `cache` argument in docs --- docs/first_steps.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/first_steps.rst b/docs/first_steps.rst index ce5daf547..c089ecab2 100644 --- a/docs/first_steps.rst +++ b/docs/first_steps.rst @@ -516,7 +516,7 @@ Image Cache and Optimization ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ WeasyPrint provides many options to deal with images: ``optimize_images``, -``jpeg_quality``, ``dpi`` and ``image_cache``. +``jpeg_quality``, ``dpi`` and ``cache``. ``optimize_images`` can enable size optimization for images. When enabled, the generated PDF will include smaller images with no quality penalty, but the @@ -540,7 +540,7 @@ generated PDF. HTML('https://weasyprint.org/').write_pdf( 'weasyprint.pdf', optimize_images=True, jpeg_quality=60, dpi=150) -``image_cache`` gives the possibility to use a cache for images, avoiding to +``cache`` gives the possibility to use a cache for images, avoiding to download, parse and optimize them each time they are used. By default, the cache is used document by document, but you can share it @@ -552,12 +552,12 @@ time when you render a lot of documents that use the same images. cache = {} for i in range(10): HTML(f'https://weasyprint.org/').write_pdf( - f'example-{i}.pdf', image_cache=cache) + f'example-{i}.pdf', cache=cache) It’s also possible to cache images on disk instead of keeping them in memory. The ``--cache-folder`` CLI option can be used to define the folder used to store temporary images. You can also provide this folder path as a string for -``image_cache``. +``cache``. Logging From 1f41486af49b2d65ec058d2bec1447632278f1fc Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Wed, 3 Jul 2024 10:18:46 +0200 Subject: [PATCH 2/2] Log warning when unknown rendering option is given --- tests/test_api.py | 9 +++++++++ weasyprint/__init__.py | 2 ++ weasyprint/__main__.py | 3 ++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_api.py b/tests/test_api.py index 86aacf464..009b3b71d 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -305,6 +305,15 @@ def test_python_render(assert_pixels_equal, tmp_path): ).write_png() == rotated_png_bytes +@assert_no_logs +def test_unknown_options(): + with capture_logs() as logs: + pdf_bytes = FakeHTML(string='test').write_pdf(zoom=2, unknown=True) + assert len(logs) == 1 + assert 'unknown' in logs[0] + assert pdf_bytes + + @assert_no_logs def test_command_line_render(tmp_path): css = b''' diff --git a/weasyprint/__init__.py b/weasyprint/__init__.py index 64e125ef9..2908524e0 100644 --- a/weasyprint/__init__.py +++ b/weasyprint/__init__.py @@ -210,6 +210,8 @@ def render(self, font_config=None, counter_style=None, **options): :returns: A :class:`document.Document` object. """ + for unknown in set(options) - set(DEFAULT_OPTIONS): + LOGGER.warning('Unknown rendering option: %s.', unknown) new_options = DEFAULT_OPTIONS.copy() new_options.update(options) options = new_options diff --git a/weasyprint/__main__.py b/weasyprint/__main__.py index 970df1afb..fcb3ff05c 100644 --- a/weasyprint/__main__.py +++ b/weasyprint/__main__.py @@ -165,7 +165,8 @@ def main(argv=None, stdout=None, stdin=None, HTML=HTML): # noqa: N803 if args.timeout is not None: url_fetcher = partial(default_url_fetcher, timeout=args.timeout) - options = vars(args) + options = { + key: value for key, value in vars(args).items() if key in DEFAULT_OPTIONS} # Default to logging to stderr. if args.debug: