Code Duplication    Length = 16-17 lines in 2 locations

glances/plugins/glances_plugin.py 2 locations

@@ 261-276 (lines=16) @@
258
        if item is None:
259
            return self._json_dumps(s)
260
261
        if isinstance(s, dict):
262
            try:
263
                return self._json_dumps({item: s[item]})
264
            except KeyError as e:
265
                logger.error("Cannot get item history {} ({})".format(item, e))
266
                return None
267
        elif isinstance(s, list):
268
            try:
269
                # Source:
270
                # http://stackoverflow.com/questions/4573875/python-get-index-of-dictionary-item-in-list
271
                return self._json_dumps({item: map(itemgetter(item), s)})
272
            except (KeyError, ValueError) as e:
273
                logger.error("Cannot get item history {} ({})".format(item, e))
274
                return None
275
        else:
276
            return None
277
278
    def get_trend(self, item, nb=6):
279
        """Get the trend regarding to the last nb values.
@@ 407-423 (lines=17) @@
404
405
        Stats should be a list of dict (processlist, network...)
406
        """
407
        if isinstance(self.stats, dict):
408
            try:
409
                return self._json_dumps({item: self.stats[item]})
410
            except KeyError as e:
411
                logger.error("Cannot get item {} ({})".format(item, e))
412
                return None
413
        elif isinstance(self.stats, list):
414
            try:
415
                # Source:
416
                # http://stackoverflow.com/questions/4573875/python-get-index-of-dictionary-item-in-list
417
                # But https://github.com/nicolargo/glances/issues/1401
418
                return self._json_dumps({item: list(map(itemgetter(item), self.stats))})
419
            except (KeyError, ValueError) as e:
420
                logger.error("Cannot get item {} ({})".format(item, e))
421
                return None
422
        else:
423
            return None
424
425
    def get_stats_value(self, item, value):
426
        """Return the stats object for a specific item=value in JSON format.