From 2f3f171ca5311332f3fd7d5fda090f3fea7ca019 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Fri, 2 Dec 2016 18:53:34 +0100 Subject: [PATCH] [core] Remove alias from module Hide alias concept for modules in the engine. That way, the individual modules never get to know about whether a module has been aliased or not. see #23 --- bumblebee/config.py | 7 +++++-- bumblebee/engine.py | 6 +++++- bumblebee/module.py | 10 ++++------ bumblebee/modules/battery.py | 4 ++-- bumblebee/modules/brightness.py | 4 ++-- bumblebee/modules/caffeine.py | 4 ++-- bumblebee/modules/cmus.py | 4 ++-- bumblebee/modules/cpu.py | 4 ++-- bumblebee/modules/disk.py | 4 ++-- bumblebee/modules/dnf.py | 4 ++-- bumblebee/modules/layout.py | 4 ++-- bumblebee/modules/load.py | 4 ++-- bumblebee/modules/memory.py | 4 ++-- bumblebee/modules/nic.py | 4 ++-- bumblebee/modules/pacman.py | 4 ++-- bumblebee/modules/ping.py | 4 ++-- bumblebee/modules/pulseaudio.py | 4 ++-- bumblebee/modules/spacer.py | 4 ++-- bumblebee/modules/time.py | 4 ++-- bumblebee/modules/xrandr.py | 4 ++-- bumblebee/outputs/i3.py | 2 -- 21 files changed, 48 insertions(+), 45 deletions(-) diff --git a/bumblebee/config.py b/bumblebee/config.py index 501393d..57958ac 100644 --- a/bumblebee/config.py +++ b/bumblebee/config.py @@ -35,10 +35,13 @@ class print_usage(argparse.Action): print("") class ModuleConfig(object): - def __init__(self, config, prefix): - self._prefix = prefix + def __init__(self, config, name, alias): + self._prefix = alias if alias else name self._config = config + def prefix(self): + return self._prefix + def set(self, name, value): name = self._prefix + name return self._config.set(name, value) diff --git a/bumblebee/engine.py b/bumblebee/engine.py index 1625ee7..cc0b7b9 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -1,6 +1,7 @@ import importlib import bumblebee.theme import bumblebee.output +import bumblebee.config import bumblebee.modules class Engine: @@ -13,7 +14,10 @@ class Engine: def load_module(self, modulespec): name = modulespec["name"] module = importlib.import_module("bumblebee.modules.{}".format(name)) - return getattr(module, "Module")(self._output, self._config, modulespec["alias"]) + cfg = bumblebee.config.ModuleConfig(self._config, name, modulespec["alias"]) + obj = getattr(module, "Module")(self._output, cfg) + obj.register_callbacks() + return obj def load_modules(self): for m in self._config.modules(): diff --git a/bumblebee/module.py b/bumblebee/module.py index 6152777..0f497a2 100644 --- a/bumblebee/module.py +++ b/bumblebee/module.py @@ -2,7 +2,6 @@ import os import pkgutil import importlib -import bumblebee.config import bumblebee.modules def modules(): @@ -27,12 +26,11 @@ class ModuleDescription(object): return getattr(self._mod, "parameters", lambda: [ "n/a" ])() class Module(object): - def __init__(self, output, config, alias=None): + def __init__(self, output, config): self._output = output - self._alias = alias - name = "{}.".format(alias if alias else self.__module__.split(".")[-1]) - self._config = bumblebee.config.ModuleConfig(config, name) + self._config = config + def register_callbacks(self): buttons = [ { "name": "left-click", "id": 1 }, { "name": "middle-click", "id": 2 }, @@ -58,6 +56,6 @@ class Module(object): return "default" def instance(self, widget=None): - return self._alias if self._alias else self.__module__.split(".")[-1] + return self._config.prefix() # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/battery.py b/bumblebee/modules/battery.py index 2a570df..8215693 100644 --- a/bumblebee/modules/battery.py +++ b/bumblebee/modules/battery.py @@ -9,8 +9,8 @@ def parameters(): return [ "battery.device: The device to read from (defaults to BAT0)" ] class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._battery = config.parameter("device", "BAT0") self._capacity = 100 self._status = "Unknown" diff --git a/bumblebee/modules/brightness.py b/bumblebee/modules/brightness.py index 114c11e..9227ce8 100644 --- a/bumblebee/modules/brightness.py +++ b/bumblebee/modules/brightness.py @@ -9,8 +9,8 @@ def parameters(): ] class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._brightness = 0 self._max = 0 self._percent = 0 diff --git a/bumblebee/modules/caffeine.py b/bumblebee/modules/caffeine.py index 39f20d5..d7c2dad 100644 --- a/bumblebee/modules/caffeine.py +++ b/bumblebee/modules/caffeine.py @@ -7,8 +7,8 @@ def description(): return "Enable/disable auto screen lock." class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._activated = 0 output.add_callback(module="caffeine.activate", button=1, cmd=[ 'notify-send "Consuming caffeine"', 'xset s off' ]) output.add_callback(module="caffeine.deactivate", button=1, cmd=[ 'notify-send "Out of coffee"', 'xset s default' ]) diff --git a/bumblebee/modules/cmus.py b/bumblebee/modules/cmus.py index b847ad2..de42ecf 100644 --- a/bumblebee/modules/cmus.py +++ b/bumblebee/modules/cmus.py @@ -15,8 +15,8 @@ def parameters(): ] class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._status = "default" self._fmt = self._config.parameter("format", "{artist} - {title} {position}/{duration}") diff --git a/bumblebee/modules/cpu.py b/bumblebee/modules/cpu.py index b156b37..619019b 100644 --- a/bumblebee/modules/cpu.py +++ b/bumblebee/modules/cpu.py @@ -11,8 +11,8 @@ def parameters(): ] class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._perc = psutil.cpu_percent(percpu=False) output.add_callback(module=self.instance(), button=1, cmd="gnome-system-monitor") diff --git a/bumblebee/modules/disk.py b/bumblebee/modules/disk.py index 679e271..67a79e1 100644 --- a/bumblebee/modules/disk.py +++ b/bumblebee/modules/disk.py @@ -12,8 +12,8 @@ def parameters(): ] class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._path = self._config.parameter("path", "/") output.add_callback(module=self.instance(), button=1, cmd="nautilus {}".format(self._path)) diff --git a/bumblebee/modules/dnf.py b/bumblebee/modules/dnf.py index ead5065..4101a8c 100644 --- a/bumblebee/modules/dnf.py +++ b/bumblebee/modules/dnf.py @@ -59,8 +59,8 @@ def get_dnf_info(obj): obj.set("other", other) class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._counter = {} self._thread = threading.Thread(target=get_dnf_info, args=(self,)) diff --git a/bumblebee/modules/layout.py b/bumblebee/modules/layout.py index 084d27d..b317655 100644 --- a/bumblebee/modules/layout.py +++ b/bumblebee/modules/layout.py @@ -13,8 +13,8 @@ def parameters(): class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._languages = self._config.parameter("lang", "en").split("|") self._idx = 0 diff --git a/bumblebee/modules/load.py b/bumblebee/modules/load.py index fba5dbd..aad9dfe 100644 --- a/bumblebee/modules/load.py +++ b/bumblebee/modules/load.py @@ -12,8 +12,8 @@ def parameters(): ] class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._cpus = 1 try: self._cpus = multiprocessing.cpu_count() diff --git a/bumblebee/modules/memory.py b/bumblebee/modules/memory.py index 70b10b2..d38ac0f 100644 --- a/bumblebee/modules/memory.py +++ b/bumblebee/modules/memory.py @@ -12,8 +12,8 @@ def parameters(): ] class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._mem = psutil.virtual_memory() output.add_callback(module=self.instance(), button=1, cmd="gnome-system-monitor") diff --git a/bumblebee/modules/nic.py b/bumblebee/modules/nic.py index 94f8bc4..64483c5 100644 --- a/bumblebee/modules/nic.py +++ b/bumblebee/modules/nic.py @@ -10,8 +10,8 @@ def parameters(): ] class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._exclude = tuple(filter(len, self._config.parameter("exclude", "lo,virbr,docker,vboxnet,veth").split(","))) self._state = "down" self._typecache = {} diff --git a/bumblebee/modules/pacman.py b/bumblebee/modules/pacman.py index 66ece38..3ecb5db 100644 --- a/bumblebee/modules/pacman.py +++ b/bumblebee/modules/pacman.py @@ -6,8 +6,8 @@ def description(): return "Displays available updates per repository for pacman." class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._count = 0 def widgets(self): diff --git a/bumblebee/modules/ping.py b/bumblebee/modules/ping.py index 20fec39..b523788 100644 --- a/bumblebee/modules/ping.py +++ b/bumblebee/modules/ping.py @@ -56,8 +56,8 @@ def get_rtt(obj): class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._counter = {} diff --git a/bumblebee/modules/pulseaudio.py b/bumblebee/modules/pulseaudio.py index a8cda45..079e07c 100644 --- a/bumblebee/modules/pulseaudio.py +++ b/bumblebee/modules/pulseaudio.py @@ -18,8 +18,8 @@ def parameters(): class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._module = self.__module__.split(".")[-1] self._left = 0 diff --git a/bumblebee/modules/spacer.py b/bumblebee/modules/spacer.py index 798338e..f9b88d3 100644 --- a/bumblebee/modules/spacer.py +++ b/bumblebee/modules/spacer.py @@ -8,8 +8,8 @@ def parameters(): return [ "spacer.text: Text to draw (defaults to '')" ] class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) def widgets(self): return bumblebee.output.Widget(self, self._config.parameter("text", "")) diff --git a/bumblebee/modules/time.py b/bumblebee/modules/time.py index 4a19649..68f1beb 100644 --- a/bumblebee/modules/time.py +++ b/bumblebee/modules/time.py @@ -21,8 +21,8 @@ def default_format(module): return default class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) module = self.__module__.split(".")[-1] diff --git a/bumblebee/modules/xrandr.py b/bumblebee/modules/xrandr.py index 51998a9..7817143 100644 --- a/bumblebee/modules/xrandr.py +++ b/bumblebee/modules/xrandr.py @@ -13,8 +13,8 @@ def parameters(): ] class Module(bumblebee.module.Module): - def __init__(self, output, config, alias): - super(Module, self).__init__(output, config, alias) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._widgets = [] diff --git a/bumblebee/outputs/i3.py b/bumblebee/outputs/i3.py index e287c6c..d9d1894 100644 --- a/bumblebee/outputs/i3.py +++ b/bumblebee/outputs/i3.py @@ -14,8 +14,6 @@ def read_input(output): if line == "[": continue if line == "]": break - DEVNULL = open(os.devnull, 'wb') - event = json.loads(line) cb = output.callback(event) if cb: