Test Failed
Push — develop ( 45c3c4...3a1e18 )
by Nicolas
05:03 queued 16s
created

glances.api   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A GlancesAPI.__init__() 0 13 4
A GlancesAPI.plugins() 0 3 1
A GlancesAPI.__getattr__() 0 12 4
1
#
2
# Glances - An eye on your system
3
#
4
# SPDX-FileCopyrightText: 2025 Nicolas Hennion <[email protected]>
5
#
6
# SPDX-License-Identifier: LGPL-3.0-only
7
#
8
9
from glances import __version__ as glances_version
10
from glances.globals import weak_lru_cache
11
from glances.main import GlancesMain
12
from glances.stats import GlancesStats
13
14
plugin_dependencies_tree = {
15
    'processlist': ['processcount'],
16
}
17
18
19
class GlancesAPI:
20
    ttl = 2.0  # Default cache TTL in seconds
21
22
    def __init__(self, config=None, args=None, args_begin_at=1):
23
        self.__version__ = glances_version.split('.')[0]  # Get the major version
24
25
        core = GlancesMain(args_begin_at)
26
        self.args = args if args is not None else core.get_args()
27
        self.config = config if config is not None else core.get_config()
28
        self._stats = GlancesStats(config=self.config, args=self.args)
29
30
        # Set the cache TTL for the API
31
        self.ttl = self.args.time if self.args.time is not None else self.ttl
32
33
        # Init the stats of all plugins in order to ensure that rate are computed
34
        self._stats.update()
35
36
    @weak_lru_cache(maxsize=1, ttl=ttl)
37
    def __getattr__(self, item):
38
        """Fallback to the stats object for any missing attributes."""
39
        if item in self._stats.getPluginsList():
40
            if item in plugin_dependencies_tree:
41
                # Ensure dependencies are updated before accessing the plugin
42
                for dependency in plugin_dependencies_tree[item]:
43
                    self._stats.get_plugin(dependency).update()
44
            # Update the plugin stats
45
            self._stats.get_plugin(item).update()
46
            return self._stats.get_plugin(item)
47
        raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{item}'")
48
49
    def plugins(self):
50
        """Return the list of available plugins."""
51
        return self._stats.getPluginsList()
52