[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.
This commit is contained in:
Tobias Witek 2016-10-31 13:03:16 +01:00
parent dbd8ccb83f
commit 0f6b418385
4 changed files with 103 additions and 19 deletions

View file

@ -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

61
bumblebee/modules/nic.py Normal file
View file

@ -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

View file

@ -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)

View file

@ -32,6 +32,22 @@
"cpu": {
"prefix": "  "
},
"nic": {
"states": {
"wireless-up": {
"prefix": "  "
},
"wireless-down": {
"prefix": "  "
},
"wired-up": {
"prefix": "  "
},
"wired-down": {
"prefix": "  "
}
}
},
"battery": {
"states": {
"charged": {