| Conditions | 20 |
| Total Lines | 105 |
| Code Lines | 64 |
| 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.network.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 | # |
||
| 232 | def msg_curse(self, args=None, max_width=None): |
||
| 233 | """Return the dict to display in the curse interface.""" |
||
| 234 | # Init the return message |
||
| 235 | ret = [] |
||
| 236 | |||
| 237 | # Only process if stats exist and display plugin enable... |
||
| 238 | if not self.stats or self.is_disabled(): |
||
| 239 | return ret |
||
| 240 | |||
| 241 | # Max size for the interface name |
||
| 242 | if max_width: |
||
| 243 | name_max_width = max_width - 12 |
||
| 244 | else: |
||
| 245 | # No max_width defined, return an empty curse message |
||
| 246 | logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") |
||
| 247 | return ret |
||
| 248 | |||
| 249 | # Header |
||
| 250 | msg = '{:{width}}'.format('NETWORK', width=name_max_width) |
||
| 251 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
| 252 | if args.network_cumul: |
||
| 253 | # Cumulative stats |
||
| 254 | if args.network_sum: |
||
| 255 | # Sum stats |
||
| 256 | msg = '{:>14}'.format('Rx+Tx') |
||
| 257 | ret.append(self.curse_add_line(msg)) |
||
| 258 | else: |
||
| 259 | # Rx/Tx stats |
||
| 260 | msg = '{:>7}'.format('Rx') |
||
| 261 | ret.append(self.curse_add_line(msg)) |
||
| 262 | msg = '{:>7}'.format('Tx') |
||
| 263 | ret.append(self.curse_add_line(msg)) |
||
| 264 | else: |
||
| 265 | # Bitrate stats |
||
| 266 | if args.network_sum: |
||
| 267 | # Sum stats |
||
| 268 | msg = '{:>14}'.format('Rx+Tx/s') |
||
| 269 | ret.append(self.curse_add_line(msg)) |
||
| 270 | else: |
||
| 271 | msg = '{:>7}'.format('Rx/s') |
||
| 272 | ret.append(self.curse_add_line(msg)) |
||
| 273 | msg = '{:>7}'.format('Tx/s') |
||
| 274 | ret.append(self.curse_add_line(msg)) |
||
| 275 | # Interface list (sorted by name) |
||
| 276 | for i in self.sorted_stats(): |
||
| 277 | # Do not display interface in down state (issue #765) |
||
| 278 | if ('is_up' in i) and (i['is_up'] is False): |
||
| 279 | continue |
||
| 280 | # Hide stats if never be different from 0 (issue #1787) |
||
| 281 | if all(self.get_views(item=i[self.get_key()], key=f, option='hidden') for f in self.hide_zero_fields): |
||
| 282 | continue |
||
| 283 | # Format stats |
||
| 284 | # Is there an alias for the interface name ? |
||
| 285 | if i['alias'] is None: |
||
| 286 | if_name = i['interface_name'].split(':')[0] |
||
| 287 | else: |
||
| 288 | if_name = i['alias'] |
||
| 289 | if len(if_name) > name_max_width: |
||
| 290 | # Cut interface name if it is too long |
||
| 291 | if_name = '_' + if_name[-name_max_width + 1 :] |
||
| 292 | |||
| 293 | if args.byte: |
||
| 294 | # Bytes per second (for dummy) |
||
| 295 | to_bit = 1 |
||
| 296 | unit = '' |
||
| 297 | else: |
||
| 298 | # Bits per second (for real network administrator | Default) |
||
| 299 | to_bit = 8 |
||
| 300 | unit = 'b' |
||
| 301 | |||
| 302 | if args.network_cumul and 'bytes_recv' in i and 'bytes_sent' in i: |
||
| 303 | rx = self.auto_unit(int(i['bytes_recv'] * to_bit)) + unit |
||
| 304 | tx = self.auto_unit(int(i['bytes_sent'] * to_bit)) + unit |
||
| 305 | ax = self.auto_unit(int(i['bytes_all'] * to_bit)) + unit |
||
| 306 | elif 'bytes_recv_rate_per_sec' in i and 'bytes_sent_rate_per_sec' in i: |
||
| 307 | rx = self.auto_unit(int(i['bytes_recv_rate_per_sec'] * to_bit)) + unit |
||
| 308 | tx = self.auto_unit(int(i['bytes_sent_rate_per_sec'] * to_bit)) + unit |
||
| 309 | ax = self.auto_unit(int(i['bytes_all_rate_per_sec'] * to_bit)) + unit |
||
| 310 | else: |
||
| 311 | # Avoid issue when a new interface is created on the fly |
||
| 312 | # Example: start Glances, then start a new container |
||
| 313 | continue |
||
| 314 | |||
| 315 | # New line |
||
| 316 | ret.append(self.curse_new_line()) |
||
| 317 | msg = '{:{width}}'.format(if_name, width=name_max_width) |
||
| 318 | ret.append(self.curse_add_line(msg)) |
||
| 319 | if args.network_sum: |
||
| 320 | msg = f'{ax:>14}' |
||
| 321 | ret.append(self.curse_add_line(msg)) |
||
| 322 | else: |
||
| 323 | msg = f'{rx:>7}' |
||
| 324 | ret.append( |
||
| 325 | self.curse_add_line( |
||
| 326 | msg, self.get_views(item=i[self.get_key()], key='bytes_recv', option='decoration') |
||
| 327 | ) |
||
| 328 | ) |
||
| 329 | msg = f'{tx:>7}' |
||
| 330 | ret.append( |
||
| 331 | self.curse_add_line( |
||
| 332 | msg, self.get_views(item=i[self.get_key()], key='bytes_sent', option='decoration') |
||
| 333 | ) |
||
| 334 | ) |
||
| 335 | |||
| 336 | return ret |
||
| 337 |