-
Notifications
You must be signed in to change notification settings - Fork 54
/
helpers.py
81 lines (67 loc) · 1.88 KB
/
helpers.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
import os
import signal
import subprocess
import sys
from time import sleep
try:
from queue import Empty
except ImportError:
from Queue import Empty
is_windows = sys.platform == 'win32'
def beep():
"""
Emits a beep sound.
"""
sys.stdout.write('\a')
sys.stdout.flush()
def clear():
"""
Clears the terminal.
"""
subprocess.call('cls' if is_windows else 'clear', shell=True)
def dequeue_all(queue, spool=None):
"""
Empties the specified queue into a list, optionally with spooling.
Spool default is 200 (ms). Set to 0 to disable.
"""
if spool is None:
spool = 200
items = []
while True:
try:
while True:
items.append(queue.get_nowait())
except Empty:
# If spooling, wait a moment and check for new items
if spool:
sleep(spool / 1000.0)
if not queue.empty():
continue
break
return items
def samepath(left, right):
"""
Determines whether two paths are the same.
"""
return (os.path.abspath(os.path.normcase(left)) ==
os.path.abspath(os.path.normcase(right)))
def send_keyboard_interrupt(proc):
"""
Sends a KeyboardInterrupt to the specified child process.
"""
if is_windows:
try:
# Send KeyboardInterrupt to self, and therefore, to child processes
try:
os.kill(0, signal.CTRL_C_EVENT)
except AttributeError:
# Python 2.6 and below
import ctypes
ctypes.windll.kernel32.GenerateConsoleCtrlEvent(0, 0)
# Immediately throws KeyboardInterrupt from the simulated CTRL-C
proc.wait()
except KeyboardInterrupt:
# Ignore the simulated CTRL-C
pass
else:
os.kill(proc.pid, signal.SIGINT)