-
Notifications
You must be signed in to change notification settings - Fork 29
/
cli.py
98 lines (80 loc) · 2.96 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import argparse
import subprocess
from pathlib import Path
import requests
TRANSLATE_URL = "http://localhost:8765/translate_pdf/"
CLEAR_TEMP_URL = "http://localhost:8765/clear_temp_dir/"
def translate_request(input_pdf_path: Path, output_dir: Path) -> None:
"""Sends a POST request to the translator server to translate a PDF.
Parameters
----------
input_pdf_path : Path
Path to the PDF to be translated.
output_dir : Path
Path to the directory where the translated PDF will be saved.
"""
print(f"Translating {input_pdf_path}...")
with open(input_pdf_path, "rb") as input_pdf:
response = requests.post(TRANSLATE_URL, files={"input_pdf": input_pdf})
if response.status_code == 200:
with open(output_dir / input_pdf_path.name, "wb") as output_pdf:
output_pdf.write(response.content)
print(f"Converted PDF saved to {output_dir / input_pdf_path.name}")
requests.get(CLEAR_TEMP_URL)
else:
print(f"An error occurred: {response.status_code}")
def main(args: argparse.Namespace) -> None:
"""Translates a PDF or all PDFs in a directory.
Parameters
----------
args : argparse.Namespace
Arguments passed to the script.
Raises
------
ValueError
If the input path is not a valid path to file or directory.
Notes
-----
args must have the following attributes:
input_pdf_path_or_dir : Path
Path to the PDF or directory of PDFs to be translated.
output_dir : Path
Path to the directory where the translated PDFs
will be saved.
"""
args.output_dir.mkdir(parents=True, exist_ok=True)
if args.input_pdf_path_or_dir.is_file():
if args.input_pdf_path_or_dir.suffix != ".pdf":
raise ValueError(
f"Input file must be a PDF or directory: {args.input_pdf_path_or_dir}"
)
translate_request(args.input_pdf_path_or_dir, args.output_dir)
elif args.input_pdf_path_or_dir.is_dir():
input_pdf_paths = args.input_pdf_path_or_dir.glob("*.pdf")
if not input_pdf_paths:
raise ValueError(f"Input directory is empty: {args.input_pdf_path_or_dir}")
for input_pdf_path in input_pdf_paths:
translate_request(input_pdf_path, args.output_dir)
else:
raise ValueError(
f"Input path must be a file or directory: {args.input_pdf_path_or_dir}"
)
print("Done.")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
"--input_pdf_path_or_dir",
type=Path,
required=True,
help="Path to the PDF or directory of PDFs to be translated.",
)
parser.add_argument(
"-o",
"--output_dir",
type=Path,
default="./outputs",
help="Path to the directory where the translated PDFs will be saved. (default: ./outputs)",
)
args = parser.parse_args()
main(args)