Revision dfe11bad qa/qa_utils.py

b/qa/qa_utils.py
42 42
  """
43 43
  global _INFO_SEQ, _WARNING_SEQ, _ERROR_SEQ, _RESET_SEQ
44 44

  
45
  # Don't use colours if stdout isn't a terminal
46
  if not sys.stdout.isatty():
47
    return
48

  
45 49
  try:
46 50
    import curses
47 51
  except ImportError:
......
201 205
  return instances
202 206

  
203 207

  
204
def _PrintWithColor(text, seq):
205
  f = sys.stdout
206

  
207
  if not f.isatty():
208
    seq = None
209

  
210
  if seq:
211
    f.write(seq)
212

  
213
  f.write(text)
214
  f.write("\n")
215

  
216
  if seq:
217
    f.write(_RESET_SEQ)
218

  
219

  
220
def PrintWarning(text):
221
  return _PrintWithColor(text, _WARNING_SEQ)
222

  
223

  
224
def PrintError(text):
225
  return _PrintWithColor(text, _ERROR_SEQ)
208
def _FormatWithColor(text, seq):
209
  if not seq:
210
    return text
211
  return "%s%s%s" % (seq, text, _RESET_SEQ)
226 212

  
227 213

  
228
def PrintInfo(text):
229
  return _PrintWithColor(text, _INFO_SEQ)
214
FormatWarning = lambda text: _FormatWithColor(text, _WARNING_SEQ)
215
FormatError = lambda text: _FormatWithColor(text, _ERROR_SEQ)
216
FormatInfo = lambda text: _FormatWithColor(text, _INFO_SEQ)

Also available in: Unified diff