Esempio n. 1
0
def shell_commands_dict_output(username,
                               d,
                               # choose format manually
                               print_format=None,
                               # choose table format if needed
                               table_format=None,
                               # more specific table arguments
                               # --------------------------
                               # for table format: "key_list"
                               indexed=False,
                               # --------------------------
                               firstheader=None,
                               header=None,
                               oneitem=False,
                               vertical_table=False,
                               title=None,
                               count=False):
    '''
    some shell commands output a dict or table, this function should be
    called when commands are printing in such way.
    if none of parameters json and table is given, the funtion will try
    to find the default printing form from db_defaults
    if db_defaults has no item 'shell_print_format', the function will
    set 'shell_print_format' = table
    
    param username:: user id
    param d:: data to print
    param print_format:: print format: table, json, csv
    
    param table_format:: choose table format if needed, DESCRIPTIONS:
    
    type1: key_list: 
                    
    accept a dict in the form:
    {key1: [list1],
     key2: [list2],
     .......
     =>
     | key1 | key2 |
     | l
     | i
     | s
     | t
    
    acceptable args: indexed: provide index
    
    param firstheader:: designed for table, provide a attribute name for the
                        first item of each row, since the dict doesn't provide
                        it
    param header:: designed for table(and used to filter input dict), a list of lists, 
                   provides column order, e.g.
                   [[a,b], [c, d], ...
                   where 'a' is the printing column name and 'b' is the attribute name
                   in the dict
                   if you don't want to change the header name but want to keep the
                   header order or filter the items to list, for each item in the list
                   you may provide a string or a list with one item instead of a list of 
                   two items
    param oneitem:: designed for table, normally the input dict should be in such
                    form:
                    {a: {...}, b:{...}}, where each subitem is a row
                    if there is only one item, input dict is the {...} of the subitem
                    above
    param title: provide a title for the table
    param count: provide count info at the end of the table
    '''
    format_type = None
    if print_format:
        format_type = print_format
    else:
        format_type = get_default_print_format(username)

    if format_type not in ALLOWED_PRINT_FORMAT:
        Console.error("wrong print format: {0}. (allowed print format: {1})".format(format_type,
                                ", ".join(ALLOWED_PRINT_FORMAT)))
        return False
    
    headers = None
    order = None
    if header:
        headers = []
        order = []
        for i in header:
            if isinstance(i, basestring):
                headers.append(i)
                order.append(i)
            elif isinstance(i, list):
                if len(i) == 1:
                    headers.append(i[0])
                    order.append(i[0])
                else:
                    headers.append(i[0])
                    order.append(i[1])
            else:
                print("ERROR: header info is not correct")
                return False
    
    # --------------------------------------------------------------------------       
    # filter the input dict
    # -------------------------------------------------------------------------- 
    if order:
        new_d = {}
        if oneitem:
            for item in order:
                if item in d:
                    new_d[item] = d[item]
        else:
            for i in d:
                new_d[i] = {}
                for item in order:
                    if item in d[i]:
                        new_d[i][item] = d[i][item]
        d = new_d
    # -------------------------------------------------------------------------- 
 
    if format_type == "json":
        if title:
            banner(title)
        print(json.dumps(d, indent=4))
        
    elif format_type == "csv":
        with open(".temp.csv", "wb") as f:
            w = csv.DictWriter(f, d.keys())
            w.writeheader()
            w.writerow(d)
        
    elif format_type == "table":
        if table_format == "key_list":
            print (dict_key_list_table_printer(d, indexed=indexed))
            return
        
        if title:
            print("+" + "-" * (len(title) - 2) + "+")
            print(title)

        print_data = []
        if oneitem:
            print_data = [d]
        else:
            for k in sorted(d):
                d[k][' '] = k
                print_data.append(d[k])
            if header:
                if firstheader:
                    headers = [firstheader] + headers
                order = [' '] + order
        
        if vertical_table:
            print(array_dict_table_printer(print_data, 
                                           order=order, 
                                           header=headers,
                                           vertical=True))
        else:
            print(array_dict_table_printer(print_data, order=order, header=headers))

        if count:
            c = len(print_data)
            sentence = "count: {0}".format(c)
            print(sentence)
            print("+" + "-" * (len(sentence) - 2) + "+")
Esempio n. 2
0
 def to_table(self, label):
     data = self.get(label)
     if data == []:
         return "No data found"
     else:
         return array_dict_table_printer(data)
Esempio n. 3
0
 def to_table(self, label):
     data = self.get(label)
     if data == []:
         return "No data found"
     else:
         return array_dict_table_printer(data)
Esempio n. 4
0
def shell_commands_dict_output(
        username,
        d,
        # choose format manually
        print_format=None,
        # choose table format if needed
        table_format=None,
        # more specific table arguments
        # --------------------------
        # for table format: "key_list"
        indexed=False,
        # --------------------------
        firstheader=None,
        header=None,
        oneitem=False,
        vertical_table=False,
        title=None,
        count=False):
    '''
    some shell commands output a dict or table, this function should be
    called when commands are printing in such way.
    if none of parameters json and table is given, the funtion will try
    to find the default printing form from db_defaults
    if db_defaults has no item 'shell_print_format', the function will
    set 'shell_print_format' = table
    
    param username:: user id
    param d:: data to print
    param print_format:: print format: table, json, csv
    
    param table_format:: choose table format if needed, DESCRIPTIONS:
    
    type1: key_list: 
                    
    accept a dict in the form:
    {key1: [list1],
     key2: [list2],
     .......
     =>
     | key1 | key2 |
     | l
     | i
     | s
     | t
    
    acceptable args: indexed: provide index
    
    param firstheader:: designed for table, provide a attribute name for the
                        first item of each row, since the dict doesn't provide
                        it
    param header:: designed for table(and used to filter input dict), a list of lists, 
                   provides column order, e.g.
                   [[a,b], [c, d], ...
                   where 'a' is the printing column name and 'b' is the attribute name
                   in the dict
                   if you don't want to change the header name but want to keep the
                   header order or filter the items to list, for each item in the list
                   you may provide a string or a list with one item instead of a list of 
                   two items
    param oneitem:: designed for table, normally the input dict should be in such
                    form:
                    {a: {...}, b:{...}}, where each subitem is a row
                    if there is only one item, input dict is the {...} of the subitem
                    above
    param title: provide a title for the table
    param count: provide count info at the end of the table
    '''
    format_type = None
    if print_format:
        format_type = print_format
    else:
        format_type = get_default_print_format(username)

    if format_type not in ALLOWED_PRINT_FORMAT:
        Console.error(
            "wrong print format: {0}. (allowed print format: {1})".format(
                format_type, ", ".join(ALLOWED_PRINT_FORMAT)))
        return False

    headers = None
    order = None
    if header:
        headers = []
        order = []
        for i in header:
            if isinstance(i, basestring):
                headers.append(i)
                order.append(i)
            elif isinstance(i, list):
                if len(i) == 1:
                    headers.append(i[0])
                    order.append(i[0])
                else:
                    headers.append(i[0])
                    order.append(i[1])
            else:
                print("ERROR: header info is not correct")
                return False

    # --------------------------------------------------------------------------
    # filter the input dict
    # --------------------------------------------------------------------------
    if order:
        new_d = {}
        if oneitem:
            for item in order:
                if item in d:
                    new_d[item] = d[item]
        else:
            for i in d:
                new_d[i] = {}
                for item in order:
                    if item in d[i]:
                        new_d[i][item] = d[i][item]
        d = new_d
    # --------------------------------------------------------------------------

    if format_type == "json":
        if title:
            banner(title)
        print(json.dumps(d, indent=4))

    elif format_type == "csv":
        with open(".temp.csv", "wb") as f:
            w = csv.DictWriter(f, d.keys())
            w.writeheader()
            w.writerow(d)

    elif format_type == "table":
        if table_format == "key_list":
            print(dict_key_list_table_printer(d, indexed=indexed))
            return

        if title:
            print("+" + "-" * (len(title) - 2) + "+")
            print(title)

        print_data = []
        if oneitem:
            print_data = [d]
        else:
            for k in sorted(d):
                d[k][' '] = k
                print_data.append(d[k])
            if header:
                if firstheader:
                    headers = [firstheader] + headers
                order = [' '] + order

        if vertical_table:
            print(
                array_dict_table_printer(print_data,
                                         order=order,
                                         header=headers,
                                         vertical=True))
        else:
            print(
                array_dict_table_printer(print_data,
                                         order=order,
                                         header=headers))

        if count:
            c = len(print_data)
            sentence = "count: {0}".format(c)
            print(sentence)
            print("+" + "-" * (len(sentence) - 2) + "+")