-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
113 lines (91 loc) · 2.97 KB
/
main.py
File metadata and controls
113 lines (91 loc) · 2.97 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
"""
source: https://dailypythonprojects.substack.com/p/build-a-pdf-toolkit-with-python-day
The project:
PDF merger to merge PDFs
PDF splitter to split pages
rotate pages
compress PDFs to reduce file size
web app
"""
import argparse
import logging
import sys
from pdf_tools.cli.parser import parse_arguments
from pdf_tools.operations.merge import (
MergeOperation,
MergeConfig,
MergeResult,
OperationError,
)
from pdf_tools.operations.rotate import (
RotateConfig,
RotateOperation,
RotateResult,
)
def configure_logging() -> None:
"""Configure basic logging for the CLI application."""
logging.basicConfig(
level=logging.INFO,
format="%(levelname)s - %(name)s - %(message)s",
#datefmt="%Y-%m-%d %H:%M:%S",
)
def handle_merge(args: argparse.Namespace) -> int:
"""
Handle the merge operation based on parsed CLI arguments.
- building `MergeConfig` is the translation layer between CLI and operation.
- the operation stays clean and independent of how the data was obtained.
"""
config = MergeConfig(
input_folder=args.path,
output_filename=args.output,
)
operation = MergeOperation(config)
try:
result: MergeResult = operation.run()
except OperationError as e:
print(f"Error: {e}", file=sys.stderr)
return 1
# Success path - print a CLI-friendly summary of the operation
print(f"Found {len(result.input_files)} PDF file(s):")
for pdf in result.input_files:
print(f" - {pdf}")
print(
f"Success! Merged {len(result.input_files)} files "
f"with ({result.total_pages} pages) into '{result.output_file}'"
)
return 0
#def handle_split(args: argparse.Namespace) -> int:
"""Handle the split operation based on parsed CLI arguments."""
def handle_rotate(args: argparse.Namespace) -> int:
"""Handle the rotate operation based on parsed CLI arguments."""
config = RotateConfig(
input_file=args.input,
output_filename=args.output,
pages_to_rotate=args.pages,
angle=90,
)
operation = RotateOperation(config)
try:
result = operation.run()
except OperationError as e:
print(f"Error: {e}", file=sys.stderr)
return 1
print(
f"Success! Rotated {len(result.pages_rotated)} page(s) "
f"in: '{result.input_file}' -> out: '{result.output_file}'"
)
return 0
#def handle_compress(args: argparse.Namespace) -> int:
def main(argv: list[str] | None = None) -> int:
configure_logging()
print("===== PDF Tools =====")
args = parse_arguments(argv) #see parser.py
if args.command == "merge": #.commands comes from parser.py subparsers container
return handle_merge(args)
if args.command == "rotate":
return handle_rotate(args)
# fallback
print(f"Unknown command: {getattr(args, 'command', None)}", file=sys.stderr)
return 1
if __name__ == "__main__":
raise SystemExit(main())