Allow envivonment variables to set verbose color

This is starting to get a bit out of hand now, but is a useful
feature. That is, if we're going to have the general feature of
colored output, then being able to choose, from the calling harness
(often shell scripts) is useful.

Probably useful to merge this soon so that it is out there for
feedback.
This commit is contained in:
Chris Dent 2015-08-19 17:08:34 +01:00
parent ab4e0d5804
commit 8c13ab59d4

View File

@ -14,6 +14,7 @@
from __future__ import print_function from __future__ import print_function
import os
import sys import sys
import httplib2 import httplib2
@ -22,7 +23,24 @@ from gabbi import utils
class VerboseHttp(httplib2.Http): class VerboseHttp(httplib2.Http):
"""A subclass of Http that verbosely reports on activity.""" """A subclass of Http that verbosely reports on activity.
If the output is a tty or ``GABBI_FORCE_COLOR`` is set in the
environment, then output will be colorized according to ``COLORMAP``.
Output can include request and response headers, request and
response body content (if of a printable content-type), or both.
The color of the output has reasonable defaults. These may be overridden
by setting the following environment variables
* GABBI_CAPTION_COLOR
* GABBI_HEADER_COLOR
* GABBI_REQUEST_COLOR
* GABBI_STATUS_COLOR
to any of: BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE
"""
# A list of request and response headers to never display. # A list of request and response headers to never display.
# Can include httplib2 response object attributes that are not # Can include httplib2 response object attributes that are not
@ -34,10 +52,10 @@ class VerboseHttp(httplib2.Http):
REQUEST_PREFIX = '>' REQUEST_PREFIX = '>'
RESPONSE_PREFIX = '<' RESPONSE_PREFIX = '<'
COLORMAP = { COLORMAP = {
'caption': 'BLUE', 'caption': os.environ.get('GABBI_CAPTION_COLOR', 'BLUE').upper(),
'header': 'YELLOW', 'header': os.environ.get('GABBI_HEADER_COLOR', 'YELLOW').upper(),
'request': 'CYAN', 'request': os.environ.get('GABBI_REQUEST_COLOR', 'CYAN').upper(),
'status': 'CYAN', 'status': os.environ.get('GABBI_STATUS_COLOR', 'CYAN').upper(),
} }
def __init__(self, **kwargs): def __init__(self, **kwargs):