| Conditions | 21 |
| Total Lines | 68 |
| Code Lines | 43 |
| 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.quicklook.PluginModel.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 -*- |
||
| 91 | def msg_curse(self, args=None, max_width=10): |
||
| 92 | """Return the list to display in the UI.""" |
||
| 93 | # Init the return message |
||
| 94 | ret = [] |
||
| 95 | |||
| 96 | # Only process if stats exist... |
||
| 97 | if not self.stats or self.is_disabled(): |
||
| 98 | return ret |
||
| 99 | |||
| 100 | # Define the data: Bar (default behavior) or Sparkline |
||
| 101 | sparkline_tag = False |
||
| 102 | if self.args.sparkline and self.history_enable() and not self.args.client: |
||
| 103 | data = Sparkline(max_width) |
||
| 104 | sparkline_tag = data.available |
||
| 105 | if not sparkline_tag: |
||
| 106 | # Fallback to bar if Sparkline module is not installed |
||
| 107 | data = Bar(max_width, percentage_char=self.get_conf_value('percentage_char', default=['|'])[0]) |
||
| 108 | |||
| 109 | # Build the string message |
||
| 110 | if 'cpu_name' in self.stats and 'cpu_hz_current' in self.stats and 'cpu_hz' in self.stats: |
||
| 111 | msg_name = self.stats['cpu_name'] |
||
| 112 | if self.stats['cpu_hz_current'] and self.stats['cpu_hz']: |
||
| 113 | msg_freq = ' - {:.2f}/{:.2f}GHz'.format( |
||
| 114 | self._hz_to_ghz(self.stats['cpu_hz_current']), self._hz_to_ghz(self.stats['cpu_hz']) |
||
| 115 | ) |
||
| 116 | else: |
||
| 117 | msg_freq = '' |
||
| 118 | if len(msg_name + msg_freq) - 6 <= max_width: |
||
| 119 | ret.append(self.curse_add_line(msg_name)) |
||
| 120 | ret.append(self.curse_add_line(msg_freq)) |
||
| 121 | ret.append(self.curse_new_line()) |
||
| 122 | for key in ['cpu', 'mem', 'swap']: |
||
| 123 | if key == 'cpu' and args.percpu: |
||
| 124 | if sparkline_tag: |
||
| 125 | raw_cpu = self.get_raw_history(item='percpu', nb=data.size) |
||
|
|
|||
| 126 | for cpu_index, cpu in enumerate(self.stats['percpu']): |
||
| 127 | if sparkline_tag: |
||
| 128 | # Sparkline display an history |
||
| 129 | data.percents = [i[1][cpu_index]['total'] for i in raw_cpu] |
||
| 130 | # A simple padding in order to align metrics to the right |
||
| 131 | data.percents += [None] * (data.size - len(data.percents)) |
||
| 132 | else: |
||
| 133 | # Bar only the last value |
||
| 134 | data.percent = cpu['total'] |
||
| 135 | if cpu[cpu['key']] < 10: |
||
| 136 | msg = '{:3}{} '.format(key.upper(), cpu['cpu_number']) |
||
| 137 | else: |
||
| 138 | msg = '{:4} '.format(cpu['cpu_number']) |
||
| 139 | ret.extend(self._msg_create_line(msg, data, key)) |
||
| 140 | ret.append(self.curse_new_line()) |
||
| 141 | else: |
||
| 142 | if sparkline_tag: |
||
| 143 | # Sparkline display an history |
||
| 144 | data.percents = [i[1] for i in self.get_raw_history(item=key, nb=data.size)] |
||
| 145 | # A simple padding in order to align metrics to the right |
||
| 146 | data.percents += [None] * (data.size - len(data.percents)) |
||
| 147 | else: |
||
| 148 | # Bar only the last value |
||
| 149 | data.percent = self.stats[key] |
||
| 150 | msg = '{:4} '.format(key.upper()) |
||
| 151 | ret.extend(self._msg_create_line(msg, data, key)) |
||
| 152 | ret.append(self.curse_new_line()) |
||
| 153 | |||
| 154 | # Remove the last new line |
||
| 155 | ret.pop() |
||
| 156 | |||
| 157 | # Return the message with decoration |
||
| 158 | return ret |
||
| 159 | |||
| 177 |