2020-05-02 13:29:20 +02:00
|
|
|
import os
|
2020-01-19 16:06:21 +01:00
|
|
|
import importlib
|
|
|
|
import logging
|
2020-05-30 17:20:41 +02:00
|
|
|
import threading
|
2020-01-19 16:06:21 +01:00
|
|
|
|
2020-05-10 12:56:30 +02:00
|
|
|
import core.config
|
2020-02-07 21:28:29 +01:00
|
|
|
import core.input
|
2020-02-15 14:04:53 +01:00
|
|
|
import core.widget
|
2020-02-23 14:47:47 +01:00
|
|
|
import core.decorators
|
2020-02-07 21:28:29 +01:00
|
|
|
|
2020-05-16 15:16:23 +02:00
|
|
|
import util.format
|
|
|
|
|
2020-03-06 14:31:29 +01:00
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
error = ModuleNotFoundError("")
|
2020-03-06 14:31:29 +01:00
|
|
|
except Exception as e:
|
|
|
|
ModuleNotFoundError = Exception
|
|
|
|
|
2020-01-19 16:06:21 +01:00
|
|
|
log = logging.getLogger(__name__)
|
2020-01-19 15:36:52 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2020-04-26 16:39:13 +02:00
|
|
|
def load(module_name, config=core.config.Config([]), theme=None):
|
2020-03-06 14:14:34 +01:00
|
|
|
error = None
|
2020-05-03 11:15:52 +02:00
|
|
|
module_short, alias = (module_name.split(":") + [module_name])[0:2]
|
|
|
|
config.set("__alias__", alias)
|
|
|
|
for namespace in ["core", "contrib"]:
|
2020-03-06 14:14:34 +01:00
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
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)
|
2020-03-06 14:14:34 +01:00
|
|
|
except ImportError as e:
|
2020-05-09 10:57:44 +02:00
|
|
|
log.debug("failed to import {}: {}".format(module_name, e))
|
2020-05-03 11:15:52 +02:00
|
|
|
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)
|
2020-01-19 15:36:52 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-07 21:28:29 +01:00
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2020-04-26 16:39:13 +02:00
|
|
|
def __init__(self, config=core.config.Config([]), theme=None, widgets=[]):
|
2020-02-07 21:28:29 +01:00
|
|
|
super().__init__()
|
2020-05-30 17:20:41 +02:00
|
|
|
self.background = False
|
|
|
|
self.__thread = None
|
2020-04-07 21:23:42 +02:00
|
|
|
self.__config = config
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__widgets = widgets if isinstance(widgets, list) else [widgets]
|
2020-04-30 12:50:24 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.module_name = self.__module__.split(".")[-1]
|
2020-04-30 12:50:24 +02:00
|
|
|
self.name = self.module_name
|
2020-05-03 11:15:52 +02:00
|
|
|
self.alias = self.__config.get("__alias__", None)
|
2020-05-02 09:40:09 +02:00
|
|
|
self.id = self.alias if self.alias else self.name
|
2020-03-23 15:32:06 +01:00
|
|
|
self.next_update = None
|
2020-02-04 21:09:11 +01:00
|
|
|
|
2020-04-26 16:40:48 +02:00
|
|
|
self.theme = theme
|
|
|
|
|
2020-05-02 09:40:09 +02:00
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2020-04-28 20:11:02 +02:00
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2020-02-04 21:09:11 +01:00
|
|
|
def parameter(self, key, default=None):
|
|
|
|
value = default
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
for prefix in [self.name, self.module_name, self.alias]:
|
|
|
|
value = self.__config.get("{}.{}".format(prefix, key), value)
|
2020-02-04 21:09:11 +01:00
|
|
|
return value
|
2020-01-26 14:06:09 +01:00
|
|
|
|
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):
|
2020-05-03 11:15:52 +02:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2020-01-19 15:36:52 +01:00
|
|
|
def update(self):
|
|
|
|
pass
|
|
|
|
|
2020-05-30 17:20:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
def update_wrapper(self):
|
|
|
|
if self.background == True:
|
2020-10-14 18:07:29 +02:00
|
|
|
if self.__thread and self.__thread.is_alive():
|
2020-05-30 17:20:41 +02:00
|
|
|
return # skip this update interval
|
|
|
|
self.__thread = threading.Thread(target=self.internal_update, args=(True,))
|
|
|
|
self.__thread.start()
|
|
|
|
else:
|
|
|
|
self.internal_update(False)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2020-05-30 17:20:41 +02:00
|
|
|
def internal_update(self, trigger_redraw=False):
|
2020-03-01 14:08:16 +01:00
|
|
|
try:
|
|
|
|
self.update()
|
2020-05-30 17:20:41 +02:00
|
|
|
if trigger_redraw:
|
|
|
|
core.event.trigger("update", [self.id], redraw_only=True)
|
2020-03-01 14:08:16 +01:00
|
|
|
except Exception as e:
|
2020-05-03 11:15:52 +02:00
|
|
|
self.set("interval", 1)
|
|
|
|
module = Error(config=self.__config, module="error", error=str(e))
|
2020-04-04 13:55:54 +02:00
|
|
|
self.__widgets = [module.widget()]
|
2020-03-01 14:08:16 +01:00
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2020-05-09 10:57:44 +02:00
|
|
|
def widgets(self):
|
2020-04-04 13:55:54 +02:00
|
|
|
return self.__widgets
|
2020-01-26 14:06:09 +01:00
|
|
|
|
2020-05-15 10:22:10 +02:00
|
|
|
"""Removes all widgets of this module"""
|
|
|
|
|
2020-05-09 10:57:44 +02:00
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
def add_widget(self, full_text="", name=None):
|
2020-06-23 20:03:17 +02:00
|
|
|
widget_id = "{}::{}".format(self.name, len(self.widgets()))
|
|
|
|
widget = core.widget.Widget(full_text=full_text, name=name, widget_id=widget_id)
|
2020-05-01 10:03:50 +02:00
|
|
|
self.widgets().append(widget)
|
2020-05-18 12:59:47 +02:00
|
|
|
widget.module = self
|
2020-05-01 10:03:50 +02:00
|
|
|
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-06-23 15:51:14 +02:00
|
|
|
def widget(self, name=None, widget_id=None):
|
|
|
|
if not name and not widget_id:
|
2020-05-03 11:15:52 +02:00
|
|
|
return self.widgets()[0]
|
2020-03-01 14:36:12 +01:00
|
|
|
|
|
|
|
for w in self.widgets():
|
2020-06-23 15:51:14 +02:00
|
|
|
if name and w.name == name:
|
|
|
|
return w
|
|
|
|
if w.id == widget_id:
|
2020-05-03 11:15:52 +02:00
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2020-02-23 21:13:49 +01:00
|
|
|
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):
|
2020-05-03 11:15:52 +02:00
|
|
|
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
|
|
|
|
|
2020-05-15 19:59:13 +02:00
|
|
|
def register_callbacks(self):
|
|
|
|
actions = [
|
2020-05-16 12:00:55 +02:00
|
|
|
{"name": "left-click", "id": core.input.LEFT_MOUSE},
|
|
|
|
{"name": "right-click", "id": core.input.RIGHT_MOUSE},
|
|
|
|
{"name": "middle-click", "id": core.input.MIDDLE_MOUSE},
|
|
|
|
{"name": "wheel-up", "id": core.input.WHEEL_UP},
|
|
|
|
{"name": "wheel-down", "id": core.input.WHEEL_DOWN},
|
2020-05-15 19:59:13 +02:00
|
|
|
]
|
|
|
|
for action in actions:
|
|
|
|
if self.parameter(action["name"]):
|
2020-05-16 15:16:23 +02:00
|
|
|
core.input.register(
|
|
|
|
self,
|
|
|
|
action["id"],
|
|
|
|
self.parameter(action["name"]),
|
|
|
|
util.format.asbool(
|
|
|
|
self.parameter("{}-wait".format(action["name"]), False)
|
|
|
|
),
|
|
|
|
)
|
2020-05-15 19:59:13 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-01-19 16:06:21 +01:00
|
|
|
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"
|
|
|
|
"""
|
|
|
|
|
2020-04-26 16:39:13 +02:00
|
|
|
def __init__(self, module, error, config=core.config.Config([]), theme=None):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.full_text))
|
2020-04-04 13:55:54 +02:00
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2020-02-23 14:47:47 +01:00
|
|
|
def full_text(self, widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{}: {}".format(self.__module, self.__error)
|
2020-01-19 16:06:21 +01:00
|
|
|
|
2020-05-15 10:22:10 +02:00
|
|
|
"""Overriden state, always returns critical (it *is* an error, after all"""
|
|
|
|
|
2020-02-24 15:07:34 +01:00
|
|
|
def state(self, widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
return ["critical"]
|
|
|
|
|
2020-02-24 15:07:34 +01:00
|
|
|
|
2020-01-19 15:36:52 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|