summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/common/text_opts.py
blob: 06c2c05663639f632091ad8459270cb1307575d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
import re

TEXT_CODES = {'bold': {'start': '\x1b[1m',
                       'end': '\x1b[22m'},
              'cyan': {'start': '\x1b[36m',
                       'end': '\x1b[39m'},
              'blue': {'start': '\x1b[34m',
                       'end': '\x1b[39m'},
              'red': {'start': '\x1b[31m',
                      'end': '\x1b[39m'},
              'magenta': {'start': '\x1b[35m',
                          'end': '\x1b[39m'},
              'green': {'start': '\x1b[32m',
                        'end': '\x1b[39m'},
              'yellow': {'start': '\x1b[33m',
                         'end': '\x1b[39m'},
              'underline': {'start': '\x1b[4m',
                            'end': '\x1b[24m'}}


def bold(text):
    return text_attribute(text, 'bold')


def cyan(text):
    return text_attribute(text, 'cyan')


def blue(text):
    return text_attribute(text, 'blue')


def red(text):
    return text_attribute(text, 'red')


def magenta(text):
    return text_attribute(text, 'magenta')


def green(text):
    return text_attribute(text, 'green')

def yellow(text):
    return text_attribute(text, 'yellow')

def underline(text):
    return text_attribute(text, 'underline')


def text_attribute(text, attribute):
    return "{start}{txt}{stop}".format(start=TEXT_CODES[attribute]['start'],
                                       txt=text,
                                       stop=TEXT_CODES[attribute]['end'])


FUNC_DICT = {'blue': blue,
             'bold': bold,
             'green': green,
             'yellow': yellow,
             'cyan': cyan,
             'magenta': magenta,
             'underline': underline,
             'red': red}


def format_text(text, *args):
    return_string = text
    for i in args:
        func = FUNC_DICT.get(i)
        if func:
            return_string = func(return_string)
    return return_string

# pretty print for JSON
def pretty_json (json_str, use_colors = True):
    pretty_str = json.dumps(json.loads(json_str), indent = 4, separators=(',', ': '), sort_keys = True)

    if not use_colors:
        return pretty_str

    try:
        # int numbers
        pretty_str = re.sub(r'([ ]*:[ ]+)(\-?[1-9][0-9]*[^.])',r'\1{0}'.format(blue(r'\2')), pretty_str)
        # float
        pretty_str = re.sub(r'([ ]*:[ ]+)(\-?[1-9][0-9]*\.[0-9]+)',r'\1{0}'.format(magenta(r'\2')), pretty_str)
        # # strings
        #
        pretty_str = re.sub(r'([ ]*:[ ]+)("[^"]*")',r'\1{0}'.format(red(r'\2')), pretty_str)
        pretty_str = re.sub(r"('[^']*')", r'{0}\1{1}'.format(TEXT_CODES['magenta']['start'],
                                                             TEXT_CODES['red']['start']), pretty_str)
    except :
        pass

    return pretty_str


if __name__ == "__main__":
    pass