summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicolargo <nicolas@nicolargo.com>2022-08-15 18:16:16 +0200
committernicolargo <nicolas@nicolargo.com>2022-08-15 18:16:16 +0200
commit004288eac5b4ffbcced7149113150d7cc42df28e (patch)
treed8c837e3e01bf76cfaed09ae264f717b1514a49b
parentf1ebbcdcf2294c3414121d37bb2e1840b8b7ed8b (diff)
Improve code quality
-rw-r--r--glances/globals.py30
-rw-r--r--glances/plugins/glances_plugin.py93
2 files changed, 53 insertions, 70 deletions
diff --git a/glances/globals.py b/glances/globals.py
index 90b437ab..09a5bf35 100644
--- a/glances/globals.py
+++ b/glances/globals.py
@@ -13,6 +13,8 @@ import errno
import os
import sys
import platform
+import json
+from operator import itemgetter
# OS constants (some libraries/features are OS-dependent)
BSD = sys.platform.find('bsd') != -1
@@ -43,3 +45,31 @@ def safe_makedirs(path):
raise
else:
raise
+
+
+def json_dumps(data):
+ """Return the object data in a JSON format.
+
+ Manage the issue #815 for Windows OS with UnicodeDecodeError catching.
+ """
+ try:
+ return json.dumps(data)
+ except UnicodeDecodeError:
+ return json.dumps(data, ensure_ascii=False)
+
+
+def json_dumps_dictlist(data, item):
+ if isinstance(data, dict):
+ try:
+ return json_dumps({item: data[item]})
+ except:
+ return None
+ elif isinstance(data, list):
+ try:
+ # Source:
+ # http://stackoverflow.com/questions/4573875/python-get-index-of-dictionary-item-in-list
+ return json_dumps({item: map(itemgetter(item), data)})
+ except:
+ return None
+ else:
+ return None
diff --git a/glances/plugins/glances_plugin.py b/glances/plugins/glances_plugin.py
index a0d1f031..06b04cd4 100644
--- a/glances/plugins/glances_plugin.py
+++ b/glances/plugins/glances_plugin.py
@@ -14,10 +14,9 @@ I am your father...
"""
import re
-import json
import copy
-from operator import itemgetter
+from glances.globals import json_dumps, json_dumps_dictlist
from glances.compat import iterkeys, itervalues, listkeys, map, mean, nativestr, PY3
from glances.actions import GlancesActions
from glances.history import GlancesHistory
@@ -162,16 +161,6 @@ class GlancesPlugin(object):
"""Return true if plugin is disabled."""
return not self.is_enabled(plugin_name=plugin_name)
- def _json_dumps(self, d):
- """Return the object 'd' in a JSON format.
-
- Manage the issue #815 for Windows OS
- """
- try:
- return json.dumps(d)
- except UnicodeDecodeError:
- return json.dumps(d, ensure_ascii=False)
-
def history_enable(self):
return self.args is not None and not self.args.disable_history and self.get_items_history_list() is not None
@@ -265,24 +254,9 @@ class GlancesPlugin(object):
s = self.get_json_history(nb=nb)
if item is None:
- return self._json_dumps(s)
+ return json_dumps(s)
- if isinstance(s, dict):
- try:
- return self._json_dumps({item: s[item]})
- except KeyError as e:
- logger.error("Cannot get item history {} ({})".format(item, e))
- return None
- elif isinstance(s, list):
- try:
- # Source:
- # http://stackoverflow.com/questions/4573875/python-get-index-of-dictionary-item-in-list
- return self._json_dumps({item: map(itemgetter(item), s)})
- except (KeyError, ValueError) as e:
- logger.error("Cannot get item history {} ({})".format(item, e))
- return None
- else:
- return None
+ return json_dumps_dictlist(s, item)
def get_trend(self, item, nb=6):
"""Get the trend regarding to the last nb values.
@@ -415,7 +389,7 @@ class GlancesPlugin(object):
def get_stats(self):
"""Return the stats object in JSON format."""
- return self._json_dumps(self.stats)
+ return json_dumps(self.stats)
def get_json(self):
"""Return the stats object in JSON format."""
@@ -426,23 +400,7 @@ class GlancesPlugin(object):
Stats should be a list of dict (processlist, network...)
"""
- if isinstance(self.stats, dict):
- try:
- return self._json_dumps({item: self.stats[item]})
- except KeyError as e:
- logger.error("Cannot get item {} ({})".format(item, e))
- return None
- elif isinstance(self.stats, list):
- try:
- # Source:
- # http://stackoverflow.com/questions/4573875/python-get-index-of-dictionary-item-in-list
- # But https://github.com/nicolargo/glances/issues/1401
- return self._json_dumps({item: list(map(itemgetter(item), self.stats))})
- except (KeyError, ValueError) as e:
- logger.error("Cannot get item {} ({})".format(item, e))
- return None
- else:
- return None
+ return json_dumps_dictlist(self.stats, item)
def get_stats_value(self, item, value):
"""Return the stats object for a specific item=value in JSON format.
@@ -455,7 +413,7 @@ class GlancesPlugin(object):
if not isinstance(value, int) and value.isdigit():
value = int(value)
try:
- return self._json_dumps({value: [i for i in self.stats if i[item] == value]})
+ return json_dumps({value: [i for i in self.stats if i[item] == value]})
except (KeyError, ValueError) as e:
logger.error("Cannot get item({})=value({}) ({})".format(item, value, e))
return None
@@ -582,7 +540,7 @@ class GlancesPlugin(object):
def get_json_views(self, item=None, key=None, option=None):
"""Return the views (in JSON)."""
- return self._json_dumps(self.get_views(item, key, option))
+ return json_dumps(self.get_views(item, key, option))
def load_limits(self, config):
"""Load limits from the configuration file, if it exists."""
@@ -1036,33 +994,28 @@ class GlancesPlugin(object):
if width is None:
msg_item = header + '{}'.format(key_name) + separator
- if unit_type == 'float':
- msg_value = '{:.1f}{}'.format(value, unit_short) + trailer
- elif 'min_symbol' in self.fields_description[key]:
- msg_value = (
- '{}{}'.format(
- self.auto_unit(int(value), min_symbol=self.fields_description[key]['min_symbol']), unit_short
- )
- + trailer
- )
- else:
- msg_value = '{}{}'.format(int(value), unit_short) + trailer
+ msg_template_float = '{:.1f}{}'
+ msg_template = '{}{}'
else:
# Define the size of the message
# item will be on the left
# value will be on the right
msg_item = header + '{:{width}}'.format(key_name, width=width - 7) + separator
- if unit_type == 'float':
- msg_value = '{:5.1f}{}'.format(value, unit_short) + trailer
- elif 'min_symbol' in self.fields_description[key]:
- msg_value = (
- '{:>5}{}'.format(
- self.auto_unit(int(value), min_symbol=self.fields_description[key]['min_symbol']), unit_short
- )
- + trailer
+ msg_template_float = '{:5.1f}{}'
+ msg_template = '{:>5}{}'
+
+ if unit_type == 'float':
+ msg_value = msg_template_float.format(value, unit_short) + trailer
+ elif 'min_symbol' in self.fields_description[key]:
+ msg_value = (
+ msg_template.format(
+ self.auto_unit(int(value), min_symbol=self.fields_description[key]['min_symbol']), unit_short
)
- else:
- msg_value = '{:>5}{}'.format(int(value), unit_short) + trailer
+ + trailer
+ )
+ else:
+ msg_value = msg_template.format(int(value), unit_short) + trailer
+
decoration = self.get_views(key=key, option='decoration')
optional = self.get_views(key=key, option='optional')