From 0f6b4183858b4ea8e072cf9493a8c4b95c2ec2f2 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Mon, 31 Oct 2016 13:03:16 +0100 Subject: [PATCH] [modules] Add NIC module Add a module that displays the status of all NICs (interface name, list of IPs and state). In its status, it also exposes whether it's a WiFi or a wired NIC. For this functionality, additional code was implemented to allow a module to add multiple elements to the bar at once. The framework calls the module until its "next()" method return False. --- bumblebee/module.py | 3 ++ bumblebee/modules/nic.py | 61 +++++++++++++++++++++++ bumblebee/outputs/i3.py | 42 +++++++++------- bumblebee/themes/solarized-powerline.json | 16 ++++++ 4 files changed, 103 insertions(+), 19 deletions(-) create mode 100644 bumblebee/modules/nic.py diff --git a/bumblebee/module.py b/bumblebee/module.py index 11549a5..0106bc8 100644 --- a/bumblebee/module.py +++ b/bumblebee/module.py @@ -15,4 +15,7 @@ class Module(object): def state(self): return "default" + def next(self): + return False + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/nic.py b/bumblebee/modules/nic.py new file mode 100644 index 0000000..a943177 --- /dev/null +++ b/bumblebee/modules/nic.py @@ -0,0 +1,61 @@ +import pyroute2 +import netifaces +import bumblebee.module + +class Module(bumblebee.module.Module): + def __init__(self, args): + super(Module, self).__init__(args) + self._exclude = ( "lo", "virbr" ) + self._interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ] + self._index = 0 + self._intf = self._interfaces[0] if len(self._interfaces) > 0 else None + self._cache = {} + self._state = "down" + + def data(self): + if len(self._interfaces) <= self._index: + return "n/a" + self._intf = self._interfaces[self._index] + self._state = "down" + addr = [] + + if netifaces.AF_INET in netifaces.ifaddresses(self._intf): + for ip in netifaces.ifaddresses(self._intf)[netifaces.AF_INET]: + if "addr" in ip and ip["addr"] != "": + addr.append(ip["addr"]) + self._state = "up" + + return "{} {} {}".format(self._intf, self._state, ", ".join(addr)) + + def next(self): + self._index += 1 + if self._index < len(self._interfaces): + return True + self._index = 0 + # reload to support hotplug + self._interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ] + return False + + def _iswlan(self, intf): + if not "wlan{}".format(intf) in self._cache: + iw = pyroute2.IW() + ip = pyroute2.IPRoute() + idx = ip.link_lookup(ifname=intf)[0] + try: + iw.get_interface_by_ifindex(idx) + self._cache["wlan{}".format(intf)] = True + except exception as e: + self._cache["wlan{}".format(intf)] = False + return self._cache + + def state(self): + t = "wireless" if self._iswlan(self._intf) else "wired" + return "{}-{}".format(t, self._state) + + def warning(self): + return self._state != "up" + + def critical(self): + return self._state == "down" + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/outputs/i3.py b/bumblebee/outputs/i3.py index 63bbeed..4d03f8a 100644 --- a/bumblebee/outputs/i3.py +++ b/bumblebee/outputs/i3.py @@ -15,28 +15,32 @@ class i3bar(bumblebee.output.Output): def add(self, obj): theme = self.theme() - data = { - u"full_text": "{}{}{}".format(theme.prefix(obj), obj.data(), theme.suffix(obj)), - "color": theme.color(obj), - "background": theme.background(obj), - } + while True: + data = { + u"full_text": "{}{}{}".format(theme.prefix(obj), obj.data(), theme.suffix(obj)), + "color": theme.color(obj), + "background": theme.background(obj), + } - if theme.urgent(obj) and obj.critical(): - data["urgent"] = True + if theme.urgent(obj) and obj.critical(): + data["urgent"] = True - if theme.default_separators(obj) == False: - data["separator"] = False - data["separator_block_width"] = 0 - if theme.separator(obj): - self._data.append({ - u"full_text": theme.separator(obj), - "color": theme.background(obj), - "background": theme.previous_background(), - "separator": False, - "separator_block_width": 0, - }) + if theme.default_separators(obj) == False: + data["separator"] = False + data["separator_block_width"] = 0 + if theme.separator(obj): + self._data.append({ + u"full_text": theme.separator(obj), + "color": theme.background(obj), + "background": theme.previous_background(), + "separator": False, + "separator_block_width": 0, + }) - self._data.append(data) + self._data.append(data) + if obj.next() == False: + break + theme.next() def get(self): data = json.dumps(self._data) diff --git a/bumblebee/themes/solarized-powerline.json b/bumblebee/themes/solarized-powerline.json index 8c3f8f8..4f39704 100644 --- a/bumblebee/themes/solarized-powerline.json +++ b/bumblebee/themes/solarized-powerline.json @@ -32,6 +32,22 @@ "cpu": { "prefix": "  " }, + "nic": { + "states": { + "wireless-up": { + "prefix": "  " + }, + "wireless-down": { + "prefix": "  " + }, + "wired-up": { + "prefix": "  " + }, + "wired-down": { + "prefix": "  " + } + } + }, "battery": { "states": { "charged": {