Skip to content

Commit

Permalink
Merge pull request #1971 from azhar316/feat-timeout
Browse files Browse the repository at this point in the history
Add timeout cli option
  • Loading branch information
grewn0uille committed Sep 25, 2023
2 parents 30a7ed7 + 29625c6 commit cee2a17
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ def test_command_line_render(tmpdir):
'-a style.css -a pattern.png --uncompressed-pdf combined.html -')
assert stdout.count(b'attachment') == 2

_run('combined.html out23.pdf --timeout 30')
assert tmpdir.join('out23.pdf').read_binary() == pdf_bytes

os.mkdir('subdirectory')
py.path.local('subdirectory').chdir()
with capture_logs() as logs:
Expand Down
12 changes: 11 additions & 1 deletion weasyprint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import logging
import platform
import sys
from functools import partial
from warnings import warn

import pydyf

from . import DEFAULT_OPTIONS, HTML, LOGGER, __version__
from .pdf import VARIANTS
from .text.ffi import pango
from .urls import default_url_fetcher


class PrintInfo(argparse.Action):
Expand Down Expand Up @@ -135,6 +137,10 @@ def docstring(self):
'-O', '--optimize-size', action='append',
help='deprecated, use other options instead',
choices=('images', 'fonts', 'hinting', 'pdf', 'all', 'none'))
PARSER.add_argument(
'-t', '--timeout', type=int,
help='Set timeout in seconds for HTTP requests'
)
PARSER.set_defaults(**DEFAULT_OPTIONS)


Expand Down Expand Up @@ -179,6 +185,10 @@ def main(argv=None, stdout=None, stdin=None, HTML=HTML):
optimize_size.add(arg)
del args.optimize_size

url_fetcher = default_url_fetcher
if args.timeout is not None:
url_fetcher = partial(default_url_fetcher, timeout=args.timeout)

options = vars(args)

# TODO: to be removed when --optimize-size is removed
Expand All @@ -203,7 +213,7 @@ def main(argv=None, stdout=None, stdin=None, HTML=HTML):

html = HTML(
source, base_url=args.base_url, encoding=args.encoding,
media_type=args.media_type)
media_type=args.media_type, url_fetcher=url_fetcher)
html.write_pdf(output, **options)


Expand Down

0 comments on commit cee2a17

Please sign in to comment.