Skip to content

Commit

Permalink
Merge pull request #2957 from ariel-anieli/plugin-port-msg
Browse files Browse the repository at this point in the history
Fixed typos, refactored  `glances.plugins.ports.PluginModel.msg_curse()`
  • Loading branch information
nicolargo authored Oct 5, 2024
2 parents f27c39c + cbd166b commit 6e485a2
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 43 deletions.
2 changes: 1 addition & 1 deletion glances/plugins/diskio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion glances/plugins/folders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion glances/plugins/fs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion glances/plugins/irq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion glances/plugins/network/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
83 changes: 48 additions & 35 deletions glances/plugins/ports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -166,55 +167,67 @@ 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:
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
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:
Expand Down
2 changes: 1 addition & 1 deletion glances/plugins/sensors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion glances/plugins/smart/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion glances/plugins/wifi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 6e485a2

Please sign in to comment.