mirror of
https://github.com/nicolargo/glances.git
synced 2025-01-04 07:34:49 +03:00
Replace getters/setters with properties - Round 2
Introduce the new sort_key method. Replace the old multitude ones, thanks to properties too.
This commit is contained in:
parent
929b832b74
commit
c5a2c25b35
@ -87,14 +87,14 @@ class GlancesLogs(object):
|
||||
else:
|
||||
# Default sort is...
|
||||
process_auto_by = 'cpu_percent'
|
||||
glances_processes.setautosortkey(process_auto_by)
|
||||
glances_processes.auto_sort = True
|
||||
glances_processes.sort_key = process_auto_by
|
||||
|
||||
def reset_process_sort(self):
|
||||
"""Reset the process_auto_by variable."""
|
||||
"""Reset the process auto sort key."""
|
||||
# Default sort is...
|
||||
process_auto_by = 'cpu_percent'
|
||||
glances_processes.setautosortkey(process_auto_by)
|
||||
glances_processes.setmanualsortkey(None)
|
||||
glances_processes.auto_sort = True
|
||||
glances_processes.sort_key = 'cpu_percent'
|
||||
|
||||
def add(self, item_state, item_type, item_value,
|
||||
proc_list=None, proc_desc="", peak_time=3):
|
||||
|
@ -250,11 +250,11 @@ class GlancesProcesses(object):
|
||||
self.process_tree = None
|
||||
|
||||
# Init stats
|
||||
self.resetsort()
|
||||
self.auto_sort = True
|
||||
self._sort_key = 'cpu_percent'
|
||||
self.allprocesslist = []
|
||||
self.processlist = []
|
||||
self.processcount = {
|
||||
'total': 0, 'running': 0, 'sleeping': 0, 'thread': 0}
|
||||
self.processcount = {'total': 0, 'running': 0, 'sleeping': 0, 'thread': 0}
|
||||
|
||||
# Tag to enable/disable the processes stats (to reduce the Glances CPU consumption)
|
||||
# Default is to enable the processes stats
|
||||
@ -263,13 +263,12 @@ class GlancesProcesses(object):
|
||||
# Extended stats for top process is enable by default
|
||||
self.disable_extended_tag = False
|
||||
|
||||
# Maximum number of processes showed in the UI interface
|
||||
# None if no limit
|
||||
self.max_processes = None
|
||||
# Maximum number of processes showed in the UI (None if no limit)
|
||||
self._max_processes = None
|
||||
|
||||
# Process filter is a regular expression
|
||||
self.process_filter = None
|
||||
self.process_filter_re = None
|
||||
self._process_filter = None
|
||||
self._process_filter_re = None
|
||||
|
||||
# Whether or not to hide kernel threads
|
||||
self.no_kernel_threads = False
|
||||
@ -292,46 +291,49 @@ class GlancesProcesses(object):
|
||||
"""Disable extended process stats."""
|
||||
self.disable_extended_tag = True
|
||||
|
||||
def set_max_processes(self, value):
|
||||
"""Set the maximum number of processes showed in the UI interfaces"""
|
||||
self.max_processes = value
|
||||
@property
|
||||
def max_processes(self):
|
||||
"""Get the maximum number of processes showed in the UI."""
|
||||
return self._max_processes
|
||||
|
||||
def get_max_processes(self):
|
||||
"""Get the maximum number of processes showed in the UI interfaces"""
|
||||
return self.max_processes
|
||||
@max_processes.setter
|
||||
def max_processes(self, value):
|
||||
"""Set the maximum number of processes showed in the UI."""
|
||||
self._max_processes = value
|
||||
|
||||
def set_process_filter(self, value):
|
||||
"""Set the process filter"""
|
||||
@property
|
||||
def process_filter(self):
|
||||
"""Get the process filter."""
|
||||
return self._process_filter
|
||||
|
||||
@process_filter.setter
|
||||
def process_filter(self, value):
|
||||
"""Set the process filter."""
|
||||
logger.info("Set process filter to {0}".format(value))
|
||||
self.process_filter = value
|
||||
self._process_filter = value
|
||||
if value is not None:
|
||||
try:
|
||||
self.process_filter_re = re.compile(value)
|
||||
logger.debug(
|
||||
"Process filter regex compilation OK: {0}".format(self.get_process_filter()))
|
||||
self._process_filter_re = re.compile(value)
|
||||
logger.debug("Process filter regex compilation OK: {0}".format(self.process_filter))
|
||||
except Exception:
|
||||
logger.error(
|
||||
"Cannot compile process filter regex: {0}".format(value))
|
||||
self.process_filter_re = None
|
||||
logger.error("Cannot compile process filter regex: {0}".format(value))
|
||||
self._process_filter_re = None
|
||||
else:
|
||||
self.process_filter_re = None
|
||||
self._process_filter_re = None
|
||||
|
||||
def get_process_filter(self):
|
||||
"""Get the process filter"""
|
||||
return self.process_filter
|
||||
|
||||
def get_process_filter_re(self):
|
||||
"""Get the process regular expression compiled"""
|
||||
return self.process_filter_re
|
||||
@property
|
||||
def process_filter_re(self):
|
||||
"""Get the process regular expression compiled."""
|
||||
return self._process_filter_re
|
||||
|
||||
def is_filtered(self, value):
|
||||
"""Return True if the value should be filtered"""
|
||||
if self.get_process_filter() is None:
|
||||
if self.process_filter is None:
|
||||
# No filter => Not filtered
|
||||
return False
|
||||
else:
|
||||
# logger.debug(self.get_process_filter() + " <> " + value + " => " + str(self.get_process_filter_re().match(value) is None))
|
||||
return self.get_process_filter_re().match(value) is None
|
||||
# logger.debug(self.process_filter + " <> " + value + " => " + str(self.process_filter_re.match(value) is None))
|
||||
return self.process_filter_re.match(value) is None
|
||||
|
||||
def disable_kernel_threads(self):
|
||||
""" Ignore kernel threads in process list. """
|
||||
@ -539,8 +541,7 @@ class GlancesProcesses(object):
|
||||
"""
|
||||
# Reset the stats
|
||||
self.processlist = []
|
||||
self.processcount = {
|
||||
'total': 0, 'running': 0, 'sleeping': 0, 'thread': 0}
|
||||
self.processcount = {'total': 0, 'running': 0, 'sleeping': 0, 'thread': 0}
|
||||
|
||||
# Do not process if disable tag is set
|
||||
if self.disable_tag:
|
||||
@ -556,11 +557,11 @@ class GlancesProcesses(object):
|
||||
if self.no_kernel_threads and not is_windows and is_kernel_thread(proc):
|
||||
continue
|
||||
|
||||
# If self.get_max_processes() is None: Only retreive mandatory stats
|
||||
# If self.max_processes is None: Only retreive mandatory stats
|
||||
# Else: retreive mandatory and standard stats
|
||||
s = self.__get_process_stats(proc,
|
||||
mandatory_stats=True,
|
||||
standard_stats=self.get_max_processes() is None)
|
||||
standard_stats=self.max_processes is None)
|
||||
# Continue to the next process if it has to be filtered
|
||||
if s is None or (self.is_filtered(s['cmdline']) and self.is_filtered(s['name'])):
|
||||
continue
|
||||
@ -594,12 +595,12 @@ class GlancesProcesses(object):
|
||||
|
||||
if self._enable_tree:
|
||||
self.process_tree = ProcessTreeNode.build_tree(processdict,
|
||||
self.getsortkey(),
|
||||
self.sort_key,
|
||||
self.no_kernel_threads)
|
||||
|
||||
for i, node in enumerate(self.process_tree):
|
||||
# Only retreive stats for visible processes (get_max_processes)
|
||||
if self.get_max_processes() is not None and i >= self.get_max_processes():
|
||||
# Only retreive stats for visible processes (max_processes)
|
||||
if self.max_processes is not None and i >= self.max_processes:
|
||||
break
|
||||
|
||||
# add standard stats
|
||||
@ -615,22 +616,21 @@ class GlancesProcesses(object):
|
||||
|
||||
else:
|
||||
# Process optimization
|
||||
# Only retreive stats for visible processes (get_max_processes)
|
||||
if self.get_max_processes() is not None:
|
||||
# Only retreive stats for visible processes (max_processes)
|
||||
if self.max_processes is not None:
|
||||
# Sort the internal dict and cut the top N (Return a list of tuple)
|
||||
# tuple=key (proc), dict (returned by __get_process_stats)
|
||||
try:
|
||||
processiter = sorted(
|
||||
processdict.items(), key=lambda x: x[1][self.getsortkey()], reverse=True)
|
||||
processdict.items(), key=lambda x: x[1][self.sort_key], reverse=True)
|
||||
except (KeyError, TypeError) as e:
|
||||
logger.error(
|
||||
"Cannot sort process list by %s (%s)" % (self.getsortkey(), e))
|
||||
logger.error("Cannot sort process list by {0}: {1}".format(self.sort_key, e))
|
||||
logger.error("%s" % str(processdict.items()[0]))
|
||||
# Fallback to all process (issue #423)
|
||||
processloop = processdict.items()
|
||||
first = False
|
||||
else:
|
||||
processloop = processiter[0:self.get_max_processes()]
|
||||
processloop = processiter[0:self.max_processes]
|
||||
first = True
|
||||
else:
|
||||
# Get all processes stats
|
||||
@ -640,7 +640,7 @@ class GlancesProcesses(object):
|
||||
for i in processloop:
|
||||
# Already existing mandatory stats
|
||||
procstat = i[1]
|
||||
if self.get_max_processes() is not None:
|
||||
if self.max_processes is not None:
|
||||
# Update with standard stats
|
||||
# and extended stats but only for TOP (first) process
|
||||
s = self.__get_process_stats(i[0],
|
||||
@ -687,35 +687,17 @@ class GlancesProcesses(object):
|
||||
"""Get the process tree."""
|
||||
return self.process_tree
|
||||
|
||||
def getsortkey(self):
|
||||
"""Get the current sort key"""
|
||||
if self.getmanualsortkey() is not None:
|
||||
return self.getmanualsortkey()
|
||||
else:
|
||||
return self.getautosortkey()
|
||||
@property
|
||||
def sort_key(self):
|
||||
"""Get the current sort key."""
|
||||
return self._sort_key
|
||||
|
||||
def getmanualsortkey(self):
|
||||
"""Get the current sort key for manual sort."""
|
||||
return self.processmanualsort
|
||||
|
||||
def getautosortkey(self):
|
||||
"""Get the current sort key for automatic sort."""
|
||||
return self.processautosort
|
||||
|
||||
def setmanualsortkey(self, sortedby):
|
||||
"""Set the current sort key for manual sort."""
|
||||
self.processmanualsort = sortedby
|
||||
if self._enable_tree and (self.process_tree is not None):
|
||||
self.process_tree.set_sorting(sortedby, sortedby != "name")
|
||||
|
||||
def setautosortkey(self, sortedby):
|
||||
"""Set the current sort key for automatic sort."""
|
||||
self.processautosort = sortedby
|
||||
|
||||
def resetsort(self):
|
||||
"""Set the default sort: Auto"""
|
||||
self.setmanualsortkey(None)
|
||||
self.setautosortkey('cpu_percent')
|
||||
@sort_key.setter
|
||||
def sort_key(self, key):
|
||||
"""Set the current sort key."""
|
||||
self._sort_key = key
|
||||
if not self.auto_sort and self._enable_tree and self.process_tree is not None:
|
||||
self.process_tree.set_sorting(key, key != "name")
|
||||
|
||||
def getsortlist(self, sortedby=None):
|
||||
"""Get the sorted processlist."""
|
||||
|
@ -36,7 +36,7 @@ class GlancesStandalone(object):
|
||||
self.stats = GlancesStats(config=config, args=args)
|
||||
|
||||
# Default number of processes to displayed is set to 50
|
||||
glances_processes.set_max_processes(50)
|
||||
glances_processes.max_processes = 50
|
||||
|
||||
# If process extended stats is disabled by user
|
||||
if not args.enable_process_extended:
|
||||
@ -48,7 +48,7 @@ class GlancesStandalone(object):
|
||||
|
||||
# Manage optionnal process filter
|
||||
if args.process_filter is not None:
|
||||
glances_processes.set_process_filter(args.process_filter)
|
||||
glances_processes.process_filter = args.process_filter
|
||||
|
||||
if (not is_windows) and args.no_kernel_threads:
|
||||
# Ignore kernel threads in process list
|
||||
|
@ -177,9 +177,6 @@ class _GlancesCurses(object):
|
||||
# Init refresh time
|
||||
self.__refresh_time = args.time
|
||||
|
||||
# Init process sort method
|
||||
self.args.process_sorted_by = 'auto'
|
||||
|
||||
# Init edit filter tag
|
||||
self.edit_filter = False
|
||||
|
||||
@ -257,17 +254,17 @@ class _GlancesCurses(object):
|
||||
# '/' > Switch between short/long name for processes
|
||||
self.args.process_short_name = not self.args.process_short_name
|
||||
elif self.pressedkey == ord('a'):
|
||||
# 'a' > Sort processes automatically
|
||||
self.args.process_sorted_by = 'auto'
|
||||
glances_processes.resetsort()
|
||||
# 'a' > Sort processes automatically and reset to 'cpu_percent'
|
||||
glances_processes.auto_sort = True
|
||||
glances_processes.sort_key = 'cpu_percent'
|
||||
elif self.pressedkey == ord('b'):
|
||||
# 'b' > Switch between bit/s and Byte/s for network IO
|
||||
# self.net_byteps_tag = not self.net_byteps_tag
|
||||
self.args.byte = not self.args.byte
|
||||
elif self.pressedkey == ord('c'):
|
||||
# 'c' > Sort processes by CPU usage
|
||||
self.args.process_sorted_by = 'cpu_percent'
|
||||
glances_processes.setmanualsortkey(self.args.process_sorted_by)
|
||||
glances_processes.auto_sort = False
|
||||
glances_processes.sort_key = 'cpu_percent'
|
||||
elif self.pressedkey == ord('d'):
|
||||
# 'd' > Show/hide disk I/O stats
|
||||
self.args.disable_diskio = not self.args.disable_diskio
|
||||
@ -295,8 +292,8 @@ class _GlancesCurses(object):
|
||||
self.args.help_tag = not self.args.help_tag
|
||||
elif self.pressedkey == ord('i'):
|
||||
# 'i' > Sort processes by IO rate (not available on OS X)
|
||||
self.args.process_sorted_by = 'io_counters'
|
||||
glances_processes.setmanualsortkey(self.args.process_sorted_by)
|
||||
glances_processes.auto_sort = False
|
||||
glances_processes.sort_key = 'io_counters'
|
||||
elif self.pressedkey == ord('I'):
|
||||
# 'I' > Show/hide IP module
|
||||
self.args.disable_ip = not self.args.disable_ip
|
||||
@ -305,15 +302,15 @@ class _GlancesCurses(object):
|
||||
self.args.disable_log = not self.args.disable_log
|
||||
elif self.pressedkey == ord('m'):
|
||||
# 'm' > Sort processes by MEM usage
|
||||
self.args.process_sorted_by = 'memory_percent'
|
||||
glances_processes.setmanualsortkey(self.args.process_sorted_by)
|
||||
glances_processes.auto_sort = False
|
||||
glances_processes.sort_key = 'memory_percent'
|
||||
elif self.pressedkey == ord('n'):
|
||||
# 'n' > Show/hide network stats
|
||||
self.args.disable_network = not self.args.disable_network
|
||||
elif self.pressedkey == ord('p'):
|
||||
# 'p' > Sort processes by name
|
||||
self.args.process_sorted_by = 'name'
|
||||
glances_processes.setmanualsortkey(self.args.process_sorted_by)
|
||||
glances_processes.auto_sort = False
|
||||
glances_processes.sort_key = 'name'
|
||||
elif self.pressedkey == ord('r'):
|
||||
# 'r' > Reset history
|
||||
self.reset_history_tag = not self.reset_history_tag
|
||||
@ -325,8 +322,8 @@ class _GlancesCurses(object):
|
||||
self.args.disable_sensors = not self.args.disable_sensors
|
||||
elif self.pressedkey == ord('t'):
|
||||
# 't' > Sort processes by TIME usage
|
||||
self.args.process_sorted_by = 'cpu_times'
|
||||
glances_processes.setmanualsortkey(self.args.process_sorted_by)
|
||||
glances_processes.auto_sort = False
|
||||
glances_processes.sort_key = 'cpu_times'
|
||||
elif self.pressedkey == ord('T'):
|
||||
# 'T' > View network traffic as sum Rx+Tx
|
||||
self.args.network_sum = not self.args.network_sum
|
||||
@ -389,7 +386,7 @@ class _GlancesCurses(object):
|
||||
"""New column in the curses interface"""
|
||||
self.column = self.next_column
|
||||
|
||||
def display(self, stats, cs_status="None"):
|
||||
def display(self, stats, cs_status=None):
|
||||
"""Display stats on the screen.
|
||||
|
||||
stats: Stats database to display
|
||||
@ -464,11 +461,10 @@ class _GlancesCurses(object):
|
||||
max_processes_displayed -= 4
|
||||
if max_processes_displayed < 0:
|
||||
max_processes_displayed = 0
|
||||
if glances_processes.get_max_processes() is None or \
|
||||
glances_processes.get_max_processes() != max_processes_displayed:
|
||||
logger.debug("Set number of displayed processes to %s" %
|
||||
max_processes_displayed)
|
||||
glances_processes.set_max_processes(max_processes_displayed)
|
||||
if (glances_processes.max_processes is None or
|
||||
glances_processes.max_processes != max_processes_displayed):
|
||||
logger.debug("Set number of displayed processes to {0}".format(max_processes_displayed))
|
||||
glances_processes.max_processes = max_processes_displayed
|
||||
|
||||
stats_processlist = stats.get_plugin(
|
||||
'processlist').get_stats_display(args=self.args)
|
||||
@ -600,7 +596,7 @@ class _GlancesCurses(object):
|
||||
self.display_plugin(stats_docker)
|
||||
self.new_line()
|
||||
self.display_plugin(stats_processcount)
|
||||
if glances_processes.get_process_filter() is None and cs_status == 'None':
|
||||
if glances_processes.process_filter is None and cs_status is None:
|
||||
# Do not display stats monitor list if a filter exist
|
||||
self.new_line()
|
||||
self.display_plugin(stats_monitor)
|
||||
@ -635,12 +631,12 @@ class _GlancesCurses(object):
|
||||
self.reset_history_tag = False
|
||||
|
||||
# Display edit filter popup
|
||||
# Only in standalone mode (cs_status == 'None')
|
||||
if self.edit_filter and cs_status == 'None':
|
||||
# Only in standalone mode (cs_status is None)
|
||||
if self.edit_filter and cs_status is None:
|
||||
new_filter = self.display_popup(_("Process filter pattern: "),
|
||||
is_input=True,
|
||||
input_value=glances_processes.get_process_filter())
|
||||
glances_processes.set_process_filter(new_filter)
|
||||
input_value=glances_processes.process_filter)
|
||||
glances_processes.process_filter = new_filter
|
||||
elif self.edit_filter and cs_status != 'None':
|
||||
self.display_popup(
|
||||
_("Process filter only available in standalone mode"))
|
||||
@ -815,7 +811,7 @@ class _GlancesCurses(object):
|
||||
"""Erase the content of the screen."""
|
||||
self.term_window.erase()
|
||||
|
||||
def flush(self, stats, cs_status="None"):
|
||||
def flush(self, stats, cs_status=None):
|
||||
"""Clear and update the screen.
|
||||
|
||||
stats: Stats database to display
|
||||
@ -827,7 +823,7 @@ class _GlancesCurses(object):
|
||||
self.erase()
|
||||
self.display(stats, cs_status=cs_status)
|
||||
|
||||
def update(self, stats, cs_status="None", return_to_browser=False):
|
||||
def update(self, stats, cs_status=None, return_to_browser=False):
|
||||
"""Update the screen.
|
||||
|
||||
Wait for __refresh_time sec / catch key every 100 ms.
|
||||
|
@ -81,10 +81,10 @@ class Plugin(GlancesPlugin):
|
||||
return ret
|
||||
|
||||
# Display the filter (if it exists)
|
||||
if glances_processes.get_process_filter() is not None:
|
||||
if glances_processes.process_filter is not None:
|
||||
msg = _("Processes filter:")
|
||||
ret.append(self.curse_add_line(msg, "TITLE"))
|
||||
msg = _(" {0} ").format(glances_processes.get_process_filter())
|
||||
msg = _(" {0} ").format(glances_processes.process_filter)
|
||||
ret.append(self.curse_add_line(msg, "FILTER"))
|
||||
msg = _("(press ENTER to edit)")
|
||||
ret.append(self.curse_add_line(msg))
|
||||
@ -117,13 +117,13 @@ class Plugin(GlancesPlugin):
|
||||
ret.append(self.curse_add_line(msg))
|
||||
|
||||
# Display sort information
|
||||
if glances_processes.getmanualsortkey() is None:
|
||||
if glances_processes.auto_sort:
|
||||
msg = _("sorted automatically")
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = _(" by {0}").format(glances_processes.getautosortkey())
|
||||
msg = _(" by {0}").format(glances_processes.sort_key)
|
||||
ret.append(self.curse_add_line(msg))
|
||||
else:
|
||||
msg = _("sorted by {0}").format(glances_processes.getmanualsortkey())
|
||||
msg = _("sorted by {0}").format(glances_processes.sort_key)
|
||||
ret.append(self.curse_add_line(msg))
|
||||
ret[-1]["msg"] += ", %s view" % ("tree" if glances_processes.is_tree_enabled() else "flat")
|
||||
|
||||
|
@ -365,7 +365,7 @@ class Plugin(GlancesPlugin):
|
||||
return ret
|
||||
|
||||
# Compute the sort key
|
||||
process_sort_key = glances_processes.getsortkey()
|
||||
process_sort_key = glances_processes.sort_key
|
||||
sort_style = 'SORT'
|
||||
|
||||
# Header
|
||||
@ -401,7 +401,7 @@ class Plugin(GlancesPlugin):
|
||||
ret.extend(self.get_process_tree_curses_data(self.sortstats(process_sort_key),
|
||||
args,
|
||||
first_level=True,
|
||||
max_node_count=glances_processes.get_max_processes()))
|
||||
max_node_count=glances_processes.max_processes))
|
||||
else:
|
||||
# Loop over processes (sorted by the sort key previously compute)
|
||||
first = True
|
||||
|
Loading…
Reference in New Issue
Block a user