add basic colorama implementation

This commit is contained in:
Robert Kaussow 2019-03-21 16:05:55 +01:00
parent 1432e62b7b
commit 0282ec4c06
1 changed files with 25 additions and 8 deletions

View File

@ -6,39 +6,56 @@ import os
import subprocess
import sys
import re
import colorama
from distutils.version import LooseVersion
from ansible.module_utils.parsing.convert_bool import boolean as to_bool
try:
import ConfigParser as configparser
except ImportError:
import configparser
try:
from ansible.utils.color import stringc
except ImportError:
from ansible.color import stringc
# try:
# from ansible.utils.color import stringc
# except ImportError:
# from ansible.color import stringc
# from yamlhelper import *
def should_do_markup():
py_colors = os.environ.get('PY_COLORS', None)
if py_colors is not None:
return to_bool(py_colors, strict=False)
return sys.stdout.isatty() and os.environ.get('TERM') != 'dumb'
colorama.init(autoreset=True, strip=not should_do_markup())
def abort(message, file=sys.stderr):
print(stringc("FATAL: %s" % message, 'red'), file=file)
return color_text(colorama.Fore.RED, "FATAL: {}".format(message))
sys.exit(1)
def error(message, file=sys.stderr):
print(stringc("ERROR: %s" % message, 'red'), file=file)
return color_text(colorama.Fore.RED, "ERROR: {}".format(message))
def warn(message, settings, file=sys.stdout):
if settings.log_level <= logging.WARNING:
print(stringc("WARN: %s" % message, 'yellow'), file=file)
return color_text(colorama.Fore.YELLOW, "WARN: {}".format(message))
def info(message, settings, file=sys.stdout):
if settings.log_level <= logging.INFO:
print(stringc("INFO: %s" % message, 'green'), file=file)
return color_text(colorama.Fore.BLUE, "INFO: {}".format(message))
def color_text(color, msg):
print('{}{}{}'.format(color, msg, colorama.Style.RESET_ALL))
def count_spaces(c_string):