From 3bbd8d4765a3385ee07d809cf9ef3f5ae2a39063 Mon Sep 17 00:00:00 2001 From: Jason Hite Date: Sat, 8 Mar 2014 19:46:06 -0500 Subject: [PATCH 1/2] Optionally skip MAC address check in network Network interfaces don't necessarily have a MAC address. For example, the tunnel devices created by OpenVPN do not. Previously, passing a network interface that did not have a MAC address caused the network module to fail, since it assumed that there would be one. This commit just adds a flag to the network module "mac", which defaults to True. If True, the module behaves like before. If False, the check for the MAC address is skipped and the {mac} format variable is replaced with "NONE". I tested this with my OpenVPN interface as well as my regular interface and it works fine. --- i3pystatus/network.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/i3pystatus/network.py b/i3pystatus/network.py index 974617d..be115a7 100644 --- a/i3pystatus/network.py +++ b/i3pystatus/network.py @@ -74,6 +74,7 @@ class Network(IntervalModule): "format_down", "color_down", ("detached_down", "If the interface doesn't exist, display it as if it were down"), "name", + ("mac", "Try and fetch the interface MAC address, default True") ) name = interface = "eth0" @@ -82,6 +83,8 @@ class Network(IntervalModule): color_up = "#00FF00" color_down = "#FF0000" detached_down = False + mac = True + def init(self): if self.interface not in netifaces.interfaces() and not self.detached_down: @@ -101,8 +104,11 @@ class Network(IntervalModule): fdict.update({ "interface": self.interface, "name": self.name, - "mac": info[netifaces.AF_PACKET][0]["addr"], }) + if self.mac: + fdict["mac"] = info[netifaces.AF_PACKET][0]["addr"] + else: + fdict["mac"] = "NONE" if up: format = self.format_up From 10aca8d48938ac3cc569eae6dd9f2391a33bfccc Mon Sep 17 00:00:00 2001 From: Jason Hite Date: Wed, 2 Apr 2014 15:49:05 -0400 Subject: [PATCH 2/2] Better checking of the MAC address Use a Try... Except... block instead of a function flag when checking for the MAC address. This has the benefit of requiring no changes for user, but fixes the problem with interfaces that do not have MACs. --- i3pystatus/network.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/i3pystatus/network.py b/i3pystatus/network.py index be115a7..57bbe66 100644 --- a/i3pystatus/network.py +++ b/i3pystatus/network.py @@ -74,7 +74,6 @@ class Network(IntervalModule): "format_down", "color_down", ("detached_down", "If the interface doesn't exist, display it as if it were down"), "name", - ("mac", "Try and fetch the interface MAC address, default True") ) name = interface = "eth0" @@ -83,8 +82,6 @@ class Network(IntervalModule): color_up = "#00FF00" color_down = "#FF0000" detached_down = False - mac = True - def init(self): if self.interface not in netifaces.interfaces() and not self.detached_down: @@ -101,14 +98,16 @@ class Network(IntervalModule): up = netifaces.AF_INET in info or netifaces.AF_INET6 in info fdict = dict( zip_longest(["v4", "v4mask", "v4cidr", "v6", "v6mask", "v6cidr"], [], fillvalue="")) + + try: + mac = info[netifaces.AF_PACKET][0]["addr"] + except KeyError: + mac = "NONE" fdict.update({ "interface": self.interface, "name": self.name, + "mac": mac, }) - if self.mac: - fdict["mac"] = info[netifaces.AF_PACKET][0]["addr"] - else: - fdict["mac"] = "NONE" if up: format = self.format_up