diff options
author | Dan Klein <danklein10@gmail.com> | 2015-10-29 11:34:03 +0200 |
---|---|---|
committer | Dan Klein <danklein10@gmail.com> | 2015-10-29 11:34:03 +0200 |
commit | aa37a0abb00cbf4cb1611f9c0eefcb1ab850bc45 (patch) | |
tree | 506f5b5fb50cfa93352557b40bc9f4ce0ba89dbc /scripts/automation/trex_control_plane/common | |
parent | 5377774afbf00b0da0ad0b74d3be207f6eb9124e (diff) |
Console redesign using trex_stateless_client module
Diffstat (limited to 'scripts/automation/trex_control_plane/common')
-rw-r--r-- | scripts/automation/trex_control_plane/common/text_opts.py | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/scripts/automation/trex_control_plane/common/text_opts.py b/scripts/automation/trex_control_plane/common/text_opts.py index 8ab03eeb..0d8323db 100644 --- a/scripts/automation/trex_control_plane/common/text_opts.py +++ b/scripts/automation/trex_control_plane/common/text_opts.py @@ -1,4 +1,5 @@ - +import json +import re TEXT_CODES = {'bold': {'start': '\x1b[1m', 'end': '\x1b[22m'}, @@ -65,7 +66,29 @@ def format_text(text, *args): func = FUNC_DICT.get(i) if func: return_string = func(return_string) - return return_string + return + +# 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__": |