[modules] Allow modules to provide default click actions

Pass the "output" object to the modules' constructor to allow them to
define their own callbacks.
Any user-provided callbacks take precedence and override those of the
module.
This commit is contained in:
Tobias Witek 2016-11-01 08:09:10 +01:00
parent 63e041259f
commit fca3171556
11 changed files with 20 additions and 15 deletions

View file

@ -11,7 +11,7 @@ def description():
return "Displays battery status, percentage and whether it's charging or discharging."
class Module(bumblebee.module.Module):
def __init__(self, args):
def __init__(self, output, args):
super(Module, self).__init__(args)
self._battery = "BAT0" if not args else args[0]
self._capacity = 0

View file

@ -11,7 +11,7 @@ def description():
return "Displays CPU utilization across all CPUs."
class Module(bumblebee.module.Module):
def __init__(self, args):
def __init__(self, output, args):
super(Module, self).__init__(args)
self._perc = psutil.cpu_percent(percpu=False)

View file

@ -12,10 +12,13 @@ def description():
return "Shows free diskspace, total diskspace and the percentage of free disk space."
class Module(bumblebee.module.Module):
def __init__(self, args):
def __init__(self, output, args):
super(Module, self).__init__(args)
self._path = args[0] if args else "/"
output.add_callback(module=self.__module__,
button=1, cmd="nautilus {instance}")
def data(self):
st = os.statvfs(self._path)

View file

@ -50,7 +50,7 @@ def get_dnf_info(obj):
class Module(bumblebee.module.Module):
def __init__(self, args):
def __init__(self, output, args):
super(Module, self).__init__(args)
self._interval = args[0] if args else 30*60
self._counter = {}

View file

@ -12,7 +12,7 @@ def description():
return "Shows available RAM, total amount of RAM and the percentage of available RAM."
class Module(bumblebee.module.Module):
def __init__(self, args):
def __init__(self, output, args):
super(Module, self).__init__(args)
self._mem = psutil.virtual_memory()

View file

@ -12,7 +12,7 @@ def description():
return "Displays the names, IP addresses and status of each available interface."
class Module(bumblebee.module.Module):
def __init__(self, args):
def __init__(self, output, args):
super(Module, self).__init__(args)
self._exclude = ( "lo", "virbr" )
self._interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ]

View file

@ -26,7 +26,7 @@ def description():
return "See 'pasource'."
class Module(bumblebee.module.Module):
def __init__(self, args):
def __init__(self, output, args):
super(Module, self).__init__(args)
self._module = self.__module__.split(".")[-1]

View file

@ -11,7 +11,7 @@ def description():
return "Draws an empty field."
class Module(bumblebee.module.Module):
def __init__(self, args):
def __init__(self, output, args):
super(Module, self).__init__(args)
def data(self):

View file

@ -18,7 +18,7 @@ def description():
return "Displays the current time, using the optional format string as input for strftime."
class Module(bumblebee.module.Module):
def __init__(self, args):
def __init__(self, output, args):
super(Module, self).__init__(args)
module = self.__module__.split(".")[-1]

View file

@ -5,6 +5,8 @@ class Output(object):
self._callbacks = {}
def add_callback(self, cmd, button, module=None):
if module:
module = module.replace("bumblebee.modules.", "")
self._callbacks[(
button,
module,

View file

@ -42,8 +42,8 @@ def print_theme_list():
def main():
parser = argparse.ArgumentParser(description="display system data in the i3bar")
parser.add_argument("-m", "--modules", nargs="+", help="List of modules to load. The order of the list determines their order in the i3bar (from left to right)")
parser.add_argument("-e", "--events", nargs="+", help="List of click events that should be handled. Format is: <module name><splitter, see -s><button ID><splitter><command to execute>")
parser.add_argument("-m", "--modules", nargs="+", help="List of modules to load. The order of the list determines their order in the i3bar (from left to right)", default=[])
parser.add_argument("-e", "--events", nargs="+", help="List of click events that should be handled. Format is: <module name><splitter, see -s><button ID><splitter><command to execute>", default=[])
parser.add_argument("-l", "--list", action="store_true", help="List all available modules and themes")
parser.add_argument("-t", "--theme", help="Specify which theme to use for drawing the modules")
parser.add_argument("-i", "--interval", help="Specify the update interval", default=1, type=int)
@ -60,6 +60,9 @@ def main():
print_theme_list()
sys.exit(0)
theme = bumblebee.theme.Theme(args.theme) if args.theme else bumblebee.theme.Theme()
output = bumblebee.outputs.i3.i3bar(theme)
modules = []
s = args.split
for m in args.modules:
@ -68,10 +71,7 @@ def main():
module_name = m if not s in m else m.split(s)[0]
module_args = None if not s in m else m.split(s)[1:]
module = importlib.import_module("bumblebee.modules.{}".format(module_name))
modules.append(getattr(module, "Module")(module_args))
theme = bumblebee.theme.Theme(args.theme) if args.theme else bumblebee.theme.Theme()
output = bumblebee.outputs.i3.i3bar(theme)
modules.append(getattr(module, "Module")(output, module_args))
for e in args.events:
ev = e.split(s)