From 660b94e99d58a0c683e69f0737a909d645cbab39 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sun, 6 Jul 2025 22:04:17 +0200 Subject: [PATCH] First version of the API --- glances/api.py | 24 +++++++++++++++++++++ tests/test_api.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 glances/api.py create mode 100755 tests/test_api.py diff --git a/glances/api.py b/glances/api.py new file mode 100644 index 00000000..1d94051d --- /dev/null +++ b/glances/api.py @@ -0,0 +1,24 @@ +# +# Glances - An eye on your system +# +# SPDX-FileCopyrightText: 2025 Nicolas Hennion +# +# SPDX-License-Identifier: LGPL-3.0-only +# + +from glances import __version__ as glances_version +from glances.main import GlancesMain +from glances.stats import GlancesStats + + +class GlancesAPI: + def __init__(self): + self.__version__ = glances_version.split('.')[0] # Get the major version + + core = GlancesMain(args_begin_at=2) + self._stats = GlancesStats(config=core.get_config(), args=core.get_args()) + + for p in self._stats.getPluginsList(): + plugin = self._stats.get_plugin(p) + if plugin is not None: + setattr(self, p, plugin) diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100755 index 00000000..21e3cf93 --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +# +# Glances - An eye on your system +# +# SPDX-FileCopyrightText: 2025 Nicolas Hennion +# +# SPDX-License-Identifier: LGPL-3.0-only +# + +"""Glances API unitary tests suite.""" + +from glances import __version__, api + +# Global variables +# ================= + +# Init Glances API +# test_config = core.get_config() +# test_args = core.get_args() +gl = api.GlancesAPI() + + +# Pytest functions to test the Glances API version +def test_glances_api_version(): + assert gl.__version__ == __version__.split('.')[0] + + +def test_glances_api_plugin_cpu(): + # Check that the cpu plugin is available + assert gl.cpu is not None + # Update stats + gl.cpu.update() + # Get list of keys (cpu stat fields) + keys = gl.cpu.keys() + # Check that the keys are not empty + assert len(keys) > 0 + # Check that the cpu plugin has a total item + assert gl.cpu['total'] is not None + # and is a float + assert isinstance(gl.cpu['total'], float) + # test the get method + assert isinstance(gl.cpu.get('total'), float) + + +def test_glances_api_plugin_network(): + # Check that the network plugin is available + assert gl.network is not None + # Update stats + gl.network.update() + # Get list of keys (interfaces) + keys = gl.network.keys() + # Check that the keys are not empty + assert len(keys) > 0 + # Check that the first item is a dictionary + assert isinstance(gl.network[keys[0]], dict)