Table.__str__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
1
#
2
# Copyright (c) 2015 SUSE Linux GmbH
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of version 3 of the GNU General Public License as
6
# published by the Free Software Foundation.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, contact SUSE LLC.
15
#
16
# To contact SUSE about this file by physical or electronic mail,
17
# you may find current contact information at www.suse.com
18
19
import prettytable
20
21
class Table(object):
22
    """A Table instance represents the result of the search
23
    """
24
    def __init__(self):
25
        """Initialize Table class"""
26
        self.__table = prettytable.PrettyTable()
27
        self.__header = []
28
29
    def add_row(self, name, values):
30
        """Add row into table
31
32
        :param str name:
33
        :param dict values:
34
        """
35
        row = [name]
36
        for key in self.__header:
37
            #ignore the Files item we have the name already
38
            if key != "Files":
39
                value = (values.get(key))
40
                #has the file this value?
41
                if value is not None:
42
                    row.append(value)
43
                else:
44
                    row.append("-")
45
        self.__table.add_row(row)
46
47
    def set_header(self, keys):
48
        """Set header of table
49
50
        :param str keys:
51
        """
52
        #prepare header first column is filename + keys
53
        header = ["Files"]
54
        header = header + list(keys)
55
        self.__table.field_names = header
56
        self.__header = header
57
58
    def add_by_list(self, file_list):
59
        """TODO:
60
61
        :param list file_list:
62
        """
63
        #first gather all header items
64
        keys = set()
65
        for filename, values in file_list.items():
66
            keys.update(values.keys())
67
        self.set_header(keys)
68
69
        #now we can create the rows
70
        for filename, values in file_list.items():
71
            self.add_row(filename, values)
72
73
    def sort(self, sortby):
74
        """Sort table
75
76
        :param sortby:
77
        """
78
        self.__table.sortby = sortby
79
80
    def __str__(self):
81
        """Return formatted table"""
82
        return self.__table