From 5a47a56d1b92a724910fcf252785b29293b9532f Mon Sep 17 00:00:00 2001 From: Hidde Bultsma Date: Fri, 20 May 2016 08:08:09 +0200 Subject: [PATCH] Replace colorclass with colorama, fixes #28 --- requirements.txt | 4 +- tortilla/wrappers.py | 92 ++++++++++++++++++++++---------------------- 2 files changed, 46 insertions(+), 50 deletions(-) diff --git a/requirements.txt b/requirements.txt index f7bc4ea..a79c80b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ -argparse==1.2.1 -colorclass==1.1.1 +colorama==0.3.7 formats==0.1.0a3 httpretty==0.8.3 pkginfo==1.1 @@ -7,4 +6,3 @@ requests==2.5.0 six==1.8.0 twine==1.3.1 urllib3==1.7.1 -wheel==0.24.0 diff --git a/tortilla/wrappers.py b/tortilla/wrappers.py index c533131..209f9ad 100644 --- a/tortilla/wrappers.py +++ b/tortilla/wrappers.py @@ -5,63 +5,62 @@ import os import time -import colorclass import requests import six +from colorama import Fore, Style, init as init_colorama from .cache import CacheWrapper, DictCache from .utils import formats, run_from_ipython, Bunch, bunchify debug_messages = { - 'request': ( - '{blue}Executing {method} request:{/blue}\n' - '{hiblack}' - ' URL: {url}\n' - ' headers: {headers}\n' - ' query: {params}\n' - ' data: {data}\n' - '{/hiblack}' - ), - 'success_response': ( - '{green}Got {status_code} {reason}:{/green}\n' - '{hiblack}' - ' {text}\n' - '{/hiblack}' - ), - 'failure_response': ( - '{red}Got {status_code} {reason}:{/red}\n' - '{hiblack}' - ' {text}\n' - '{/hiblack}' - ), - 'cached_response': ( - '{cyan}Cached response:{/cyan}\n' - '{hiblack}' - ' {text}\n' - '{/hiblack}' - ), - 'incorrect_format_response': ( - '{red}Got {status_code} {reason} (not {format}):{/red}\n' - '{hiblack}' - ' {text}\n' - '{/hiblack}' - ) + 'request': ''.join([ + Fore.BLUE, 'Executing {method} request:\n', + Fore.BLACK, Style.BRIGHT, + ' URL: {url}\n', + ' headers: {headers}\n', + ' query: {params}\n', + ' data: {data}\n', + Style.RESET_ALL + ]), + 'success_response': ''.join([ + Fore.GREEN, 'Got {status_code} {reason}:\n', + Fore.BLACK, Style.BRIGHT, + ' {text}\n', + Style.RESET_ALL + ]), + 'failure_response': ''.join([ + Fore.RED, 'Got {status_code} {reason}:\n', + Fore.BLACK, Style.BRIGHT, + ' {text}\n', + Style.RESET_ALL + ]), + 'cached_response': ''.join([ + Fore.CYAN, 'Cached response:\n', + Fore.BLACK, Style.BRIGHT, + ' {text}\n', + Style.RESET_ALL + ]), + 'incorrect_format_response': ''.join([ + Fore.RED, 'Got {status_code} {reason} (not {format}):\n', + Fore.BLACK, Style.BRIGHT, + ' {text}\n', + Style.RESET_ALL + ]) } DEBUG_MAX_TEXT_LENGTH = 100 -if os.name == 'nt': - if run_from_ipython(): - # IPython stops working properly when it loses control of - # `stdout` on Windows. In this case we won't enable Windows - # color support and we'll strip out all colors from the debug - # messages. - colorclass.disable_all_colors() - else: - colorclass.Windows.enable() +if os.name == 'nt' and run_from_ipython(): + # IPython stops working properly when it loses control of + # `stdout` on Windows. In this case we won't enable Windows + # color support and we'll strip out all colors from the debug + # messages. + init_colorama(wrap=False) +else: + init_colorama() class Client(object): @@ -76,8 +75,8 @@ def __init__(self, debug=False, cache=None): self._last_request_time = None def _log(self, message, debug=None, **kwargs): - """Outputs a colored and formatted message in the console - if the debug mode is activated. + """Outputs a formatted message in the console if the + debug mode is activated. :param message: the message that will be printed :param debug: (optional) Overwrite of `Client.debug` @@ -88,8 +87,7 @@ def _log(self, message, debug=None, **kwargs): if debug is not None: display_log = debug if display_log: - colored_message = colorclass.Color(message) - print(colored_message.format(**kwargs)) + print(message.format(**kwargs)) def request(self, method, url, path=(), extension=None, suffix=None, params=None, headers=None, data=None, debug=None,