Extract color output handling for reuse

Move the colorize stuff from the reporter to utils, use it in
VerboseHttp.

What's there now is merely a demonstration of capabilities, to be
resolved by further discussion.
This commit is contained in:
Chris Dent 2015-08-10 20:43:18 +01:00
parent 9e1172fc83
commit 2b0df54fb8
3 changed files with 30 additions and 17 deletions

View File

@ -38,15 +38,15 @@ class VerboseHttp(httplib2.Http):
self._show_headers = kwargs.pop('headers')
self._use_color = kwargs.pop('colorize')
self._stream = kwargs.pop('stream')
if not self._stream.isatty():
self._use_color = False
if self._use_color:
self.colorize = utils.get_colorizer(self._stream)
super(VerboseHttp, self).__init__(**kwargs)
def _request(self, conn, host, absolute_uri, request_uri, method, body,
headers, redirections, cachekey):
"""Display request parameters before requesting."""
self._verbose_output('#### %s ####' % self.caption)
self._verbose_output('#### %s ####' % self.caption, color='BLUE')
self._verbose_output('%s %s' % (method, request_uri),
prefix=self.REQUEST_PREFIX)
self._verbose_output('Host: %s' % host,
@ -91,6 +91,8 @@ class VerboseHttp(httplib2.Http):
stream = stream or self._stream
if prefix and message:
print(prefix, end=' ', file=self._stream)
if color:
message = self.colorize(color, message)
print(message, file=self._stream)
@ -99,7 +101,7 @@ def get_http(verbose=False, caption=''):
if verbose:
body = True
headers = True
colorize = False
colorize = True
stream = sys.stdout
if verbose == 'body':
headers = False

View File

@ -15,7 +15,7 @@
from unittest import TextTestResult
from unittest import TextTestRunner
import colorama
from gabbi import utils
class ConciseTestResult(TextTestResult):
@ -23,11 +23,7 @@ class ConciseTestResult(TextTestResult):
def __init__(self, stream, descriptions, verbosity):
super(ConciseTestResult, self).__init__(
stream, descriptions, verbosity)
if stream.isatty():
colorama.init()
self.colorize = _colorize
else:
self.colorize = lambda x, y: y
self.colorize = utils.get_colorizer(stream)
def startTest(self, test):
super(TextTestResult, self).startTest(test)
@ -99,10 +95,3 @@ class ConciseTestResult(TextTestResult):
class ConciseTestRunner(TextTestRunner):
resultclass = ConciseTestResult
def _colorize(color, message):
try:
return getattr(colorama.Fore, color) + message + colorama.Fore.RESET
except AttributeError:
return message

View File

@ -12,6 +12,8 @@
# under the License.
"""Utility functions grab bag."""
import colorama
try: # Python 3
ConnectionRefused = ConnectionRefusedError
@ -52,6 +54,18 @@ def extract_content_type(header_dict):
return (content_type, charset)
def get_colorizer(stream):
"""Return a function to colorize a string.
Only if stream is a tty .
"""
if stream.isatty():
colorama.init()
return _colorize
else:
return lambda x, y: y
def not_binary(content_type):
"""Decide if something is content we'd like to treat as a string."""
return (content_type.startswith('text/') or
@ -59,3 +73,11 @@ def not_binary(content_type):
content_type.endswith('+json') or
content_type == 'application/javascript' or
content_type.startswith('application/json'))
def _colorize(color, message):
"""Add a color to the message."""
try:
return getattr(colorama.Fore, color) + message + colorama.Fore.RESET
except AttributeError:
return message