From 6d47240d6adb7b6eff1ae398d010e61e353c5d6f Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sun, 26 Feb 2017 09:08:36 +0100 Subject: [PATCH] [modules/nic] reduce code complexity (hopefully) fixes #37 --- bumblebee/modules/nic.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/bumblebee/modules/nic.py b/bumblebee/modules/nic.py index bd94f94..68ef7d1 100644 --- a/bumblebee/modules/nic.py +++ b/bumblebee/modules/nic.py @@ -47,6 +47,16 @@ class Module(bumblebee.engine.Module): def _istunnel(self, intf): return intf.startswith("tun") + def get_addresses(self, intf): + retval = [] + try: + for ip in netifaces.ifaddresses(intf).get(netifaces.AF_INET, []): + if ip.get("addr", "") != "": + retval.append(ip.get("addr")) + except Exception: + return [] + return retval + def _update_widgets(self, widgets): interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ] @@ -56,14 +66,9 @@ class Module(bumblebee.engine.Module): 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 = [] + for ip in self.get_addresses(intf): + addr.append(ip) + state = "up" widget = self.widget(intf) if not widget: widget = bumblebee.output.Widget(name=intf)