First version of the API

This commit is contained in:
nicolargo 2025-07-06 22:04:17 +02:00
parent 65393b0771
commit 660b94e99d
2 changed files with 79 additions and 0 deletions

24
glances/api.py Normal file
View File

@ -0,0 +1,24 @@
#
# Glances - An eye on your system
#
# SPDX-FileCopyrightText: 2025 Nicolas Hennion <nicolas@nicolargo.com>
#
# 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)

55
tests/test_api.py Executable file
View File

@ -0,0 +1,55 @@
#!/usr/bin/env python
#
# Glances - An eye on your system
#
# SPDX-FileCopyrightText: 2025 Nicolas Hennion <nicolas@nicolargo.com>
#
# 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)