utils.py 1012 Bytes
Newer Older
Johannes Bechberger's avatar
Johannes Bechberger committed
1 2 3
import logging
from os import path
import sys
4 5
from typing import Tuple

6
COLOR_OUTPUT_IF_POSSIBLE = False
7

8 9 10 11 12 13
if sys.stdout.isatty():
    try:
        import termcolor
        COLOR_OUTPUT_IF_POSSIBLE = True
    except ImportError:
        COLOR_OUTPUT_IF_POSSIBLE = False
14 15


16
def force_colored_output():
17
    global COLOR_OUTPUT_IF_POSSIBLE
18
    COLOR_OUTPUT_IF_POSSIBLE = True
19

Johannes Bechberger's avatar
Johannes Bechberger committed
20 21

def get_mjtest_basedir() -> str:
22
    return path.dirname(path.dirname(path.dirname(path.realpath(__file__))))
Johannes Bechberger's avatar
Johannes Bechberger committed
23 24


25 26 27 28
def colored(text: str, *args, **kwargs):
    """
    Wrapper around termcolor.colored (if it's loadable)
    """
29 30 31 32
    global COLOR_OUTPUT_IF_POSSIBLE
    if COLOR_OUTPUT_IF_POSSIBLE:
        return termcolor.colored(text, *args, **kwargs)
    else:
33 34 35 36 37 38 39
        return text


def cprint(text: str, *args, **kwargs):
    """
    Wrapper around termcolor.cprint (if it's loadable)
    """
40 41 42 43
    global COLOR_OUTPUT_IF_POSSIBLE
    if COLOR_OUTPUT_IF_POSSIBLE:
        termcolor.cprint(text, *args, **kwargs)
    else:
44
        print(text)