# -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <nicolas@nicolargo.com>
#
# SPDX-License-Identifier: LGPL-3.0-only
#
"""
I am your father...
...of all Glances model plugins.
"""
import re
import copy
from glances.globals import iterkeys, itervalues, listkeys, mean, nativestr, json_dumps, json_dumps_dictlist, dictlist
from glances.actions import GlancesActions
from glances.history import GlancesHistory
from glances.logger import logger
from glances.events import glances_events
from glances.thresholds import glances_thresholds
from glances.timer import Counter, Timer
from glances.outputs.glances_unicode import unicode_message
fields_unit_short = {'percent': '%'}
fields_unit_type = {
'percent': 'float',
'percents': 'float',
'number': 'int',
'numbers': 'int',
'int': 'int',
'ints': 'int',
'float': 'float',
'floats': 'float',
'second': 'int',
'seconds': 'int',
'byte': 'int',
'bytes': 'int',
}
class GlancesPluginModel(object):
"""Main class for Glances plugin model."""
def __init__(self, args=None, config=None, items_history_list=None, stats_init_value={}, fields_description=None):
"""Init the plugin of plugins model class.
All Glances' plugins model should inherit from this class. Most of the
methods are already implemented in the father classes.
Your plugin should return a dict or a list of dicts (stored in the
self.stats). As an example, you can have a look on the mem plugin
(for dict) or network (for list of dicts).
A plugin should implement:
- the reset method: to set your self.stats variable to {} or []
- the update method: where your self.stats variable is set
and optionally:
- the get_key method: set the key of the dict (only for list of dict)
- all others methods you want to overwrite
:args: args parameters
:config: configuration parameters
:items_history_list: list of items to store in the history
:stats_init_value: Default value for a stats item
"""
# Build the plugin name
# Internal or external module (former prefixed by 'glances.plugins')
_mod = self.__class__.__module__.replace('glances.plugins.', '')
self.plugin_name = _mod.split('.')[0]
if self.plugin_name.startswith('glances_'):
self.plugin_name = self.plugin_name.split('glances_')[1]
logger.debug("Init {} plugin".format(self.plugin_name))
# Init the args
self.args = args
# Init the default alignment (for curses)
self._align = 'left'
# Init the input method
self._input_method = 'local'
self._short_system_name = None
# Init the history list
self.items_history_list = items_history_list
self.stats_history = self.init_stats_history()
# Init the limits (configuration keys) dictionary
self._limits = dict()
if config is not None:
logger.debug('Load section {} in {}'.format(self.plugin_name, config.config_file_paths()))
self.load_limits(config=config)
# Init the alias (dictionnary)
self.alias = self.read_alias()
# Init the actions
self.actions = GlancesActions(args=args)
# Init the views
self.views =