bumblebee-status/bumblebee_status/core/module.py

233 lines
7.2 KiB
Python
Raw Normal View History

import os
import importlib
import logging
2020-05-10 12:56:30 +02:00
import core.config
import core.input
2020-02-15 14:04:53 +01:00
import core.widget
import core.decorators
try:
error = ModuleNotFoundError("")
except Exception as e:
ModuleNotFoundError = Exception
log = logging.getLogger(__name__)
2020-05-15 10:22:10 +02:00
"""Loads a module by name
:param module_name: Name of the module to load
:param config: Configuration to apply to the module (defaults to an empty configuration)
:param theme: Theme for this module, defaults to None, which means whatever is configured in "config"
:return: A module object representing the module, or an Error module if loading failed
:rtype: class bumblebee_status.module.Module
"""
def load(module_name, config=core.config.Config([]), theme=None):
error = None
module_short, alias = (module_name.split(":") + [module_name])[0:2]
config.set("__alias__", alias)
for namespace in ["core", "contrib"]:
try:
mod = importlib.import_module(
"modules.{}.{}".format(namespace, module_short)
)
log.debug(
"importing {} from {}.{}".format(module_short, namespace, module_short)
)
return getattr(mod, "Module")(config, theme)
except ImportError as e:
log.debug("failed to import {}: {}".format(module_name, e))
error = e
log.fatal("failed to import {}: {}".format(module_name, error))
2020-03-28 13:44:45 +01:00
return Error(config=config, module=module_name, error=error)
class Module(core.input.Object):
2020-05-15 10:22:10 +02:00
"""Represents a module (single piece of functionality) of the bar
:param config: Configuration to apply to the module (defaults to an empty configuration)
:param theme: Theme for this module, defaults to None, which means whatever is configured in "config"
:param widgets: A list of bumblebee_status.widget.Widget objects that the module is comprised of
"""
def __init__(self, config=core.config.Config([]), theme=None, widgets=[]):
super().__init__()
2020-04-07 21:23:42 +02:00
self.__config = config
self.__widgets = widgets if isinstance(widgets, list) else [widgets]
self.module_name = self.__module__.split(".")[-1]
self.name = self.module_name
self.alias = self.__config.get("__alias__", None)
self.id = self.alias if self.alias else self.name
self.next_update = None
2020-04-26 16:40:48 +02:00
self.theme = theme
for widget in self.__widgets:
widget.module = self
2020-05-15 10:22:10 +02:00
"""Override this to determine when to show this module
:return: True if the module should be hidden, False otherwise
:rtype: boolean
"""
def hidden(self):
return False
2020-05-15 10:22:10 +02:00
"""Retrieve CLI/configuration parameters for this module. For example, if
the module is called "test" and the user specifies "-p test.x=123" on the
commandline, using self.parameter("x") retrieves the value 123.
:param key: Name of the parameter to retrieve
:param default: Default value, if parameter is not set by user (defaults to None)
:return: Parameter value, or default
:rtype: string
"""
def parameter(self, key, default=None):
value = default
for prefix in [self.name, self.module_name, self.alias]:
value = self.__config.get("{}.{}".format(prefix, key), value)
return value
2020-05-15 10:22:10 +02:00
"""Set a parameter for this module
:param key: Name of the parameter to set
:param value: New value of the parameter
"""
2020-02-23 14:52:58 +01:00
def set(self, key, value):
self.__config.set("{}.{}".format(self.name, key), value)
2020-02-23 14:52:58 +01:00
2020-05-15 10:22:10 +02:00
"""Override this method to define tasks that should be done during each
update interval (for instance, querying an API, calling a CLI tool to get new
date, etc.
"""
def update(self):
pass
2020-05-15 10:22:10 +02:00
"""Wrapper method that ensures that all exceptions thrown by the
update() method are caught and displayed in a bumblebee_status.module.Error
module
"""
def update_wrapper(self):
try:
self.update()
except Exception as e:
self.set("interval", 1)
module = Error(config=self.__config, module="error", error=str(e))
self.__widgets = [module.widget()]
self.update = module.update
2020-05-15 10:22:10 +02:00
"""Retrieves the list of widgets for this module
:return: A list of widgets
:rtype: list of bumblebee_status.widget.Widgets
"""
def widgets(self):
return self.__widgets
2020-05-15 10:22:10 +02:00
"""Removes all widgets of this module"""
def clear_widgets(self):
del self.__widgets[:]
2020-05-15 10:22:10 +02:00
"""Adds a widget to the module
:param full_text: Text or callable (method) that defines the text of the widget (defaults to "")
:param name: Name of the widget, defaults to None, which means autogenerate
:return: The new widget
:rtype: bumblebee_status.widget.Widget
"""
def add_widget(self, full_text="", name=None):
widget = core.widget.Widget(full_text=full_text, name=name, module=self)
self.widgets().append(widget)
return widget
2020-05-15 10:22:10 +02:00
"""Convenience method to retrieve a named widget
:param name: Name of widget to retrieve, defaults to None (in which case the first widget is returned)
:return: The widget with the corresponding name, None if not found
:rtype: bumblebee_status.widget.Widget
"""
2020-03-01 14:36:12 +01:00
def widget(self, name=None):
if not name:
return self.widgets()[0]
2020-03-01 14:36:12 +01:00
for w in self.widgets():
if w.name == name:
return w
2020-03-01 14:36:12 +01:00
return None
2020-05-15 10:22:10 +02:00
"""Override this method to define states for the module
:param widget: Widget for which state should be returned
:return: a list of states for this widget
:rtype: list of strings
"""
def state(self, widget):
return []
2020-05-15 10:22:10 +02:00
"""Convenience method that sets warning and critical state for numbers
:param value: Current value to calculate state against
:param warn: Warning threshold
:parm crit: Critical threshold
:return: None if value is below both thresholds, "critical", "warning" as appropriate otherwise
:rtype: string
"""
2020-03-05 21:13:15 +01:00
def threshold_state(self, value, warn, crit):
if value > float(self.parameter("critical", crit)):
return "critical"
if value > float(self.parameter("warning", warn)):
return "warning"
2020-03-05 21:13:15 +01:00
return None
class Error(Module):
2020-05-15 10:22:10 +02:00
"""Represents an "error" module
:param module: The module name that produced the error
:param error: The error message to display
:param config: Configuration to apply to the module (defaults to an empty configuration)
:param theme: Theme for this module, defaults to None, which means whatever is configured in "config"
"""
def __init__(self, module, error, config=core.config.Config([]), theme=None):
super().__init__(config, theme, core.widget.Widget(self.full_text))
self.__module = module
self.__error = error
2020-02-15 14:04:53 +01:00
2020-05-15 10:22:10 +02:00
"""Returns the error message
:param widget: the error widget to display
"""
def full_text(self, widget):
return "{}: {}".format(self.__module, self.__error)
2020-05-15 10:22:10 +02:00
"""Overriden state, always returns critical (it *is* an error, after all"""
def state(self, widget):
return ["critical"]
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4