-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaca.py
1215 lines (992 loc) · 48.1 KB
/
aca.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
another checksum application (aca)
author: Thomas Luke Ruane
github repo: https://github.com/realgoodegg/another-checksum-application
version: 1.1.1
last modified: 2024-02-19
"""
import wx
import logging
import time
from concurrent import futures
import os
from pubsub import pub
import filehashingservice
import subprocess
import pyperclip
from datetime import timedelta
### set up logging
def initialise_logging():
try:
if not os.path.isdir("~/Documents/aca"):
os.mkdir(os.path.expanduser("~/Documents/aca"))
except OSError:
pass
log_file_location = os.path.expanduser("~/Documents/aca/logs")
if not os.path.isdir(log_file_location):
os.mkdir(log_file_location)
else:
pass
return log_file_location
log_file_location = initialise_logging()
log_timestamp = time.strftime("%Y%m%d%H%M%S_aca.log")
log_write = os.path.join(log_file_location, log_timestamp)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s:%(module)s:%(levelname)s:%(message)s")
file_handler = logging.FileHandler(log_write)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
### Initiate threading to file processing off the main thread
thread_pool_executor = futures.ThreadPoolExecutor(max_workers=1)
### UI tab panels
class TabPanel(wx.Notebook):
def __init__(self, parent):
wx.Notebook.__init__(self, parent)
aca_panel = AcaInterface(self)
report_panel = ReportInterface(self)
self.AddPage(aca_panel, "aca")
self.AddPage(report_panel, "Report")
### Main UI frame to hold the tab panels
class MainUIFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="aca", size=(720, 700))
self.SetSizeHints(660, 650, -1, -1)
tab_panels = TabPanel(self)
main_ui_sizer = wx.BoxSizer(wx.VERTICAL)
main_ui_sizer.Add(tab_panels, 1, wx.ALL | wx.EXPAND, 10)
self.SetSizer(main_ui_sizer)
### status bar used to show the user messages
self.live_reporting_status_bar = self.CreateStatusBar(2)
self.Show()
### pubsub message subscription for the live_reporting_status_bar
pub.subscribe(self.status_message_updater, "status_message_update")
def status_message_updater(self, message, column):
self.live_reporting_status_bar.PushStatusText(message, column)
### main application UI
class AcaInterface(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.is_dark_mode = (
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW).GetLuminance() < 1
) # detect system dark mode to adjust colour scheme
self.selected_source_location = os.getcwd()
self.selected_destination_location = os.getcwd()
self.column_no = None
### UI labels and icons
self.set_source_button_label = "Select Source Files"
self.set_destination_button_label = "Select Destination "
self.refresh_state_button_label = "\u21BB" # refresh button icon "↻"
self.select_all_button_label = "Select All"
self.clear_selected_button_label = "Clear Selected"
self.generate_column_icon = "\u25B3" # generate column icon "△"
self.copy_column_icon = "\u25B7" # copy column icon "▷"
self.verify_column_icon = "\u25BD" # verify column icon "▽"
self.generate_button_label = f"Generate {self.generate_column_icon}"
self.copy_button_label = f"Copy {self.copy_column_icon}"
self.verify_button_label = f"Verify {self.verify_column_icon}"
self.pass_status = " \u2B58" # pass symbol "○"
self.ignore_status = " \u002D" # ignore symbol "-"
self.fail_status = " \u0058" # fail symbol "X"
self.selected_items = [] # List of selected items in the ui_file_list
self.progress_bar_division = 1
self.start_time = None
self.end_time = None
### initialise Report page status lists
self.generate_complete = []
self.generate_skip = []
self.copy_complete = []
self.copy_fail = []
self.copy_skip = []
self.verify_complete = []
self.verify_skip = []
self.verify_fail = []
super().__init__(parent)
### aca interface elements
self.set_source_button = wx.Button(self, label=self.set_source_button_label)
self.source_location = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER)
self.source_location.Bind(wx.EVT_TEXT_ENTER, self.enter_source)
self.refresh_state_button = wx.Button(self, label=self.refresh_state_button_label)
self.set_source_button.Bind(wx.EVT_BUTTON, self.set_source_directory)
self.refresh_state_button.Bind(wx.EVT_BUTTON, self.on_button_press)
self.set_destination_button = wx.Button(self, label=self.set_destination_button_label)
self.destination_location = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER)
self.destination_location.Bind(wx.EVT_TEXT_ENTER, self.enter_destination)
self.set_destination_button.Bind(wx.EVT_BUTTON, self.set_destination_location)
self.select_all_button = wx.Button(self, label=self.select_all_button_label)
self.clear_selected_button = wx.Button(self, label=self.clear_selected_button_label)
self.select_all_button.Bind(wx.EVT_BUTTON, self.on_button_press)
self.clear_selected_button.Bind(wx.EVT_BUTTON, self.on_button_press)
self.sort_button = wx.Button(self, -1, "\u21C5", size=(30, 40))
self.sort_button.Bind(wx.EVT_BUTTON, self.on_sort_click)
### aca interface central file list
self.ui_file_list = wx.ListCtrl(
self,
size=(-1, 660),
style=wx.LC_REPORT | wx.LC_HRULES | wx.LC_VRULES | wx.SUNKEN_BORDER,
)
self.ui_file_list.InsertColumn(0, "FILE")
self.ui_file_list.InsertColumn(1, "CHECKSUM")
self.ui_file_list.InsertColumn(2, self.generate_column_icon, format=wx.LIST_FORMAT_CENTER)
self.ui_file_list.InsertColumn(3, self.copy_column_icon, format=wx.LIST_FORMAT_CENTER)
self.ui_file_list.InsertColumn(4, self.verify_column_icon, format=wx.LIST_FORMAT_CENTER)
self.ui_file_list.SetColumnWidth(2, 40)
self.ui_file_list.SetColumnWidth(3, 40)
self.ui_file_list.SetColumnWidth(4, 40)
self.ui_file_list.Bind(wx.EVT_SIZE, self.on_size)
self.ui_file_list.Bind(
wx.EVT_LIST_ITEM_SELECTED, self.ui_file_list_item_selected
)
self.ui_file_list.Bind(
wx.EVT_LIST_ITEM_DESELECTED, self.ui_file_list_item_deselected
)
### aca file opration buttons
self.generate_button = wx.Button(self, label=self.generate_button_label)
self.copy_button = wx.Button(self, label=self.copy_button_label)
self.verify_button = wx.Button(self, label=self.verify_button_label)
self.generate_button.Bind(wx.EVT_BUTTON, self.on_button_press)
self.copy_button.Bind(wx.EVT_BUTTON, self.on_button_press)
self.verify_button.Bind(wx.EVT_BUTTON, self.on_button_press)
### file processing live progress bar
self.progress_bar = wx.Gauge(
self,
range=100,
size=(200, 15),
style=wx.GA_HORIZONTAL | wx.GA_SMOOTH | wx.GA_TEXT,
)
### aca interface layout
self.source_layout = wx.BoxSizer(wx.HORIZONTAL)
self.source_layout.Add(
self.set_source_button, 0, wx.TOP | wx.LEFT | wx.RIGHT | wx.EXPAND, 4
)
self.source_layout.Add(
self.source_location, 1, wx.TOP | wx.LEFT | wx.RIGHT | wx.EXPAND, 4
)
self.source_layout.Add(
self.refresh_state_button, 0, wx.TOP | wx.LEFT | wx.RIGHT | wx.EXPAND, 4
)
self.selection_layout = wx.BoxSizer(wx.HORIZONTAL)
self.selection_layout.Add(
self.select_all_button, 1, wx.TOP | wx.BOTTOM | wx.RIGHT | wx.EXPAND, 8
)
self.selection_layout.Add(
self.clear_selected_button, 1, wx.TOP | wx.BOTTOM | wx.RIGHT | wx.EXPAND, 8
)
self.selection_layout.Add(self.sort_button, 0, wx.TOP | wx.BOTTOM | wx.EXPAND, 8)
self.destination_layout = wx.BoxSizer(wx.HORIZONTAL)
self.destination_layout.Add(
self.set_destination_button, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, 3
)
self.destination_layout.Add(
self.destination_location, 1, wx.LEFT | wx.EXPAND, 6
)
self.source_destination_stack = wx.BoxSizer(wx.VERTICAL)
self.source_destination_stack.Add(self.source_layout, 0, wx.ALL | wx.EXPAND, 6)
self.source_destination_stack.Add(
self.destination_layout, 0, wx.ALL | wx.EXPAND, 8
)
self.source_destination_stack.Add(
self.selection_layout, 1, wx.LEFT | wx.RIGHT | wx.EXPAND, 60
)
self.process_buttons = wx.BoxSizer(wx.HORIZONTAL)
self.process_buttons.Add(self.generate_button, 0, wx.ALL | wx.EXPAND, 4)
self.process_buttons.Add(self.copy_button, 1, wx.ALL | wx.EXPAND, 4)
self.process_buttons.Add(self.verify_button, 0, wx.ALL | wx.EXPAND, 4)
self.progress_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.progress_sizer.Add(self.progress_bar, 1, wx.ALL | wx.EXPAND, 15)
self.process_spacer = wx.BoxSizer(wx.HORIZONTAL)
self.process_spacer.Add(self.process_buttons, 1, wx.TOP, 15) # add spacer between process buttons and listctrl
### aca vertial layout stack
self.aca_vertical_stack = wx.BoxSizer(wx.VERTICAL)
self.aca_vertical_stack.Add(
self.source_destination_stack, 1, wx.LEFT | wx.RIGHT | wx.EXPAND, 30
)
self.aca_vertical_stack.Add(self.ui_file_list, 1, wx.ALL | wx.EXPAND, 8)
self.aca_vertical_stack.Add(
self.process_spacer, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, 60
)
self.aca_vertical_stack.Add(
self.progress_sizer, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, 100
)
self.SetSizerAndFit(self.aca_vertical_stack)
pub.subscribe(self.update_progress_bar, "progress_update")
### set initial button access for aca
self.initial_button_access()
### adjust the ui_file_list to resize in proportion to the interface
def on_size(self, event):
width = (
self.ui_file_list.GetClientSize().GetWidth() - 120
) # -120 to retain the status symbol columns (40px * 3)
columns = (
self.ui_file_list.GetColumnCount() - 3
) # -3 excludes status symbol columns from resize
column_width = width // columns
for i in range(columns):
self.ui_file_list.SetColumnWidth(i, column_width)
event.Skip()
### initial button access at start up
def initial_button_access(self):
self.set_source_button.Enable(True)
self.refresh_state_button.Enable(True)
self.select_all_button.Enable(False)
self.clear_selected_button.Enable(False)
self.sort_button.Enable(False)
self.generate_button.Enable(False)
self.copy_button.Enable(False)
self.verify_button.Enable(False)
if self.selected_destination_location != None:
self.set_destination_button.Enable(True)
### Disable buttons during operations
def disable_buttons(self):
self.set_source_button.Enable(False)
self.set_destination_button.Enable(False)
self.refresh_state_button.Enable(False)
self.select_all_button.Enable(False)
self.clear_selected_button.Enable(False)
self.sort_button.Enable(False)
self.generate_button.Enable(False)
self.copy_button.Enable(False)
self.verify_button.Enable(False)
### enable buttons to start file processing operations
def enable_buttons(self):
self.set_source_button.Enable(True)
self.set_destination_button.Enable(True)
self.refresh_state_button.Enable(True)
self.select_all_button.Enable(True)
self.clear_selected_button.Enable(True)
self.sort_button.Enable(True)
self.generate_button.Enable(True)
self.verify_button.Enable(True)
if self.selected_destination_location != None and os.path.exists(self.selected_destination_location):
self.copy_button.Enable(True)
def enter_source(self, event):
self.capture_source_location()
def enter_destination(self, event):
self.capture_destination_location()
### capture the directory from the source_location textctrl
def capture_source_location(self):
self.selected_source_location = self.source_location.GetValue()
if os.path.exists(self.selected_source_location):
### call the filehashingservice and pass the source directory to it
self.fhs = filehashingservice.FileHashingService(self.selected_source_location)
### publisher to send source location to Report page
pub.sendMessage(
"source_report_update",
data=self.selected_source_location,
)
### Get the file list from the get_file_list method and populate the list view
self.fhs.get_file_list()
self.populate_ui_file_list_view()
else:
self.ui_file_list.DeleteAllItems()
self.selected_items.clear()
pub.sendMessage(
"status_message_update",
message="Source directory does not exist",
column=0,
)
def capture_destination_location(self):
self.selected_destination_location = self.destination_location.GetValue()
if os.path.exists(self.selected_destination_location):
self.enable_buttons()
pub.sendMessage(
"destination_report_update", data=self.selected_destination_location
)
else:
pub.sendMessage(
"status_message_update",
message="Destination directory does not exist",
column=0,
)
### open the source location dialog for the user to select and set
def set_source_directory(self, event):
### clear previous status reports and reset the progress bar
pub.sendMessage("status_message_update", message="", column=0)
pub.sendMessage("status_message_update", message="", column=1)
self.progress_bar.SetValue(0)
self.source_location.Clear()
self.ui_file_list.DeleteAllItems()
with wx.DirDialog(
self,
"Choose a directory:",
defaultPath=self.selected_source_location,
style=wx.DD_DEFAULT_STYLE,
) as dialog:
if dialog.ShowModal() == wx.ID_OK:
self.selected_source_location = dialog.GetPath()
self.source_location.write(self.selected_source_location)
self.capture_source_location()
else:
pub.sendMessage(
"status_message_update", message="No Directory Set", column=0
)
pass
### writes the filename and hash labels to the ui_file_list
def set_item_labels(self, index, data):
self.ui_file_list.SetItem(index, column=0, label=str(" " + data["filename"]))
self.ui_file_list.SetItem(index, column=1, label=str(" " + data["hash"]))
### alternates each row colour on the ui_file_list for better visibility
def ui_list_row_colour(self, file_index):
if file_index % 2 and self.is_dark_mode:
self.ui_file_list.SetItemBackgroundColour(
file_index, (wx.Colour(40, 40, 40))
)
elif file_index % 2:
self.ui_file_list.SetItemBackgroundColour(
file_index, (wx.Colour(240, 240, 240))
)
else:
pass
### populates the ui_file_list view with the filehashingservice.file_data_list
def populate_ui_file_list_view(self):
if len(self.fhs.file_data_list) != 0:
for file_index, file_data in enumerate(self.fhs.file_data_list, start=0):
self.ui_file_list.InsertItem(file_index, file_data["filename"])
self.set_item_labels(file_index, file_data)
self.ui_list_row_colour(file_index)
### publisher sends the number of files found and the number of files with checksums to live_reporting_status_bar
total_files = len(self.fhs.file_data_list)
no_hash = [data["hash"] for data in self.fhs.file_data_list].count(
self.fhs.empty_state
)
with_hash = int(total_files - no_hash)
pub.sendMessage(
"status_message_update",
message=f"{total_files} files found, {with_hash} files with checksums",
column=0,
)
self.enable_buttons()
else:
pub.sendMessage(
"status_message_update",
message="There are no files in the directory ¯\_(ツ)_/¯",
column=0,
)
def sort_list_processor(self, sorted_list_type):
self.fhs.file_data_list = sorted_list_type # update file_data_list with sorted list
self.ui_file_list.DeleteAllItems()
for file_index, file_data in enumerate(sorted_list_type, start=0):
self.ui_file_list.InsertItem(file_index, file_data["filename"])
self.set_item_labels(file_index, file_data)
self.ui_list_row_colour(file_index)
### list sorting functions
def on_alpha_sort(self, event):
self.selected_items.clear()
alpha_sort = sorted(self.fhs.file_data_list, key=lambda x: os.path.basename(x["filename"]).lower())
self.sort_list_processor(alpha_sort)
def on_reverse_sort(self, event):
self.selected_items.clear()
reverse_sort = sorted(self.fhs.file_data_list, key=lambda x: os.path.basename(x["filename"]).lower(), reverse=True)
self.sort_list_processor(reverse_sort)
def on_format_sort(self, event):
self.selected_items.clear()
format_sort = sorted(self.fhs.file_data_list, key=lambda x: os.path.splitext(x["filename"])[1].lower())
self.sort_list_processor(format_sort)
def on_date_sort(self, event):
self.selected_items.clear()
date_sort = sorted(self.fhs.file_data_list, key=lambda x: x["mod_date"], reverse=True)
self.sort_list_processor(date_sort)
def on_date_reverse_sort(self, event):
self.selected_items.clear()
date_reverse_sort = sorted(self.fhs.file_data_list, key=lambda x: x["mod_date"])
self.sort_list_processor(date_reverse_sort)
def on_sort_click(self, event):
self.menu = wx.Menu(title="Sort By:")
item1 = self.menu.Append(wx.ID_ANY, "A to Z", kind=wx.ITEM_CHECK)
item2 = self.menu.Append(wx.ID_ANY, "Z to A", kind=wx.ITEM_CHECK)
item3 = self.menu.Append(wx.ID_ANY, "Format Type", kind=wx.ITEM_CHECK)
item4 = self.menu.Append(wx.ID_ANY, "Date Modified (New to Old)", kind=wx.ITEM_CHECK)
item5 = self.menu.Append(wx.ID_ANY, "Date Modified (Old to New)", kind=wx.ITEM_CHECK)
self.Bind(wx.EVT_MENU, self.on_alpha_sort, id=item1.GetId())
self.Bind(wx.EVT_MENU, self.on_reverse_sort, id=item2.GetId())
self.Bind(wx.EVT_MENU, self.on_format_sort, id=item3.GetId())
self.Bind(wx.EVT_MENU, self.on_date_sort, id=item4.GetId())
self.Bind(wx.EVT_MENU, self.on_date_reverse_sort, id=item5.GetId())
self.PopupMenu(self.menu)
self.menu.Destroy()
### open the destination location dialog for the user to select and set
def set_destination_location(self, event):
self.destination_location.Clear()
with wx.DirDialog(
self,
"Choose a directory:",
defaultPath=self.selected_destination_location,
style=wx.DD_DEFAULT_STYLE,
) as dialog:
if dialog.ShowModal() == wx.ID_OK:
self.selected_destination_location = dialog.GetPath()
self.destination_location.write(self.selected_destination_location)
self.capture_destination_location()
### add selected item in ui_file_list to selected_items list for processing
def ui_file_list_item_selected(self, event):
index = event.GetIndex()
data = self.ui_file_list.GetItem(index, 1).GetText()
if index not in self.selected_items:
self.selected_items.append(index)
### remove deselected item in ui_file_list from the selected_items list
def ui_file_list_item_deselected(self, event):
index = event.GetIndex()
if index in self.selected_items:
self.selected_items.remove(index)
### Insert a new item into the ui_file_list view
def insert_list_view(self, file_index, file_data):
self.ui_file_list.DeleteItem(file_index)
self.ui_file_list.InsertItem(file_index, file_data["filename"])
self.set_item_labels(file_index, file_data)
self.ui_list_row_colour(file_index)
### Update the status column symbols (o, -, x)
def update_status(self, file_index, column_no, status):
self.ui_file_list.SetItem(file_index, column=column_no, label=status)
### subscribes to filehashingservice publisher to receive file data to update progress_bar
def update_progress_bar(
self, file_data, file_size, byte_section, progress_bar_refactor
):
percent = int(((byte_section / file_size) * 100) / self.progress_bar_division)
if self.progress_bar_division != 1:
percent += progress_bar_refactor
self.progress_bar.SetValue(round(percent))
### publisher sends file progress updates to the live_reporting_status_bar
pub.sendMessage(
"status_message_update",
message=f"Current File: {round(percent)}% | {file_data['filename']}",
column=0,
)
### reports the total file operations progress to the user
def update_total_progress(self, current_item, max_value):
total_progress = int((current_item / max_value) * 100)
### publisher sends total progress status messages to live_reporting_status_bar
pub.sendMessage(
"status_message_update",
message=f"Total Progress: {total_progress}% | {current_item} of {max_value} Files Complete",
column=1,
)
if not total_progress == 100:
pass
else:
### 100% file operations complete
self.end_time = time.time() - self.start_time
elapsed_time = timedelta(seconds=self.end_time)
### publsiher sends elapsed process time to Report page
pub.sendMessage("time_report_update", data=elapsed_time)
### publisher sends file data for generate operations to Report page
pub.sendMessage("file_report_update", data=self.selected_items)
pub.sendMessage(
"generate_report_update",
data=(self.generate_complete, self.generate_skip),
)
### publisher sends file data for copy operations to Report page
pub.sendMessage(
"copy_report_update",
data=[
self.copy_complete,
self.copy_skip,
self.copy_fail,
],
)
### publisher sends file data for verify operations to Report page
pub.sendMessage(
"verify_report_update",
data=[
self.verify_complete,
self.verify_skip,
self.verify_fail,
],
)
### clears Report page lists
self.selected_items.clear()
self.generate_complete.clear()
self.generate_skip.clear()
self.copy_complete.clear()
self.copy_skip.clear()
self.copy_fail.clear()
self.verify_complete.clear()
self.verify_skip.clear()
self.verify_fail.clear()
### reset intial button access on 100% complete
self.initial_button_access()
### run filehashingservice to generate file checksums
def on_generate(self, current_item, max_value, file_index, file_data):
self.column_no = 2
if file_data["hash"] == self.fhs.empty_state:
self.fhs.generate_hash(file_data)
wx.CallAfter(self.insert_list_view, file_index, file_data)
wx.CallAfter(
self.update_status, file_index, self.column_no, self.pass_status
)
wx.CallAfter(self.update_total_progress, current_item, max_value)
logger.info(f"{file_data['filename']}, {file_data['hash']}, generated")
self.generate_complete.append(file_data["filename"])
else:
wx.CallAfter(
self.update_status, file_index, self.column_no, self.ignore_status
)
wx.CallAfter(self.update_total_progress, current_item, max_value)
logger.info(
f"{file_data['filename']}, {file_data['hash']}, skipped generate"
)
self.generate_skip.append(file_data["filename"])
### run filehashingservice to verify checksums
def on_verify(self, current_item, max_value, file_index, file_data, location):
self.column_no = 4
if file_data["hash"] == self.fhs.empty_state:
wx.CallAfter(
self.update_status, file_index, self.column_no, self.ignore_status
)
wx.CallAfter(self.update_total_progress, current_item, max_value)
logger.info(f"{file_data['filename']}, no hash, skipped verify")
self.verify_skip.append(file_data["filename"])
else:
self.fhs.verify_files(file_data, location)
if self.fhs.hash_verified:
wx.CallAfter(
self.update_status, file_index, self.column_no, self.pass_status
)
wx.CallAfter(self.update_total_progress, current_item, max_value)
logger.info(f"{file_data['filename']}, {file_data['hash']}, verified")
self.verify_complete.append(file_data["filename"])
else:
wx.CallAfter(
self.update_status, file_index, self.column_no, self.fail_status
)
logger.critical(
f"{file_data['filename']}, {file_data['hash']}, FAILED verification"
)
self.verify_fail.append(file_data["filename"])
wx.CallAfter(self.update_total_progress, current_item, max_value)
### run filehashingservice to generate, copy and verify checksums
def on_copy(self, current_item, max_value, file_index, file_data):
self.progress_bar.SetValue(0)
### service to generate checksums
self.column_no = 2
if file_data["hash"] == self.fhs.empty_state:
self.fhs.generate_hash(file_data)
wx.CallAfter(self.insert_list_view, file_index, file_data)
wx.CallAfter(
self.update_status, file_index, self.column_no, self.pass_status
)
logger.info(f"{file_data['filename']}, {file_data['hash']}, generated")
self.generate_complete.append(file_data["filename"])
else:
wx.CallAfter(
self.update_status, file_index, self.column_no, self.ignore_status
)
logger.info(
f"{file_data['filename']}, {file_data['hash']}, skipped generate"
)
self.generate_skip.append(file_data["filename"])
### service to copy files
self.column_no = 3
file_destination_check = os.path.join(
self.selected_destination_location, file_data["filename"]
)
### check if destination is still available before copy
if not os.path.exists(self.selected_destination_location):
pub.sendMessage(
"status_message_update",
message=f"{self.selected_destination_location} not available",
column=0,
)
wx.CallAfter(
self.update_status, file_index, self.column_no, self.fail_status
)
logger.critical(
f"{self.selected_destination_location}, not available, FAILED copy"
)
self.copy_fail("copy_report_update", data=file_data["filename"])
### check if file exists in destination before copy and skips if true
elif os.path.isfile(file_destination_check):
pub.sendMessage(
"status_message_update",
message=f"{file_data['filename']} EXISTS",
column=0,
)
wx.CallAfter(
self.update_status, file_index, self.column_no, self.ignore_status
)
logger.warning(
f"{file_data['filename']}, exists in {self.selected_destination_location}, skipped copy"
)
self.copy_skip.append(file_data["filename"])
### service to verify existing file checksum if present in destination
if os.path.isfile(f"{file_destination_check}.md5"):
self.on_verify(
current_item,
max_value,
file_index,
file_data,
self.selected_destination_location,
)
else:
### skips verification if file in destination has no pre-existing checksum file
self.column_no = 4
wx.CallAfter(
self.update_status, file_index, self.column_no, self.ignore_status
)
logger.warning(
f"{file_data['filename']}, has no checksum in {self.selected_destination_location}, skipped verify"
)
self.verify_skip.append(file_data["filename"])
else:
### service to copy file if not in destination
self.fhs.copy_file(file_data, self.selected_destination_location)
wx.CallAfter(
self.update_status, file_index, self.column_no, self.pass_status
)
logger.info(
f"{file_data['filename']}, source: {self.selected_source_location}, destination: {self.selected_destination_location}, successfully copied"
)
self.copy_complete.append(file_data["filename"])
### service to verify file at destination after copy
self.on_verify(
current_item,
max_value,
file_index,
file_data,
self.selected_destination_location,
)
def on_button_press(self, event):
button_label = event.GetEventObject().GetLabel()
### User refreshes the source location
if button_label == self.refresh_state_button_label:
self.ui_file_list.DeleteAllItems()
self.selected_items.clear()
pub.sendMessage("status_message_update", message="", column=0)
pub.sendMessage("status_message_update", message="", column=1)
self.progress_bar.SetValue(0)
self.capture_source_location()
### user selects all items in ui_file_list
elif button_label == self.select_all_button_label:
for file_index in range(self.ui_file_list.GetItemCount()):
self.ui_file_list.Select(file_index)
### user clears selected items in ui_file_list
elif button_label == self.clear_selected_button_label:
self.selected_items.clear()
self.ui_file_list.SetItemState(-1, 0, wx.LIST_STATE_SELECTED)
### user selects generate file checksums
elif button_label == self.generate_button_label:
logger.info(f"user selected generate")
### disable buttons during file operations
self.disable_buttons()
self.start_time = time.time()
pub.sendMessage("status_message_update", message="", column=1)
self.progress_bar_division = 1
self.progress_bar.SetValue(0)
if len(self.selected_items) > 0:
max_value = len(
self.selected_items
) # set the item range for the progress bar
current_item = 0 # set item number variable to update progress bar, -1 starts the count from 0
pub.sendMessage(
"status_message_update",
message=f"Total Progress: 0% | {current_item} of {max_value} Files Complete",
column=1,
)
for index in sorted(self.selected_items):
current_item += (
1 # increment with each item to update the progress bar
)
file_data = self.fhs.file_data_list[index]
thread_pool_executor.submit(
self.on_generate,
current_item,
max_value,
index,
file_data,
)
else:
pass
### user selects to generate checksums, copy, verify files
elif button_label == self.copy_button_label:
logger.info(f"user selected generate: copy: verify")
self.capture_destination_location()
# check destination path is valid before proceeding
if os.path.exists(self.selected_destination_location):
### disable buttons during file operations
self.disable_buttons()
self.start_time = time.time()
pub.sendMessage("status_message_update", message="", column=1)
self.progress_bar_division = 3
self.progress_bar.SetValue(0)
if len(self.selected_items) > 0:
max_value = len(
self.selected_items
) # set the item range for the progress bar
current_item = 0 # set item number variable to update progress bar
pub.sendMessage(
"status_message_update",
message=f"Total Progress: 0% | 0 of {max_value} Files Complete",
column=1,
)
for index in sorted(self.selected_items):
current_item += (
1 # increment with each item to update the progress bar
)
file_data = self.fhs.file_data_list[index]
thread_pool_executor.submit(
self.on_copy,
current_item,
max_value,
index,
file_data,
)
else:
# exit process if select_destination_location path is not valid
pass
### user selects verify file checksums
elif button_label == self.verify_button_label:
logger.info(f"user selected verify")
### disable buttons during file operations
self.disable_buttons()
self.start_time = time.time()
pub.sendMessage("status_message_update", message="", column=1)
self.progress_bar_division = 1
self.progress_bar.SetValue(0)
if len(self.selected_items) > 0:
max_value = len(
self.selected_items
) # set the item range for the progress bar
current_item = 0 # set item number variable to update progress bar
pub.sendMessage(
"status_message_update",
message=f"Total Progress: 0% | {current_item} of {max_value} Files Complete",
column=1,
)
for index in sorted(self.selected_items):
current_item += (
1 # increment with each item to update the progress bar
)
file_data = self.fhs.file_data_list[index]
thread_pool_executor.submit(
self.on_verify,
current_item,
max_value,
index,
file_data,
self.selected_source_location,
)
else:
pass
### Report page UI
class ReportInterface(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.is_dark_mode = (
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW).GetLuminance() < 1
) # detect system dark mode to adjust colour scheme
### Report page UI elements
self.source_label = wx.StaticText(self, label="Source")
self.source_stat = wx.TextCtrl(self, value="None", style=wx.TE_READONLY)
self.destination_label = wx.StaticText(self, label="Destination")
self.destination_stat = wx.TextCtrl(self, value="None", style=wx.TE_READONLY)
self.total_files_label = wx.StaticText(self, label="Processed Files")
self.total_files_stat = wx.TextCtrl(self, value="", style=wx.TE_READONLY)
self.time_label = wx.StaticText(self, label="Processing Time (h:m:s:ms)")
self.time_stat = wx.TextCtrl(self, value="00:00:00", style=wx.TE_READONLY)
self.generate_label = wx.StaticText(self, label="○ Generated")
self.generate_stat = wx.TextCtrl(self, value="", style=wx.TE_READONLY)
self.generate_skip_label = wx.StaticText(self, label="- Skipped")
self.generate_skip_stat = wx.TextCtrl(self, value="", style=wx.TE_READONLY)
self.generate_space = wx.StaticText(self, label=" ")
self.copy_label = wx.StaticText(self, label="○ Copied")
self.copy_stat = wx.TextCtrl(self, value="", style=wx.TE_READONLY)
self.copy_skip_label = wx.StaticText(self, label="- Skipped ")
self.copy_skip_stat = wx.TextCtrl(self, value="", style=wx.TE_READONLY)
self.copy_fail_label = wx.StaticText(self, label="X Failed")
self.copy_fail_stat = wx.TextCtrl(self, value="", style=wx.TE_READONLY)
self.verify_label = wx.StaticText(self, label="○ Verified")
self.verify_stat = wx.TextCtrl(self, value="", style=wx.TE_READONLY)
self.verify_skip_label = wx.StaticText(self, label="- Skipped")
self.verify_skip_stat = wx.TextCtrl(self, value="", style=wx.TE_READONLY)
self.verify_fail_label = wx.StaticText(self, label="X Failed")
self.verify_fail_stat = wx.TextCtrl(self, value="", style=wx.TE_READONLY)
stat_font = wx.Font(wx.FontInfo(13).Bold())
self.total_files_stat.SetFont(stat_font)
self.time_stat.SetFont(stat_font)
self.generate_stat.SetFont(stat_font)
self.generate_skip_stat.SetFont(stat_font)
self.copy_stat.SetFont(stat_font)
self.copy_skip_stat.SetFont(stat_font)
self.copy_fail_stat.SetFont(stat_font)
self.verify_stat.SetFont(stat_font)
self.verify_skip_stat.SetFont(stat_font)
self.verify_fail_stat.SetFont(stat_font)
source_box = wx.StaticBox(self, -1, "File Locations")
source_sizer = wx.StaticBoxSizer(source_box, wx.HORIZONTAL)
source_stack = wx.BoxSizer(wx.VERTICAL)
source_stack.Add(self.source_label, 0, wx.LEFT | wx.TOP, 5)
source_stack.Add(self.source_stat, 1, wx.ALL | wx.EXPAND, 5)
destination_stack = wx.BoxSizer(wx.VERTICAL)
destination_stack.Add(self.destination_label, 0, wx.LEFT | wx.TOP, 5)
destination_stack.Add(self.destination_stat, 1, wx.ALL | wx.EXPAND, 5)
source_sizer.Add(source_stack, 1, wx.EXPAND)
source_sizer.Add(destination_stack, 1, wx.EXPAND)
file_box = wx.StaticBox(self, -1, "All Files")
file_sizer = wx.StaticBoxSizer(file_box, wx.HORIZONTAL)
file_stack_1 = wx.BoxSizer(wx.VERTICAL)
file_stack_1.Add(self.total_files_label, 0, wx.LEFT | wx.TOP, 5)
file_stack_1.Add(self.total_files_stat, 1, wx.ALL | wx.EXPAND, 5)