-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_manager_window.py
executable file
·344 lines (289 loc) · 14.2 KB
/
process_manager_window.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
344
import os
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib
from process_manager import ProcessManager
from process_progress_bar import ProcessProgressBar
from process import Process
class Row(Gtk.ListBoxRow):
def __init__(self, progress_bar):
super().__init__()
super().add(progress_bar)
self.process = progress_bar.process
class ProcessManagerWindow(Gtk.Window):
# __________________________________________________________________________
def __init__(self) -> None:
super().__init__(title="Process Manager")
self.connect("destroy", Gtk.main_quit)
self.set_border_width(5)
self.expropiated_process: Process = None
self.process_manager = ProcessManager()
self.timeout_id = None
self.init_components()
# __________________________________________________________________________
def init_components(self) -> None:
grid = Gtk.Grid(
row_homogeneous=True,
column_homogeneous=True,
row_spacing=5,
column_spacing=5,
)
# Simulate Switch ------------------------------------------------------
top_box = Gtk.Box(spacing=2)
self.simulating_label = Gtk.Label(label="Start Simulating")
self.simulating_switch = Gtk.Switch()
self.simulating_switch.set_active(False)
self.simulating_switch.connect("notify::active", self.execute_simulation)
self.executing_spinner = Gtk.Spinner()
self.quantum_rat_spin_button = Gtk.SpinButton(
adjustment=Gtk.Adjustment(
value=750,
lower=125,
upper=10000,
step_increment=125,
page_increment=0,
page_size=0
), climb_rate=1, digits=0,
)
self.quantum_rat_spin_button.connect("value-changed", self.change_quantum_rat)
# Inactive Processes List ----------------------------------------------
self.inactive_processes_list_box = Gtk.ListBox()
self.inactive_processes_list_box.set_selection_mode(
Gtk.SelectionMode.NONE
)
inactive_processes_scrolled_window = Gtk.ScrolledWindow()
inactive_processes_scrolled_window.add(self.inactive_processes_list_box)
for i in self.process_manager.inactive_processes:
process_button = Gtk.Button(label=i.name)
process_button.connect("clicked", self.prepare_process_action)
self.inactive_processes_list_box.add(process_button)
add_process_button = Gtk.Button(label="Add Process")
add_process_button.connect("clicked", self.add_process_action)
# Prepared Processes List ----------------------------------------------
self.prepared_processes_list_box = Gtk.ListBox()
self.prepared_processes_list_box.set_selection_mode(
Gtk.SelectionMode.NONE
)
prepared_processes_scrolled_window = Gtk.ScrolledWindow()
prepared_processes_scrolled_window.add(self.prepared_processes_list_box)
# Executed Process Progress Bar ----------------------------------------
self.executed_process_list_box = Gtk.ListBox()
self.executed_process_list_box.set_selection_mode(
Gtk.SelectionMode.NONE
)
executed_process_scrolled_window = Gtk.ScrolledWindow()
executed_process_scrolled_window.add(self.executed_process_list_box)
# Suspended Processes List ---------------------------------------------
self.suspended_processes_list_box = Gtk.ListBox()
self.suspended_processes_list_box.set_selection_mode(
Gtk.SelectionMode.NONE
)
suspended_processes_scrolled_window = Gtk.ScrolledWindow()
suspended_processes_scrolled_window.add(
self.suspended_processes_list_box
)
# Add Components -------------------------------------------------------
top_box.add(Gtk.Label(label='Quantum Rat (ms):'))
top_box.add(self.quantum_rat_spin_button)
top_box.add(self.simulating_label)
top_box.add(self.simulating_switch)
top_box.add(self.executing_spinner)
grid.attach(top_box, 0, 0, 3, 1)
grid.attach(Gtk.Label(label="Inactive Processes"), 0, 1, 1, 1)
grid.attach(inactive_processes_scrolled_window, 0, 2, 1, 15)
grid.attach(add_process_button, 0, 17, 1, 1)
grid.attach(Gtk.Label(label="Prepared Processes"), 1, 1, 3, 1)
grid.attach(prepared_processes_scrolled_window, 1, 2, 3, 16)
grid.attach(Gtk.Label(label="Executed Process"), 4, 1, 3, 1)
grid.attach(executed_process_scrolled_window, 4, 2, 3, 1)
grid.attach(Gtk.Label(label="Suspended Processes"), 4, 3, 3, 1)
grid.attach(suspended_processes_scrolled_window, 4, 4, 3, 14)
self.update_components()
self.add(grid)
# __________________________________________________________________________
def update_components(self) -> None:
# Add Prepared Processes -----------------------------------------------
self.remove_list_box(
self.process_manager.prepared_processes,
self.prepared_processes_list_box
)
for i in self.process_manager.prepared_processes:
process_progress_bar = ProcessProgressBar(i)
flag = True
for j in self.prepared_processes_list_box.get_children():
if j.process == i:
flag = False
break
if flag:
self.prepared_processes_list_box.add(Row(process_progress_bar))
# Add Executed Processes -----------------------------------------------
executed_process = self.process_manager.executed_process
self.clear_list_box(self.executed_process_list_box)
if executed_process:
process_progress_bar = ProcessProgressBar(executed_process)
self.executed_process_list_box.add(process_progress_bar)
# Add Suspended Processes ----------------------------------------------
self.remove_list_box(
self.process_manager.suspended_processes,
self.suspended_processes_list_box
)
for i in self.process_manager.suspended_processes:
process_progress_bar = ProcessProgressBar(i)
flag = True
for j in self.suspended_processes_list_box.get_children():
if j.process == i:
flag = False
break
if flag:
self.suspended_processes_list_box.add(Row(process_progress_bar))
# Update lists components ----------------------------------------------
self.inactive_processes_list_box.show_all()
self.prepared_processes_list_box.show_all()
self.executed_process_list_box.show_all()
self.suspended_processes_list_box.show_all()
# __________________________________________________________________________
def remove_list_box(self, processes_list, list_box):
for i in list_box.get_children():
if i.process not in processes_list:
i.destroy()
# __________________________________________________________________________
def clear_list_box(self, list_box) -> None:
for i in list_box.get_children():
i.destroy()
# __________________________________________________________________________
def execute_simulation(self, switch, _):
f = self.complete_execution if self.expropiated_process else self.iteration
if self.timeout_id:
GLib.source_remove(self.timeout_id)
del self.timeout_id
self.timeout_id = None
if switch.get_active():
self.timeout_id = GLib.timeout_add(
self.process_manager.quantum_rat * 1000, f, None
)
self.executing_spinner.start()
self.simulating_label.set_label("Stop Simulating")
else:
self.executing_spinner.stop()
self.simulating_label.set_label("Start Simulating")
# __________________________________________________________________________
def change_quantum_rat(self, spin_button):
quantum_rat = spin_button.get_value_as_int()
self.process_manager.set_quantum_rat(quantum_rat / 1000)
# __________________________________________________________________________
def add_process_action(self, _) -> None:
add_process_message_dialog = Gtk.MessageDialog(
parent=None,
title="Add New Process",
text="Process Name:",
secondary_text="Empty name will not be added",
buttons=Gtk.ButtonsType.OK_CANCEL,
)
process_name_entry = Gtk.Entry()
add_process_message_dialog.get_content_area().pack_end(
process_name_entry, True, True, 0
)
add_process_message_dialog.show_all()
response = add_process_message_dialog.run()
process_name = process_name_entry.get_text()
add_process_message_dialog.destroy()
if process_name and response == Gtk.ResponseType.OK:
self.process_manager.add_process(process_name)
process_button = Gtk.Button(label=process_name)
process_button.connect("clicked", self.prepare_process_action)
self.inactive_processes_list_box.add(process_button)
self.update_components()
# __________________________________________________________________________
def prepare_process_action(self, button) -> None:
process = self.process_manager.prepare_process(button.get_label())
if process:
print("+ prepare:", process.__str__(True))
if (process.priority == Process.PRIORITIES[0]
and not self.expropiated_process):
self.expropiation(process)
self.update_components()
else:
show_error_message_error = Gtk.MessageDialog(
parent=None,
title="Can not execute more processes",
text="Error:",
secondary_text="Can not execute more processes\n"
"Processes Limit: 999",
buttons=Gtk.ButtonsType.OK_CANCEL,
)
show_error_message_error.run()
show_error_message_error.destroy()
# __________________________________________________________________________
def execute_process_action(self, process) -> None:
if process:
print("* execute:", process.__str__(True))
self.process_manager.execute_process(process)
self.update_components()
# __________________________________________________________________________
def deactivate_process_action(self) -> None:
process = self.process_manager.deactivate_process()
print("- deactivate:", process.__str__(True))
self.update_components()
# __________________________________________________________________________
def suspend_process_action(self) -> None:
process = self.process_manager.suspend_process()
print("/ suspend:", process.__str__(True))
self.update_components()
# __________________________________________________________________________
def iteration(self, _) -> bool:
# Suspended to prepared
i = 0
while i < len(self.process_manager.suspended_processes):
process = self.process_manager.suspended_processes[0]
if hasattr(process, 'next') and process.next:
delattr(process, 'next')
self.process_manager.suspended_processes.remove(process)
self.process_manager.prepared_processes.append(process)
elif process.interaction:
process.next = True
i += 1
else:
self.process_manager.suspended_processes.remove(process)
self.process_manager.prepared_processes.append(process)
# Executed to suspended or deactivated
executed_process = self.process_manager.executed_process
if executed_process:
executed_process.progress += executed_process.advance
if executed_process.progress >= 1:
self.deactivate_process_action()
else:
self.suspend_process_action()
# Prepared to executed
process_to_execute = self.process_manager.compete()
self.execute_process_action(process_to_execute)
return self.expropiated_process == None
# __________________________________________________________________________
def expropiation(self, process: Process) -> None:
executed = self.process_manager.executed_process
if executed:
self.process_manager.prepared_processes.append(executed)
self.process_manager.execute_process(process)
self.expropiated_process = process
self.execute_simulation(self.simulating_switch, None)
print("^ expropiate:", process.__str__(True))
self.process_manager.execute_process(process)
self.expropiated_process = process
self.execute_simulation(self.simulating_switch, None)
# __________________________________________________________________________
def complete_execution(self, _) -> bool:
# Execute until complete, then deactivate
executed_process = self.process_manager.executed_process
if executed_process:
executed_process.progress += executed_process.advance
print("* execute:", executed_process.__str__(True))
if executed_process.progress >= 1:
self.deactivate_process_action()
del self.expropiated_process
self.expropiated_process = None
self.execute_simulation(self.simulating_switch, None)
self.update_components()
return self.expropiated_process != None
if __name__ == "__main__":
os.system('clear')
ProcessManagerWindow().show_all()
Gtk.main()