[modules/nic] Re-enable NIC module
Re-add the NIC module with all its functionality (hopefully...). This introduces a new concept: Instead of having separate queries for critical and warning (which really are just another set of states), a module can now return a list of states for each widget. All the state information is then merged together into a single theme. So, for instance, the NIC module can return a state saying "critical - wlan-down", which applies the theme information for both "critical" and "wlan-down". see #23
This commit is contained in:
parent
c820223d0c
commit
a045962d00
8 changed files with 104 additions and 19 deletions
|
@ -80,3 +80,5 @@ class Module(bumblebee.engine.Module):
|
|||
self._repeat = False if "false" in line else True
|
||||
if line.startswith("set shuffle "):
|
||||
self._shuffle = False if "false" in line else True
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
67
bumblebee/modules/nic.py
Normal file
67
bumblebee/modules/nic.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
#pylint: disable=C0111,R0903
|
||||
|
||||
import netifaces
|
||||
|
||||
import bumblebee.util
|
||||
import bumblebee.input
|
||||
import bumblebee.output
|
||||
import bumblebee.engine
|
||||
|
||||
"""Displays the name, IP address(es) and status of each available network interface."""
|
||||
|
||||
class Module(bumblebee.engine.Module):
|
||||
def __init__(self, engine, config):
|
||||
widgets = []
|
||||
super(Module, self).__init__(engine, config, widgets)
|
||||
self._exclude = tuple(filter(len, self.parameter("exclude", "lo,virbr,docker,vboxnet,veth").split(",")))
|
||||
self._update_widgets(widgets)
|
||||
|
||||
def update(self, widgets):
|
||||
self._update_widgets(widgets)
|
||||
|
||||
def state(self, widget):
|
||||
states = []
|
||||
|
||||
if widget.get("state") == "down":
|
||||
states.append("critical")
|
||||
elif widget.get("state") != "up":
|
||||
states.append("warning")
|
||||
|
||||
intf = widget.get("intf")
|
||||
iftype = "wireless" if self._iswlan(intf) else "wired"
|
||||
iftype = "tunnel" if self._istunnel(intf) else iftype
|
||||
|
||||
states.append("{}-{}".format(iftype, widget.get("state")))
|
||||
|
||||
return states
|
||||
|
||||
def _iswlan(self, intf):
|
||||
# wifi, wlan, wlp, seems to work for me
|
||||
if intf.startswith("w"): return True
|
||||
return False
|
||||
|
||||
def _istunnel(self, intf):
|
||||
return intf.startswith("tun")
|
||||
|
||||
def _update_widgets(self, widgets):
|
||||
interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ]
|
||||
for intf in interfaces:
|
||||
addr = []
|
||||
state = "down"
|
||||
try:
|
||||
if netifaces.AF_INET in netifaces.ifaddresses(intf):
|
||||
for ip in netifaces.ifaddresses(intf)[netifaces.AF_INET]:
|
||||
if "addr" in ip and ip["addr"] != "":
|
||||
addr.append(ip["addr"])
|
||||
state = "up"
|
||||
except Exception as e:
|
||||
addr = []
|
||||
widget = self.widget(intf)
|
||||
if not widget:
|
||||
widget = bumblebee.output.Widget(name=intf)
|
||||
widgets.append(widget)
|
||||
widget.full_text("{} {} {}".format(intf, state, ", ".join(addr)))
|
||||
widget.set("intf", intf)
|
||||
widget.set("state", state)
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
Loading…
Add table
Add a link
Reference in a new issue