-
Notifications
You must be signed in to change notification settings - Fork 9
/
fuji.py
698 lines (590 loc) · 24.9 KB
/
fuji.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
from dataclasses import dataclass
import os
import re
import string
import subprocess
import sys
import threading
from pathlib import Path
from typing import Iterable, List
import humanize
import wx
import wx.lib.agw.hyperlink as hl
from acquisition.abstract import AcquisitionMethod, Parameters
from acquisition.asr import AsrMethod
from acquisition.rsync import RsyncMethod
from acquisition.sysdiagnose import SysdiagnoseMethod
from checks.folders import FoldersCheck
from checks.free_space import FreeSpaceCheck
from checks.network import NetworkCheck
from meta import AUTHOR, HOMEPAGE, VERSION
from shared.utils import command_to_properties, lines_to_properties
METHODS = [AsrMethod(), RsyncMethod(), SysdiagnoseMethod()]
CHECKS = [FoldersCheck(), FreeSpaceCheck(), NetworkCheck()]
PARAMS = Parameters()
INPUT_WINDOW: "InputWindow"
OVERVIEW_WINDOW: "OverviewWindow"
PROCESSING_WINDOW: "ProcessingWindow"
class RedirectText(object):
out: wx.TextCtrl
max_lines = 500
def __init__(self, control: wx.TextCtrl):
self.out = control
def write(self, value):
wx.CallAfter(self.append_shrink, value)
def append_shrink(self, value):
self.out.AppendText(value)
lines = self.out.GetNumberOfLines()
if lines > self.max_lines:
delta = lines - self.max_lines
position = self.out.XYToPosition(0, delta - 1)
self.out.Remove(0, position)
self.out.ShowPosition(self.out.GetLastPosition())
@dataclass
class DiskSpaceInfo:
identifier: str = ""
size: int = 0
used_space: int = 0
free_space: int = 0
mount_point: str = ""
@dataclass
class DeviceInfo:
indent: int = 0
type: str = ""
name: str = ""
size: str = ""
identifier: str = ""
status: str = ""
disk_space: DiskSpaceInfo = None
class DevicesWindow(wx.Frame):
def _parse_stanza(self, stanza: str, mount_info: dict) -> Iterable[DeviceInfo]:
lines = stanza.splitlines()
first, second = lines[:2]
status = ""
if "(" in first:
status = first.split("(")[1].split(")")[0]
pivot_1 = second.index(":") + 1
pivot_2 = second.index(" NAME")
pivot_3 = second.index(" SIZE")
pivot_4 = second.index(" IDENTIFIER")
is_disk = True
for line in lines[2:]:
type = line[pivot_1 + 1 : pivot_2].strip()
name = line[pivot_2:pivot_3].strip()
size = line[pivot_3 + 1 : pivot_4].strip()
identifier = line[pivot_4:].strip()
if not identifier:
continue
indent = identifier[4:].count("s")
if identifier == "-":
indent = 1
device_info = DeviceInfo(
indent=indent,
type=type,
name=name,
size=size,
identifier=identifier,
status=status if is_disk else "",
disk_space=mount_info.get(identifier),
)
is_disk = False
yield device_info
def __init__(self, parent):
super().__init__(parent, title="Fuji - Drives and partitions")
self.parent = parent
panel = wx.Panel(self)
title = wx.StaticText(panel, label="List of drives and partitions")
title_font: wx.Font = title.GetFont()
title_font.SetPointSize(18)
title_font.SetWeight(wx.FONTWEIGHT_BOLD)
title.SetFont(title_font)
devices_label = wx.StaticText(
panel,
label="The source can be set by double-clicking on a mounted partition",
)
self.list_ctrl = wx.ListCtrl(panel, style=wx.LC_REPORT | wx.BORDER_SUNKEN)
self.list_ctrl.Bind(wx.EVT_LIST_ITEM_FOCUSED, self.on_item_focused)
self.list_ctrl.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.on_item_activated)
mount_info = {}
df_lines = subprocess.check_output(["df"], universal_newlines=True).splitlines()
for line in df_lines:
if not line.startswith("/dev/disk"):
continue
identifier, size, used, free, _, _, _, _, mount_point = re.split(
"\s+", line, maxsplit=8
)
short_identifier = identifier[5:]
mount_info[short_identifier] = DiskSpaceInfo(
identifier=identifier,
size=int(size) * 512,
used_space=int(used) * 512,
free_space=int(free) * 512,
mount_point=mount_point,
)
self.devices: List[DeviceInfo] = []
diskutil_list = subprocess.check_output(
["diskutil", "list"], universal_newlines=True
)
stanzas = diskutil_list.strip().split("\n\n")
for stanza in stanzas:
self.devices.extend(self._parse_stanza(stanza, mount_info))
# Add columns to the list control
columns = [
"Identifier",
"Type",
"Name",
"Size",
"Device status",
"Mount point",
"Used space",
]
for index, col in enumerate(columns):
self.list_ctrl.InsertColumn(index, col, width=-1)
self.selected_index = -1
highlight = wx.Colour()
highlight.SetRGBA(0x18808080)
for index, line in enumerate(self.devices):
mount_point = ""
size_str = line.size
used_str = ""
if line.type in ("APFS Volume", "APFS Snapshot"):
used_str = size_str
size_str = "^"
disk_space = line.disk_space
if disk_space:
mount_point = disk_space.mount_point
used_str = humanize.naturalsize(disk_space.used_space)
if mount_point == "/":
estimated_used = humanize.naturalsize(
disk_space.size - disk_space.free_space
)
used_str = f"{estimated_used} (~)"
index = self.list_ctrl.InsertItem(
index, f"{' ' * line.indent}{line.identifier}"
)
self.list_ctrl.SetItem(index, 1, line.type)
self.list_ctrl.SetItem(index, 2, line.name)
self.list_ctrl.SetItem(index, 3, size_str)
self.list_ctrl.SetItem(index, 4, line.status)
self.list_ctrl.SetItem(index, 5, mount_point)
self.list_ctrl.SetItem(index, 6, used_str)
self.list_ctrl.SetItemData(index, index)
if f"{PARAMS.source}" == mount_point:
self.list_ctrl.Select(index)
self.list_ctrl.Focus(index)
self.selected_index = index
if index % 2:
self.list_ctrl.SetItemBackgroundColour(index, highlight)
if not mount_point:
self.list_ctrl.SetItemTextColour(index, (128, 128, 128))
padding = 10
width = padding * 4
height = padding * 4
for index in range(len(columns)):
self.list_ctrl.SetColumnWidth(index, wx.LIST_AUTOSIZE)
# Add a bit of padding
padded_width = self.list_ctrl.GetColumnWidth(index) + padding
padded_width = max(padded_width, 100)
if index == 2:
padded_width = min(padded_width, 180)
self.list_ctrl.SetColumnWidth(index, padded_width)
width = width + padded_width
for index in range((self.list_ctrl.ItemCount)):
rect: wx.Rect = self.list_ctrl.GetItemRect(index)
height = height + rect.GetHeight()
self.list_ctrl.SetMinSize(wx.Size(width, height))
# Add controls to the sizer
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(title, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 20)
vbox.Add(devices_label, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 10)
vbox.Add((0, 10))
vbox.Add(self.list_ctrl, 1, wx.EXPAND | wx.ALL, border=10)
panel.SetSizerAndFit(vbox)
sizer = wx.GridSizer(1)
sizer.Add(panel, 1, wx.EXPAND | wx.ALL)
self.SetSizerAndFit(sizer)
# The list control might become quite big, thus this line sets a
# reasonable minimum so the window can be reduced
self.SetMinSize(wx.Size(480, 240))
def _back_to_selected(self):
try:
self.list_ctrl.Select(self.selected_index)
self.list_ctrl.Focus(self.selected_index)
except:
pass
def on_item_focused(self, event):
index = event.GetIndex()
device: DeviceInfo = self.devices[index]
if device.disk_space and device.disk_space.mount_point:
self.selected_index = event.GetIndex()
else:
self._back_to_selected()
def on_item_activated(self, event):
index = event.GetIndex()
device: DeviceInfo = self.devices[index]
if device.disk_space and device.disk_space.mount_point:
PARAMS.source = device.disk_space.mount_point
self.parent.source_picker.SetPath(device.disk_space.mount_point)
self.parent.source_picker.SetFocus()
# Clean up event listeners and close
self.list_ctrl.Unbind(
wx.EVT_LIST_ITEM_FOCUSED, handler=self.on_item_focused
)
self.list_ctrl.Unbind(
wx.EVT_LIST_ITEM_ACTIVATED, handler=self.on_item_activated
)
self.Close()
else:
self._back_to_selected()
class InputWindow(wx.Frame):
method: AcquisitionMethod
def __init__(self):
super().__init__(
parent=None,
title="Fuji - Forensic Unattended Juicy Imaging",
size=(600, 400),
style=wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX),
)
self.EnableMaximizeButton(False)
panel = wx.Panel(self)
# Components
title = wx.StaticText(panel, label="Fuji")
title_font: wx.Font = title.GetFont()
title_font.SetPointSize(36)
title_font.SetWeight(wx.FONTWEIGHT_EXTRABOLD)
title.SetFont(title_font)
desc = wx.StaticText(panel, label="Forensic Unattended Juicy Imaging")
desc_font: wx.Font = desc.GetFont()
desc_font.SetPointSize(18)
desc_font.SetWeight(wx.FONTWEIGHT_BOLD)
desc.SetFont(desc_font)
byline_text = wx.StaticText(panel, label=f"Version {VERSION} by {AUTHOR}")
byline_link = hl.HyperLinkCtrl(panel, label=HOMEPAGE, URL=HOMEPAGE)
accent = wx.Colour(181, 78, 78)
byline_link.SetColours(accent, accent, accent)
byline_link.SetBold(True)
byline_link.UpdateLink()
case_label = wx.StaticText(panel, label="Case name:")
self.case_text = wx.TextCtrl(panel, value=PARAMS.case)
examiner_label = wx.StaticText(panel, label="Examiner:")
self.examiner_text = wx.TextCtrl(panel, value=PARAMS.examiner)
notes_label = wx.StaticText(panel, label="Notes:")
self.notes_text = wx.TextCtrl(panel, value=PARAMS.notes)
output_label = wx.StaticText(panel, label="Image name:")
self.output_text = wx.TextCtrl(panel, value=PARAMS.image_name)
self.output_text.Bind(wx.EVT_CHAR, self._validate_image_name)
source_label = wx.StaticText(panel, label="Source:")
self.source_picker = wx.DirPickerCtrl(panel)
self.source_picker.SetInitialDirectory("/")
self.source_picker.SetPath(str(PARAMS.source))
# Add Devices button
devices_button = wx.Button(panel, label="List of drives and partitions")
devices_button.Bind(wx.EVT_BUTTON, self.on_open_devices)
tmp_label = wx.StaticText(panel, label="Temp image location:")
self.tmp_picker = wx.DirPickerCtrl(panel)
self.tmp_picker.SetInitialDirectory("/Volumes")
if os.path.isdir(PARAMS.tmp):
self.tmp_picker.SetPath(str(PARAMS.tmp))
destination_label = wx.StaticText(panel, label="DMG destination:")
self.tmp_picker.Bind(wx.EVT_DIRPICKER_CHANGED, self._tmp_location_changed)
self.destination_picker = wx.DirPickerCtrl(panel)
self.destination_picker.SetInitialDirectory("/Volumes")
if os.path.isdir(PARAMS.destination):
self.destination_picker.SetPath(str(PARAMS.destination))
method_label = wx.StaticText(panel, label="Acquisition method:")
self.method_choice = wx.Choice(panel, choices=[m.name for m in METHODS])
self.method_choice.SetSelection(0)
# Prepare method descriptions
self.description_texts = []
for method in METHODS:
description_label = f"<b>{method.name}:</b> {method.description}"
description_text = wx.StaticText(panel)
description_text.SetLabelMarkup(description_label)
self.description_texts.append(description_text)
# Sound checkbox
self.sound_checkbox = wx.CheckBox(
panel, label="Play loud sound when acquisition is completed"
)
self.sound_checkbox.SetValue(True)
# Buttons
continue_btn = wx.Button(panel, label="Continue")
continue_btn.Bind(wx.EVT_BUTTON, self.on_continue)
# Layout
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(title, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 20)
vbox.Add(desc, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 5)
vbox.Add((0, 10))
vbox.Add(byline_text, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 0)
vbox.Add(byline_link, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 5)
vbox.Add((0, 20))
# Create a FlexGridSizer for labels and text controls
case_info = wx.FlexGridSizer(cols=2, hgap=10, vgap=10)
case_info.Add(case_label, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
case_info.Add(self.case_text, 1, wx.EXPAND)
case_info.Add(examiner_label, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
case_info.Add(self.examiner_text, 1, wx.EXPAND)
case_info.Add(notes_label, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
case_info.Add(self.notes_text, 1, wx.EXPAND)
case_info.AddGrowableCol(1, 1)
vbox.Add(case_info, 0, wx.EXPAND | wx.ALL, 10)
output_info = wx.FlexGridSizer(cols=2, hgap=10, vgap=10)
output_info.Add(output_label, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
output_info.Add(self.output_text, 1, wx.EXPAND)
output_info.Add(source_label, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
output_info.Add(self.source_picker, 1, wx.EXPAND)
output_info.Add((0, 0))
output_info.Add(devices_button, 0, wx.EXPAND)
output_info.Add(tmp_label, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
output_info.Add(self.tmp_picker, 1, wx.EXPAND)
output_info.Add(destination_label, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
output_info.Add(self.destination_picker, 1, wx.EXPAND)
output_info.Add(method_label, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
output_info.Add(self.method_choice, 1, wx.EXPAND)
output_info.AddGrowableCol(1, 1)
vbox.Add(output_info, 0, wx.EXPAND | wx.ALL, 10)
for description_text in self.description_texts:
vbox.Add(description_text, 0, wx.LEFT | wx.RIGHT | wx.BOTTOM, 10)
vbox.Add((0, 20))
vbox.Add(self.sound_checkbox, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM, 10)
vbox.Add(continue_btn, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM, 20)
panel.SetSizer(vbox)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(panel)
self.SetSizerAndFit(sizer)
# Bind close
self.Bind(wx.EVT_CLOSE, self.on_close)
def on_open_devices(self, event):
devices_window = DevicesWindow(self)
devices_window.Show()
devices_window.Move(64, 64)
def on_tmp_location_changed(self, event):
temp_location = self.tmp_picker.GetPath()
destination_location = self.destination_picker.GetPath()
if not destination_location:
self.destination_picker.SetPath(temp_location)
def _validate_image_name(self, event):
key = event.GetKeyCode()
valid_characters = "-_" + string.ascii_letters + string.digits
if chr(key) in valid_characters:
event.Skip()
return
else:
return False
def _tmp_location_changed(self, event):
temp_location = self.tmp_picker.GetPath()
destination_location = self.destination_picker.GetPath()
if not destination_location:
self.destination_picker.SetPath(temp_location)
def on_continue(self, event):
PARAMS.case = self.case_text.Value
PARAMS.examiner = self.examiner_text.Value
PARAMS.notes = self.notes_text.Value
PARAMS.image_name = self.output_text.Value
PARAMS.source = Path(self.source_picker.GetPath().strip())
PARAMS.tmp = Path(self.tmp_picker.GetPath().strip())
PARAMS.destination = Path(self.destination_picker.GetPath().strip())
PARAMS.sound = self.sound_checkbox.GetValue()
self.method = METHODS[self.method_choice.GetSelection()]
self.Hide()
OVERVIEW_WINDOW.update_overview()
OVERVIEW_WINDOW.Show()
def on_close(self, event):
app: wx.App = wx.GetApp()
app.ExitMainLoop()
class OverviewWindow(wx.Frame):
def __init__(self):
super().__init__(
parent=None,
title="Fuji - Overview",
size=(800, 400),
style=wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX),
)
panel = wx.Panel(self)
# Components
title = wx.StaticText(panel, label="Acquisition overview")
title_font: wx.Font = title.GetFont()
title_font.SetPointSize(18)
title_font.SetWeight(wx.FONTWEIGHT_BOLD)
title.SetFont(title_font)
# Overview grid container of 2 columns
self.overview_grid = wx.FlexGridSizer(cols=2, hgap=20, vgap=10)
self.overview_grid.AddGrowableCol(1, 1)
# Buttons
back_btn = wx.Button(panel, label="Back")
back_btn.Bind(wx.EVT_BUTTON, self.on_back)
confirm_btn = wx.Button(panel, label="Confirm")
confirm_btn.Bind(wx.EVT_BUTTON, self.on_confirm)
# Layout
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(title, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 20)
vbox.Add((0, 10))
vbox.Add(self.overview_grid, 0, wx.EXPAND | wx.ALL, 10)
vbox.Add((0, 20))
hbox = wx.BoxSizer(wx.HORIZONTAL)
hbox.Add(back_btn, 0, wx.RIGHT, 10)
hbox.Add(confirm_btn, 0)
vbox.Add(hbox, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM, 20)
panel.SetSizer(vbox)
self.panel = panel
# Bind close
self.Bind(wx.EVT_CLOSE, self.on_close)
def update_overview(self):
# Clear the existing grid content
self.overview_grid.Clear(True)
data = {
"Case name": PARAMS.case,
"Examiner": PARAMS.examiner,
"Notes": PARAMS.notes,
"Image name": PARAMS.image_name,
"Source": PARAMS.source,
"Temp image location": PARAMS.tmp,
"DMG destination": PARAMS.destination,
"Acquisition method": INPUT_WINDOW.method.name,
"Play sound": "Yes" if PARAMS.sound else "No",
}
max_text_width = 600
# Insert rows into the grid
for label, value in data.items():
label_text = wx.StaticText(self.panel, label=label)
label_text_font = label_text.GetFont()
label_text_font.SetWeight(wx.FONTWEIGHT_BOLD)
label_text.SetFont(label_text_font)
value_text = wx.StaticText(
self.panel,
label=f"{value}",
size=(max_text_width, -1),
)
value_text.Wrap(max_text_width)
self.overview_grid.Add(label_text, 0, wx.ALIGN_LEFT | wx.ALIGN_TOP)
self.overview_grid.Add(value_text, 1, wx.ALIGN_LEFT | wx.ALIGN_TOP)
# Perform checks
for check in CHECKS:
result = check.execute(PARAMS)
label_text = wx.StaticText(self.panel, label=check.name)
label_text_font = label_text.GetFont()
label_text_font.SetWeight(wx.FONTWEIGHT_BOLD)
label_text.SetFont(label_text_font)
if not result.passed:
label_text.SetForegroundColour((240, 20, 20))
value_text = wx.StaticText(
self.panel,
label=result.message,
size=(max_text_width, -1),
)
value_text.Wrap(max_text_width)
self.overview_grid.Add(label_text, 0, wx.ALIGN_LEFT | wx.ALIGN_TOP)
self.overview_grid.Add(value_text, 1, wx.ALIGN_LEFT | wx.ALIGN_TOP)
# Update the layout
self.panel.Layout()
self.panel.Fit()
self.Fit()
def on_back(self, event):
# Hide the overview window and show the input window again
self.Hide()
INPUT_WINDOW.Show()
def on_confirm(self, event):
# Start acquisition
self.Hide()
PROCESSING_WINDOW.activate()
def on_close(self, event):
self.on_back(event)
class ProcessingWindow(wx.Frame):
def __init__(self):
super().__init__(
parent=None,
title="Fuji - Acquisition",
size=(800, 600),
)
self.panel = wx.Panel(self)
# Components
self.title = wx.StaticText(self.panel, label="Acquisition in progress")
self.title_font: wx.Font = self.title.GetFont()
self.title_font.SetPointSize(18)
self.title_font.SetWeight(wx.FONTWEIGHT_BOLD)
self.title.SetFont(self.title_font)
self.output_text = wx.TextCtrl(
self.panel, style=wx.TE_MULTILINE | wx.TE_READONLY | wx.VSCROLL
)
# Layout
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(self.title, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, 20)
vbox.Add((0, 10))
vbox.Add(self.output_text, 1, wx.EXPAND | wx.ALL, 10)
self.panel.SetSizer(vbox)
# Bind close
self.Bind(wx.EVT_CLOSE, self.on_close)
def activate(self):
self.running = True
# Reset initial status
self.title.SetLabel("Acquisition in progress")
self.title.SetForegroundColour(wx.NullColour)
self.title.SetFont(self.title_font)
self.output_text.SetValue("")
self.Show()
# Redirect sys.stdout to the custom file-like object
redir = RedirectText(self.output_text)
sys.stdout = redir
sys.stderr = redir
# Start acquisition process in a separate thread
self.acquisition_thread = threading.Thread(target=self.execute_acquisition)
self.acquisition_thread.start()
def execute_acquisition(self):
try:
method = INPUT_WINDOW.method
result = method.execute(PARAMS)
# Process ended
wx.CallAfter(self.set_completion_status, result.success)
if PARAMS.sound:
self.play_sound(result.success)
except Exception as e:
# Acquisition failed
wx.CallAfter(self.set_completion_status, False)
wx.CallAfter(sys.stdout.write, f"Error: {str(e)}\n")
def play_sound(self, success: bool):
MAX_VOLUME = 7
volume_settings = subprocess.check_output(
["osascript", "-e", "get volume settings"], universal_newlines=True
)
volume_properties = lines_to_properties(volume_settings.split(","))
try:
current_volume = int(volume_properties.get("output volume"))
except:
# Keep reasonable volume
current_volume = 50
scaled = MAX_VOLUME * (current_volume / 100.0)
rounded = round(scaled, 4)
# Play the sound
subprocess.call(["osascript", "-e", f"set Volume {MAX_VOLUME}"])
sound = "Glass" if success else "Basso"
subprocess.call(["afplay", f"/System/Library/Sounds/{sound}.aiff"])
subprocess.call(["osascript", "-e", f"set Volume {rounded}"])
def set_completion_status(self, success):
if success:
self.title.SetLabel("Acquisition completed")
self.title.SetForegroundColour((20, 240, 20))
else:
self.title.SetLabel("Acquisition failed")
self.title.SetForegroundColour((240, 20, 20))
self.title.SetFont(self.title_font)
self.running = False
def on_close(self, event):
if not self.running:
self.Hide()
INPUT_WINDOW.Show()
if __name__ == "__main__":
# Try to find the serial number
information = command_to_properties(
["ioreg", "-rd1", "-c", "IOPlatformExpertDevice"],
separator="=",
strip_chars='"<> ',
)
if "IOPlatformSerialNumber" in information:
serial_number = information["IOPlatformSerialNumber"]
PARAMS.image_name = f"{serial_number}_Acquisition"
app = wx.App()
INPUT_WINDOW = InputWindow()
OVERVIEW_WINDOW = OverviewWindow()
PROCESSING_WINDOW = ProcessingWindow()
INPUT_WINDOW.Show()
app.MainLoop()
app.Destroy()