First version of the API

This commit is contained in:
nicolargo 2025-07-06 22:03:58 +02:00
parent 45c3c489a0
commit 65393b0771
3 changed files with 55 additions and 5 deletions

View File

@ -91,6 +91,9 @@ test: ## Run All unit tests
test-core: ## Run Core unit tests
$(PYTEST) tests/test_core.py
test-api: ## Run API unit tests
$(PYTEST) tests/test_api.py
test-memoryleak: ## Run Memory-leak unit tests
$(PYTEST) tests/test_memoryleak.py

View File

@ -375,6 +375,13 @@ def json_loads(data: Union[str, bytes, bytearray]) -> Union[dict, list]:
return json.loads(data)
def list_to_dict(data):
"""Convert a list of dict (with key in 'key') to a dict with key as key and value as value."""
if not isinstance(data, list):
return None
return {item[item['key']]: item for item in data if 'key' in item}
def dictlist(data, item):
if isinstance(data, dict):
try:

View File

@ -17,7 +17,17 @@ import re
from glances.actions import GlancesActions
from glances.events_list import glances_events
from glances.globals import dictlist, dictlist_json_dumps, iterkeys, itervalues, json_dumps, listkeys, mean, nativestr
from glances.globals import (
dictlist,
dictlist_json_dumps,
iterkeys,
itervalues,
json_dumps,
list_to_dict,
listkeys,
mean,
nativestr,
)
from glances.history import GlancesHistory
from glances.logger import logger
from glances.outputs.glances_unicode import unicode_message
@ -128,14 +138,44 @@ class GlancesPluginModel:
self.stats_previous = None
self.reset()
def __repr__(self):
"""Return the raw stats."""
return str(self.stats)
def __str__(self):
"""Return the human-readable stats."""
return str(self.stats)
def __repr__(self):
"""Return the raw stats."""
if isinstance(self.stats, list):
return str(list_to_dict(self.stats))
return str(self.stats)
def __getitem__(self, item):
"""Return the stats item."""
if self.stats is not None:
# Stats is a dict try, to return the item
if isinstance(self.stats, dict) and item in self.stats:
return self.stats[item]
if isinstance(self.stats, list):
ltd = list_to_dict(self.stats)
if item in ltd:
return ltd[item]
raise KeyError(f"'{self.__class__.__name__}' object has no key '{item}'")
def keys(self):
"""Return the keys of the stats."""
if isinstance(self.stats, dict):
return listkeys(self.stats)
if isinstance(self.stats, list):
return listkeys(list_to_dict(self.stats))
return []
def get(self, item, default=None):
"""Return the stats item or default if not found."""
try:
return self[item]
except KeyError:
return default
def get_init_value(self):
"""Return a copy of the init value."""
return copy.copy(self.stats_init_value)