-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
90 lines (69 loc) · 2.63 KB
/
util.py
File metadata and controls
90 lines (69 loc) · 2.63 KB
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
#System Imports
import logging, traceback
#Qt Imports
from PyQt6 import QtCore
class QSignalHandler(logging.Handler): #logging handler that emits all log entries through a specified signal
def __init__(self, sig):
super().__init__()
self.sig = sig
def emit(self, record):
msg = self.format(record)
self.sig.emit(msg)
def write(self, m):
pass
class Sig(QtCore.QObject): #Signal "wrapper" to allow programmatic definition of signals
s = QtCore.pyqtSignal(object, object)
def __init__(self, name):
super().__init__()
self.name = name
def emit(self, object):
self.s.emit(object, self.name)
def connect(self, obj):
self.s.connect(obj)
class RunningThreads(QtCore.QObject):
allDone = QtCore.pyqtSignal()
countChange = QtCore.pyqtSignal(int)
def __init__(self):
super().__init__()
self.active_threads = []
def __len__(self):
return len(self.active_threads)
def watchThread(self, thrd):
if thrd.isRunning():
self._addItem(thrd)
#print("Thread added imm.: %s" % thrd)
else:
thrd.started.connect(lambda thrd=thrd: self._addItem(thrd))
thrd.finished.connect(lambda thrd=thrd: self._removeItem(thrd))
#print("Thread watched: %s" % thrd)
def _addItem(self, thrd):
#print("Thread added: %s" % thrd)
if thrd not in self.active_threads:
self.active_threads.append(thrd)
self.countChange.emit(len(self.active_threads))
#print(self.active_threads)
def _removeItem(self, thrd):
#print("Thread stopped: %s" % thrd)
try:
self.active_threads.remove(thrd)
except Exception as e:
raise Exception("Error tracking active threads: %s" % e)
#print(self.active_threads)
finally:
if len(self.active_threads) == 0:
self.allDone.emit()
else:
self.countChange.emit(len(self.active_threads))
class SBlock:
def __init__(self, obj):
self.target = obj
def __enter__(self):
self.target.blockSignals(True)
return self.target
def __exit__(self, *args):
self.target.blockSignals(False)
def my_excepthook(type, value, traceb):
#print('Unhandled error:', type, value, traceb)
for l in traceback.format_exception(type, value, traceb):
print(l)
#print(traceback.format_exception(type, value, traceb))