This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
343 lines (250 loc) · 13.6 KB
/
application.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import utilities as utils
from os import path
from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon
from json import load, dump
from pyqt5themes import FusionDark
from contextlib import contextmanager
from controller import BatchController
from multiprocessing import freeze_support
from interface import Ui_batch_media_file_converter
#: Name of the file to write saved interface values.
SAVE_STATE_FILE = "bmfc_state.json"
class Application:
"""Utility wrapper around all application functionality and interface control."""
def __init__(self, app=QtWidgets.QApplication([]), window=QtWidgets.QMainWindow()):
"""Self initialize the application and interface and prepare for start."""
self.app = app
self.window = window
window.setWindowIcon(QIcon(path.abspath("ffmpeg.png")))
self.active = False
self.active_pool = None
self.window_theme = "custom"
self.interface = Interface(window)
fetched_state = self.read_state()
if fetched_state:
self.interface.push_console("Loaded options from save state file.")
else:
self.interface.push_console("Couldn't load save state data. Is this the first run?")
self.set_state(**fetched_state)
self.update_ready()
self._bind_actions()
self._bind_changes()
self._batch_controller = None
self.interface.push_status("Interface loaded.", 5000)
def start(self):
"""Start and show the application."""
self.window.show()
sys.exit(self.app.exec_())
def exit(self):
"""Lock the interface and close the application cleanly and safely."""
self.interface.set_locked(True)
self.interface.set_files_progress_undetermined(True)
self.interface.set_data_progress_undetermined(True)
exit()
def set_window_theme(self, theme="custom"):
"""Set the window theme based on a string, defaulting to the custom dark fusion theme."""
self.window_theme = theme
if theme != "custom":
self.app.setStyle(theme)
else:
FusionDark().set_app(self.app)
@staticmethod
def _read_state():
"""Read JSON data from the `SAVE_STATE_FILE` as dictionary."""
with open(SAVE_STATE_FILE, "r") as file:
return load(file)
@staticmethod
def _save_state(**kwargs):
"""Write a dictionary passed as arguments to the `SAVE_STATE_FILE`."""
with open(SAVE_STATE_FILE, "w") as file:
return dump(kwargs, file, indent=2)
def read_state(self):
"""Protected read from the `SAVE_STATE_FILE`, swallows errors and returns an empty dictionary."""
try:
return self._read_state()
except (FileNotFoundError, ValueError):
return {}
def save_state(self):
"""Save the current state of the application (retrieved from `self.get_state()`)."""
self._save_state(**self.get_state())
def set_state(self, **kwargs):
"""Set the application state by keyword arguments passed."""
self.interface.set_state(**kwargs)
self.window.resize(*kwargs.get("window_size", (350, 450)))
self.set_window_theme(kwargs.get("window_theme", self.window_theme))
def get_state(self):
"""Get the current application and interface state as a dictionary."""
config = self.interface.get_state()
window_size = self.window.size()
config["window_size"] = (window_size.width(), window_size.height())
config["window_theme"] = self.window_theme
return config
def _bind_changes(self):
"""During initialization, bind `self.save_state()` to each element on change."""
self.interface.input_directory_edit.textChanged.connect(self.save_state)
self.interface.input_directory_edit.textChanged.connect(self.update_ready)
self.interface.output_directory_edit.textChanged.connect(self.save_state)
self.interface.output_directory_edit.textChanged.connect(self.update_ready)
self.interface.input_format_combo.currentIndexChanged.connect(self.save_state)
self.interface.input_format_combo.currentIndexChanged.connect(self.update_ready)
self.interface.output_format_combo.currentIndexChanged.connect(self.save_state)
self.interface.output_format_combo.currentIndexChanged.connect(self.update_ready)
self.interface.overwrite_output_checkbox.stateChanged.connect(self.save_state)
self.interface.thread_count_spinbox.valueChanged.connect(self.save_state)
self.interface.start_button.clicked.connect(self.save_state)
self.interface.exit_button.clicked.connect(self.save_state)
self.window.resizeEvent = lambda _: self.save_state()
def _bind_actions(self):
"""During initialization, bind specific actions to each element."""
def pick_input_directory():
dir_name = utils.open_directory_picker(self.window, native=self.window_theme is None,
title="Select Input Directory")
if dir_name:
self.interface.input_directory_edit.setText(dir_name)
self.interface.push_console("Set input directory: " + dir_name)
def pick_output_directory():
dir_name = utils.open_directory_picker(self.window, native=self.window_theme is None,
title="Select Output Directory")
if dir_name:
self.interface.output_directory_edit.setText(dir_name)
self.interface.push_console("Set output directory: " + dir_name)
self.interface.input_directory_picker.clicked.connect(pick_input_directory)
self.interface.output_directory_picker.clicked.connect(pick_output_directory)
self.interface.start_button.clicked.connect(lambda: (self.interface.set_data_progress_undetermined(True),
self.start_conversion()))
self.interface.cancel_button.clicked.connect(lambda: (self.interface.set_data_progress_undetermined(False),
self.cancel_conversion()))
self.interface.exit_button.clicked.connect(self.exit)
def valid_input_directory(self):
"""Return `True` if the current input directory value is a valid path and exists."""
return path.exists(self.interface.input_directory_edit.text())
def valid_output_directory(self):
"""Return `True` if the current output directory is a valid path that exists or is creatable."""
return utils.path_exists_or_creatable(self.interface.output_directory_edit.text())
def update_ready(self):
"""Check all input fields for validity and enable or disable the start and cancel buttons."""
if (self.valid_input_directory() and self.valid_output_directory() and
self.interface.input_format_combo.currentText() and
self.interface.output_format_combo.currentText()):
self.interface.start_button.setEnabled(True)
self.interface.push_status("Ready.")
return True
else:
self.interface.start_button.setEnabled(False)
self.interface.push_status("Not ready.")
return False
def set_active(self, state=True):
"""Set the application active state and enable or disable corresponding interface elements."""
self.active = state
self.interface.set_locked(state)
self.interface.start_button.setEnabled(not state)
self.interface.cancel_button.setEnabled(state)
@contextmanager
def active_state(self):
"""Set the interface as active so no changes can be made to values and yield the current state."""
self.set_active(True)
try:
yield self.get_state()
finally:
self.set_active(False)
class Interface(Ui_batch_media_file_converter):
"""Interface wrapper around the pre-generated Qt interface with added utility methods and fields."""
def __init__(self, *args, **kwargs):
"""Wrapped interface initializer around `self.setupUi()` that also sets initial field values."""
self.locked = False
self.setupUi(*args, **kwargs)
def set_locked(self, state=True):
"""Enable or disable all the interface elements that classify as configuration values."""
self.locked = state
self.input_directory_edit.setDisabled(state)
self.input_directory_picker.setDisabled(state)
self.output_directory_edit.setDisabled(state)
self.output_directory_picker.setDisabled(state)
self.input_format_combo.setDisabled(state)
self.output_format_combo.setDisabled(state)
self.overwrite_output_checkbox.setDisabled(state)
self.thread_count_spinbox.setDisabled(state)
def get_state(self):
"""Get the values of all configuration interface elements as a dictionary."""
return {
"locked": self.locked,
"input_dir": self.input_directory_edit.text(),
"output_dir": self.output_directory_edit.text(),
"input_fmt": self.input_format_combo.currentText(),
"output_fmt": self.output_format_combo.currentText(),
"overwrite_output": self.overwrite_output_checkbox.checkState(),
"thread_count": self.thread_count_spinbox.value()
}
def set_state(self, **kwargs):
"""Set the values of the configuration interface elements to the values specified by keyword arguments."""
self.locked = kwargs.get("locked", False)
self.input_directory_edit.setText(kwargs.get("input_dir", ""))
self.output_directory_edit.setText(kwargs.get("output_dir", ""))
utils.set_combo(self.input_format_combo, kwargs.get("input_fmt", "flac"))
utils.set_combo(self.output_format_combo, kwargs.get("output_fmt", "mp3"))
self.overwrite_output_checkbox.setChecked(kwargs.get("overwrite_output", False))
self.thread_count_spinbox.setValue(kwargs.get("thread_count", 4))
def push_status(self, message="", duration=0):
"""Push a status to the status bar for X milliseconds."""
# Show it on the status bar for the time specified if any (by default infinite)
self.statusbar.showMessage(message, duration)
def push_console(self, message, force=False):
"""Push a message to the information console if there is no duplicate (or `force` is `True`)."""
if force: # Push regardless of any duplicate
list_item = QtWidgets.QListWidgetItem()
list_item.setText(message)
self.information_console_list.addItem(list_item)
self.information_console_list.scrollToBottom()
else: # Run the duplicate checking logic here so it isn't done if it isn't needed
message_count = self.information_console_list.count() # Get the number of messages in information console
if message_count:
last_message = self.information_console_list.item(message_count - 1)
if message != last_message.text(): # Don't send a duplicate
self.push_console(message, force=True)
else:
self.push_console(message, force=True)
def push_both(self, message, duration=0, force=False):
"""Push a message to both the status bar and information console."""
self.push_status(message, duration=duration)
self.push_console(message, force=force)
def set_files_progress_undetermined(self, state=True):
self.files_completed_progress.setRange(0, int(not state))
def set_data_progress_undetermined(self, state=True):
self.data_completed_progress.setRange(0, int(not state))
@contextmanager
def files_progress_bar(self, maximum=0, fmt="%p% (%v / %m)"):
"""Set initial and final values for the files progress bar, and yield the `QtWidgets.QProgressBar` instance."""
self.files_completed_progress.setMaximum(maximum)
self.files_completed_progress.setFormat(fmt)
try:
yield self.files_completed_progress
finally:
self.files_completed_progress.format("%p%")
self.files_completed_progress.reset()
@contextmanager
def data_progress_bar(self, maximum=0, fmt="%p% ({}h {}m {}s)"):
"""Set initial and final values for the data progress bar, and yield the `QtWidgets.QProgressBar` instance."""
self.files_completed_progress.setMaximum(maximum)
self.files_completed_progress.setFormat(fmt)
def set_value(value):
s = value // 100 # Get seconds, value is ms
m, s = divmod(s, 60) # Get m and s once s is divided out
h, m = divmod(m, 60) # Get h and m once m is divided out
self.files_completed_progress.setFormat(fmt.format(h, m, s)) # Qt doesn't support the values we want to
# display so we have to change the format every value update
self.files_completed_progress.setValue(value) # Set the value, in ms
original_set_value = self.files_completed_progress.setValue # Save the original setValue for later
self.files_completed_progress.setValue = set_value # Overwrite the default setValue
try:
yield self.files_completed_progress
finally:
self.files_completed_progress.setValue = original_set_value # Revert change
self.files_completed_progress.format("%p%")
self.files_completed_progress.reset()
if __name__ == "__main__":
freeze_support()
Application().start()