Total Complexity | 40 |
Total Lines | 136 |
Duplicated Lines | 0 % |
Complex classes like MultiColumnTable often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
49 | class MultiColumnTable(formatters.Formatter): |
||
50 | |||
51 | @classmethod |
||
52 | def format(cls, entries, *args, **kwargs): |
||
53 | attributes = kwargs.get('attributes', []) |
||
54 | attribute_transform_functions = kwargs.get('attribute_transform_functions', {}) |
||
55 | widths = kwargs.get('widths', []) |
||
56 | widths = widths or [] |
||
57 | |||
58 | if not widths and attributes: |
||
59 | # Dynamically calculate column size based on the terminal size |
||
60 | lines, cols = get_terminal_size() |
||
61 | |||
62 | if attributes[0] == 'id': |
||
63 | # consume iterator and save as entries so collection is accessible later. |
||
64 | entries = [e for e in entries] |
||
65 | # first column contains id, make sure it's not broken up |
||
66 | first_col_width = cls._get_required_column_width(values=[e.id for e in entries], |
||
67 | minimum_width=MIN_ID_COL_WIDTH) |
||
68 | cols = (cols - first_col_width) |
||
69 | col_width = int(math.floor((cols / len(attributes)))) |
||
70 | else: |
||
71 | col_width = int(math.floor((cols / len(attributes)))) |
||
72 | first_col_width = col_width |
||
73 | |||
74 | widths = [] |
||
75 | subtract = 0 |
||
76 | for index in range(0, len(attributes)): |
||
77 | attribute_name = attributes[index] |
||
78 | |||
79 | if index == 0: |
||
80 | widths.append(first_col_width) |
||
81 | continue |
||
82 | |||
83 | if attribute_name in COLORIZED_ATTRIBUTES: |
||
84 | current_col_width = COLORIZED_ATTRIBUTES[attribute_name]['col_width'] |
||
85 | subtract += (current_col_width - col_width) |
||
86 | else: |
||
87 | # Make sure we subtract the added width from the last column so we account |
||
88 | # for the fixed width columns and make sure table is not wider than the |
||
89 | # terminal width. |
||
90 | if index == (len(attributes) - 1) and subtract: |
||
91 | current_col_width = (col_width - subtract) |
||
92 | |||
93 | if current_col_width <= MIN_COL_WIDTH: |
||
94 | # Make sure column width is always grater than MIN_COL_WIDTH |
||
95 | current_col_width = MIN_COL_WIDTH |
||
96 | else: |
||
97 | current_col_width = col_width |
||
98 | |||
99 | widths.append(current_col_width) |
||
100 | |||
101 | if not attributes or 'all' in attributes: |
||
102 | attributes = sorted([attr for attr in entries[0].__dict__ |
||
103 | if not attr.startswith('_')]) |
||
104 | |||
105 | # Determine table format. |
||
106 | if len(attributes) == len(widths): |
||
107 | # Customize width for each column. |
||
108 | columns = zip(attributes, widths) |
||
109 | else: |
||
110 | # If only 1 width value is provided then |
||
111 | # apply it to all columns else fix at 28. |
||
112 | width = widths[0] if len(widths) == 1 else 28 |
||
113 | columns = zip(attributes, |
||
114 | [width for i in range(0, len(attributes))]) |
||
115 | |||
116 | # Format result to table. |
||
117 | table = PrettyTable() |
||
118 | for column in columns: |
||
119 | table.field_names.append(column[0]) |
||
120 | table.max_width[column[0]] = column[1] |
||
121 | table.padding_width = 1 |
||
122 | table.align = 'l' |
||
123 | table.valign = 't' |
||
124 | for entry in entries: |
||
125 | # TODO: Improve getting values of nested dict. |
||
126 | values = [] |
||
127 | for field_name in table.field_names: |
||
128 | if '.' in field_name: |
||
129 | field_names = field_name.split('.') |
||
130 | value = getattr(entry, field_names.pop(0), {}) |
||
131 | for name in field_names: |
||
132 | value = cls._get_field_value(value, name) |
||
133 | if type(value) is str: |
||
134 | break |
||
135 | value = strutil.unescape(value) |
||
136 | values.append(value) |
||
137 | else: |
||
138 | value = cls._get_simple_field_value(entry, field_name) |
||
139 | transform_function = attribute_transform_functions.get(field_name, |
||
140 | lambda value: value) |
||
141 | value = transform_function(value=value) |
||
142 | value = strutil.unescape(value) |
||
143 | values.append(value) |
||
144 | table.add_row(values) |
||
145 | return table |
||
146 | |||
147 | @staticmethod |
||
148 | def _get_simple_field_value(entry, field_name): |
||
149 | """ |
||
150 | Format a value for a simple field. |
||
151 | """ |
||
152 | value = getattr(entry, field_name, '') |
||
153 | if isinstance(value, (list, tuple)): |
||
154 | if len(value) == 0: |
||
155 | value = '' |
||
156 | elif isinstance(value[0], (str, unicode)): |
||
157 | # List contains simple string values, format it as comma |
||
158 | # separated string |
||
159 | value = ', '.join(value) |
||
160 | |||
161 | return value |
||
162 | |||
163 | @staticmethod |
||
164 | def _get_field_value(value, field_name): |
||
165 | r_val = value.get(field_name, None) |
||
166 | if r_val is None: |
||
167 | return '' |
||
168 | |||
169 | if isinstance(r_val, list) or isinstance(r_val, dict): |
||
170 | return r_val if len(r_val) > 0 else '' |
||
171 | return r_val |
||
172 | |||
173 | @staticmethod |
||
174 | def _get_friendly_column_name(name): |
||
175 | if not name: |
||
176 | return None |
||
177 | |||
178 | friendly_name = name.replace('_', ' ').replace('.', ' ').capitalize() |
||
179 | return friendly_name |
||
180 | |||
181 | @staticmethod |
||
182 | def _get_required_column_width(values, minimum_width=0): |
||
183 | max_width = len(max(values, key=len)) if values else minimum_width |
||
184 | return max_width if max_width > minimum_width else minimum_width |
||
185 | |||
245 |
It is generally discouraged to redefine built-ins as this makes code very hard to read.