class UI object def __init__ self verbose False quiet False debug Fals

 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
class UI(object):
def __init__(self, verbose=False, quiet=False, debug=False,
traceback=False):
self.quiet = quiet and not (verbose or debug)
self.verbose = verbose or debug
self.debug = debug
self.traceback = traceback
def write(self, *messages):
for m in messages:
sys.stdout.write(m)
def write_err(self, *messages):
for m in messages:
sys.stderr.write(m)
status = lambda self, *m: not self.quiet and self.write(*m)
note = lambda self, *m: self.verbose and self.write(*m)
warn = lambda self, *m: self.write_err(*m)
debug = lambda self, *m: self.debug and self.write(*m)
def abort(self, message):
raise Abort(message)
def print_exc(self):
if self.traceback:
traceback.print_exc()
return self.traceback
def _readline(self, message=''):
try:
import readline
readline.read_history_file
except Exception:
# windows can raise something other than ImportError
pass
line = raw_input(message)
# When stdin is in binary mode on Windows, it can cause
# raw_input() to emit an extra trailing carriage return
if os.linesep == '\r\n' and line and line[-1] == '\r':
line = line[:-1]
return line
def prompt(self, message, pattern=None, default="y"):
while True:
try:
inp = self._readline(message + ' ')
if not inp and default:
return default
if not pattern or re.match(pattern, inp):
return inp
else:
self.write('unrecognized response\n')
except EOFError:
self.abort('response excepted')