-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
100 lines (84 loc) · 2.91 KB
/
model.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
99
100
from dataclasses import dataclass, field
from pathlib import Path
@dataclass
class Model:
source_folder: Path = None
pdfs: list[int] = field(default_factory=list)
add: bool = False
existing_pdf: Path = None
custom_output_folder: bool = True
custom_output_folder_path: Path = None
custom_output_filename: bool = True
custom_output_filename_str: str = ""
flatten: bool = False
def set_source_folder(self, folder):
if folder and Path(folder).is_dir():
self.source_folder = Path(folder)
else:
self.source_folder = False
return self.source_folder
def find_pdfs(self):
if self.source_folder:
self.pdfs = sorted(self.source_folder.rglob("*.pdf"))
else:
self.pdfs = []
return self.pdfs
def reset_pdfs(self):
self.pdfs = []
def set_existing_pdf(self, file: str):
if file and Path(file).is_file() and file.endswith(".pdf"):
self.existing_pdf = Path(file)
else:
self.existing_pdf = None
return self.existing_pdf
def set_custom_output_folder(self, folder):
if folder and Path(folder).is_dir():
self.custom_output_folder_path = Path(folder)
else:
self.custom_output_folder_path = ""
return self.custom_output_folder_path
def set_custom_output_filename(self, filename):
if filename:
self.custom_output_filename_str = filename
else:
self.custom_output_filename_str = ""
@property
def output_folder(self):
if self.add:
if self.custom_output_folder:
if self.custom_output_folder_path:
return self.custom_output_folder_path
if self.existing_pdf:
return self.existing_pdf.parent
else:
if self.custom_output_folder:
if self.custom_output_folder_path:
return self.custom_output_folder_path
else:
desktop = Path.home() / "Desktop"
if desktop.is_dir():
return desktop
else:
return Path.home()
else:
desktop = Path.home() / "Desktop"
if desktop.is_dir():
return desktop
else:
return Path.home()
@property
def output_filename(self):
if self.add:
if self.existing_pdf:
return f"{self.existing_pdf.stem} - PDFs added"
if self.add:
return ""
else:
if self.custom_output_filename:
if self.custom_output_filename_str:
return self.custom_output_filename_str
else:
return "PDFs merged"
else:
return "PDFs merged"
model = Model()