From 9d6053403aadff3381c0c940883fa1a63ac71d0e Mon Sep 17 00:00:00 2001 From: Kameron Kenny <1267885+kkenny@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:18:48 -0400 Subject: [PATCH] Move worker classes to thread_my_stuff --- BitMover_ui.py | 79 ++++------------------------------------------ thread_my_stuff.py | 73 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 73 deletions(-) diff --git a/BitMover_ui.py b/BitMover_ui.py index 3243b36..29e4935 100755 --- a/BitMover_ui.py +++ b/BitMover_ui.py @@ -1,9 +1,8 @@ #!/usr/bin/env python import os import sys -import traceback -from PyQt6.QtCore import pyqtSignal, pyqtSlot, QRunnable, QObject, QThreadPool +from PyQt6.QtCore import QThreadPool from PyQt6.QtWidgets import QMainWindow, QApplication from PyQt6.QtGui import QIcon,QPixmap @@ -16,76 +15,10 @@ from get_image_tag import get_exif_tag from media import Media from lumberjack import timber from raw_photo import extract_jpg_thumb, get_raw_image_dimensions +from thread_my_stuff import Worker log = timber(__name__) -class WorkerSignals(QObject): - """ - Defines the signals avail from a running worker thread. - - Supported signals are: - - finished - No Data - - error - tuple (exctype, value, traceback.format_exc() ) - - result - object data returned from processing, anything - - progress - int indicating % progress - """ - started = pyqtSignal() - finished = pyqtSignal() - error = pyqtSignal(tuple) - result = pyqtSignal(object) - progress = pyqtSignal(int) - -class Worker(QRunnable): - """ - Worker Thread - - Inherits from QRunnable to handler worker thread setup, signals and wrap-up. - - :param callback: The function callback to run on this worker thread. Supplied args and - kwargs will be passed through to the runner. - :type callback: function - :param args: Arguments to pass to the callback function - :param kwargs: Keywords to pass to the callback function - """ - - def __init__(self,fn,*args,**kwargs): - super(Worker,self).__init__() - - # Store constructor args (re-used for processing) - self.fn = fn - self.args = args - self.kwargs = kwargs - self.signals = WorkerSignals() - - # Add the callback to our kwargs - self.kwargs['progress_callback'] = self.signals.progress - - @pyqtSlot() - def run(self): - """ - Initialise the runner function with passed args, kwargs. - """ - # Retrieve args/kwargs here; and fire processing using them - try: - self.signals.started.emit() - result = self.fn(*self.args, **self.kwargs) - except: - traceback.print_exc() - exctype, value = sys.exc_info()[:2] - self.signals.error.emit((exctype,value,traceback.format_exc())) - else: - self.signals.result.emit(result) - finally: - self.signals.finished.emit() - # Subclass QMainWindow to customize your application's main window class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, *args, **kwargs): @@ -268,7 +201,8 @@ class MainWindow(QMainWindow, Ui_MainWindow): progress_callback.emit((file_count / self.file_total) * 100) return "Done." - def print_output(self,s): + @staticmethod + def print_output(s): print(s) def scan_thread_started(self): @@ -279,18 +213,17 @@ class MainWindow(QMainWindow, Ui_MainWindow): print('scan thread complete.') self.toggle_scan_button(True) - def thread_complete(self): + @staticmethod + def thread_complete(): print("THREAD COMPLETE.") def find_files(self): """ find files to build a dictionary out of """ worker = Worker(self.t_find_files) - # worker.signals.started.connect(self.toggle_scan_button(enable=False)) worker.signals.started.connect(self.scan_thread_started) worker.signals.result.connect(self.print_output) worker.signals.finished.connect(self.thread_complete) worker.signals.finished.connect(self.scan_thread_done) - # worker.signals.finished.connect(self.toggle_scan_button(enable=True)) worker.signals.progress.connect(self.progress_fn) # Execute. diff --git a/thread_my_stuff.py b/thread_my_stuff.py index e69de29..f96c1d9 100644 --- a/thread_my_stuff.py +++ b/thread_my_stuff.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python + + +import traceback + +from PyQt6.QtCore import pyqtSignal, pyqtSlot, QRunnable, QObject + +class WorkerSignals(QObject): + """ + Defines the signals avail from a running worker thread. + + Supported signals are: + + finished + No Data + + error + tuple (exctype, value, traceback.format_exc() ) + + result + object data returned from processing, anything + + progress + int indicating % progress + """ + started = pyqtSignal() + finished = pyqtSignal() + error = pyqtSignal(tuple) + result = pyqtSignal(object) + progress = pyqtSignal(int) + +class Worker(QRunnable): + """ + Worker Thread + + Inherits from QRunnable to handler worker thread setup, signals and wrap-up. + + :param callback: The function callback to run on this worker thread. Supplied args and + kwargs will be passed through to the runner. + :type callback: function + :param args: Arguments to pass to the callback function + :param kwargs: Keywords to pass to the callback function + """ + + def __init__(self,fn,*args,**kwargs): + super(Worker,self).__init__() + + # Store constructor args (re-used for processing) + self.fn = fn + self.args = args + self.kwargs = kwargs + self.signals = WorkerSignals() + + # Add the callback to our kwargs + self.kwargs['progress_callback'] = self.signals.progress + + @pyqtSlot() + def run(self): + """ + Initialise the runner function with passed args, kwargs. + """ + # Retrieve args/kwargs here; and fire processing using them + try: + self.signals.started.emit() + result = self.fn(*self.args, **self.kwargs) + except: + traceback.print_exc() + exctype, value = sys.exc_info()[:2] + self.signals.error.emit((exctype,value,traceback.format_exc())) + else: + self.signals.result.emit(result) + finally: + self.signals.finished.emit() \ No newline at end of file