Skip to content

Commit

Permalink
Replace colorclass with colorama, fixes #28
Browse files Browse the repository at this point in the history
  • Loading branch information
redodo committed May 20, 2016
1 parent 6371043 commit 5a47a56
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 50 deletions.
4 changes: 1 addition & 3 deletions requirements.txt
@@ -1,10 +1,8 @@
argparse==1.2.1
colorclass==1.1.1
colorama==0.3.7
formats==0.1.0a3
httpretty==0.8.3
pkginfo==1.1
requests==2.5.0
six==1.8.0
twine==1.3.1
urllib3==1.7.1
wheel==0.24.0
92 changes: 45 additions & 47 deletions tortilla/wrappers.py
Expand Up @@ -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):
Expand All @@ -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`
Expand All @@ -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,
Expand Down

0 comments on commit 5a47a56

Please sign in to comment.