bumblebee-status/core/module.py

115 lines
3.4 KiB
Python
Raw Normal View History

import importlib
import logging
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__)
def load(module_name, config=core.config.Config([]), theme=None):
error = None
2020-04-07 21:23:42 +02:00
module_short, alias = (module_name.split(':') + [module_name])[0:2]
config.set('__alias__', alias)
for namespace in [ 'core', 'contrib' ]:
try:
2020-04-07 21:23:42 +02:00
mod = importlib.import_module('modules.{}.{}'.format(namespace, module_short))
return getattr(mod, 'Module')(config, theme)
except ImportError as e:
2020-04-07 21:23:42 +02:00
log.fatal('failed to import {}: {}'.format(module_short, e))
if not error or module_short in error:
error = str(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):
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 ]
for widget in self.__widgets:
widget.module(self)
self.__name = None
2020-04-07 21:23:42 +02:00
self.alias = self.__config.get('__alias__', None)
self.next_update = None
2020-04-26 16:40:48 +02:00
self.theme = theme
def hidden(self):
return False
def parameter(self, key, default=None):
value = default
2020-04-07 21:23:42 +02:00
for prefix in [ self.name(), self.module_name(), self.alias ]:
value = self.__config.get('{}.{}'.format(prefix, key), value)
# TODO retrieve from config file
return value
2020-02-23 14:52:58 +01:00
def set(self, key, value):
2020-04-07 21:23:42 +02:00
self.__config.set('{}.{}'.format(self.name(), key), value)
2020-02-23 14:52:58 +01:00
def update(self):
pass
def update_wrapper(self):
try:
self.update()
except Exception as e:
self.set('interval', 1)
2020-04-07 21:23:42 +02:00
module = Error(config=self.__config, module='error', error=str(e))
self.__widgets = [module.widget()]
self.update = module.update
def name(self):
return self.__name if self.__name else self.module_name()
def module_name(self):
return self.__module__.split('.')[-1]
2020-03-13 13:56:08 +01:00
def widgets(self, widgets=None):
if widgets:
self.__widgets = widgets
return self.__widgets
2020-03-01 14:36:12 +01:00
def widget(self, name=None):
if not name: return self.widgets()[0]
for w in self.widgets():
if w.name() == name: return w
return None
def state(self, widget):
return []
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'
return None
class Error(Module):
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-02-23 14:52:58 +01:00
self.set('scrolling.bounce', False)
self.set('scrolling.speed', 2)
self.set('width', 15)
@core.decorators.scrollable
def full_text(self, widget):
return '{}: {}'.format(self.__module, self.__error)
def state(self, widget):
return ['critical']
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4