utils.py 934 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 7 8 9 10 11 12 13 14 15 16 17
COLOR_OUTPUT_IF_POSSIBLE = True

try:
    import termcolor
except ImportError:
    COLOR_OUTPUT_IF_POSSIBLE = False


def disable_colored_output():
    global COLOR_OUTPUT_IF_POSSIBLE
    COLOR_OUTPUT_IF_POSSIBLE = False

Johannes Bechberger's avatar
Johannes Bechberger committed
18 19

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


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


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