forked from awwaawwa/PDFMathTranslate
-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathmain.py
More file actions
111 lines (83 loc) · 3.17 KB
/
main.py
File metadata and controls
111 lines (83 loc) · 3.17 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
"""A command line tool for extracting text and images from PDF and
output it to plain text, html, xml or tags.
"""
from __future__ import annotations
import asyncio
import logging
import os
import sys
from pathlib import Path
import babeldoc.assets.assets
from pdf2zh_next.config import ConfigManager
from pdf2zh_next.high_level import do_translate_file_async
__version__ = "2.8.2"
logger = logging.getLogger(__name__)
def find_all_files_in_directory(directory_path):
"""
Recursively search all PDF files in the given directory and return their paths as a list.
:param directory_path: str, the path to the directory to search
:return: list of PDF file paths
"""
directory_path = Path(directory_path)
# Check if the provided path is a directory
if not directory_path.is_dir():
raise ValueError(f"The provided path '{directory_path}' is not a directory.")
file_paths = []
# Walk through the directory recursively
for root, _, files in os.walk(directory_path):
for file in files:
# Check if the file is a PDF
if file.lower().endswith(".pdf"):
# Append the full file path to the list
file_paths.append(Path(root) / file)
return file_paths
async def main() -> int:
from rich.logging import RichHandler
logging.basicConfig(level=logging.INFO, handlers=[RichHandler()])
settings = ConfigManager().initialize_config()
if settings.basic.debug:
logging.getLogger().setLevel(logging.DEBUG)
# disable httpx, openai, httpcore, http11 logs
logging.getLogger("httpx").setLevel("CRITICAL")
logging.getLogger("httpx").propagate = False
logging.getLogger("openai").setLevel("CRITICAL")
logging.getLogger("openai").propagate = False
logging.getLogger("httpcore").setLevel("CRITICAL")
logging.getLogger("httpcore").propagate = False
logging.getLogger("http11").setLevel("CRITICAL")
logging.getLogger("http11").propagate = False
for v in logging.Logger.manager.loggerDict.values():
if getattr(v, "name", None) is None:
continue
if (
v.name.startswith("pdfminer")
or v.name.startswith("peewee")
or v.name.startswith("httpx")
or "http11" in v.name
or "openai" in v.name
or "pdfminer" in v.name
):
v.disabled = True
v.propagate = False
logger.debug(f"settings: {settings}")
if settings.basic.version:
print(f"pdf2zh-next version: {__version__}")
return 0
logger.info("Warmup babeldoc assets...")
babeldoc.assets.assets.warmup()
if settings.basic.gui:
from pdf2zh_next.gui import setup_gui
setup_gui(
auth_file=settings.gui_settings.auth_file,
welcome_page=settings.gui_settings.welcome_page,
server_port=settings.gui_settings.server_port,
)
return 0
assert len(settings.basic.input_files) >= 1, "At least one input file is required"
await do_translate_file_async(settings, ignore_error=True)
return 0
def cli():
sys.exit(asyncio.run(main()))
if __name__ == "__main__":
cli()