| Total Complexity | 8 |
| Total Lines | 57 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # -*- coding: utf-8 -*- |
||
| 2 | # |
||
| 3 | # This file is part of Glances. |
||
| 4 | # |
||
| 5 | # SPDX-FileCopyrightText: 2022 Nicolas Hennion <[email protected]> |
||
| 6 | # |
||
| 7 | # SPDX-License-Identifier: LGPL-3.0-only |
||
| 8 | # |
||
| 9 | |||
| 10 | """Stdout interface class.""" |
||
| 11 | |||
| 12 | import time |
||
| 13 | |||
| 14 | from glances.logger import logger |
||
| 15 | from glances.compat import printandflush |
||
| 16 | |||
| 17 | |||
| 18 | class GlancesStdoutJson(object): |
||
| 19 | |||
| 20 | """This class manages the Stdout JSON display.""" |
||
| 21 | |||
| 22 | def __init__(self, config=None, args=None): |
||
| 23 | # Init |
||
| 24 | self.config = config |
||
| 25 | self.args = args |
||
| 26 | |||
| 27 | # Build the list of plugin to display |
||
| 28 | self.plugins_list = self.build_list() |
||
| 29 | |||
| 30 | def build_list(self): |
||
| 31 | """Return a list of tuples taken from self.args.stdout_json |
||
| 32 | |||
| 33 | :return: A list of tuples. Example -[(plugin, attribute), ... ] |
||
| 34 | """ |
||
| 35 | return self.args.stdout_json.split(',') |
||
| 36 | |||
| 37 | def end(self): |
||
| 38 | pass |
||
| 39 | |||
| 40 | def update(self, stats, duration=3): |
||
| 41 | """Display stats in JSON format to stdout. |
||
| 42 | |||
| 43 | Refresh every duration second. |
||
| 44 | """ |
||
| 45 | for plugin in self.plugins_list: |
||
| 46 | # Check if the plugin exist and is enable |
||
| 47 | if plugin in stats.getPluginsList() and stats.get_plugin(plugin).is_enabled(): |
||
| 48 | stat = stats.get_plugin(plugin).get_json() |
||
| 49 | else: |
||
| 50 | continue |
||
| 51 | # Display stats |
||
| 52 | printandflush('{}: {}'.format(plugin, stat)) |
||
| 53 | |||
| 54 | # Wait until next refresh |
||
| 55 | if duration > 0: |
||
| 56 | time.sleep(duration) |
||
| 57 |