Code Duplication    Length = 21-24 lines in 2 locations

glances/plugins/glances_plugin.py 2 locations

@@ 193-216 (lines=24) @@
190
        See get_raw_history for a full description"""
191
        return self.get_raw_history(item=item)
192
193
    def get_stats_history(self, item=None, nb=0):
194
        """Return the stats history as a JSON object (dict or None).
195
        Limit to lasts nb items (all if nb=0)"""
196
        s = self.get_json_history(nb=nb)
197
198
        if item is None:
199
            return self._json_dumps(s)
200
201
        if isinstance(s, dict):
202
            try:
203
                return self._json_dumps({item: s[item]})
204
            except KeyError as e:
205
                logger.error("Cannot get item history {} ({})".format(item, e))
206
                return None
207
        elif isinstance(s, list):
208
            try:
209
                # Source:
210
                # http://stackoverflow.com/questions/4573875/python-get-index-of-dictionary-item-in-list
211
                return self._json_dumps({item: map(itemgetter(item), s)})
212
            except (KeyError, ValueError) as e:
213
                logger.error("Cannot get item history {} ({})".format(item, e))
214
                return None
215
        else:
216
            return None
217
218
    @property
219
    def input_method(self):
@@ 314-334 (lines=21) @@
311
        """Return the stats object in JSON format."""
312
        return self._json_dumps(self.stats)
313
314
    def get_stats_item(self, item):
315
        """Return the stats object for a specific item in JSON format.
316
317
        Stats should be a list of dict (processlist, network...)
318
        """
319
        if isinstance(self.stats, dict):
320
            try:
321
                return self._json_dumps({item: self.stats[item]})
322
            except KeyError as e:
323
                logger.error("Cannot get item {} ({})".format(item, e))
324
                return None
325
        elif isinstance(self.stats, list):
326
            try:
327
                # Source:
328
                # http://stackoverflow.com/questions/4573875/python-get-index-of-dictionary-item-in-list
329
                return self._json_dumps({item: map(itemgetter(item), self.stats)})
330
            except (KeyError, ValueError) as e:
331
                logger.error("Cannot get item {} ({})".format(item, e))
332
                return None
333
        else:
334
            return None
335
336
    def get_stats_value(self, item, value):
337
        """Return the stats object for a specific item=value in JSON format.