check the license headers carefully for all files
[platform/upstream/mic.git] / mic / msger.py
1 #!/usr/bin/python -tt
2 # vim: ai ts=4 sts=4 et sw=4
3 #
4 # Copyright 2009, 2010, 2011 Intel, Inc.
5 #
6 # This copyrighted material is made available to anyone wishing to use, modify,
7 # copy, or redistribute it subject to the terms and conditions of the GNU
8 # General Public License v.2.  This program is distributed in the hope that it
9 # will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
10 # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 # See the GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License along with
14 # this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  Any Red Hat
16 # trademarks that are incorporated in the source code or documentation are not
17 # subject to the GNU General Public License and may only be used or replicated
18 # with the express permission of Red Hat, Inc.
19 #
20
21 import os,sys
22 import re
23
24 __ALL__ = ['set_mode', 'set_loglevel', 'raw' 'debug', 'verbose', 'info', 'warning', 'error', 'ask']
25
26 # COLORs in ANSI
27 INFO_COLOR = 32 # green
28 WARN_COLOR = 33 # yellow
29 ERR_COLOR  = 31 # red
30 ASK_COLOR  = 34 # blue
31 NO_COLOR = 0
32
33 PREFIX_RE = re.compile('^<(.*?)>\s*(.*)')
34
35 INTERACTIVE = True
36
37 LOG_LEVELS = {
38                 'quiet': 0,
39                 'normal': 1,
40                 'verbose': 2,
41                 'debug': 3,
42              }
43 LOG_LEVEL = 1
44
45 def _color_print(head, color, msg = None, stream = sys.stdout, level = 'normal'):
46
47     if LOG_LEVELS[level] > LOG_LEVEL:
48         # skip
49         return
50
51     colored = True
52     if color == NO_COLOR or \
53        not stream.isatty() or \
54        os.getenv('ANSI_COLORS_DISABLED') is not None:
55         colored = False
56
57     if head.startswith('\r'):
58         # need not \n at last
59         newline = False
60     else:
61         newline = True
62
63     if colored:
64         head = '\033[%dm%s:\033[0m ' %(color, head)
65         if not newline:
66             # ESC cmd to clear line
67             head = '\033[2K' + head
68     else:
69         if head:
70             head += ': '
71             if head.startswith('\r'):
72                 head = head.lstrip()
73                 newline = True
74
75     if msg:
76         stream.write('%s%s' % (head, msg))
77         if newline:
78             stream.write('\n')
79
80     stream.flush()
81
82 def _color_perror(head, color, msg, level = 'normal'):
83     _color_print(head, color, msg, sys.stderr, level)
84
85 def _split_msg(head, msg):
86     if isinstance(msg, list):
87         msg = '\n'.join(map(str, msg))
88
89     if msg.startswith('\n'):
90         # means print \n at first
91         msg = msg.lstrip()
92         head = '\n' + head
93
94     elif msg.startswith('\r'):
95         # means print \r at first
96         msg = msg.lstrip()
97         head = '\r' + head
98
99     m = PREFIX_RE.match(msg)
100     if m:
101         head += ' <%s>' % m.group(1)
102         msg = m.group(2)
103
104     return head, msg
105
106 def set_loglevel(level):
107     global LOG_LEVEL
108     if level not in LOG_LEVELS:
109         # no effect
110         return
111
112     LOG_LEVEL = LOG_LEVELS[level]
113
114 def set_mode(interactive):
115     global INTERACTIVE
116     if interactive:
117         INTERACTIVE = True
118     else:
119         INTERACTIVE = False
120
121 def raw(msg=None):
122     if msg is None:
123         msg = ''
124     sys.stdout.write(msg)
125     sys.stdout.write('\n')
126
127 def info(msg):
128     head, msg = _split_msg('Info', msg)
129     _color_print(head, INFO_COLOR, msg)
130
131 def verbose(msg):
132     head, msg = _split_msg('Verbose', msg)
133     _color_print(head, INFO_COLOR, msg, level = 'verbose')
134
135 def warning(msg):
136     head, msg = _split_msg('Warning', msg)
137     _color_perror(head, WARN_COLOR, msg)
138
139 def debug(msg):
140     head, msg = _split_msg('Debug', msg)
141     _color_perror(head, ERR_COLOR, msg, level = 'debug')
142
143 def error(msg):
144     head, msg = _split_msg('Error', msg)
145     _color_perror(head, ERR_COLOR, msg)
146     sys.exit(1)
147
148 def ask(msg, default=True):
149     _color_print('Q', ASK_COLOR, '')
150     try:
151         if default:
152             msg += '(Y/n) '
153         else:
154             msg += '(y/N) '
155         if INTERACTIVE:
156             repl = raw_input(msg)
157             if repl.lower() == 'y':
158                 return True
159             elif repl.lower() == 'n':
160                 return False
161             else:
162                 return default
163
164         else:
165             sys.stdout.write('%s ' % msg)
166             if default:
167                 sys.stdout.write('Y\n')
168             else:
169                 sys.stdout.write('N\n')
170             return default
171     except KeyboardInterrupt:
172         sys.stdout.write('\n')
173         sys.exit(2)