-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommunicator.py
461 lines (394 loc) · 16.5 KB
/
communicator.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
## Dependencies: python-mechanize, python-keyring, curses
## Author: Gijs Timmers: https://github.com/GijsTimmers
## Contributors: Gijs Timmers: https://github.com/GijsTimmers
## Jo Van Bulck: https://github.com/jovanbulck
##
## Licence: GPLv3
##
## kotnetcli is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## kotnetcli is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with kotnetcli. If not, see <http://www.gnu.org/licenses/>.
import re ## Basislib voor reguliere expressies
import time ## Voor timeout om venster te sluiten
import sys ## Basislib
import os ## Basislib
from colorama import ( ## Om de tekst kleur te geven
Fore, ##
Style, ##
init as colorama_init) ##
if os.name == "posix":
try:
import curses ## Voor tekenen op scherm.
except ImportError:
print "Couldn't import the curses library."
pass
try:
import notify2 ## OS-specifieke notificaties
except ImportError:
print "Couldn't import the notify2 library."
pass
try:
from dialog import Dialog ## Voor tekenen op scherm.
except ImportError:
print "Couldn't import the dialog library."
pass
if os.name == "nt":
print "Windows system detected. Will not import curses, notify and dialog"
class QuietCommunicator():
def __init__(self):
pass
def eventPingSuccess(self):
pass
def eventPingFailure(self):
pass
def eventPingAlreadyOnline(self):
pass
def eventNetloginStart(self):
pass
def eventNetloginSuccess(self):
pass
def eventNetloginFailure(self):
pass
def eventKuleuvenStart(self):
pass
def eventKuleuvenSuccess(self):
pass
def eventKuleuvenFailure(self):
pass
def eventInvoerenStart(self):
pass
def eventInvoerenSuccess(self):
pass
def eventInvoerenFailure(self):
pass
def eventOpsturenStart(self):
pass
def eventOpsturenSuccess(self):
pass
def eventOpsturenFailure(self):
pass
def eventTegoedenBekend(self, downloadpercentage, uploadpercentage):
pass
def beeindig_sessie(self, error_code=0):
pass
class BubbleCommunicator(QuietCommunicator):
def __init__(self):
notify2.init("kotnetcli")
def eventTegoedenBekend(self, downloadpercentage, uploadpercentage):
n = notify2.Notification("kotnetcli", \
"Download: %s%%, Upload: %s%%" % \
(downloadpercentage, uploadpercentage), \
"notification-network-ethernet-connected")
n.show()
class DialogCommunicator(QuietCommunicator):
def __init__(self):
# some constant definitions to avoid using magic numbers
# for the DialogCommunicator mixedgauge dialog
self.WAIT = 7
self.DONE = 0
self.FAIL = 1
self.d = Dialog(dialog="dialog")
self.d.set_background_title("kotnetcli")
self.netlogin = self.WAIT
self.kuleuven = self.WAIT
self.invoeren = self.WAIT
self.opsturen = self.WAIT
self.download = self.WAIT
self.upload = self.WAIT
self.overal = 0
self.update()
def update(self):
self.d.mixedgauge("",
title="kotnetcli",
percent= self.overal,
elements= [ ("Netlogin openen", self.netlogin),
("KU Leuven kiezen", self.kuleuven),
("Gegevens invoeren", self.invoeren),
("Gegevens opsturen", self.opsturen),
("", ""),
("Download", self.download),
("Upload", self.upload)
])
def eventPingFailure(self):
self.d.infobox("Niet verbonden met het KU Leuven-netwerk.", 5, 30)
def eventPingAlreadyOnline(self):
self.d.infobox("U bent al online.", 5, 30)
def eventNetloginSuccess(self):
self.netlogin = self.DONE
self.overal = 40
self.update()
def eventNetloginFailure(self):
self.netlogin = self.FAIL
self.overal = 40
self.update()
def eventKuleuvenSuccess(self):
self.kuleuven = self.DONE
self.overal = 60
self.update()
def eventKuleuvenFailure(self):
self.kuleuven = self.FAIL
self.overal = 60
self.update()
def eventInvoerenSuccess(self):
self.invoeren = self.DONE
self.overal = 80
self.update()
def eventInvoerenFailure(self):
self.invoeren = self.FAIL
self.overal = 80
self.update()
def eventOpsturenSuccess(self):
self.opsturen = self.DONE
self.overal = 100
self.update()
def eventOpsturenFailure(self):
self.opsturen = self.FAIL
self.overal = 100
self.update()
def eventTegoedenBekend(self, downloadpercentage, uploadpercentage):
self.download = -downloadpercentage
self.upload = -uploadpercentage
self.overal = 100
self.update()
def beeindig_sessie(self, error_code=0):
print "" # print newline to clean prompt under dialog
class SummaryCommunicator(QuietCommunicator):
def eventPingFailure(self):
print "Niet verbonden met het KU Leuven-netwerk."
def eventPingAlreadyOnline(self):
print "U bent al online."
def eventTegoedenBekend(self, downloadpercentage, uploadpercentage):
print "Download: " + str(downloadpercentage) + "%" + ",",
print "Upload: " + str(uploadpercentage) + "%"
class ColoramaCommunicator(QuietCommunicator):
def __init__(self):
colorama_init()
if os.name == "posix":
## Hide the terminal cursor using ANSI escape codes
sys.stdout.write("\033[?25l")
sys.stdout.flush()
def eventPingFailure(self):
print Style.BRIGHT + Fore.RED + \
"Niet verbonden met het KU Leuven-netwerk." + \
Style.RESET_ALL + Fore.RESET
def eventPingAlreadyOnline(self):
print Style.BRIGHT + Fore.YELLOW + \
"U bent al online." + \
Fore.RESET + Style.RESET_ALL
def eventNetloginStart(self):
print "Netlogin openen....... " + Style.BRIGHT + "[" + Fore.YELLOW + \
"WAIT" + Fore.RESET + "]" + Style.RESET_ALL + "\b\b\b\b\b\b\b",
sys.stdout.flush()
def eventNetloginSuccess(self):
print Style.BRIGHT + "[" + Fore.GREEN + " OK " + \
Fore.RESET + "]" + Style.RESET_ALL
def eventNetloginFailure(self):
print Style.BRIGHT + "[" + Fore.RED + "FAIL" + \
Fore.RESET + "]" + Style.RESET_ALL
def eventKuleuvenStart(self):
print "KU Leuven kiezen...... " + Style.BRIGHT + "[" + Fore.YELLOW + \
"WAIT" + Fore.RESET + "]" + Style.RESET_ALL + "\b\b\b\b\b\b\b",
sys.stdout.flush()
def eventKuleuvenSuccess(self):
print Style.BRIGHT + "[" + Fore.GREEN + " OK " + \
Fore.RESET + "]" + Style.RESET_ALL
def eventKuleuvenFailure(self):
print Style.BRIGHT + "[" + Fore.RED + "FAIL" + \
Fore.RESET + "]" + Style.RESET_ALL
def eventInvoerenStart(self):
print "Gegevens invoeren..... " + Style.BRIGHT + "[" + Fore.YELLOW + \
"WAIT" + Fore.RESET + "]" + Style.RESET_ALL + "\b\b\b\b\b\b\b",
sys.stdout.flush()
def eventInvoerenSuccess(self):
print Style.BRIGHT + "[" + Fore.GREEN + " OK " + \
Fore.RESET + "]" + Style.RESET_ALL
def eventInvoerenFailure(self):
print Style.BRIGHT + "[" + Fore.RED + "FAIL" + \
Fore.RESET + "]" + Style.RESET_ALL
def eventOpsturenStart(self):
print "Gegevens opsturen..... " + Style.BRIGHT + "[" + Fore.YELLOW + \
"WAIT" + Fore.RESET + "]" + Style.RESET_ALL + "\b\b\b\b\b\b\b",
sys.stdout.flush()
def eventOpsturenSuccess(self):
print Style.BRIGHT + "[" + Fore.GREEN + " OK " + \
Fore.RESET + "]" + Style.RESET_ALL
def eventOpsturenFailure(self):
print Style.BRIGHT + "[" + Fore.RED + "FAIL" + \
Fore.RESET + "]" + Style.RESET_ALL
def eventTegoedenBekend(self, downloadpercentage, uploadpercentage):
print "Download: " + Style.BRIGHT + "[ ][ ]" + \
Style.RESET_ALL + "\r",
balkgetal_download = int(round(float(downloadpercentage) / 10.0))
if downloadpercentage <= 10:
voorwaardelijke_kleur_download = \
Fore.RED
elif 10 < downloadpercentage < 60:
voorwaardelijke_kleur_download = \
Fore.YELLOW
else:
voorwaardelijke_kleur_download = \
Fore.GREEN
print "Download: " + \
Style.BRIGHT + "[" + voorwaardelijke_kleur_download + \
"=" * balkgetal_download + Fore.RESET + \
" " * (10-balkgetal_download) + \
"]" + \
"[" + \
" " * (3 - len(str(downloadpercentage))) + \
voorwaardelijke_kleur_download + str(downloadpercentage) + \
"%" + Fore.RESET + \
"]" + Style.RESET_ALL
print "Upload: " + Style.BRIGHT + "[ ][ ]" + \
Style.RESET_ALL + "\r",
balkgetal_upload = int(round(float(uploadpercentage) / 10.0))
if uploadpercentage <= 10:
voorwaardelijke_kleur_upload = \
Fore.RED
elif 10 < uploadpercentage < 60:
voorwaardelijke_kleur_upload = \
Fore.YELLOW
else:
voorwaardelijke_kleur_upload = \
Fore.GREEN
print "Upload: " + \
Style.BRIGHT + "[" + voorwaardelijke_kleur_upload + \
"=" * balkgetal_upload + Fore.RESET + \
" " * (10-balkgetal_upload) + \
"]" + \
"[" + \
" " * (3 - len(str(uploadpercentage))) + \
voorwaardelijke_kleur_upload + str(uploadpercentage) + \
"%" + Fore.RESET + \
"]" + Style.RESET_ALL
def beeindig_sessie(self, error_code=0):
if os.name == "posix":
## re-display the terminal cursor using ANSI escape codes
sys.stdout.write("\033[?25h")
sys.stdout.flush()
else:
time.sleep(3)
class PlaintextCommunicator(ColoramaCommunicator):
def __init__(self):
Style.BRIGHT = ""
Style.RESET = ""
Fore.GREEN = ""
Fore.YELLOW = ""
Fore.RED = ""
class CursesCommunicator():
def __init__(self):
self.scherm = curses.initscr()
curses.curs_set(0) ## cursor invisible
curses.start_color() ## Kleuren aanmaken
curses.use_default_colors()
curses.init_pair(1, 1, -1) ## Paren aanmaken: ndz vr curses.
curses.init_pair(2, 2, -1) ## Ik heb de curses-conventie
curses.init_pair(3, 3, -1) ## aangehouden, 1 is dus rood,
## 2 is groen, 3 is geel.
self.tekstKleurRood = curses.color_pair(1)
self.tekstKleurGroen = curses.color_pair(2)
self.tekstKleurGeel = curses.color_pair(3)
self.tekstOpmaakVet = curses.A_BOLD
self.tekstKleurRoodOpmaakVet = curses.color_pair(1) | curses.A_BOLD
self.tekstKleurGroenOpmaakVet = curses.color_pair(2) | curses.A_BOLD
self.tekstKleurGeelOpmaakVet = curses.color_pair(3) | curses.A_BOLD
self.scherm.addstr(0, 0, "Netlogin openen.......")
self.scherm.addstr(0, 22, "[ ]", self.tekstOpmaakVet)
self.scherm.addstr(1, 0, "KU Leuven kiezen......")
self.scherm.addstr(1, 22, "[ ]", self.tekstOpmaakVet)
self.scherm.addstr(2, 0, "Gegevens invoeren.....")
self.scherm.addstr(2, 22, "[ ]", self.tekstOpmaakVet)
self.scherm.addstr(3, 0, "Gegevens opsturen.....")
self.scherm.addstr(3, 22, "[ ]", self.tekstOpmaakVet)
self.scherm.addstr(4, 0, "Download:")
self.scherm.addstr(4, 10, "[ ][ ]", self.tekstOpmaakVet)
self.scherm.addstr(5, 0, "Upload:")
self.scherm.addstr(5, 10, "[ ][ ]", self.tekstOpmaakVet)
self.scherm.refresh()
def kprint(self, pos_y, pos_x, tekst, *args):
if args:
self.scherm.addstr(pos_y, pos_x, tekst, args[0])
self.scherm.refresh()
else:
self.scherm.addstr(pos_y, pos_x, tekst)
self.scherm.refresh()
def eventPingSuccess(self):
pass
def eventPingFailure(self):
self.kprint(6, 0, "Niet verbonden met het KU Leuven-netwerk.", \
self.tekstKleurRoodOpmaakVet)
def eventPingAlreadyOnline(self):
self.kprint(6, 0, "U bent al online.", \
self.tekstKleurGeelOpmaakVet)
def eventNetloginStart(self):
self.kprint(0, 23, "WAIT", self.tekstKleurGeelOpmaakVet)
def eventNetloginSuccess(self):
self.kprint(0, 23, " OK ", self.tekstKleurGroenOpmaakVet)
def eventNetloginFailure(self):
self.kprint(0, 23, "FAIL", self.tekstKleurRoodOpmaakVet)
def eventKuleuvenStart(self):
self.kprint(1, 23, "WAIT", self.tekstKleurGeelOpmaakVet)
def eventKuleuvenSuccess(self):
self.kprint(1, 23, " OK ", self.tekstKleurGroenOpmaakVet)
def eventKuleuvenFailure(self):
self.kprint(1, 23, "FAIL", self.tekstKleurRoodOpmaakVet)
def eventInvoerenStart(self):
self.kprint(2, 23, "WAIT", self.tekstKleurGeelOpmaakVet)
def eventInvoerenSuccess(self):
self.kprint(2, 23, " OK ", self.tekstKleurGroenOpmaakVet)
def eventInvoerenFailure(self):
self.kprint(2, 23, "FAIL", self.tekstKleurRoodOpmaakVet)
def eventOpsturenStart(self):
self.kprint(3, 23, "WAIT", self.tekstKleurGeelOpmaakVet)
def eventOpsturenSuccess(self):
self.kprint(3, 23, " OK ", self.tekstKleurGroenOpmaakVet)
def eventOpsturenFailure(self):
self.kprint(3, 23, "FAIL", self.tekstKleurRoodOpmaakVet)
def eventTegoedenBekend(self, downloadpercentage, uploadpercentage):
balkgetal_download = int(round(float(downloadpercentage) / 10.0))
if downloadpercentage <= 10:
voorwaardelijke_kleur_download = \
self.tekstKleurRoodOpmaakVet
elif 10 < downloadpercentage < 60:
voorwaardelijke_kleur_download = \
self.tekstKleurGeelOpmaakVet
else:
voorwaardelijke_kleur_download = \
self.tekstKleurGroenOpmaakVet
self.kprint(4, 23, " " * (3 - len(str(downloadpercentage))) + \
str(downloadpercentage) + \
"%", voorwaardelijke_kleur_download)
self.kprint(4, 11, "=" * balkgetal_download + \
" " * (10-balkgetal_download), voorwaardelijke_kleur_download)
balkgetal_upload = \
int(round(float(uploadpercentage) / 10.0))
if uploadpercentage <= 10:
voorwaardelijke_kleur_upload = \
self.tekstKleurRoodOpmaakVet
elif 10 < uploadpercentage < 60:
voorwaardelijke_kleur_upload = \
self.tekstKleurGeelOpmaakVet
else:
voorwaardelijke_kleur_upload = \
self.tekstKleurGroenOpmaakVet
self.kprint(5, 23, " " * (3 - len(str(uploadpercentage))) + \
str(uploadpercentage) + \
"%", voorwaardelijke_kleur_upload)
self.kprint(5, 11, "=" * balkgetal_upload + \
" " * (10-balkgetal_upload), voorwaardelijke_kleur_upload)
def beeindig_sessie(self, error_code=0):
time.sleep(2)
curses.nocbreak()
self.scherm.keypad(0)
curses.echo()
curses.endwin()