Code Duplication    Length = 21-24 lines in 2 locations

glances/plugins/glances_plugin.py 2 locations

@@ 174-197 (lines=24) @@
171
        See get_raw_history for a full description"""
172
        return self.get_raw_history(item=item)
173
174
    def get_stats_history(self, item=None, nb=0):
175
        """Return the stats history as a JSON object (dict or None).
176
        Limit to lasts nb items (all if nb=0)"""
177
        s = self.get_json_history(nb=nb)
178
179
        if item is None:
180
            return self._json_dumps(s)
181
182
        if isinstance(s, dict):
183
            try:
184
                return self._json_dumps({item: s[item]})
185
            except KeyError as e:
186
                logger.error("Cannot get item history {0} ({1})".format(item, e))
187
                return None
188
        elif isinstance(s, list):
189
            try:
190
                # Source:
191
                # http://stackoverflow.com/questions/4573875/python-get-index-of-dictionary-item-in-list
192
                return self._json_dumps({item: map(itemgetter(item), s)})
193
            except (KeyError, ValueError) as e:
194
                logger.error("Cannot get item history {0} ({1})".format(item, e))
195
                return None
196
        else:
197
            return None
198
199
    @property
200
    def input_method(self):
@@ 295-315 (lines=21) @@
292
        """Return the stats object in JSON format."""
293
        return self._json_dumps(self.stats)
294
295
    def get_stats_item(self, item):
296
        """Return the stats object for a specific item in JSON format.
297
298
        Stats should be a list of dict (processlist, network...)
299
        """
300
        if isinstance(self.stats, dict):
301
            try:
302
                return self._json_dumps({item: self.stats[item]})
303
            except KeyError as e:
304
                logger.error("Cannot get item {} ({})".format(item, e))
305
                return None
306
        elif isinstance(self.stats, list):
307
            try:
308
                # Source:
309
                # http://stackoverflow.com/questions/4573875/python-get-index-of-dictionary-item-in-list
310
                return self._json_dumps({item: map(itemgetter(item), self.stats)})
311
            except (KeyError, ValueError) as e:
312
                logger.error("Cannot get item {} ({})".format(item, e))
313
                return None
314
        else:
315
            return None
316
317
    def get_stats_value(self, item, value):
318
        """Return the stats object for a specific item=value in JSON format.