From 8c12daa4079f8caa830bf5d465e9ae2065d568f5 Mon Sep 17 00:00:00 2001 From: Frank Scherrer Date: Fri, 24 Aug 2018 10:22:28 +0200 Subject: [PATCH] add 'which' function to utils and use for 'iwgetid' --- bumblebee/modules/nic.py | 4 +++- bumblebee/util.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/bumblebee/modules/nic.py b/bumblebee/modules/nic.py index ca47409..c1c60f8 100644 --- a/bumblebee/modules/nic.py +++ b/bumblebee/modules/nic.py @@ -35,6 +35,8 @@ class Module(bumblebee.engine.Module): self._states["include"].append(state) self._format = self.parameter("format","{intf} {state} {ip} {ssid}"); self._update_widgets(widgets) + self.iwgetid = bumblebee.util.which("iwgetid") + def update(self, widgets): self._update_widgets(widgets) @@ -106,7 +108,7 @@ class Module(bumblebee.engine.Module): def get_ssid(self, intf): if self._iswlan(intf): try: - return subprocess.check_output(["iwgetid","-r",intf]).strip().decode('utf-8') + return subprocess.check_output([self.iwgetid,"-r",intf]).strip().decode('utf-8') except: return "" return "" diff --git a/bumblebee/util.py b/bumblebee/util.py index 8032032..f5bb4ea 100644 --- a/bumblebee/util.py +++ b/bumblebee/util.py @@ -60,4 +60,21 @@ def durationfmt(duration, shorten=False, suffix=False): return "{}{}".format(res, suf if suffix else "") +def which(program): + import os + def is_exe(fpath): + return os.path.isfile(fpath) and os.access(fpath, os.X_OK) + + fpath, fname = os.path.split(program) + if fpath: + if is_exe(program): + return program + else: + for path in os.environ["PATH"].split(os.pathsep) + ["/sbin", "/usr/sbin/", "/usr/local/sbin"]: + exe_file = os.path.join(path, program) + if is_exe(exe_file): + return exe_file + + return None + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4