Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
162 lines (126 loc) · 6.38 KB
/
Copy pathmain.py
File metadata and controls
162 lines (126 loc) · 6.38 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import importlib
import logging
import os
import sys
# ── Logging setup ──────────────────────────────────────────────────────────────
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger("toolkit")
# ── Config ─────────────────────────────────────────────────────────────────────
import config # noqa: F401
# ── Module registry ───────────────────────────────────────────────────────────
# To add a new feature:
# 1. Create modules/your_module.py with a run() function
# 2. Import it below
# 3. Add an entry to MENU and a launcher to LAUNCHERS
def _try_import(module_path: str):
"""Import a module, returning *None* if its dependencies are missing."""
try:
return importlib.import_module(module_path)
except (SystemExit, Exception):
# SystemExit: module called sys.exit(1) on missing deps
# Exception: any other import-time failure (bug, bad config, syntax)
# Either way, treat the module as unavailable rather than crashing.
return None
pdf_scanner = _try_import("modules.document_manager")
brand_reader = _try_import("modules.brand_reader")
batch_print = _try_import("modules.batch_printer")
config_editor = _try_import("modules.config_editor")
# ── Menu definitions ──────────────────────────────────────────────────────────
# Each entry is (label, module_or_None, launcher_func_or_None).
MENU_ENTRIES = [
(
"Scan drive and copy matching PDFs (pdf_scanner)",
pdf_scanner,
"launch_pdf_scanner",
),
(
"Extract brand names to Excel (brand_reader)",
brand_reader,
"launch_brand_reader",
),
(
"Batch print PDFs to printer (batch_print)",
batch_print,
"launch_batch_print",
),
(
"Configure Toolkit Settings (config_editor)",
config_editor,
"launch_config_editor",
),
]
# ── Display ───────────────────────────────────────────────────────────────────
def print_menu():
print("\n" + "=" * 55)
print(" PDF Processing Toolkit")
print("=" * 55)
for i, (label, module, _) in enumerate(MENU_ENTRIES, 1):
status = "" if module else " ⚠ missing deps"
print(f" {i}. {label}{status}")
print(" 0. Exit")
print("=" * 55)
# ── Prompt helpers ────────────────────────────────────────────────────────────
def prompt_path(label: str, must_exist: bool = True) -> str:
while True:
value = input(f"{label}: ").strip().strip('"')
if must_exist and not os.path.exists(value):
print(f" Error: '{value}' does not exist. Try again.")
continue
return value
# ── Launchers ─────────────────────────────────────────────────────────────────
# All user prompts live here. Modules only receive ready-to-use values.
def launch_pdf_scanner():
if pdf_scanner is None:
_missing_deps_notice("documentManager", "pypdf pdfplumber")
return
print("\n── PDF Scanner ──────────────────────────────────────")
root = prompt_path("Enter drive or folder to scan (e.g. C:\\ or D:\\docs)", must_exist=True)
dest = prompt_path("Enter destination folder for matched PDFs", must_exist=False)
pdf_scanner.run(root, dest)
def launch_brand_reader():
if brand_reader is None:
_missing_deps_notice("brandReader", "pypdf pdfplumber openpyxl")
return
print("\n── Brand Reader ─────────────────────────────────────")
folder = prompt_path("Enter folder containing PDFs", must_exist=True)
brand_reader.run(folder)
def launch_batch_print():
if batch_print is None:
_missing_deps_notice("batchPrinter", "pywin32")
return
print("\n── Batch Print ──────────────────────────────────────")
folder = prompt_path("Enter folder containing PDFs to print", must_exist=True)
batch_print.run(folder)
def launch_config_editor():
if config_editor is None:
_missing_deps_notice("configEditor", "")
return
config_editor.run()
def _missing_deps_notice(module_name: str, packages: str) -> None:
print(f"\n ⚠ Module '{module_name}' cannot run — missing dependencies.")
print(f" Install with: pip install {packages}\n")
# ── Main loop ─────────────────────────────────────────────────────────────────
def main():
logger.info("PDF Processing Toolkit started")
while True:
print_menu()
choice = input("Select an option: ").strip()
if choice == "0":
print("Goodbye.")
sys.exit(0)
if not choice.isdigit() or not (1 <= int(choice) <= len(MENU_ENTRIES)):
print(f" Invalid option. Enter a number between 0 and {len(MENU_ENTRIES)}.")
continue
_, module, launcher_name = MENU_ENTRIES[int(choice) - 1]
if module is None:
# Already shown in menu; give a short reminder
print(" This module is unavailable. Check the warnings above.")
else:
globals()[launcher_name]()
input("\nPress Enter to return to the menu...")
if __name__ == "__main__":
main()