diff --git a/html2pdf4doc/html2pdf4doc.py b/html2pdf4doc/html2pdf4doc.py index 32ac15b..dfc7f48 100644 --- a/html2pdf4doc/html2pdf4doc.py +++ b/html2pdf4doc/html2pdf4doc.py @@ -11,7 +11,7 @@ from datetime import datetime from pathlib import Path from time import sleep, time -from typing import Dict, Iterator, List, Optional, Tuple +from typing import Any, Dict, Iterator, List, Optional, Tuple, Union import requests from pypdf import PdfReader @@ -63,6 +63,29 @@ def extract_page_count(logs: List[Dict[str, str]]) -> int: raise ValueError("No page count found in logs.") +class IntRange: + def __init__(self, imin: int, imax: int) -> None: + self.imin: int = imin + self.imax: int = imax + + def __call__(self, arg: Any) -> Union[int, argparse.ArgumentTypeError]: + value: Optional[int] + try: + value = int(arg) + except ValueError: + value = None + + if value is not None and self.imin <= value <= self.imax: + return value + + raise argparse.ArgumentTypeError( + f"Must be an integer in the range [{self.imin}, {self.imax}]." + ) + + def __str__(self) -> str: + return f"{self.imin}-{self.imax}" + + class ChromeDriverManager: def get_chrome_driver(self, path_to_cache_dir: str) -> str: chrome_version: Optional[str] = self.get_chrome_version() @@ -486,15 +509,15 @@ def main() -> None: ) command_parser_print.add_argument( "--page-load-timeout", - type=int, - default=2 * 60, # 10 minutes should be enough to print even the largest documents. - choices=range(0, 10 * 60), + type=IntRange(0, 10 * 60), + default=2 * 60, help=( "How long shall html2pdf4doc Python driver wait while the " "Chrome Driver is printing a given HTML page to PDF. " "This is mainly driven by the time it takes for Chrome to open an " - "HTML file, load it, and let HTML2PDF4Doc.js finish its job." + "HTML file, load it, and let HTML2PDF4Doc.js finish its job. " + "Allowed range: %(type)s seconds, default: %(default)s seconds." ), ) command_parser_print.add_argument( diff --git a/tests/integration/06_page_load_timeout_validation/index1.html b/tests/integration/06_page_load_timeout_validation/index1.html new file mode 100644 index 0000000..0330137 --- /dev/null +++ b/tests/integration/06_page_load_timeout_validation/index1.html @@ -0,0 +1 @@ +NOT RELEVANT FOR THIS TEST. \ No newline at end of file diff --git a/tests/integration/06_page_load_timeout_validation/test.itest b/tests/integration/06_page_load_timeout_validation/test.itest new file mode 100644 index 0000000..13d5458 --- /dev/null +++ b/tests/integration/06_page_load_timeout_validation/test.itest @@ -0,0 +1,3 @@ +RUN: %expect_exit 2 %html2pdf print --page-load-timeout 1000000 %S/index1.html %S/Output/index1.pdf 2>&1 | filecheck %s + +CHECK: html2pdf4doc.py print: error: argument --page-load-timeout: Must be an integer in the range [0, 600].