Go to the documentation of this file.00001 from __future__ import print_function
00002 import sys
00003
00004
00005 _ansi = {'red': 91, 'yellow': 93}
00006
00007 def is_tty(stream):
00008 """Returns True if the given stream is a tty, else False"""
00009 return hasattr(stream, 'isatty') and stream.isatty()
00010
00011
00012 def colorize(msg, color, file=sys.stderr, alt_text=None):
00013 if color and is_tty(file):
00014 return '\033[%dm%s\033[0m' % (_ansi[color], msg)
00015 elif alt_text:
00016 return '%s%s' % (alt_text, msg)
00017 else:
00018 return msg
00019
00020
00021 def message(msg, *args, **kwargs):
00022 file = kwargs.get('file', sys.stderr)
00023 alt_text = kwargs.get('alt_text', None)
00024 color = kwargs.get('color', None)
00025 print(colorize(msg, color, file, alt_text), *args, file=file)
00026
00027
00028 def warning(*args, **kwargs):
00029 defaults = dict(file=sys.stderr, alt_text='warning: ', color='yellow')
00030 defaults.update(kwargs)
00031 message(*args, **defaults)
00032
00033
00034 def error(*args, **kwargs):
00035 defaults = dict(file=sys.stderr, alt_text='error: ', color='red')
00036 defaults.update(kwargs)
00037 message(*args, **defaults)