2 # vim: ai ts=4 sts=4 et sw=4
4 # Copyright 2009, 2010, 2011 Intel, Inc.
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.
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.
24 __ALL__ = ['set_mode', 'set_loglevel', 'raw' 'debug', 'verbose', 'info', 'warning', 'error', 'ask', 'pause']
27 INFO_COLOR = 32 # green
28 WARN_COLOR = 33 # yellow
33 PREFIX_RE = re.compile('^<(.*?)>\s*(.*)')
45 def _color_print(head, color, msg = None, stream = sys.stdout, level = 'normal'):
47 if LOG_LEVELS[level] > LOG_LEVEL:
52 if color == NO_COLOR or \
53 not stream.isatty() or \
54 os.getenv('ANSI_COLORS_DISABLED') is not None:
57 if head.startswith('\r'):
64 head = '\033[%dm%s:\033[0m ' %(color, head)
66 # ESC cmd to clear line
67 head = '\033[2K' + head
71 if head.startswith('\r'):
76 stream.write('%s%s' % (head, msg))
82 def _color_perror(head, color, msg, level = 'normal'):
83 _color_print(head, color, msg, sys.stderr, level)
85 def _split_msg(head, msg):
86 if isinstance(msg, list):
87 msg = '\n'.join(map(str, msg))
89 if msg.startswith('\n'):
90 # means print \n at first
94 elif msg.startswith('\r'):
95 # means print \r at first
99 m = PREFIX_RE.match(msg)
101 head += ' <%s>' % m.group(1)
106 def set_loglevel(level):
108 if level not in LOG_LEVELS:
112 LOG_LEVEL = LOG_LEVELS[level]
114 def set_mode(interactive):
124 sys.stdout.write(msg)
125 sys.stdout.write('\n')
128 head, msg = _split_msg('Info', msg)
129 _color_print(head, INFO_COLOR, msg)
132 head, msg = _split_msg('Verbose', msg)
133 _color_print(head, INFO_COLOR, msg, level = 'verbose')
136 head, msg = _split_msg('Warning', msg)
137 _color_perror(head, WARN_COLOR, msg)
140 head, msg = _split_msg('Debug', msg)
141 _color_perror(head, ERR_COLOR, msg, level = 'debug')
144 head, msg = _split_msg('Error', msg)
145 _color_perror(head, ERR_COLOR, msg)
148 def ask(msg, default=True):
149 _color_print('Q', ASK_COLOR, '')
157 repl = raw_input(msg)
158 if repl.lower() == 'y':
160 elif repl.lower() == 'n':
162 elif not repl.strip():
168 sys.stdout.write('%s ' % msg)
170 sys.stdout.write('Y\n')
172 sys.stdout.write('N\n')
174 except KeyboardInterrupt:
175 sys.stdout.write('\n')
180 _color_print('Q', ASK_COLOR, '')
182 msg = 'press <ENTER> to continue ...'