-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFlatCAMProcess.py
156 lines (107 loc) · 3.92 KB
/
FlatCAMProcess.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
############################################################
# FlatCAM: 2D Post-processing for Manufacturing #
# http://flatcam.org #
# Author: Juan Pablo Caram (c) #
# Date: 2/5/2014 #
# MIT Licence #
############################################################
from FlatCAMGUI import FlatCAMActivityView
from PyQt5 import QtCore
import weakref
# 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()
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.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)
self.something_changed.emit()
def update_view(self):
if len(self.procs) == 0:
self.view.set_idle()
elif len(self.procs) == 1:
self.view.set_busy(self.procs[0]().status_msg())
else:
self.view.set_busy("%d processes running." % len(self.procs))