2
2
from concurrent .futures import ThreadPoolExecutor
3
3
4
4
import attr
5
+ import enlighten
5
6
import InquirerPy
6
7
from fpdf import FPDF
7
- from src .constants import THREADS
8
- from src .driver import OrderStatusBarBaseClass
8
+ from src .constants import THREADS , States
9
+ from src .order import CardOrder
10
+ from src .utils import TEXT_BOLD , TEXT_END
9
11
10
12
11
13
@attr .s
12
- class PdfExporter (OrderStatusBarBaseClass ):
14
+ class PdfExporter :
15
+ order : CardOrder = attr .ib (default = attr .Factory (CardOrder .from_xml_in_folder ))
16
+ state : str = attr .ib (init = False , default = States .initialising )
13
17
pdf : FPDF = attr .ib (default = None )
14
18
card_width_in_inches : float = attr .ib (default = 2.73 )
15
19
card_height_in_inches : float = attr .ib (default = 3.71 )
@@ -19,6 +23,29 @@ class PdfExporter(OrderStatusBarBaseClass):
19
23
save_path : str = attr .ib (default = "" )
20
24
separate_faces : bool = attr .ib (default = False )
21
25
current_face : str = attr .ib (default = "all" )
26
+ manager : enlighten .Manager = attr .ib (init = False , default = attr .Factory (enlighten .get_manager ))
27
+ status_bar : enlighten .StatusBar = attr .ib (init = False , default = False )
28
+ download_bar : enlighten .Counter = attr .ib (init = False , default = None )
29
+ processed_bar : enlighten .Counter = attr .ib (init = False , default = None )
30
+
31
+ def configure_bars (self ) -> None :
32
+ num_images = len (self .order .fronts .cards ) + len (self .order .backs .cards )
33
+ status_format = "State: {state}"
34
+ self .status_bar = self .manager .status_bar (
35
+ status_format = status_format ,
36
+ state = f"{ TEXT_BOLD } { self .state } { TEXT_END } " ,
37
+ position = 1 ,
38
+ )
39
+ self .download_bar = self .manager .counter (total = num_images , desc = "Images Downloaded" , position = 2 )
40
+ self .processed_bar = self .manager .counter (total = num_images , desc = "Images Processed" , position = 3 )
41
+
42
+ self .download_bar .refresh ()
43
+ self .processed_bar .refresh ()
44
+
45
+ def set_state (self , state : str ) -> None :
46
+ self .state = state
47
+ self .status_bar .update (state = f"{ TEXT_BOLD } { self .state } { TEXT_END } " )
48
+ self .status_bar .refresh ()
22
49
23
50
def __attrs_post_init__ (self ) -> None :
24
51
self .ask_questions ()
@@ -45,22 +72,24 @@ def ask_questions(self) -> None:
45
72
+ "the longer the processing will take and the larger the file size will be." ,
46
73
"default" : 60 ,
47
74
"when" : lambda result : result ["split_faces" ] is False ,
48
- "transformer" : lambda result : 1 if int (result ) < 1 else int ( result ) ,
75
+ "transformer" : lambda result : 1 if ( int_result := int (result )) < 1 else int_result ,
49
76
},
50
77
]
51
78
answers = InquirerPy .prompt (questions )
52
79
if answers ["split_faces" ]:
53
80
self .separate_faces = True
54
81
self .number_of_cards_per_file = 1
55
82
else :
56
- self .number_of_cards_per_file = 1 if int (answers ["cards_per_file" ]) < 1 else int (answers ["cards_per_file" ])
83
+ self .number_of_cards_per_file = (
84
+ 1 if (int_cards_per_file := int (answers ["cards_per_file" ])) < 1 else int_cards_per_file
85
+ )
57
86
58
87
def generate_file_path (self ) -> None :
59
88
basename = os .path .basename (str (self .order .name ))
60
89
if not basename :
61
90
basename = "cards.xml"
62
91
file_name = os .path .splitext (basename )[0 ]
63
- self .save_path = "export/%s/" % file_name
92
+ self .save_path = f "export/{ file_name } /"
64
93
os .makedirs (self .save_path , exist_ok = True )
65
94
if self .separate_faces :
66
95
for face in ["backs" , "fronts" ]:
@@ -77,8 +106,8 @@ def add_image(self, image_path: str) -> None:
77
106
def save_file (self ) -> None :
78
107
extra = ""
79
108
if self .separate_faces :
80
- extra = "%s/" % self .current_face
81
- self .pdf .output (self .save_path + extra + str ( self .file_num ) + " .pdf" )
109
+ extra = f" { self .current_face } /"
110
+ self .pdf .output (f" { self .save_path } { extra } { self .file_num } .pdf" )
82
111
83
112
def download_and_collect_images (self ) -> None :
84
113
with ThreadPoolExecutor (max_workers = THREADS ) as pool :
@@ -107,37 +136,38 @@ def execute(self) -> None:
107
136
else :
108
137
self .export ()
109
138
110
- print ("Finished exporting files! They should be accessible at %s" % self .save_path )
139
+ print (f "Finished exporting files! They should be accessible at { self .save_path } ." )
111
140
112
141
def export (self ) -> None :
113
142
for slot in sorted (self .paths_by_slot .keys ()):
114
143
(back_path , front_path ) = self .paths_by_slot [slot ]
115
- print ( "Working on slot %s" % str ( slot ) )
144
+ self . set_state ( f "Working on slot { slot } " )
116
145
if slot == 0 :
117
146
self .generate_pdf ()
118
147
elif slot % self .number_of_cards_per_file == 0 :
119
- print ( "Saving PDF #%s" % str ( self . file_num ) )
148
+ self . set_state ( f "Saving PDF #{ slot } " )
120
149
self .save_file ()
121
150
self .file_num = self .file_num + 1
122
151
self .generate_pdf ()
123
- print ( "Adding images for slot %s" % str ( slot ) )
152
+ self . set_state ( f "Adding images for slot { slot } " )
124
153
self .add_image (back_path )
125
154
self .add_image (front_path )
155
+ self .processed_bar .update ()
126
156
127
- print ( "Saving PDF #%s" % str ( self .file_num ) )
157
+ self . set_state ( f "Saving PDF #{ self .file_num } " )
128
158
self .save_file ()
129
159
130
160
def export_separate_faces (self ) -> None :
131
161
all_faces = ["backs" , "fronts" ]
132
162
for slot in sorted (self .paths_by_slot .keys ()):
133
163
image_paths_tuple = self .paths_by_slot [slot ]
134
- print ( "Working on slot " + str ( slot ) )
164
+ self . set_state ( f "Working on slot { slot } " )
135
165
for face in all_faces :
136
166
face_index = all_faces .index (face )
137
167
self .current_face = face
138
168
self .generate_pdf ()
139
169
self .add_image (image_paths_tuple [face_index ])
140
- print ( "Saving %s PDF for slot %s" % ( face , str ( slot )) )
170
+ self . set_state ( f "Saving { face } PDF for slot { slot } " )
141
171
self .save_file ()
142
172
if face_index == 1 :
143
173
self .file_num = self .file_num + 1
0 commit comments