<commit_before>## Copyright (c) 2012, 2013 Aldebaran Robotics. All rights reserved.
## Use of this source code is governed by a BSD-style license that can be
## found in the COPYING file.


# Colorized output to console code inspired by
# pygments (http://pygments.org/) BSD License.

""" Tools for a nice user interface

"""

import sys
import os
import datetime
import functools

# Try using pyreadline so that we can
# have colors on windows, too.
_console = None
HAS_PYREADLINE = True
if os.name == 'nt':
    try:
        # pylint: disable-msg=F0401
        from pyreadline.console import Console
        _console = Console()
    except:
        HAS_PYREADLINE = False

# ANSI color codes, as classes,
# so that we can use ::
#
#  qisys.ui.msg(qisys.ui.bold, "This is bold", qisys.ui.reset)


class _Color:
    def __init__(self, code, modifier=None):
        self.code = '\033[%d' % code
        if modifier is not None:
            self.code += ';%dm' % modifier
        else:
            self.code += 'm'

reset     = _Color(0)
bold      = _Color(1)
faint     = _Color(2)
standout  = _Color(3)
underline = _Color(4)
blink     = _Color(5)
overline  = _Color(6)

black      = _Color(30)
darkred    = _Color(31)
darkgreen  = _Color(32)
brown      = _Color(33)
darkblue   = _Color(34)
purple     = _Color(35)
teal       = _Color(36)
lightgray  = _Color(37)

darkgray   = _Color(30, 1)
red        = _Color(31, 1)
green      = _Color(32, 1)
yellow     = _Color(33, 1)
blue       = _Color(34, 1)
fuchsia    = _Color(35, 1)
turquoise  = _Color(36, 1)
white      = _Color(37, 1)

darkteal   = turquoise
darkyellow = brown
fuscia     = fuchsia


# Global variable to store qisys.ui configuration
# Useful settings when running qibuild on a buildfarm:
#    CONFIG['timestamps'] = True
#    CONFIG['interative'] = False


CONFIG = {
    "verbose": os.environ.get("VERBOSE"),
    "quiet": False,
    "color": True,
    "timestamp": False,
    "interactive": True,
    "record": False  # used for testing
}


# used for testing
_MESSAGES = list()

def configure_logging(args):
    verbose = os.environ.get("VERBOSE", False)
    if not verbose:
        verbose = args.verbose
    CONFIG["color"] = args.color
    CONFIG["verbose"] = verbose
    CONFIG["quiet"] = args.quiet
    CONFIG["timestamp"] = args.timestamp

def _msg(*tokens, **kwargs):
    """ Helper method for error, warning, info, debug

    """
    if CONFIG["record"]:
        CONFIG["color"] = False
    fp = kwargs.get("fp", sys.stdout)
    sep = kwargs.get("sep", " ")
    end = kwargs.get("end", "\n")
    with_color = CONFIG["color"]
    if os.name == 'nt' and not HAS_PYREADLINE or not fp.isatty():
        with_color = False
    res = list()  # Initialize result list, to be concatenated before printing
    if CONFIG["timestamp"]:
        now = datetime.datetime.now()
        res.append(now.strftime("[%Y-%m-%d %H:%M:%S] "))
    for token in tokens:
        if isinstance(token, _Color):
            if with_color:
                res.append(token.code)
        else:
            if sep == " " and token == "\n":
                res.append("\n")
            else:
                res.append(str(token))
                res.append(sep)
    # always reset:
    if with_color:
        res.append(reset.code)
    res.append(end)
    stringres = ''.join(res)
    if CONFIG["record"]:
        _MESSAGES.append(stringres)
    if _console and with_color:
        _console.write_color(stringres)
    else:
        fp.write(stringres)
        fp.flush()

def error(*tokens, **kwargs):
    """ Print an error message """
    tokens = [bold, red, "[ERROR]: "] + list(tokens)
    kwargs["fp"] = sys.stderr
    _msg(*tokens, **kwargs)

def warning(*tokens, **kwargs):
    """ Print a warning message """
    tokens = [brown, "[WARN ]: "] + list(tokens)
    kwargs["fp"] = sys.stderr
    _msg(*tokens, **kwargs)

def info(*tokens, **kwargs):
    """ Print an informative message """
    if CONFIG["quiet"]:
        return
    _msg(*tokens, **kwargs)

def info_count(i, n, *rest):
    """ Same as info, but displays a nice counter
    coler will be reset
    >>> count(0, 4)
    * (1/5)
    >>> count(4, 12)
    * ( 4/12)
    >>> count(4, 10)
    * (10/12)

    """
    num_digits = len(str(n)) # lame, I know
    counter_format = "(%{}d/%d)".format(num_digits)
    counter_str = counter_format % (i+1, n)
    info(green, "*", reset, counter_str, reset, *rest)

def debug(*tokens, **kwargs):
    """ Print a debug message """
    if not CONFIG["verbose"]:
        return
    tokens = [blue, "[DEBUG]: "] + list(tokens)
    _msg(*tokens, **kwargs)

def indent_iterable(elems, num=2):
    """Indent an iterable."""
    return [" " * num + l for l in elems]

def indent(text, num=2):
    """Indent a piece of text."""
    lines = text.splitlines()
    return '\n'.join(indent_iterable(lines, num=num))

def tabs(num):
    """ Compute a blank tab """
    return "  " * num

class timer:
    """ To be used as a decorator,
    or as a with statement:

    >>> @timer("something")
        def do_something():
            foo()
            bar()
    # Or:<commit_msg>qisys.ui.info_count: forward optional arguments to ui.info()<commit_after>## Copyright (c) 2012, 2013 Aldebaran Robotics. All rights reserved.
## Use of this source code is governed by a BSD-style license that can be
## found in the COPYING file.


# Colorized output to console code inspired by
# pygments (http://pygments.org/) BSD License.

""" Tools for a nice user interface

"""

import sys
import os
import datetime
import functools

# Try using pyreadline so that we can
# have colors on windows, too.
_console = None
HAS_PYREADLINE = True
if os.name == 'nt':
    try:
        # pylint: disable-msg=F0401
        from pyreadline.console import Console
        _console = Console()
    except:
        HAS_PYREADLINE = False

# ANSI color codes, as classes,
# so that we can use ::
#
#  qisys.ui.msg(qisys.ui.bold, "This is bold", qisys.ui.reset)


class _Color:
    def __init__(self, code, modifier=None):
        self.code = '\033[%d' % code
        if modifier is not None:
            self.code += ';%dm' % modifier
        else:
            self.code += 'm'

reset     = _Color(0)
bold      = _Color(1)
faint     = _Color(2)
standout  = _Color(3)
underline = _Color(4)
blink     = _Color(5)
overline  = _Color(6)

black      = _Color(30)
darkred    = _Color(31)
darkgreen  = _Color(32)
brown      = _Color(33)
darkblue   = _Color(34)
purple     = _Color(35)
teal       = _Color(36)
lightgray  = _Color(37)

darkgray   = _Color(30, 1)
red        = _Color(31, 1)
green      = _Color(32, 1)
yellow     = _Color(33, 1)
blue       = _Color(34, 1)
fuchsia    = _Color(35, 1)
turquoise  = _Color(36, 1)
white      = _Color(37, 1)

darkteal   = turquoise
darkyellow = brown
fuscia     = fuchsia


# Global variable to store qisys.ui configuration
# Useful settings when running qibuild on a buildfarm:
#    CONFIG['timestamps'] = True
#    CONFIG['interative'] = False


CONFIG = {
    "verbose": os.environ.get("VERBOSE"),
    "quiet": False,
    "color": True,
    "timestamp": False,
    "interactive": True,
    "record": False  # used for testing
}


# used for testing
_MESSAGES = list()

def configure_logging(args):
    verbose = os.environ.get("VERBOSE", False)
    if not verbose:
        verbose = args.verbose
    CONFIG["color"] = args.color
    CONFIG["verbose"] = verbose
    CONFIG["quiet"] = args.quiet
    CONFIG["timestamp"] = args.timestamp

def _msg(*tokens, **kwargs):
    """ Helper method for error, warning, info, debug

    """
    if CONFIG["record"]:
        CONFIG["color"] = False
    fp = kwargs.get("fp", sys.stdout)
    sep = kwargs.get("sep", " ")
    end = kwargs.get("end", "\n")
    with_color = CONFIG["color"]
    if os.name == 'nt' and not HAS_PYREADLINE or not fp.isatty():
        with_color = False
    res = list()  # Initialize result list, to be concatenated before printing
    if CONFIG["timestamp"]:
        now = datetime.datetime.now()
        res.append(now.strftime("[%Y-%m-%d %H:%M:%S] "))
    for token in tokens:
        if isinstance(token, _Color):
            if with_color:
                res.append(token.code)
        else:
            if sep == " " and token == "\n":
                res.append("\n")
            else:
                res.append(str(token))
                res.append(sep)
    # always reset:
    if with_color:
        res.append(reset.code)
    res.append(end)
    stringres = ''.join(res)
    if CONFIG["record"]:
        _MESSAGES.append(stringres)
    if _console and with_color:
        _console.write_color(stringres)
    else:
        fp.write(stringres)
        fp.flush()

def error(*tokens, **kwargs):
    """ Print an error message """
    tokens = [bold, red, "[ERROR]: "] + list(tokens)
    kwargs["fp"] = sys.stderr
    _msg(*tokens, **kwargs)

def warning(*tokens, **kwargs):
    """ Print a warning message """
    tokens = [brown, "[WARN ]: "] + list(tokens)
    kwargs["fp"] = sys.stderr
    _msg(*tokens, **kwargs)

def info(*tokens, **kwargs):
    """ Print an informative message """
    if CONFIG["quiet"]:
        return
    _msg(*tokens, **kwargs)

def info_count(i, n, *rest, **kwargs):
    """ Same as info, but displays a nice counter
    coler will be reset
    >>> count(0, 4)
    * (1/5)
    >>> count(4, 12)
    * ( 4/12)
    >>> count(4, 10)
    * (10/12)

    """
    num_digits = len(str(n)) # lame, I know
    counter_format = "(%{}d/%d)".format(num_digits)
    counter_str = counter_format % (i+1, n)
    info(green, "*", reset, counter_str, reset, *rest, **kwargs)

def debug(*tokens, **kwargs):
    """ Print a debug message """
    if not CONFIG["verbose"]:
        return
    tokens = [blue, "[DEBUG]: "] + list(tokens)
    _msg(*tokens, **kwargs)

def indent_iterable(elems, num=2):
    """Indent an iterable."""
    return [" " * num + l for l in elems]

def indent(text, num=2):
    """Indent a piece of text."""
    lines = text.splitlines()
    return '\n'.join(indent_iterable(lines, num=num))

def tabs(num):
    """ Compute a blank tab """
    return "  " * num

class timer:
    """ To be used as a decorator,
    or as a with statement:

    >>> @timer("something")
        def do_something():
            foo()
            bar()
    # Or:
{'fraction_non_alphanumeric': 0.1456490034030141, 'fraction_numerical': 0.01594555177442878, 'mean_word_length': 2.5852213314743815, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7013999', 'n_tokens_mistral': 3572, 'n_tokens_neox': 3407, 'n_words': 968}