def print_data_dict(heading_string,
                    data_dict,
                    colors_template={},
                    printLevel=0):
    TABS = ''
    if printLevel > 0:
        TABS = "\t" * printLevel
    c0 = Fore.LIGHTBLACK_EX
    c1 = Fore.BLUE
    c2 = Fore.GREEN
    cd = Fore.YELLOW
    c1 = default_colors_template.get('1stlevel',
                                     {}).get('key_color', Fore.BLUE)
    c2 = default_colors_template.get('1stlevel',
                                     {}).get('data_color', Fore.WHITE)
    is_success = False
    is_error = False
    is_warning = False
    previous_key = ""

    if not data_dict:
        msg = f"{Fore.YELLOW}" + "{" + '}' + Fore.RESET
    else:
        if len(data_dict) == 1:
            msg = f"{Fore.YELLOW}" + "{"
        else:
            msg = f"{Fore.YELLOW}" + "{"

        for k in data_dict:
            if not k == 'api_data':
                ck = default_colors_template.get(k, {}).get('key_color', c1)
                cd = default_colors_template.get(k, {}).get('data_color', c2)
                if k == 'api_message':
                    if is_success:
                        cd = Fore.GREEN
                    elif is_error:
                        cd = Fore.RED
                    elif is_warning:
                        cd = Fore.MAGENTA
                vv = data_dict.get(k)
                v = get_printformatted_data(vv, default_colors_template)
                if k == 'api_status':
                    if v.find('success') >= 0:
                        is_success = True
                    elif v.find('error') >= 0:
                        is_error = True
                    elif v.find('warning') >= 0:
                        is_warning = True

                msg1 = f"{c0}'{ck}{k}{c0}':{cd}{v}{c0},"
                if not previous_key:
                    separator = ""
                elif (previous_key == 'api_status'
                      and k == 'api_message') or (previous_key == 'api_message'
                                                  and k == 'api_status'):
                    separator = " "
                elif (previous_key == 'api_action'
                      and k == 'api_name') or (previous_key == 'api_name'
                                               and k == 'api_action'):
                    separator = " "
                elif (previous_key.find('rows') >= 0 and k.find('rows') >= 0):
                    separator = " "
                elif k == 'api_data':
                    separator = "\n"
                else:
                    separator = "\n"

                msg = msg + separator + msg1
                previous_key = k

        k = 'api_data'
        if k in data_dict.keys():
            ck = default_colors_template.get(k, {}).get('key_color', c1)
            cd = default_colors_template.get(k, {}).get('data_color', c2)
            vv = data_dict.get(k)
            v = get_printformatted_data(vv, default_colors_template)
            msg1 = f"{c0}'{ck}{k}{c0}':{cd}{v}{c0}, \n"
            msg = msg + "\n" + msg1

        msg = msg.strip()
        if msg[-1] == ",":
            msg = msg[0:-1]
        #msg = msg + f"\n{Fore.YELLOW}" + "}" + c0 + Fore.RESET
        msg = msg + f"{Fore.YELLOW}" + "}" + c0 + Fore.RESET

    c0 = Fore.LIGHTBLACK_EX
    c1 = default_colors_template.get('prefix', {}).get('key_color',
                                                       Fore.YELLOW)
    c2 = default_colors_template.get('prefix', {}).get('data_color',
                                                       Fore.GREEN)
    suffix1 = default_colors_template.get('suffix',
                                          {}).get('key_color', Fore.WHITE)
    suffix2 = default_colors_template.get('suffix', {}).get('data_color', '\n')
    heading_string = apply_colors(heading_string, c0)
    msg = msg.replace('\n', '\n' + TABS)
    heading_string = heading_string.replace('\n', '\n' + TABS)
    msgP = f"{Style.NORMAL}{heading_string}{msg}{suffix1}{suffix2}{Fore.RESET}{Back.RESET}"
    print(msgP)
def colorized_message(msgP, msgColor=Fore.LIGHTBLACK_EX):
    msgC = apply_colors(msgP, msgColor)
    return msgC
Beispiel #3
0
def print_message(msgP, msgColor=Fore.LIGHTBLACK_EX, printLevel=0):
    msgP = apply_colors(msgP, msgColor)
    if printLevel > 0:
        msgP = "\t" * printLevel + msgP
    print(msgP)