vmware-archive / vcd-cli

Command Line Interface for VMware vCloud Director
https://vmware.github.io/vcd-cli
Other
165 stars 105 forks source link

Bug in as_table() #525

Open andrew-ni opened 4 years ago

andrew-ni commented 4 years ago
def as_table(obj_list,
             show_id=False,
             sort_headers=True,
             hide_fields=['href', 'type']):
    if len(obj_list) == 0:
        return ''
    else:
        if sort_headers:
            headers = sorted(obj_list[0].keys())
        else:
            headers = obj_list[0].keys()
        if not show_id and 'id' in headers:
            headers.remove('id')
        for field in hide_fields:
            if field in headers:
                headers.remove(field)
        table = []
        for obj in obj_list:
            table.append(
                [obj.get(k) if k in obj.keys() else '' for k in headers])
        return tabulate(table, headers)

If sort_headers=False and a key from hide_fields is used ('href'), headers.remove(field) will cause an error because dict_keys has no attribute remove

headers = sorted(obj_list[0].keys()) results in a list, which has the attribute remove headers = obj_list[0].keys() results in a dict_keys, which does not have the attribute remove

This bug prevents tables to have any header defined in hide_fields if sort_headers=False