Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 import sys
00012
00013
00014
00015
00016
00017
00018 def console_has_colours(stream):
00019 if not hasattr(stream, "isatty"):
00020 return False
00021 if not stream.isatty():
00022 return False
00023 try:
00024 import curses
00025 curses.setupterm()
00026 return curses.tigetnum("colors") > 2
00027 except:
00028
00029 return False
00030
00031 has_colours = console_has_colours(sys.stdout)
00032
00033 reset = "\x1b[0m"
00034
00035 bold = '1'
00036 black, red, green, yellow, blue, magenta, cyan, white = [str(i) for i in range(30, 38)]
00037 bold_black, bold_red, bold_green, bold_yellow, bold_blue, bold_magenta, bold_cyan, bold_white = ['1;' + str(i) for i in range(30, 38)]
00038 colours = [
00039 bold,
00040 black, red, green, yellow, blue, magenta, cyan, white,
00041 bold_black, bold_red, bold_green, bold_yellow, bold_blue, bold_magenta, bold_cyan, bold_white
00042 ]
00043
00044
00045 def pretty_print(msg, colour=white):
00046 if has_colours:
00047 seq = "\x1b[%sm" % (colour) + msg + reset
00048 sys.stdout.write(seq)
00049 else:
00050 sys.stdout.write(msg)
00051
00052
00053
00054
00055
00056
00057 def debug(msg):
00058 pretty_print("%s\n" % msg, green)
00059
00060
00061 def warning(msg):
00062 pretty_print("%s\n" % msg, yellow)
00063
00064
00065 def error(msg):
00066 pretty_print("%s\n" % msg, red)
00067
00068
00069 def logdebug(message):
00070 pretty_print("[debug] " + message + "\n", green)
00071
00072
00073 def logwarn(message):
00074 pretty_print("[warning] " + message + "\n", yellow)
00075
00076
00077 def logerror(message):
00078 pretty_print("[error] " + message + "\n", red)
00079
00080
00081 def logfatal(message):
00082 pretty_print("[error] " + message + "\n", bold_red)
00083
00084
00085
00086
00087
00088
00089 if __name__ == '__main__':
00090 for colour in colours:
00091 pretty_print("dude\n", colour)
00092 logdebug("info message")
00093 logwarn("warning message")
00094 logerror("error message")
00095 logfatal("fatal message")
00096 pretty_print("red\n", red)
00097 print("some normal text")