Skip to content

Commit 25f7fa7

Browse files
authored
fix: adjust print command port validation and help message (#51)
Previously, choices= was used to define an acceptable range and all choice values were printed with the help message.
1 parent 632fce7 commit 25f7fa7

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

html2pdf4doc/html2pdf4doc.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from datetime import datetime
1212
from pathlib import Path
1313
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
1515

1616
import requests
1717
from pypdf import PdfReader
@@ -63,6 +63,29 @@ def extract_page_count(logs: List[Dict[str, str]]) -> int:
6363
raise ValueError("No page count found in logs.")
6464

6565

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+
6689
class ChromeDriverManager:
6790
def get_chrome_driver(self, path_to_cache_dir: str) -> str:
6891
chrome_version: Optional[str] = self.get_chrome_version()
@@ -486,15 +509,15 @@ def main() -> None:
486509
)
487510
command_parser_print.add_argument(
488511
"--page-load-timeout",
489-
type=int,
490-
default=2 * 60,
491512
# 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,
493515
help=(
494516
"How long shall html2pdf4doc Python driver wait while the "
495517
"Chrome Driver is printing a given HTML page to PDF. "
496518
"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."
498521
),
499522
)
500523
command_parser_print.add_argument(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NOT RELEVANT FOR THIS TEST.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RUN: %expect_exit 2 %html2pdf print --page-load-timeout 1000000 %S/index1.html %S/Output/index1.pdf 2>&1 | filecheck %s
2+
3+
CHECK: html2pdf4doc.py print: error: argument --page-load-timeout: Must be an integer in the range [0, 600].

0 commit comments

Comments
 (0)