Disable colored output if stdout or stderr is not a TTY

This will make it easy to spread colors everywhere without worrying
about garbage test baselines. It will also clean build logs.

Change-Id: I976b74fe1712ea8e5fbfd4476f4814ae375448eb
TN: minor
This commit is contained in:
Pierre-Marie de Rodat
2016-04-15 12:18:31 +02:00
parent 479b135c62
commit 0314ffb5df

View File

@@ -10,6 +10,7 @@ from copy import copy
from itertools import takewhile
import inspect
import sys
class StructEq(object):
@@ -95,6 +96,22 @@ class Colors:
WARNING = YELLOW
FAIL = RED
@classmethod
def disable_colors(cls):
"""
Reset color codes to empty strings.
"""
for name in dir(cls):
value = getattr(cls, name)
if (all(c.isalpha() for c in name) and
name.upper() == name and
isinstance(value, str)):
setattr(cls, name, '')
if not sys.stdout.isatty() or not sys.stderr.isatty():
Colors.disable_colors()
def col(msg, color):
return "{0}{1}{2}".format(color, msg, Colors.ENDC)