Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
ARIA-103 Remove dependency on Clint
We no longer require this third-party library, instead the utils/console
module uses the existing cli/color module.

This commit also fixes the cli/color module to properly support Unicode,
and also properly deinitialize Colorama in Windows.
  • Loading branch information
tliron committed Jul 10, 2017
1 parent b30a7ed commit f903006b013fdc9c77b7be42a915dfb72fb16b96
Showing 4 changed files with 52 additions and 27 deletions.
@@ -18,11 +18,20 @@
"""

from StringIO import StringIO
import atexit
import re

import colorama

from ..utils.formatting import safe_str


def _restore_terminal():
colorama.deinit()


colorama.init()
atexit.register(_restore_terminal)


class StringStylizer(object):
@@ -33,20 +42,20 @@ def __init__(self, str_, color_spec=None):
def __repr__(self):
if self._color_spec:
return '{schema}{str}{reset}'.format(
schema=self._color_spec, str=str(self._str), reset=Colors.Style.RESET_ALL)
schema=self._color_spec, str=safe_str(self._str), reset=Colors.Style.RESET_ALL)
return self._str

def __add__(self, other):
return str(self) + other
return safe_str(self) + other

def __radd__(self, other):
return other + str(self)
return other + safe_str(self)

def color(self, color_spec):
self._color_spec = color_spec

def replace(self, old, new, **kwargs):
self._str = self._str.replace(str(old), str(new), **kwargs)
self._str = self._str.replace(safe_str(old), safe_str(new), **kwargs)

def format(self, *args, **kwargs):
self._str = self._str.format(*args, **kwargs)
@@ -79,8 +88,8 @@ class Colors(object):
class ColorSpec(object):
def __init__(self, fore=None, back=None, style=None):
"""
It is possible to provide fore, back and style arguments. each could be either
the color is lower case letter, or the actual color from Colorama.
It is possible to provide fore, back and style arguments. Each could be either the color as
lowercase letters, or the full color name for Colorama.
"""
self._kwargs = dict(fore=fore, back=back, style=style)
self._str = StringIO()
@@ -17,52 +17,71 @@
Abstraction API above terminal color libraries.
"""

from clint.textui.core import STDOUT
from clint.textui import puts as _puts
from clint.textui.colored import ColoredString as _ColoredString
from clint.textui import indent # pylint: disable=unused-import
import os
import sys

from contextlib import contextmanager

from .formatting import safe_str
from ..cli import color


_indent_string = ''


class ColoredString(_ColoredString):
def __init__(self, color, str_, always_color=False, bold=False):
super(ColoredString, self).__init__(color, safe_str(str_), always_color, bold)
def puts(string='', newline=True, stream=sys.stdout):
stream.write(_indent_string)
stream.write(safe_str(string))
if newline:
stream.write(os.linesep)


def puts(string='', newline=True, stream=STDOUT):
_puts(safe_str(string), newline, stream)
@contextmanager
def indent(size=4):
global _indent_string
original_indent_string = _indent_string
try:
_indent_string += ' ' * size
yield
finally:
_indent_string = original_indent_string


class Colored(object):
@staticmethod
def black(string, always=False, bold=False):
return ColoredString('BLACK', string, always_color=always, bold=bold)
return Colored._color(string, color.Colors.Fore.BLACK, bold)

@staticmethod
def red(string, always=False, bold=False):
return ColoredString('RED', string, always_color=always, bold=bold)
return Colored._color(string, color.Colors.Fore.RED, bold)

@staticmethod
def green(string, always=False, bold=False):
return ColoredString('GREEN', string, always_color=always, bold=bold)
return Colored._color(string, color.Colors.Fore.GREEN, bold)

@staticmethod
def yellow(string, always=False, bold=False):
return ColoredString('YELLOW', string, always_color=always, bold=bold)
return Colored._color(string, color.Colors.Fore.YELLOW, bold)

@staticmethod
def blue(string, always=False, bold=False):
return ColoredString('BLUE', string, always_color=always, bold=bold)
return Colored._color(string, color.Colors.Fore.BLUE, bold)

@staticmethod
def magenta(string, always=False, bold=False):
return ColoredString('MAGENTA', string, always_color=always, bold=bold)
return Colored._color(string, color.Colors.Fore.MAGENTA, bold)

@staticmethod
def cyan(string, always=False, bold=False):
return ColoredString('CYAN', string, always_color=always, bold=bold)
return Colored._color(string, color.Colors.Fore.CYAN, bold)

@staticmethod
def white(string, always=False, bold=False):
return ColoredString('WHITE', string, always_color=always, bold=bold)
return Colored._color(string, color.Colors.Fore.WHITE, bold)

@staticmethod
def _color(string, fore, bold):
return color.StringStylizer(string, color.ColorSpec(
fore=fore,
style=color.Colors.Style.BRIGHT if bold else color.Colors.Style.NORMAL))
@@ -22,7 +22,6 @@ ruamel.yaml>=0.11.12, <0.12.0 # version 0.12.0 dropped support of python 2.6
Jinja2>=2.8, <2.9
shortuuid>=0.5, <0.6
CacheControl[filecache]>=0.11.0, <0.13
clint>=0.5.0, <0.6
SQLAlchemy>=1.1.0, <1.2 # version 1.2 dropped support of python 2.6
wagon==0.6.0
bottle>=0.12.0, <0.13
@@ -2,17 +2,15 @@
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --output-file requirements.txt requirements.in
# pip-compile --output-file ./requirements.txt ./requirements.in
#
appdirs==1.4.3 # via setuptools
args==0.1.0 # via clint
backports.shutil_get_terminal_size==1.0.0
blinker==1.4
bottle==0.12.13
cachecontrol[filecache]==0.12.1
click==6.7
click_didyoumean==0.0.3
clint==0.5.1
colorama==0.3.9
decorator==4.0.11 # via networkx
importlib==1.0.4 ; python_version < "2.7"

0 comments on commit f903006

Please sign in to comment.