Merge pull request #286 from donfranio/add-which-function

add 'which' function to utils and use for 'iwgetid'
This commit is contained in:
tobi-wan-kenobi 2018-08-24 12:06:13 +02:00 committed by GitHub
commit e7e786b65e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View file

@ -35,6 +35,8 @@ class Module(bumblebee.engine.Module):
self._states["include"].append(state) self._states["include"].append(state)
self._format = self.parameter("format","{intf} {state} {ip} {ssid}"); self._format = self.parameter("format","{intf} {state} {ip} {ssid}");
self._update_widgets(widgets) self._update_widgets(widgets)
self.iwgetid = bumblebee.util.which("iwgetid")
def update(self, widgets): def update(self, widgets):
self._update_widgets(widgets) self._update_widgets(widgets)
@ -106,7 +108,7 @@ class Module(bumblebee.engine.Module):
def get_ssid(self, intf): def get_ssid(self, intf):
if self._iswlan(intf): if self._iswlan(intf):
try: 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: except:
return "" return ""
return "" return ""

View file

@ -60,4 +60,23 @@ def durationfmt(duration, shorten=False, suffix=False):
return "{}{}".format(res, suf if suffix else "") 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:
localPATH = os.environ["PATH"].split(os.pathsep)
localPATH += ["/sbin", "/usr/sbin/", "/usr/local/sbin"]
for path in localPATH:
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 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4