Test Failed
Push — master ( 7cfc0c...4c3161 )
by Nicolas
04:02
created

GlancesStatsClient.set_plugins()   B

Complexity

Conditions 6

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nop 2
dl 0
loc 26
rs 8.6666
c 0
b 0
f 0
1
#
2
# This file is part of Glances.
3
#
4
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <[email protected]>
5
#
6
# SPDX-License-Identifier: LGPL-3.0-only
7
#
8
9
"""The stats server manager."""
10
11
import importlib
12
import sys
13
14
from glances.globals import sys_path
15
from glances.logger import logger
16
from glances.stats import GlancesStats
17
18
19
class GlancesStatsClient(GlancesStats):
20
    """This class stores, updates and gives stats for the client."""
21
22
    def __init__(self, config=None, args=None):
23
        """Init the GlancesStatsClient class."""
24
        super().__init__(config=config, args=args)
25
26
        # Init the configuration
27
        self.config = config
28
29
        # Init the arguments
30
        self.args = args
31
32
    def set_plugins(self, input_plugins):
33
        """Set the plugin list according to the Glances server."""
34
        header = "glances.plugins."
35
        for item in input_plugins:
36
            # Import the plugin
37
            try:
38
                plugin = importlib.import_module(header + item)
39
            except ImportError as e:
40
                # Server plugin can not be imported from the client side
41
                logger.error(f"Can not import {item} plugin ({e}). Please upgrade your Glances client/server version.")
42
            else:
43
                # Add the plugin to the dictionary
44
                # The key is the plugin name
45
                # for example, the file glances_xxx.py
46
                # generate self._plugins_list["xxx"] = ...
47
                logger.debug(f"Server uses {item} plugin")
48
                if hasattr(plugin, 'PluginModel'):
49
                    # Old fashion way to load the plugin (before Glances 5.0)
50
                    # Should be removed in Glances 5.0 - see #3170
51
                    self._plugins[item] = getattr(plugin, 'PluginModel')(args=self.args)
52
                elif hasattr(plugin, item.capitalize() + 'Plugin'):
53
                    # New fashion way to load the plugin (after Glances 5.0)
54
                    self._plugins[item] = getattr(plugin, item.capitalize() + 'Plugin')(args=self.args)
55
56
        # Restoring system path
57
        sys.path = sys_path
58
59
    def update(self, input_stats):
60
        """Update all the stats."""
61
        # For Glances client mode
62
        for p in input_stats:
63
            # Update plugin stats with items sent by the server
64
            self._plugins[p].set_stats(input_stats[p])
65
            # Update the views for the updated stats
66
            self._plugins[p].update_views()
67