From 996414e82920b9aa508b9b8a2415b6ef192c7bf0 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Wed, 10 May 2017 14:52:32 +0200 Subject: [PATCH] Add getViews() method for then XML API, first step for the issue977 --- glances/plugins/glances_plugin.py | 4 ++++ glances/stats.py | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/glances/plugins/glances_plugin.py b/glances/plugins/glances_plugin.py index 4603a89f..98bec585 100644 --- a/glances/plugins/glances_plugin.py +++ b/glances/plugins/glances_plugin.py @@ -457,6 +457,10 @@ class GlancesPlugin(object): else: return 'DEFAULT' + def get_json_views(self, item=None, key=None, option=None): + """Return views in JSON""" + return self._json_dumps(self.get_views(item, key, option)) + def load_limits(self, config): """Load limits from the configuration file, if it exists.""" diff --git a/glances/stats.py b/glances/stats.py index ce320781..05414aa7 100644 --- a/glances/stats.py +++ b/glances/stats.py @@ -54,9 +54,21 @@ class GlancesStats(object): The goal is to dynamically generate the following methods: - getPlugname(): return Plugname stat in JSON format + - getViewsPlugname(): return views of the Plugname stat in JSON format """ # Check if the attribute starts with 'get' - if item.startswith('get'): + if item.startswith('getViews'): + # Get the plugin name + plugname = item[len('getViews'):].lower() + # Get the plugin instance + plugin = self._plugins[plugname] + if hasattr(plugin, 'get_json_views'): + # The method get_views exist, return it + return getattr(plugin, 'get_json_views') + else: + # The method get_views is not found for the plugin + raise AttributeError(item) + elif item.startswith('get'): # Get the plugin name plugname = item[len('get'):].lower() # Get the plugin instance