replace most the print with msger module
[tools/mic.git] / mic / msger.py
1 #!/usr/bin/python -tt
2 # vim: ai ts=4 sts=4 et sw=4
3
4 #    Copyright (c) 2009 Intel Corporation
5 #
6 #    This program is free software; you can redistribute it and/or modify it
7 #    under the terms of the GNU General Public License as published by the Free
8 #    Software Foundation; version 2 of the License
9 #
10 #    This program is distributed in the hope that it will be useful, but
11 #    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 #    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 #    for more details.
14 #
15 #    You should have received a copy of the GNU General Public License along
16 #    with this program; if not, write to the Free Software Foundation, Inc., 59
17 #    Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 import os,sys
20 import re
21
22 __ALL__ = ['set_mode', 'info', 'warning', 'error', 'ask']
23
24 # COLORs in ANSI
25 INFO_COLOR = 32 # green
26 WARN_COLOR = 33 # yellow
27 ERR_COLOR  = 31 # red
28 ASK_COLOR  = 34 # blue
29 NO_COLOR = 0
30
31 PREFIX_RE = re.compile('^<(.*?)>\s*(.*)')
32
33 INTERACTIVE = True
34
35 def _color_print(head, color, msg = None, stream = sys.stdout):
36
37     colored = True
38     if color == NO_COLOR or \
39        not stream.isatty() or \
40        os.getenv('ANSI_COLORS_DISABLED') is not None:
41         colored = False
42
43     if head.startswith('\r'):
44         # need not \n at last
45         newline = False
46     else:
47         newline = True
48
49     if colored:
50         head = '\033[%dm%s:\033[0m ' %(color, head)
51         if not newline:
52             # ESC cmd to clear line
53             head = '\033[2K' + head
54     else:
55         if head:
56             head += ': '
57
58     if msg:
59         stream.write('%s%s' % (head, msg))
60         if newline:
61             stream.write('\n')
62     else:
63         stream.write('%s ' % head)
64
65     stream.flush()
66
67 def _color_perror(head, color, msg):
68     _color_print(head, color, msg, sys.stderr)
69
70 def _split_msg(head, msg):
71     if msg.startswith('\n'):
72         # means print \n at first
73         msg = msg.lstrip()
74         head = '\n' + head
75
76     elif msg.startswith('\r'):
77         # means print \r at first
78         msg = msg.lstrip()
79         head = '\r' + head
80
81     m = PREFIX_RE.match(msg)
82     if m:
83         head += ' <%s>' % m.group(1)
84         msg = m.group(2)
85
86     return head, msg
87
88 def set_mode(interactive):
89     global INTERACTIVE
90     if interactive:
91         INTERACTIVE = True
92     else:
93         INTERACTIVE = False
94
95 def raw(msg):
96     head, msg = _split_msg('', msg)
97     _color_print(head, NO_COLOR, msg)
98
99 def info(msg):
100     head, msg = _split_msg('Info', msg)
101     _color_print(head, INFO_COLOR, msg)
102
103 def warning(msg):
104     head, msg = _split_msg('Warning', msg)
105     _color_perror(head, WARN_COLOR, msg)
106
107 def error(msg):
108     head, msg = _split_msg('Error', msg)
109     _color_perror(head, ERR_COLOR, msg)
110     sys.exit(1)
111
112 def ask(msg, default=True):
113     _color_print('Q', ASK_COLOR, '')
114     try:
115         if default:
116             msg += '(Y/n) '
117         else:
118             msg += '(y/N) '
119         if INTERACTIVE:
120             repl = raw_input(msg)
121             if repl.lower() == 'y':
122                 return True
123             elif repl.lower() == 'n':
124                 return False
125             else:
126                 return default
127
128         else:
129             sys.stdout.write('%s ' % msg)
130             if default:
131                 sys.stdout.write('Y\n')
132             else:
133                 sys.stdout.write('N\n')
134             return default
135     except KeyboardInterrupt:
136         sys.stdout.write('\n')
137         sys.exit(2)