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
|
from __future__ import print_function
import os
import sys
import getpass
import subprocess
try:
import termios
except ImportError:
# Windows does not have a termios module
termios = None
def input(prompt):
try:
return raw_input(prompt)
except NameError:
import builtins
return builtins.input(prompt)
class Colors(object):
def __wrap_with(raw_code):
@staticmethod
def inner(text, bold=False):
code = raw_code
if bold:
code = u"1;{}".format(code)
return u"\033[{}m{}\033[0m".format(code, text)
return inner
red = __wrap_with("31")
green = __wrap_with("32")
yellow = __wrap_with("33")
blue = __wrap_with("34")
magenta = __wrap_with("35")
cyan = __wrap_with("36")
white = __wrap_with("37")
class Screen(object):
@staticmethod
def set_echo(enabled):
if not termios:
return
handle = sys.stdin.fileno()
if not os.isatty(handle):
return
attrs = termios.tcgetattr(handle)
if enabled:
attrs[3] |= termios.ECHO
else:
attrs[3] &= ~termios.ECHO
termios.tcsetattr(handle, termios.TCSANOW, attrs)
@staticmethod
def clear():
sys.stdout.write("\x1b[2J\x1b[H")
sys.stdout.flush()
@staticmethod
def print_error(msg):
print(Colors.red(msg))
@staticmethod
def print_success(msg):
print(Colors.green(msg))
@staticmethod
def get_string(prompt):
while True:
value = input(prompt).strip()
if not value:
print(Colors.red("Value Required!"))
else:
return value
@staticmethod
def get_password(prompt="Password: "):
while True:
value = getpass.getpass(prompt)
if not value:
print(Colors.red("Value Required!"))
else:
return value
@staticmethod
def get_integer(prompt):
"""Gather user input and convert it to an integer
Will keep trying till the user enters an interger or until they ^C the
program.
"""
while True:
try:
return int(input(prompt).strip())
except ValueError:
print(Colors.red("Invalid Input!"))
def clear_screen():
"""Clear the terminal
"""
sys.stdout.write("\x1b[2J\x1b[H")
sys.stdout.flush()
def iterate_forever(func, *args, **kwargs):
"""Iterate over a finite iterator forever
When the iterator is exhausted will call the function again to generate a
new iterator and keep iterating.
"""
output = func(*args, **kwargs)
while True:
try:
playlist_item = next(output)
playlist_item.prepare_playback()
yield playlist_item
except StopIteration:
output = func(*args, **kwargs)
class SilentPopen(subprocess.Popen):
"""A Popen varient that dumps it's output and error
"""
def __init__(self, *args, **kwargs):
self._dev_null = open(os.devnull, "w")
kwargs["stdin"] = subprocess.PIPE
kwargs["stdout"] = subprocess.PIPE
kwargs["stderr"] = self._dev_null
super(SilentPopen, self).__init__(*args, **kwargs)
def __del__(self):
self._dev_null.close()
super(SilentPopen, self).__del__()
|