-
Notifications
You must be signed in to change notification settings - Fork 21
/
handlers.py
57 lines (44 loc) · 1.59 KB
/
handlers.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
#!/usr/bin/env python
import os
import time
import readline
from signal import signal, SIGINT
from mmwave_gesture.utils import warning
class SignalHandler:
def __init__(self, queue, timeout=1):
self.queue = queue
self.timeout = timeout
self.signal_cnt = 0
self.max_signal_cnt = 3
self.detected_time = -1
self.exit_state = False
signal(SIGINT, self.ctrl_c_handler)
def check_timeout(self):
timeout = True
limit = 3*self.timeout if self.exit_state else self.timeout
if time.perf_counter() - self.detected_time < limit:
timeout = False
self.detected_time = time.perf_counter()
return timeout
def ctrl_c_handler(self, signal_received, frame):
if not self.check_timeout():
if self.exit_state:
os._exit(1)
self.signal_cnt += 1
if self.signal_cnt >= self.max_signal_cnt - 1:
self.exit_state = True
warning('\nPress <Ctrl-C> one more time to exit '
f'({3*self.timeout} sec left)\n')
else:
self.signal_cnt = 0
self.exit_state = False
self.queue.put(signal_received)
class Completer(object):
def __init__(self, complete_list):
def list_completer(text, state):
line = readline.get_line_buffer()
if not line:
return [f'{c} ' for c in complete_list][state]
else:
return [f'{c} ' for c in complete_list if c.startswith(line)][state]
self.list_completer = list_completer