-
Notifications
You must be signed in to change notification settings - Fork 3
/
appProcess.py
181 lines (129 loc) · 4.92 KB
/
appProcess.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
# ##########################################################
# FlatCAM: 2D Post-processing for Manufacturing #
# http://flatcam.org #
# Author: Juan Pablo Caram (c) #
# Date: 2/5/2014 #
# MIT Licence #
# ##########################################################
from appGUI.GUIElements import FlatCAMActivityView
from PyQt6 import QtCore
import weakref
import gettext
import appTranslation as fcTranslate
import builtins
fcTranslate.apply_language('strings')
if '_' not in builtins.__dict__:
_ = gettext.gettext
# import logging
# log = logging.getLogger('base2')
# #log.setLevel(logging.DEBUG)
# log.setLevel(logging.WARNING)
# #log.setLevel(logging.INFO)
# formatter = logging.Formatter('[%(levelname)s] %(message)s')
# handler = logging.StreamHandler()
# handler.setFormatter(formatter)
# log.addHandler(handler)
class FCProcess(object):
app = None
def __init__(self, descr):
self.callbacks = {
"done": []
}
self.descr = descr
self.status = "Active"
def __del__(self):
self.done()
def __enter__(self):
pass
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is not None:
self.app.log.error("Abnormal termination of process!")
self.app.log.error(exc_type)
self.app.log.error(exc_val)
self.app.log.error(exc_tb)
self.done()
def done(self):
for fcn in self.callbacks["done"]:
fcn(self)
def connect(self, callback, event="done"):
if callback not in self.callbacks[event]:
self.callbacks[event].append(callback)
def disconnect(self, callback, event="done"):
try:
self.callbacks[event].remove(callback)
except ValueError:
pass
def set_status(self, status_string):
self.status = status_string
def status_msg(self):
return self.descr
class FCProcessContainer(object):
"""
This is the process container, or controller (as in MVC)
of the Process/Activity tracking.
FCProcessContainer keeps weak references to the FCProcess'es
such that their __del__ method is called when the user
looses track of their reference.
"""
app = None
def __init__(self):
self.procs = []
def add(self, proc):
self.procs.append(weakref.ref(proc))
def new(self, descr):
proc = FCProcess(descr)
proc.connect(self.on_done, event="done")
self.add(proc)
self.on_change(proc)
return proc
def on_change(self, proc):
pass
def on_done(self, proc):
self.remove(proc)
def remove(self, proc):
to_be_removed = []
for pref in self.procs:
if pref() == proc or pref() is None:
to_be_removed.append(pref)
for pref in to_be_removed:
self.procs.remove(pref)
class FCVisibleProcessContainer(QtCore.QObject, FCProcessContainer):
something_changed = QtCore.pyqtSignal()
# this will signal that the application is IDLE
idle_flag = QtCore.pyqtSignal()
def __init__(self, view):
assert isinstance(view, FlatCAMActivityView), \
"Expected a FlatCAMActivityView, got %s" % type(view)
FCProcessContainer.__init__(self)
QtCore.QObject.__init__(self)
self.view = view
self.text_to_display_in_activity = ''
self.new_text = ' '
self.something_changed.connect(self.update_view)
def on_done(self, proc):
# self.app.log.debug("FCVisibleProcessContainer.on_done()")
super(FCVisibleProcessContainer, self).on_done(proc)
self.something_changed.emit()
def on_change(self, proc):
# self.app.log.debug("FCVisibleProcessContainer.on_change()")
super(FCVisibleProcessContainer, self).on_change(proc)
# whenever there is a change update the message on activity
self.text_to_display_in_activity = self.procs[0]().status_msg()
self.something_changed.emit()
def update_view(self):
if len(self.procs) == 0:
self.new_text = ''
self.view.set_idle()
self.idle_flag.emit()
elif len(self.procs) == 1:
self.view.set_busy(self.text_to_display_in_activity + self.new_text)
else:
self.view.set_busy("%d %s" % (len(self.procs), _("processes running.")))
def update_view_text(self, new_text, clear=False):
# this has to be called after the method 'new' inherited by this class is called with a string text as param
self.new_text = new_text
if len(self.procs) == 1:
if clear is False:
self.view.set_busy(self.text_to_display_in_activity + self.new_text, no_movie=True)
else:
self.view.set_busy(self.new_text, no_movie=True)