Skip to content

Commit

Permalink
Change the way AMP are loaded #1930
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolargo committed Oct 15, 2023
1 parent 48251c8 commit 3a7c479
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 27 deletions.
2 changes: 1 addition & 1 deletion glances/amps/glances_amp.py → glances/amps/amp.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, name=None, args=None):

# AMP name (= module name without glances_)
if name is None:
self.amp_name = self.__class__.__module__[len('glances_') :]
self.amp_name = self.__class__.__module__
else:
self.amp_name = name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

from glances.globals import u, to_ascii
from glances.logger import logger
from glances.amps.glances_amp import GlancesAmp
from glances.amps.amp import GlancesAmp


class Amp(GlancesAmp):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import requests

from glances.logger import logger
from glances.amps.glances_amp import GlancesAmp
from glances.amps.amp import GlancesAmp


class Amp(GlancesAmp):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

from glances.logger import logger
from glances.globals import iteritems, to_ascii
from glances.amps.glances_amp import GlancesAmp
from glances.amps.amp import GlancesAmp


class Amp(GlancesAmp):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

from glances.logger import logger
from glances.globals import iteritems
from glances.amps.glances_amp import GlancesAmp
from glances.amps.amp import GlancesAmp


class Amp(GlancesAmp):
Expand Down
31 changes: 10 additions & 21 deletions glances/amps_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,41 +45,30 @@ def load_configs(self):
if self.config is None:
return False

# Display a warning (deprecated) message if the monitor section exist
if "monitor" in self.config.sections():
logger.warning(
"A deprecated [monitor] section exists in the Glances configuration file. You should use the new \
Applications Monitoring Process module instead \
(http://glances.readthedocs.io/en/develop/aoa/amps.html)."
)

# TODO: Change the way AMP are loaded (use folder/module instead of glances_foo.py file)
# See https://github.com/nicolargo/glances/issues/1930
header = "glances_"
# For each AMP script, call the load_config method
for s in self.config.sections():
if s.startswith("amp_"):
# An AMP section exists in the configuration file
# If an AMP script exist in the glances/amps folder, use it
amp_conf_name = s[4:]
amp_script = os.path.join(amps_path, header + s[4:] + ".py")
if not os.path.exists(amp_script):
# If an AMP module exist in amps_path (glances/amps) folder then use it
amp_name = s[4:]
amp_module = os.path.join(amps_path, amp_name)
if not os.path.exists(amp_module):
# If not, use the default script
amp_script = os.path.join(amps_path, "glances_default.py")
amp_module = os.path.join(amps_path, "default")
try:
amp = __import__(os.path.basename(amp_script)[:-3])
amp = __import__(os.path.basename(amp_module))
except ImportError as e:
logger.warning("Missing Python Lib ({}), cannot load {} AMP".format(e, amp_conf_name))
logger.warning("Missing Python Lib ({}), cannot load AMP {}".format(e, amp_name))
except Exception as e:
logger.warning("Cannot load {} AMP ({})".format(amp_conf_name, e))
logger.warning("Cannot load AMP {} ({})".format(amp_name, e))
else:
# Add the AMP to the dictionary
# The key is the AMP name
# for example, the file glances_xxx.py
# generate self._amps_list["xxx"] = ...
self.__amps_dict[amp_conf_name] = amp.Amp(name=amp_conf_name, args=self.args)
self.__amps_dict[amp_name] = amp.Amp(name=amp_name, args=self.args)
# Load the AMP configuration
self.__amps_dict[amp_conf_name].load_config(self.config)
self.__amps_dict[amp_name].load_config(self.config)
# Log AMPs list
logger.debug("AMPs list: {}".format(self.getList()))

Expand Down
2 changes: 1 addition & 1 deletion glances/exports/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class GlancesExport(object):
def __init__(self, config=None, args=None):
"""Init the export class."""
# Export name (= module name without glances_)
self.export_name = self.__class__.__module__[len('glances_') :]
self.export_name = self.__class__.__module__
logger.debug("Init export module %s" % self.export_name)

# Init the config & args
Expand Down
File renamed without changes.

0 comments on commit 3a7c479

Please sign in to comment.