From 3573c1a2df4c3974956521de3838f06d85515ae5 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sun, 26 Sep 2021 17:47:09 +0200 Subject: [PATCH] Refactor plugins as MVC (only M implement for the moment) --- glances/plugins/README.rst | 21 +++++++------------ .../plugins/alert/{__init__.py => model.py} | 4 ++-- .../plugins/amps/{__init__.py => model.py} | 8 +++---- .../plugins/cloud/{__init__.py => model.py} | 8 +++---- .../connections/{__init__.py => model.py} | 8 +++---- .../plugins/core/{__init__.py => model.py} | 8 +++---- glances/plugins/cpu/{__init__.py => model.py} | 10 ++++----- .../plugins/diskio/{__init__.py => model.py} | 8 +++---- .../plugins/folders/{__init__.py => model.py} | 8 +++---- glances/plugins/fs/{__init__.py => model.py} | 8 +++---- .../glances_docker/{__init__.py => model.py} | 8 +++---- glances/plugins/gpu/{__init__.py => model.py} | 8 +++---- .../plugins/help/{__init__.py => model.py} | 4 ++-- glances/plugins/ip/{__init__.py => model.py} | 8 +++---- glances/plugins/irq/{__init__.py => model.py} | 8 +++---- .../plugins/load/{__init__.py => model.py} | 10 ++++----- glances/plugins/mem/{__init__.py => model.py} | 8 +++---- .../plugins/memswap/{__init__.py => model.py} | 8 +++---- .../plugins/network/{__init__.py => model.py} | 8 +++---- glances/plugins/now/{__init__.py => model.py} | 4 ++-- .../plugins/percpu/{__init__.py => model.py} | 8 +++---- glances/plugins/plugin/__init__.py | 0 .../plugins/{plugin.py => plugin/model.py} | 16 ++++++-------- .../plugins/ports/{__init__.py => model.py} | 8 +++---- .../processcount/{__init__.py => model.py} | 8 +++---- .../processlist/{__init__.py => model.py} | 6 +++--- .../psutilversion/{__init__.py => model.py} | 8 +++---- .../quicklook/{__init__.py => model.py} | 8 +++---- .../plugins/raid/{__init__.py => model.py} | 8 +++---- .../plugins/sensors/{__init__.py => model.py} | 8 +++---- .../sensors/sensor/glances_batpercent.py | 8 +++---- .../plugins/sensors/sensor/glances_hddtemp.py | 8 +++---- .../plugins/smart/{__init__.py => model.py} | 8 +++---- .../plugins/system/{__init__.py => model.py} | 12 +++++++---- .../plugins/uptime/{__init__.py => model.py} | 14 ++++++++----- .../plugins/wifi/{__init__.py => model.py} | 8 +++---- glances/stats.py | 11 +++++----- unitest.py | 2 +- 38 files changed, 156 insertions(+), 158 deletions(-) rename glances/plugins/alert/{__init__.py => model.py} (99%) rename glances/plugins/amps/{__init__.py => model.py} (96%) rename glances/plugins/cloud/{__init__.py => model.py} (97%) rename glances/plugins/connections/{__init__.py => model.py} (97%) rename glances/plugins/core/{__init__.py => model.py} (94%) rename glances/plugins/cpu/{__init__.py => model.py} (98%) rename glances/plugins/diskio/{__init__.py => model.py} (98%) rename glances/plugins/folders/{__init__.py => model.py} (96%) rename glances/plugins/fs/{__init__.py => model.py} (98%) rename glances/plugins/glances_docker/{__init__.py => model.py} (99%) rename glances/plugins/gpu/{__init__.py => model.py} (98%) rename glances/plugins/help/{__init__.py => model.py} (99%) rename glances/plugins/ip/{__init__.py => model.py} (97%) rename glances/plugins/irq/{__init__.py => model.py} (97%) rename glances/plugins/load/{__init__.py => model.py} (96%) rename glances/plugins/mem/{__init__.py => model.py} (98%) rename glances/plugins/memswap/{__init__.py => model.py} (97%) rename glances/plugins/network/{__init__.py => model.py} (98%) rename glances/plugins/now/{__init__.py => model.py} (96%) rename glances/plugins/percpu/{__init__.py => model.py} (95%) create mode 100644 glances/plugins/plugin/__init__.py rename glances/plugins/{plugin.py => plugin/model.py} (98%) rename glances/plugins/ports/{__init__.py => model.py} (98%) rename glances/plugins/processcount/{__init__.py => model.py} (96%) rename glances/plugins/processlist/{__init__.py => model.py} (99%) rename glances/plugins/psutilversion/{__init__.py => model.py} (89%) rename glances/plugins/quicklook/{__init__.py => model.py} (97%) rename glances/plugins/raid/{__init__.py => model.py} (97%) rename glances/plugins/sensors/{__init__.py => model.py} (98%) rename glances/plugins/smart/{__init__.py => model.py} (97%) rename glances/plugins/system/{__init__.py => model.py} (96%) rename glances/plugins/uptime/{__init__.py => model.py} (86%) rename glances/plugins/wifi/{__init__.py => model.py} (97%) diff --git a/glances/plugins/README.rst b/glances/plugins/README.rst index 250357eb..a25bf98b 100644 --- a/glances/plugins/README.rst +++ b/glances/plugins/README.rst @@ -6,21 +6,14 @@ This is the Glances plugins folder. A Glances plugin is a Python module hosted in a folder. -The name of the foo Glances plugin folder is foo (glances_foo). +It should be based on thr MVC model. +- model: datamodel +- view: input for UI +- controler: output from UI -The plugin is a Python class named Plugin inherits the GlancesPlugin object: - -.. code-block:: python - - class Plugin(GlancesPlugin): - """Glances foo plugin.""" - - def __init__(self, args=None, config=None): - super(Plugin, self).__init__(args=args, - config=config, - items_history_list=items_history_list, - fields_description=fields_description) - pass +//// +TODO +//// A plugin should define the following global variables: diff --git a/glances/plugins/alert/__init__.py b/glances/plugins/alert/model.py similarity index 99% rename from glances/plugins/alert/__init__.py rename to glances/plugins/alert/model.py index b92646a0..a51eb451 100644 --- a/glances/plugins/alert/__init__.py +++ b/glances/plugins/alert/model.py @@ -25,7 +25,7 @@ from glances.logger import logger from glances.events import glances_events from glances.thresholds import glances_thresholds # from glances.logger import logger -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel # Static decision tree for the global alert message # - msg: Message to be displayed (result of the decision tree) @@ -177,7 +177,7 @@ def global_message(): return tree[0]['msg'] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances alert plugin. Only for display. diff --git a/glances/plugins/amps/__init__.py b/glances/plugins/amps/model.py similarity index 96% rename from glances/plugins/amps/__init__.py rename to glances/plugins/amps/model.py index d33339e7..b46e6f02 100644 --- a/glances/plugins/amps/__init__.py +++ b/glances/plugins/amps/model.py @@ -21,10 +21,10 @@ from glances.globals import iteritems from glances.amps_list import AmpsList as glancesAmpsList -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances AMPs plugin.""" def __init__(self, args=None, config=None): @@ -45,8 +45,8 @@ class Plugin(GlancesPlugin): """Return the key of the list.""" return 'name' - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update the AMP list.""" # Init new stats diff --git a/glances/plugins/cloud/__init__.py b/glances/plugins/cloud/model.py similarity index 97% rename from glances/plugins/cloud/__init__.py rename to glances/plugins/cloud/model.py index 2743ded8..f63d4e8a 100644 --- a/glances/plugins/cloud/__init__.py +++ b/glances/plugins/cloud/model.py @@ -26,7 +26,7 @@ Supported Cloud API: import threading from glances.globals import iteritems, to_ascii -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel from glances.logger import logger # Import plugin specific dependency @@ -40,7 +40,7 @@ else: import_error_tag = False -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances' cloud plugin. The goal of this plugin is to retreive additional information @@ -73,8 +73,8 @@ class Plugin(GlancesPlugin): # Call the father class super(Plugin, self).exit() - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update the cloud stats. diff --git a/glances/plugins/connections/__init__.py b/glances/plugins/connections/model.py similarity index 97% rename from glances/plugins/connections/__init__.py rename to glances/plugins/connections/model.py index 6417fbac..f6d6d982 100644 --- a/glances/plugins/connections/__init__.py +++ b/glances/plugins/connections/model.py @@ -21,7 +21,7 @@ from __future__ import unicode_literals from glances.logger import logger -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel from glances.globals import nativestr import psutil @@ -35,7 +35,7 @@ import psutil # 'y_unit': 'bit/s'}] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances connections plugin. stats is a dict @@ -69,8 +69,8 @@ class Plugin(GlancesPlugin): self.net_connections_enabled = True self.nf_conntrack_enabled = True - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update connections stats using the input method. diff --git a/glances/plugins/core/__init__.py b/glances/plugins/core/model.py similarity index 94% rename from glances/plugins/core/__init__.py rename to glances/plugins/core/model.py index 0e309f4e..13ba4a4c 100644 --- a/glances/plugins/core/__init__.py +++ b/glances/plugins/core/model.py @@ -19,7 +19,7 @@ """CPU core plugin.""" -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel import psutil @@ -35,7 +35,7 @@ physical cores multiplied by the number of threads that can run on each core.', 'unit': 'number'}, } -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances CPU core plugin. Get stats about CPU core number. @@ -54,8 +54,8 @@ class Plugin(GlancesPlugin): self.display_curse = False # Do *NOT* uncomment the following line - # @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + # @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update core stats. diff --git a/glances/plugins/cpu/__init__.py b/glances/plugins/cpu/model.py similarity index 98% rename from glances/plugins/cpu/__init__.py rename to glances/plugins/cpu/model.py index d7f7afd7..dc296e87 100644 --- a/glances/plugins/cpu/__init__.py +++ b/glances/plugins/cpu/model.py @@ -23,8 +23,8 @@ from glances.logger import logger from glances.timer import getTimeSinceLastUpdate from glances.globals import LINUX, iterkeys from glances.cpu_percent import cpu_percent -from glances.plugins.core import Plugin as CorePlugin -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.core.model import Plugin as CorePlugin +from glances.plugins.plugin.model import GlancesPluginModel import psutil @@ -96,7 +96,7 @@ items_history_list = [{'name': 'user', 'y_unit': '%'}] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances CPU plugin. 'stats' is a dictionary that contains the system-wide CPU utilization as a @@ -119,8 +119,8 @@ class Plugin(GlancesPlugin): except Exception: self.nb_log_core = 1 - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update CPU stats using the input method.""" # Grab stats into self.stats diff --git a/glances/plugins/diskio/__init__.py b/glances/plugins/diskio/model.py similarity index 98% rename from glances/plugins/diskio/__init__.py rename to glances/plugins/diskio/model.py index ab2aabf5..5948126e 100644 --- a/glances/plugins/diskio/__init__.py +++ b/glances/plugins/diskio/model.py @@ -22,7 +22,7 @@ from __future__ import unicode_literals from glances.globals import nativestr from glances.timer import getTimeSinceLastUpdate -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel from glances.logger import logger import psutil @@ -37,7 +37,7 @@ items_history_list = [{'name': 'read_bytes', 'y_unit': 'B/s'}] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances disks I/O plugin. stats is a list @@ -69,8 +69,8 @@ class Plugin(GlancesPlugin): """Return the key of the list.""" return 'disk_name' - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update disk I/O stats using the input method.""" # Init new stats diff --git a/glances/plugins/folders/__init__.py b/glances/plugins/folders/model.py similarity index 96% rename from glances/plugins/folders/__init__.py rename to glances/plugins/folders/model.py index 0313ae54..ca22bf80 100644 --- a/glances/plugins/folders/__init__.py +++ b/glances/plugins/folders/model.py @@ -24,11 +24,11 @@ import numbers from glances.globals import nativestr from glances.folder_list import FolderList as glancesFolderList -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel from glances.logger import logger -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances folder plugin.""" def __init__(self, args=None, config=None): @@ -49,8 +49,8 @@ class Plugin(GlancesPlugin): """Return the key of the list.""" return 'path' - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update the foldered list.""" # Init new stats diff --git a/glances/plugins/fs/__init__.py b/glances/plugins/fs/model.py similarity index 98% rename from glances/plugins/fs/__init__.py rename to glances/plugins/fs/model.py index fc5b5c7e..35de735a 100644 --- a/glances/plugins/fs/__init__.py +++ b/glances/plugins/fs/model.py @@ -23,7 +23,7 @@ from __future__ import unicode_literals import operator from glances.globals import u, nativestr, PermissionError -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel import psutil @@ -64,7 +64,7 @@ items_history_list = [{'name': 'percent', 'y_unit': '%'}] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances file system plugin. stats is a list @@ -84,8 +84,8 @@ class Plugin(GlancesPlugin): """Return the key of the list.""" return 'mnt_point' - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update the FS stats using the input method.""" # Init new stats diff --git a/glances/plugins/glances_docker/__init__.py b/glances/plugins/glances_docker/model.py similarity index 99% rename from glances/plugins/glances_docker/__init__.py rename to glances/plugins/glances_docker/model.py index 0ecba25f..fccbe75c 100644 --- a/glances/plugins/glances_docker/__init__.py +++ b/glances/plugins/glances_docker/model.py @@ -27,7 +27,7 @@ from copy import deepcopy from glances.logger import logger from glances.globals import iterkeys, itervalues, nativestr from glances.timer import getTimeSinceLastUpdate -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel from glances.processes import sort_stats as sort_stats_processes, weighted, glances_processes # Docker-py library (optional and Linux-only) @@ -71,7 +71,7 @@ items_history_list = [{'name': 'cpu_percent', export_exclude_list = ['cpu', 'io', 'memory', 'network'] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances Docker plugin. stats is a dict: {'version': {...}, 'containers': [{}, {}]} @@ -160,8 +160,8 @@ class Plugin(GlancesPlugin): else: return all_tag[0].lower() == 'true' - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update Docker stats using the input method.""" # Init new stats diff --git a/glances/plugins/gpu/__init__.py b/glances/plugins/gpu/model.py similarity index 98% rename from glances/plugins/gpu/__init__.py rename to glances/plugins/gpu/model.py index 1aea937e..4d7697c5 100644 --- a/glances/plugins/gpu/__init__.py +++ b/glances/plugins/gpu/model.py @@ -21,7 +21,7 @@ from glances.globals import nativestr, to_fahrenheit from glances.logger import logger -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel # In Glances 3.1.4 or higher, we use the py3nvml lib (see issue #1523) try: @@ -43,7 +43,7 @@ items_history_list = [{'name': 'proc', 'y_unit': '%'}] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances GPU plugin (limited to NVIDIA chipsets). stats is a list of dictionaries with one entry per GPU @@ -80,8 +80,8 @@ class Plugin(GlancesPlugin): """Return the key of the list.""" return 'gpu_id' - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update the GPU stats.""" # Init new stats diff --git a/glances/plugins/help/__init__.py b/glances/plugins/help/model.py similarity index 99% rename from glances/plugins/help/__init__.py rename to glances/plugins/help/model.py index de5ceef6..583d6573 100644 --- a/glances/plugins/help/__init__.py +++ b/glances/plugins/help/model.py @@ -24,10 +24,10 @@ Just a stupid plugin to display the help screen. """ from glances import __version__, psutil_version -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances help plugin.""" def __init__(self, args=None, config=None): diff --git a/glances/plugins/ip/__init__.py b/glances/plugins/ip/model.py similarity index 97% rename from glances/plugins/ip/__init__.py rename to glances/plugins/ip/model.py index cd056685..373a039b 100644 --- a/glances/plugins/ip/__init__.py +++ b/glances/plugins/ip/model.py @@ -25,7 +25,7 @@ from json import loads from glances.globals import iterkeys, urlopen, queue from glances.logger import logger from glances.timer import Timer -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel # Import plugin specific dependency try: @@ -47,7 +47,7 @@ urls = [('https://ip.42.pl/raw', False, None), ('https://api.ipify.org/?format=json', True, 'ip')] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances IP Plugin. stats is a dict @@ -64,8 +64,8 @@ class Plugin(GlancesPlugin): if not import_error_tag: self.public_address = PublicIpAddress().get() - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update IP stats using the input method. diff --git a/glances/plugins/irq/__init__.py b/glances/plugins/irq/model.py similarity index 97% rename from glances/plugins/irq/__init__.py rename to glances/plugins/irq/model.py index 3f66b55e..13bd08f8 100644 --- a/glances/plugins/irq/__init__.py +++ b/glances/plugins/irq/model.py @@ -24,10 +24,10 @@ import operator from glances.globals import LINUX from glances.timer import getTimeSinceLastUpdate -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances IRQ plugin. stats is a list @@ -49,8 +49,8 @@ class Plugin(GlancesPlugin): """Return the key of the list.""" return self.irq.get_key() - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update the IRQ stats.""" # Init new stats diff --git a/glances/plugins/load/__init__.py b/glances/plugins/load/model.py similarity index 96% rename from glances/plugins/load/__init__.py rename to glances/plugins/load/model.py index 4ec71b75..d4bfb787 100644 --- a/glances/plugins/load/__init__.py +++ b/glances/plugins/load/model.py @@ -23,8 +23,8 @@ import os import psutil from glances.globals import iteritems -from glances.plugins.core import Plugin as CorePlugin -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.core.model import Plugin as CorePlugin +from glances.plugins.plugin.model import GlancesPluginModel from glances.logger import logger # Fields description @@ -63,7 +63,7 @@ items_history_list = [{'name': 'min1', 'description': '15 minutes load'}] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances load plugin. stats is a dict @@ -97,8 +97,8 @@ class Plugin(GlancesPlugin): except (AttributeError, OSError): return None - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update load stats.""" # Init new stats diff --git a/glances/plugins/mem/__init__.py b/glances/plugins/mem/model.py similarity index 98% rename from glances/plugins/mem/__init__.py rename to glances/plugins/mem/model.py index a0c87c1c..bd3f3703 100644 --- a/glances/plugins/mem/__init__.py +++ b/glances/plugins/mem/model.py @@ -21,7 +21,7 @@ from glances.logger import logger from glances.globals import iterkeys -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel import psutil @@ -95,7 +95,7 @@ items_history_list = [{'name': 'percent', 'y_unit': '%'}] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances' memory plugin. stats is a dict @@ -111,8 +111,8 @@ class Plugin(GlancesPlugin): # We want to display the stat in the curse interface self.display_curse = True - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update RAM memory stats using the input method.""" # Init new stats diff --git a/glances/plugins/memswap/__init__.py b/glances/plugins/memswap/model.py similarity index 97% rename from glances/plugins/memswap/__init__.py rename to glances/plugins/memswap/model.py index 8586782d..0ed2c455 100644 --- a/glances/plugins/memswap/__init__.py +++ b/glances/plugins/memswap/model.py @@ -21,7 +21,7 @@ from glances.globals import iterkeys from glances.timer import getTimeSinceLastUpdate -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel import psutil @@ -65,7 +65,7 @@ items_history_list = [{'name': 'percent', 'y_unit': '%'}] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances swap memory plugin. stats is a dict @@ -81,8 +81,8 @@ class Plugin(GlancesPlugin): # We want to display the stat in the curse interface self.display_curse = True - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update swap memory stats using the input method.""" # Init new stats diff --git a/glances/plugins/network/__init__.py b/glances/plugins/network/model.py similarity index 98% rename from glances/plugins/network/__init__.py rename to glances/plugins/network/model.py index eb61f447..0e67eae4 100644 --- a/glances/plugins/network/__init__.py +++ b/glances/plugins/network/model.py @@ -23,7 +23,7 @@ from __future__ import unicode_literals import base64 from glances.timer import getTimeSinceLastUpdate -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel from glances.logger import logger import psutil @@ -74,7 +74,7 @@ items_history_list = [{'name': 'rx', 'y_unit': 'bit/s'}] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances network plugin. stats is a list @@ -107,8 +107,8 @@ class Plugin(GlancesPlugin): """Return the key of the list.""" return 'interface_name' - # @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + # @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update network stats using the input method. diff --git a/glances/plugins/now/__init__.py b/glances/plugins/now/model.py similarity index 96% rename from glances/plugins/now/__init__.py rename to glances/plugins/now/model.py index 47d2549e..8d2f927c 100644 --- a/glances/plugins/now/__init__.py +++ b/glances/plugins/now/model.py @@ -20,10 +20,10 @@ """Now (current date) plugin.""" from time import tzname, strftime -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Plugin to get the current date/time. stats is (string) diff --git a/glances/plugins/percpu/__init__.py b/glances/plugins/percpu/model.py similarity index 95% rename from glances/plugins/percpu/__init__.py rename to glances/plugins/percpu/model.py index 4f5bddef..f3fb088a 100644 --- a/glances/plugins/percpu/__init__.py +++ b/glances/plugins/percpu/model.py @@ -20,7 +20,7 @@ """Per-CPU plugin.""" from glances.cpu_percent import cpu_percent -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel # Define the history items list items_history_list = [{'name': 'user', @@ -31,7 +31,7 @@ items_history_list = [{'name': 'user', 'y_unit': '%'}] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances per-CPU plugin. 'stats' is a list of dictionaries that contain the utilization percentages @@ -52,8 +52,8 @@ class Plugin(GlancesPlugin): """Return the key of the list.""" return 'cpu_number' - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update per-CPU stats using the input method.""" # Init new stats diff --git a/glances/plugins/plugin/__init__.py b/glances/plugins/plugin/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/glances/plugins/plugin.py b/glances/plugins/plugin/model.py similarity index 98% rename from glances/plugins/plugin.py rename to glances/plugins/plugin/model.py index 189a331c..7b352ab0 100644 --- a/glances/plugins/plugin.py +++ b/glances/plugins/plugin/model.py @@ -57,8 +57,8 @@ fields_unit_type = { } -class GlancesPlugin(object): - """Main class for Glances plugin.""" +class GlancesPluginModel(object): + """Main class for Glances plugin model.""" def __init__(self, args=None, @@ -88,14 +88,10 @@ class GlancesPlugin(object): :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:] - # TODO: For Glances 4 => 3 next line to be removed when all plugins are migrated - if self.__class__.__module__.startswith('glances_'): - self.plugin_name = self.__class__.__module__.split('glances_')[1] - else: - self.plugin_name = self.__class__.__module__ + # Build the plugin name + self.plugin_name = self.__class__.__module__.split('.')[2] + 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 diff --git a/glances/plugins/ports/__init__.py b/glances/plugins/ports/model.py similarity index 98% rename from glances/plugins/ports/__init__.py rename to glances/plugins/ports/model.py index 0b1bd873..eb9af3fa 100644 --- a/glances/plugins/ports/__init__.py +++ b/glances/plugins/ports/model.py @@ -31,7 +31,7 @@ from glances.ports_list import GlancesPortsList from glances.web_list import GlancesWebList from glances.timer import Timer, Counter from glances.logger import logger -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel try: import requests @@ -41,7 +41,7 @@ except ImportError as e: logger.warning("Missing Python Lib ({}), Ports plugin is limited to port scanning".format(e)) -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances ports scanner plugin.""" def __init__(self, args=None, config=None): @@ -69,8 +69,8 @@ class Plugin(GlancesPlugin): # Call the father class super(Plugin, self).exit() - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update the ports list.""" if self.input_method == 'local': diff --git a/glances/plugins/processcount/__init__.py b/glances/plugins/processcount/model.py similarity index 96% rename from glances/plugins/processcount/__init__.py rename to glances/plugins/processcount/model.py index 95ece5a1..71cf1e1e 100644 --- a/glances/plugins/processcount/__init__.py +++ b/glances/plugins/processcount/model.py @@ -20,7 +20,7 @@ """Process count plugin.""" from glances.processes import glances_processes -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel # Define the history items list items_history_list = [{'name': 'total', @@ -37,7 +37,7 @@ items_history_list = [{'name': 'total', 'y_unit': ''}] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances process count plugin. stats is a list @@ -62,8 +62,8 @@ class Plugin(GlancesPlugin): # Note: 'glances_processes' is already init in the glances_processes.py script - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update processes stats using the input method.""" # Init new stats diff --git a/glances/plugins/processlist/__init__.py b/glances/plugins/processlist/model.py similarity index 99% rename from glances/plugins/processlist/__init__.py rename to glances/plugins/processlist/model.py index 78af2158..d4345661 100644 --- a/glances/plugins/processlist/__init__.py +++ b/glances/plugins/processlist/model.py @@ -25,8 +25,8 @@ import copy from glances.logger import logger from glances.globals import WINDOWS, key_exist_value_not_none_not_v from glances.processes import glances_processes, sort_stats -from glances.plugins.core import Plugin as CorePlugin -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.core.model import Plugin as CorePlugin +from glances.plugins.plugin.model import GlancesPluginModel def seconds_to_hms(input_seconds): @@ -48,7 +48,7 @@ def split_cmdline(cmdline): return path, cmd, arguments -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances' processes plugin. stats is a list diff --git a/glances/plugins/psutilversion/__init__.py b/glances/plugins/psutilversion/model.py similarity index 89% rename from glances/plugins/psutilversion/__init__.py rename to glances/plugins/psutilversion/model.py index 6180b597..72c661cf 100644 --- a/glances/plugins/psutilversion/__init__.py +++ b/glances/plugins/psutilversion/model.py @@ -20,10 +20,10 @@ """psutil plugin.""" from glances import psutil_version_info -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Get the psutil version for client/server purposes. stats is a tuple @@ -39,8 +39,8 @@ class Plugin(GlancesPlugin): """Reset/init the stats.""" self.stats = None - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update the stats.""" # Reset stats diff --git a/glances/plugins/quicklook/__init__.py b/glances/plugins/quicklook/model.py similarity index 97% rename from glances/plugins/quicklook/__init__.py rename to glances/plugins/quicklook/model.py index dd367dc2..5702bac7 100644 --- a/glances/plugins/quicklook/__init__.py +++ b/glances/plugins/quicklook/model.py @@ -23,7 +23,7 @@ from glances.cpu_percent import cpu_percent from glances.logger import logger from glances.outputs.glances_bars import Bar from glances.outputs.glances_sparklines import Sparkline -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel import psutil @@ -44,7 +44,7 @@ items_history_list = [{'name': 'cpu', 'y_unit': '%'}] -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances quicklook plugin. 'stats' is a dictionary. @@ -58,8 +58,8 @@ class Plugin(GlancesPlugin): # We want to display the stat in the curse interface self.display_curse = True - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update quicklook stats using the input method.""" # Init new stats diff --git a/glances/plugins/raid/__init__.py b/glances/plugins/raid/model.py similarity index 97% rename from glances/plugins/raid/__init__.py rename to glances/plugins/raid/model.py index 85a2397b..3632f66a 100644 --- a/glances/plugins/raid/__init__.py +++ b/glances/plugins/raid/model.py @@ -21,7 +21,7 @@ from glances.globals import iterkeys from glances.logger import logger -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel # Import plugin specific dependency try: @@ -33,7 +33,7 @@ else: import_error_tag = False -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances RAID plugin. stats is a dict (see pymdstat documentation) @@ -46,8 +46,8 @@ class Plugin(GlancesPlugin): # We want to display the stat in the curse interface self.display_curse = True - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update RAID stats using the input method.""" # Init new stats diff --git a/glances/plugins/sensors/__init__.py b/glances/plugins/sensors/model.py similarity index 98% rename from glances/plugins/sensors/__init__.py rename to glances/plugins/sensors/model.py index 83f8d9bb..2c249c59 100644 --- a/glances/plugins/sensors/__init__.py +++ b/glances/plugins/sensors/model.py @@ -27,13 +27,13 @@ from glances.globals import iteritems, to_fahrenheit from glances.timer import Counter from glances.plugins.sensors.sensor.glances_batpercent import Plugin as BatPercentPlugin from glances.plugins.sensors.sensor.glances_hddtemp import Plugin as HddTempPlugin -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel SENSOR_TEMP_UNIT = 'C' SENSOR_FAN_UNIT = 'R' -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances sensors plugin. The stats list includes both sensors and hard disks stats, if any. @@ -78,8 +78,8 @@ class Plugin(GlancesPlugin): """Return the key of the list.""" return 'label' - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update sensors stats using the input method.""" # Init new stats diff --git a/glances/plugins/sensors/sensor/glances_batpercent.py b/glances/plugins/sensors/sensor/glances_batpercent.py index f7a262b6..b471d32e 100644 --- a/glances/plugins/sensors/sensor/glances_batpercent.py +++ b/glances/plugins/sensors/sensor/glances_batpercent.py @@ -22,7 +22,7 @@ import psutil from glances.logger import logger -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel # Batinfo library (optional; Linux-only) batinfo_tag = True @@ -43,7 +43,7 @@ except Exception as e: psutil_tag = False -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances battery capacity plugin. stats is a list @@ -69,8 +69,8 @@ class Plugin(GlancesPlugin): # The HDD temp is displayed within the sensors plugin self.display_curse = False - # @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + # @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update battery capacity stats using the input method.""" # Init new stats diff --git a/glances/plugins/sensors/sensor/glances_hddtemp.py b/glances/plugins/sensors/sensor/glances_hddtemp.py index a070bd70..780ade9a 100644 --- a/glances/plugins/sensors/sensor/glances_hddtemp.py +++ b/glances/plugins/sensors/sensor/glances_hddtemp.py @@ -24,10 +24,10 @@ import socket from glances.globals import nativestr from glances.logger import logger -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances HDD temperature sensors plugin. stats is a list @@ -52,8 +52,8 @@ class Plugin(GlancesPlugin): # The HDD temp is displayed within the sensors plugin self.display_curse = False - # @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + # @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update HDD stats using the input method.""" # Init new stats diff --git a/glances/plugins/smart/__init__.py b/glances/plugins/smart/model.py similarity index 97% rename from glances/plugins/smart/__init__.py rename to glances/plugins/smart/model.py index 0480f251..d51f5f97 100644 --- a/glances/plugins/smart/__init__.py +++ b/glances/plugins/smart/model.py @@ -44,7 +44,7 @@ Check for admin access. If no admin access, disable SMART plugin. If smartmontools is not installed, we should catch the error upstream in plugin initialization. """ -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel from glances.logger import logger from glances.main import disable from glances.globals import is_admin @@ -128,7 +128,7 @@ def get_smart_data(): return stats -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """ Glances' HDD SMART plugin. @@ -150,8 +150,8 @@ class Plugin(GlancesPlugin): # We want to display the stat in the curse interface self.display_curse = True - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update SMART stats using the input method.""" # Init new stats diff --git a/glances/plugins/system/__init__.py b/glances/plugins/system/model.py similarity index 96% rename from glances/plugins/system/__init__.py rename to glances/plugins/system/model.py index 8c58a3c2..6e242107 100644 --- a/glances/plugins/system/__init__.py +++ b/glances/plugins/system/model.py @@ -26,7 +26,7 @@ from io import open from glances.logger import logger from glances.globals import iteritems -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel # SNMP OID snmp_oid = {'default': {'hostname': '1.3.6.1.2.1.1.5.0', @@ -77,7 +77,7 @@ def _linux_os_release(): return pretty_name -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances' host/system plugin. @@ -95,8 +95,8 @@ class Plugin(GlancesPlugin): if self.get_refresh(): self.set_refresh(60) - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update the host/system info using the input method. @@ -171,6 +171,10 @@ class Plugin(GlancesPlugin): # Init the return message ret = [] + # Only process if stats exist and plugin not disabled + if not self.stats or self.is_disable(): + return ret + # Build the string message if args.client: # Client mode diff --git a/glances/plugins/uptime/__init__.py b/glances/plugins/uptime/model.py similarity index 86% rename from glances/plugins/uptime/__init__.py rename to glances/plugins/uptime/model.py index 65153f5f..d70cd027 100644 --- a/glances/plugins/uptime/__init__.py +++ b/glances/plugins/uptime/model.py @@ -21,7 +21,7 @@ from datetime import datetime, timedelta -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel import psutil @@ -29,7 +29,7 @@ import psutil snmp_oid = {'_uptime': '1.3.6.1.2.1.1.3.0'} -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances uptime plugin. stats is date (string) @@ -57,8 +57,8 @@ class Plugin(GlancesPlugin): # Correct issue #1092 (thanks to @IanTAtWork) return {'seconds': int(self.uptime.total_seconds())} - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update uptime stat using the input method.""" # Init new stats @@ -86,4 +86,8 @@ class Plugin(GlancesPlugin): def msg_curse(self, args=None, max_width=None): """Return the string to display in the curse interface.""" - return [self.curse_add_line('Uptime: {}'.format(self.stats))] + # Only process if stats exist and plugin not disabled + if not self.stats or self.is_disable(): + return [] + else: + return [self.curse_add_line('Uptime: {}'.format(self.stats))] diff --git a/glances/plugins/wifi/__init__.py b/glances/plugins/wifi/model.py similarity index 97% rename from glances/plugins/wifi/__init__.py rename to glances/plugins/wifi/model.py index b3a4af68..9e95074c 100644 --- a/glances/plugins/wifi/__init__.py +++ b/glances/plugins/wifi/model.py @@ -23,7 +23,7 @@ import operator from glances.globals import nativestr from glances.logger import logger -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel import psutil # Use the Wifi Python lib (https://pypi.python.org/pypi/wifi) @@ -42,7 +42,7 @@ import_error_tag = True logger.warning("Wifi lib is not compliant with Python 3, Wifi plugin is disabled") -class Plugin(GlancesPlugin): +class Plugin(GlancesPluginModel): """Glances Wifi plugin. Get stats of the current Wifi hotspots. @@ -64,8 +64,8 @@ class Plugin(GlancesPlugin): """ return 'ssid' - @GlancesPlugin._check_decorator - @GlancesPlugin._log_result_decorator + @GlancesPluginModel._check_decorator + @GlancesPluginModel._log_result_decorator def update(self): """Update Wifi stats using the input method. diff --git a/glances/stats.py b/glances/stats.py index 60f07d66..43a67496 100644 --- a/glances/stats.py +++ b/glances/stats.py @@ -105,9 +105,9 @@ class GlancesStats(object): def _load_plugin(self, plugin_path, args=None, config=None): """Load the plugin, init it and add to the _plugin dict.""" - # The key is the plugin name = plugin_path - # for example, the path ./glances/plugins/xxx - # generate self._plugins_list["xxx"] = ... + # The key is the plugin_path + # except when it starts with glances_xxx + # generate self._plugins_list["xxx"] = if plugin_path.startswith('glances_'): # Avoid circular loop when Glances plugin uses lib with same name # Example: docker should be named to glances_docker @@ -118,7 +118,7 @@ class GlancesStats(object): # Load the plugin class try: # Import the plugin - plugin = import_module(plugin_path) + plugin = import_module('glances.plugins.' + plugin_path + '.model') # Init and add the plugin to the dictionary self._plugins[name] = plugin.Plugin(args=args, config=config) except Exception as e: @@ -144,7 +144,8 @@ class GlancesStats(object): for item in os.listdir(plugins_path): if os.path.isdir(os.path.join(plugins_path, item)) and \ - not item.startswith('__'): + not item.startswith('__') and \ + item != 'plugin': # Load the plugin start_duration.reset() self._load_plugin(os.path.basename(item), diff --git a/unitest.py b/unitest.py index 14bcb185..19e1d61f 100755 --- a/unitest.py +++ b/unitest.py @@ -39,7 +39,7 @@ from glances.thresholds import GlancesThresholdCareful from glances.thresholds import GlancesThresholdWarning from glances.thresholds import GlancesThresholdCritical from glances.thresholds import GlancesThresholds -from glances.plugins.plugin import GlancesPlugin +from glances.plugins.plugin.model import GlancesPluginModel from glances.secure import secure_popen # Global variables