Conditions | 14 |
Total Lines | 73 |
Code Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like glances.plugins.glances_raid.Plugin.msg_curse() 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 | # -*- coding: utf-8 -*- |
||
80 | def msg_curse(self, args=None, max_width=None): |
||
81 | """Return the dict to display in the curse interface.""" |
||
82 | # Init the return message |
||
83 | ret = [] |
||
84 | |||
85 | # Only process if stats exist... |
||
86 | if not self.stats or self.is_disable(): |
||
87 | return ret |
||
88 | |||
89 | # Max size for the interface name |
||
90 | name_max_width = max_width - 12 |
||
91 | |||
92 | # Header |
||
93 | msg = '{:{width}}'.format('RAID disks', |
||
94 | width=name_max_width) |
||
95 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
96 | msg = '{:>7}'.format('Used') |
||
97 | ret.append(self.curse_add_line(msg)) |
||
98 | msg = '{:>7}'.format('Avail') |
||
99 | ret.append(self.curse_add_line(msg)) |
||
100 | # Data |
||
101 | arrays = sorted(iterkeys(self.stats)) |
||
102 | for array in arrays: |
||
103 | # New line |
||
104 | ret.append(self.curse_new_line()) |
||
105 | # Display the current status |
||
106 | status = self.raid_alert(self.stats[array]['status'], |
||
107 | self.stats[array]['used'], |
||
108 | self.stats[array]['available'], |
||
109 | self.stats[array]['type']) |
||
110 | # Data: RAID type name | disk used | disk available |
||
111 | array_type = self.stats[array]['type'].upper() if self.stats[array]['type'] is not None else 'UNKNOWN' |
||
112 | # Build the full name = array type + array name |
||
113 | full_name = '{} {}'.format(array_type, array) |
||
114 | msg = '{:{width}}'.format(full_name, |
||
115 | width=name_max_width) |
||
116 | ret.append(self.curse_add_line(msg)) |
||
117 | if self.stats[array]['type'] == 'raid0' and self.stats[array]['status'] == 'active': |
||
118 | msg = '{:>7}'.format(len(self.stats[array]['components'])) |
||
119 | ret.append(self.curse_add_line(msg, status)) |
||
120 | msg = '{:>7}'.format('-') |
||
121 | ret.append(self.curse_add_line(msg, status)) |
||
122 | elif self.stats[array]['status'] == 'active': |
||
123 | msg = '{:>7}'.format(self.stats[array]['used']) |
||
124 | ret.append(self.curse_add_line(msg, status)) |
||
125 | msg = '{:>7}'.format(self.stats[array]['available']) |
||
126 | ret.append(self.curse_add_line(msg, status)) |
||
127 | elif self.stats[array]['status'] == 'inactive': |
||
128 | ret.append(self.curse_new_line()) |
||
129 | msg = '└─ Status {}'.format(self.stats[array]['status']) |
||
130 | ret.append(self.curse_add_line(msg, status)) |
||
131 | components = sorted(iterkeys(self.stats[array]['components'])) |
||
132 | for i, component in enumerate(components): |
||
133 | if i == len(components) - 1: |
||
134 | tree_char = '└─' |
||
135 | else: |
||
136 | tree_char = '├─' |
||
137 | ret.append(self.curse_new_line()) |
||
138 | msg = ' {} disk {}: '.format(tree_char, self.stats[array]['components'][component]) |
||
139 | ret.append(self.curse_add_line(msg)) |
||
140 | msg = '{}'.format(component) |
||
141 | ret.append(self.curse_add_line(msg)) |
||
142 | if self.stats[array]['type'] != 'raid0' and (self.stats[array]['used'] < self.stats[array]['available']): |
||
143 | # Display current array configuration |
||
144 | ret.append(self.curse_new_line()) |
||
145 | msg = '└─ Degraded mode' |
||
146 | ret.append(self.curse_add_line(msg, status)) |
||
147 | if len(self.stats[array]['config']) < 17: |
||
148 | ret.append(self.curse_new_line()) |
||
149 | msg = ' └─ {}'.format(self.stats[array]['config'].replace('_', 'A')) |
||
150 | ret.append(self.curse_add_line(msg)) |
||
151 | |||
152 | return ret |
||
153 | |||
170 |