# -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
# Copyright (C) 2019 Nicolargo <nicolas@nicolargo.com>
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
I am your father...
...for all Glances plugins.
"""
import re
import json
import copy
from operator import itemgetter
from glances.compat import iterkeys, itervalues, listkeys, map, mean, nativestr
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
class GlancesPlugin(object):
"""Main class for Glances plugin."""
def __init__(self,
args=None,
config=None,
items_history_list=None,
stats_init_value={},
fields_description=None):
"""Init the plugin of plugins class.
All Glances' plugins 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 __init__ constructor: define the self.display_curse
- the reset method: to set your self.stats variable to {} or []
- the update method: where your self.stats variable is set
and optionnaly:
- the get_key method: set the key of the dict (only for list of dict)
- the update_view method: only if you need to trick your output
- the msg_curse: define the curse (UI) message (if display_curse is True)
:args: args parameters
:items_history_list: list of items to store in the history
:stats_init_value: Default value for a stats item
"""
# Plugin name (= module name without glances_)
pos = self.__class__.__module__.find('glances_') + len('glances') + 1
self.plugin_name = self.__class__.__module__[pos:]
# logger.debug("Init plugin %s" % self.plugin_name)
# Init the args
self.args = args
# Init the default alignement (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) dictionnary
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 actions
self.actions = GlancesActions(args=args)
# Init the views
self.views = dict()
# Hide stats if all the hide_zero_fields has never been != 0
# Default is False, always display stats
self.hide_zero = False
self.hide_zero_fields = []
# Set the initial refresh time to display stats the first time
self.refresh_timer = Timer(0)
# Init stats description
self.fields_description = fields_description
# Init the stats
self.stats_init_value = stats_init_value
self.stats = None
self.reset()
def __repr__(self):
"""Return the raw stats."""
return self.stats
def __str__(self):
"""Return the human-readable stats."""
return str(self.stats)
def get_init_value(self):
"""Return a copy of the init value."""
return copy.copy(self.stats_init_value)
def reset(self):
"""Reset the stats.
This method should be overwrited by childs' classes.
"""
self.stats = self.get_init_value()
def exit(self):
"""Just log an event when Glances exit."""
logger.debug("Stop the {} plugin".format(self.plugin_name))
def get_key(self):
"""Return the key of the list."""
return None
def is_enable(self, plugin_name=None):
"""Return true if plugin is enabled."""
if not plugin_name:
plugin_name = self.plugin_name
try:
d = getattr(self.args, 'disable_' + plugin_name)
except AttributeError:
d = getattr(self.args, 'enable_' + plugin_name, True)
return d is False
def is_disable(self, plugin_name=None):
"""Return true if plugin is disabled."""
return not self.is_enable(plugin_name=plugin_name)
def _json_dumps(self, d):
"""Return the object 'd' in a JSON format.
Manage the issue #815 for Windows OS
"""
try:
return json.dumps(d)