|
11 | 11 | from datetime import datetime |
12 | 12 | from pathlib import Path |
13 | 13 | from time import sleep, time |
14 | | -from typing import Dict, Iterator, List, Optional, Tuple |
| 14 | +from typing import Any, Dict, Iterator, List, Optional, Tuple, Union |
15 | 15 |
|
16 | 16 | import requests |
17 | 17 | from pypdf import PdfReader |
@@ -63,6 +63,29 @@ def extract_page_count(logs: List[Dict[str, str]]) -> int: |
63 | 63 | raise ValueError("No page count found in logs.") |
64 | 64 |
|
65 | 65 |
|
| 66 | +class IntRange: |
| 67 | + def __init__(self, imin: int, imax: int) -> None: |
| 68 | + self.imin: int = imin |
| 69 | + self.imax: int = imax |
| 70 | + |
| 71 | + def __call__(self, arg: Any) -> Union[int, argparse.ArgumentTypeError]: |
| 72 | + value: Optional[int] |
| 73 | + try: |
| 74 | + value = int(arg) |
| 75 | + except ValueError: |
| 76 | + value = None |
| 77 | + |
| 78 | + if value is not None and self.imin <= value <= self.imax: |
| 79 | + return value |
| 80 | + |
| 81 | + raise argparse.ArgumentTypeError( |
| 82 | + f"Must be an integer in the range [{self.imin}, {self.imax}]." |
| 83 | + ) |
| 84 | + |
| 85 | + def __str__(self) -> str: |
| 86 | + return f"{self.imin}-{self.imax}" |
| 87 | + |
| 88 | + |
66 | 89 | class ChromeDriverManager: |
67 | 90 | def get_chrome_driver(self, path_to_cache_dir: str) -> str: |
68 | 91 | chrome_version: Optional[str] = self.get_chrome_version() |
@@ -486,15 +509,15 @@ def main() -> None: |
486 | 509 | ) |
487 | 510 | command_parser_print.add_argument( |
488 | 511 | "--page-load-timeout", |
489 | | - type=int, |
490 | | - default=2 * 60, |
491 | 512 | # 10 minutes should be enough to print even the largest documents. |
492 | | - choices=range(0, 10 * 60), |
| 513 | + type=IntRange(0, 10 * 60), |
| 514 | + default=2 * 60, |
493 | 515 | help=( |
494 | 516 | "How long shall html2pdf4doc Python driver wait while the " |
495 | 517 | "Chrome Driver is printing a given HTML page to PDF. " |
496 | 518 | "This is mainly driven by the time it takes for Chrome to open an " |
497 | | - "HTML file, load it, and let HTML2PDF4Doc.js finish its job." |
| 519 | + "HTML file, load it, and let HTML2PDF4Doc.js finish its job. " |
| 520 | + "Allowed range: %(type)s seconds, default: %(default)s seconds." |
498 | 521 | ), |
499 | 522 | ) |
500 | 523 | command_parser_print.add_argument( |
|
0 commit comments