From cadd32dac52b2e2638533fa52dd652f3f94ce7ef Mon Sep 17 00:00:00 2001 From: nicolargo Date: Mon, 21 Sep 2020 14:49:58 +0200 Subject: Report implementation of #1733 to stdout-csv --- docs/quickstart.rst | 3 ++ glances/compat.py | 40 ---------------- glances/outputs/glances_stdout.py | 18 ------- glances/outputs/glances_stdout_csv.py | 90 ++++------------------------------- 4 files changed, 13 insertions(+), 138 deletions(-) diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 566d0cfb..a8334ee2 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -31,6 +31,9 @@ It is also possible to display RAW JSON stats directly to stdout using: $ glances --stdout load,cpu.user,mem.used,network:lo,processlist:456.cpu_percent Syntax (comma separated stats): + +.. code-block:: console + - - . - : diff --git a/glances/compat.py b/glances/compat.py index 00374a1d..bdc6273a 100644 --- a/glances/compat.py +++ b/glances/compat.py @@ -303,43 +303,3 @@ def get_stat_from_path(stats, stat_path): logger.error('Plugin {} does not exist or is disabled'.format(plugin)) return ret - - - -def get_stat_from_path_old(stats, stat_path): - """ - For a path ['cpu, 'user'] or ['processlist', 'pid=534', 'name'] - Get the value in the stats. - """ - ret = None - if len(stat_path) == 0: - ret = stats - elif len(stat_path) == 1 and stats: - try: - ret = stats[stat_path[0]] - except KeyError: - logger.error( - 'Key {} does not exist. Should be one of: {}'.format(stat_path[0], - ', '.join(stats.keys()))) - elif stats: - if '=' in stat_path[0]: - stat, match = stat_path[0].split('=') - try: - match_dict = next( - (sub for sub in stats if str(sub[stat]) == str(match)), None) - except KeyError: - if len(stats) > 0: - logger.error( - 'Key {} does not exist. Should be one of: {}'.format(stat, - ', '.join(stats[0].keys()))) - else: - logger.error('Key {} does not exist'.format(stat)) - else: - ret = get_stat_from_path(match_dict, stat_path[1:]) - else: - try: - ret = get_stat_from_path(stats[stat_path[0]], stat_path[1:]) - except TypeError: - logger.error( - 'Multiple {}, please select one using {}='.format(stat_path[0], stat_path[0])) - return ret diff --git a/glances/outputs/glances_stdout.py b/glances/outputs/glances_stdout.py index d3fca1c9..9bf34665 100644 --- a/glances/outputs/glances_stdout.py +++ b/glances/outputs/glances_stdout.py @@ -48,21 +48,3 @@ class GlancesStdout(object): # Wait until next refresh if duration > 0: time.sleep(duration) - - def update_old(self, stats, duration=3): - for stat_name in self.args.stdout.split(','): - stat_path = stat_name.split('.') - plugin = stat_path[0] - stat_path = stat_path[1:] - if plugin in stats.getPluginsList() and \ - stats.get_plugin(plugin).is_enable(): - printandflush("{}: {}".format(stat_name, - get_stat_from_path(stats.get_plugin(plugin).get_export(), - stat_path))) - else: - logger.error('Plugin {} does not exist or is disabled'.format(plugin)) - continue - - # Wait until next refresh - if duration > 0: - time.sleep(duration) diff --git a/glances/outputs/glances_stdout_csv.py b/glances/outputs/glances_stdout_csv.py index 7fd34a1f..2d5f08f5 100644 --- a/glances/outputs/glances_stdout_csv.py +++ b/glances/outputs/glances_stdout_csv.py @@ -22,7 +22,7 @@ import time from glances.logger import logger -from glances.compat import printandflush +from glances.compat import printandflush, get_stat_from_path, iteritems class GlancesStdoutCsv(object): @@ -42,70 +42,9 @@ class GlancesStdoutCsv(object): # Display the header only on the first line self.header = True - # Build the list of plugin and/or plugin.attribute to display - self.plugins_list = self.build_list() - - def build_list(self): - """Return a list of tuples taken from self.args.stdout - [(plugin, attribute), ... ]""" - ret = [] - for p in self.args.stdout_csv.split(','): - if '.' in p: - p, a = p.split('.') - else: - a = None - ret.append((p, a)) - return ret - def end(self): pass - def build_header(self, plugin, attribute, stat): - """Build and return the header line""" - line = '' - - if attribute is not None: - line += '{}.{}{}'.format(plugin, attribute, self.separator) - else: - if isinstance(stat, dict): - for k in stat.keys(): - line += '{}.{}{}'.format(plugin, - str(k), - self.separator) - elif isinstance(stat, list): - for i in stat: - if isinstance(i, dict) and 'key' in i: - for k in i.keys(): - line += '{}.{}.{}{}'.format(plugin, - str(i['key']), - str(k), - self.separator) - else: - line += '{}{}'.format(plugin, self.separator) - - return line - - def build_data(self, plugin, attribute, stat): - """Build and return the data line""" - line = '' - - if attribute is not None: - line += '{}{}'.format(str(stat.get(attribute, self.na)), - self.separator) - else: - if isinstance(stat, dict): - for v in stat.values(): - line += '{}{}'.format(str(v), self.separator) - elif isinstance(stat, list): - for i in stat: - if isinstance(i, dict) and 'key' in i: - for v in i.values(): - line += '{}{}'.format(str(v), self.separator) - else: - line += '{}{}'.format(str(stat), self.separator) - - return line - def update(self, stats, duration=3): @@ -113,26 +52,17 @@ class GlancesStdoutCsv(object): Refresh every duration second. """ # Build the stats list - line = '' - for plugin, attribute in self.plugins_list: - # Check if the plugin exist and is enable - if plugin in stats.getPluginsList() and \ - stats.get_plugin(plugin).is_enable(): - stat = stats.get_plugin(plugin).get_export() - else: - continue - - # Build the line to display (header or data) - if self.header: - line += self.build_header(plugin, attribute, stat) - else: - line += self.build_data(plugin, attribute, stat) + stat_flatten = dict() + for stat_name in self.args.stdout_csv.split(','): + stat_path = stat_name.split('.') + stat_flatten.update(get_stat_from_path(stats, stat_path)) - # Display the line (without the last 'separator') - printandflush(line[:-1]) + if self.header: + # Display header one time + printandflush(self.separator.join(stat_flatten.keys())) + self.header = False - # Display header one time - self.header = False + printandflush(self.separator.join([str(i) for i in stat_flatten.values()])) # Wait until next refresh if duration > 0: -- cgit v1.2.3