From 3ede32a35904b90d8024b27f3299f31811ee799b Mon Sep 17 00:00:00 2001 From: Ariel Otilibili Date: Fri, 4 Oct 2024 20:19:28 +0200 Subject: [PATCH 1/2] Fixed typos ``` $ git grep 'emptu' | awk -F: '{print $1}' | xargs -I {} sed -ie 's/emptu/empty/' {}; echo $? 0 ``` Signed-off-by: Ariel Otilibili --- glances/plugins/diskio/__init__.py | 2 +- glances/plugins/folders/__init__.py | 2 +- glances/plugins/fs/__init__.py | 2 +- glances/plugins/irq/__init__.py | 2 +- glances/plugins/network/__init__.py | 2 +- glances/plugins/ports/__init__.py | 2 +- glances/plugins/sensors/__init__.py | 2 +- glances/plugins/smart/__init__.py | 2 +- glances/plugins/wifi/__init__.py | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/glances/plugins/diskio/__init__.py b/glances/plugins/diskio/__init__.py index 8864b4786d..a4b314ec13 100644 --- a/glances/plugins/diskio/__init__.py +++ b/glances/plugins/diskio/__init__.py @@ -165,7 +165,7 @@ def msg_curse(self, args=None, max_width=None): if max_width: name_max_width = max_width - 13 else: - # No max_width defined, return an emptu curse message + # No max_width defined, return an empty curse message logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") return ret diff --git a/glances/plugins/folders/__init__.py b/glances/plugins/folders/__init__.py index dd5ce09a7c..eb137a0be7 100644 --- a/glances/plugins/folders/__init__.py +++ b/glances/plugins/folders/__init__.py @@ -132,7 +132,7 @@ def msg_curse(self, args=None, max_width=None): if max_width: name_max_width = max_width - 7 else: - # No max_width defined, return an emptu curse message + # No max_width defined, return an empty curse message logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") return ret diff --git a/glances/plugins/fs/__init__.py b/glances/plugins/fs/__init__.py index 5507aef836..068b952aca 100644 --- a/glances/plugins/fs/__init__.py +++ b/glances/plugins/fs/__init__.py @@ -276,7 +276,7 @@ def msg_curse(self, args=None, max_width=None): if max_width: name_max_width = max_width - 13 else: - # No max_width defined, return an emptu curse message + # No max_width defined, return an empty curse message logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") return ret diff --git a/glances/plugins/irq/__init__.py b/glances/plugins/irq/__init__.py index ff43ae8823..fbc7966eaa 100644 --- a/glances/plugins/irq/__init__.py +++ b/glances/plugins/irq/__init__.py @@ -99,7 +99,7 @@ def msg_curse(self, args=None, max_width=None): if max_width: name_max_width = max_width - 7 else: - # No max_width defined, return an emptu curse message + # No max_width defined, return an empty curse message logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") return ret diff --git a/glances/plugins/network/__init__.py b/glances/plugins/network/__init__.py index 77cca71c09..a562a6b8fc 100644 --- a/glances/plugins/network/__init__.py +++ b/glances/plugins/network/__init__.py @@ -236,7 +236,7 @@ def msg_curse(self, args=None, max_width=None): if max_width: name_max_width = max_width - 12 else: - # No max_width defined, return an emptu curse message + # No max_width defined, return an empty curse message logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") return ret diff --git a/glances/plugins/ports/__init__.py b/glances/plugins/ports/__init__.py index 5ffce4944e..1a588a311c 100644 --- a/glances/plugins/ports/__init__.py +++ b/glances/plugins/ports/__init__.py @@ -179,7 +179,7 @@ def msg_curse(self, args=None, max_width=None): if max_width: name_max_width = max_width - 7 else: - # No max_width defined, return an emptu curse message + # No max_width defined, return an empty curse message logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") return ret diff --git a/glances/plugins/sensors/__init__.py b/glances/plugins/sensors/__init__.py index 8449f01b08..5aa7788a54 100644 --- a/glances/plugins/sensors/__init__.py +++ b/glances/plugins/sensors/__init__.py @@ -269,7 +269,7 @@ def msg_curse(self, args=None, max_width=None): if max_width: name_max_width = max_width - 12 else: - # No max_width defined, return an emptu curse message + # No max_width defined, return an empty curse message logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") return ret diff --git a/glances/plugins/smart/__init__.py b/glances/plugins/smart/__init__.py index 09367ffa8b..35bea3e483 100644 --- a/glances/plugins/smart/__init__.py +++ b/glances/plugins/smart/__init__.py @@ -170,7 +170,7 @@ def msg_curse(self, args=None, max_width=None): if max_width: name_max_width = max_width - 6 else: - # No max_width defined, return an emptu curse message + # No max_width defined, return an empty curse message logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") return ret diff --git a/glances/plugins/wifi/__init__.py b/glances/plugins/wifi/__init__.py index 4b8a5215d4..ed35675a7c 100644 --- a/glances/plugins/wifi/__init__.py +++ b/glances/plugins/wifi/__init__.py @@ -179,7 +179,7 @@ def msg_curse(self, args=None, max_width=None): if max_width: if_name_max_width = max_width - 5 else: - # No max_width defined, return an emptu curse message + # No max_width defined, return an empty curse message logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") return ret From cbd166bf0baef31650f67cda4f357c8f4de38064 Mon Sep 17 00:00:00 2001 From: Ariel Otilibili Date: Fri, 4 Oct 2024 20:39:33 +0200 Subject: [PATCH 2/2] Refactored `glances.plugins.ports.PluginModel.msg_curse()` Part of #2801. Signed-off-by: Ariel Otilibili --- glances/plugins/ports/__init__.py | 81 ++++++++++++++++++------------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/glances/plugins/ports/__init__.py b/glances/plugins/ports/__init__.py index 1a588a311c..34caab02ca 100644 --- a/glances/plugins/ports/__init__.py +++ b/glances/plugins/ports/__init__.py @@ -14,6 +14,7 @@ import subprocess import threading import time +from functools import partial, reduce from glances.globals import BSD, MACOS, WINDOWS, bool_type from glances.logger import logger @@ -166,14 +167,55 @@ def get_web_alert(self, web, header="", log=False): return ret + def set_status_if_host(self, p): + if p['host'] is None: + status = 'None' + elif p['status'] is None: + status = 'Scanning' + elif isinstance(p['status'], bool_type) and p['status'] is True: + status = 'Open' + elif p['status'] == 0: + status = 'Timeout' + else: + # Convert second to ms + status = '{:.0f}ms'.format(p['status'] * 1000.0) + + return status + + def set_status_if_url(self, p): + if isinstance(p['status'], numbers.Number): + status = 'Code {}'.format(p['status']) + elif p['status'] is None: + status = 'Scanning' + else: + status = p['status'] + + return status + + def build_str(self, name_max_width, ret, p): + if 'host' in p: + helper = self.get_ports_alert + status = self.set_status_if_host(p) + elif 'url' in p: + helper = self.get_web_alert + status = self.set_status_if_url(p) + + msg = '{:{width}}'.format(p['description'][0:name_max_width], width=name_max_width) + ret.append(self.curse_add_line(msg)) + msg = f'{status:>9}' + ret.append(self.curse_add_line(msg, helper(p, header=p['indice'] + '_rtt'))) + ret.append(self.curse_new_line()) + + return ret + def msg_curse(self, args=None, max_width=None): """Return the dict to display in the curse interface.""" # Init the return message # Only process if stats exist and display plugin enable... - ret = [] + init = [] if not self.stats or args.disable_ports: - return ret + return init # Max size for the interface name if max_width: @@ -181,40 +223,11 @@ def msg_curse(self, args=None, max_width=None): else: # No max_width defined, return an empty curse message logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") - return ret + return init # Build the string message - for p in self.stats: - if 'host' in p: - if p['host'] is None: - status = 'None' - elif p['status'] is None: - status = 'Scanning' - elif isinstance(p['status'], bool_type) and p['status'] is True: - status = 'Open' - elif p['status'] == 0: - status = 'Timeout' - else: - # Convert second to ms - status = '{:.0f}ms'.format(p['status'] * 1000.0) - - msg = '{:{width}}'.format(p['description'][0:name_max_width], width=name_max_width) - ret.append(self.curse_add_line(msg)) - msg = f'{status:>9}' - ret.append(self.curse_add_line(msg, self.get_ports_alert(p, header=p['indice'] + '_rtt'))) - ret.append(self.curse_new_line()) - elif 'url' in p: - msg = '{:{width}}'.format(p['description'][0:name_max_width], width=name_max_width) - ret.append(self.curse_add_line(msg)) - if isinstance(p['status'], numbers.Number): - status = 'Code {}'.format(p['status']) - elif p['status'] is None: - status = 'Scanning' - else: - status = p['status'] - msg = f'{status:>9}' - ret.append(self.curse_add_line(msg, self.get_web_alert(p, header=p['indice'] + '_rtt'))) - ret.append(self.curse_new_line()) + build_str_with_this_max_width = partial(self.build_str, name_max_width) + ret = reduce(build_str_with_this_max_width, self.stats, init) # Delete the last empty line try: