From 2dee830be88dea42e92c445abec37d058a28ef1b Mon Sep 17 00:00:00 2001 From: "J.M. Dana" Date: Sat, 21 Feb 2015 12:43:58 +0000 Subject: New bluetooth module added It shows the status of the bluetooth connection --- py3status/modules/bluetooth.py | 77 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 py3status/modules/bluetooth.py diff --git a/py3status/modules/bluetooth.py b/py3status/modules/bluetooth.py new file mode 100644 index 0000000..9840794 --- /dev/null +++ b/py3status/modules/bluetooth.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +# encoding: utf-8 + +""" +Module for displaying bluetooth status + +Requires: + - hcitool + - sed + +@author jmdana +@license GPLv3 +""" + +from time import time +import subprocess +import shlex + +class Py3status: + # configuration parameters + cache_timeout = 10 + color_good = None + color_bad = None + + def __init__(self): + pass + + def kill(self, i3s_output_list, i3s_config): + pass + + def on_click(self, i3s_output_list, i3s_config, event): + pass + + def get_status(self, i3s_output_list, i3s_config): + # The whole command: + # hcitool name `hcitool con | sed -n -r 's/.*([0-9A-F:]{17}).*/\\1/p'` + + cmd1 = shlex.split("hcitool con") + cmd2 = shlex.split("sed -n -r 's/.*([0-9A-F:]{17}).*/\\1/p'") + output = "BT: " + color = self.color_bad or i3s_config['color_bad'] + + p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE) + p2 = subprocess.Popen(cmd2, stdin=p1.stdout, stdout=subprocess.PIPE) + + mac = p2.communicate()[0].strip().decode("utf-8") + + if mac != "": + cmd3 = shlex.split("hcitool name %s" % mac) + p3 = subprocess.Popen(cmd3, stdout=subprocess.PIPE) + output += p3.communicate()[0].strip().decode("utf-8") + color = self.color_good or i3s_config['color_good'] + else: + output += "OFF" + + response = { + 'cached_until': time() + self.cache_timeout, + 'full_text': output, + 'color': color, + } + + return response + +if __name__ == "__main__": + """ + Test this module by calling it directly. + """ + from time import sleep + x = Py3status() + config = { + "color_good": "#00FF00", + "color_bad": "#FF0000", + } + + while True: + print(x.get_status([], config)) + sleep(1) -- cgit v1.3 From a5616fdad821b02ca224d169e993f934747e2f65 Mon Sep 17 00:00:00 2001 From: "J.M. Dana" Date: Sat, 21 Feb 2015 14:34:54 +0000 Subject: Provides information about multiple devices --- py3status/modules/bluetooth.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/py3status/modules/bluetooth.py b/py3status/modules/bluetooth.py index 9840794..00fdb84 100644 --- a/py3status/modules/bluetooth.py +++ b/py3status/modules/bluetooth.py @@ -43,12 +43,17 @@ class Py3status: p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE) p2 = subprocess.Popen(cmd2, stdin=p1.stdout, stdout=subprocess.PIPE) - mac = p2.communicate()[0].strip().decode("utf-8") + macs = p2.communicate()[0].strip().decode("utf-8").split() + + if macs != []: + names = [] + for mac in macs: + cmd3 = shlex.split("hcitool name %s" % mac) + p3 = subprocess.Popen(cmd3, stdout=subprocess.PIPE) + names.append(p3.communicate()[0].strip().decode("utf-8")) + + output += "|".join(names) - if mac != "": - cmd3 = shlex.split("hcitool name %s" % mac) - p3 = subprocess.Popen(cmd3, stdout=subprocess.PIPE) - output += p3.communicate()[0].strip().decode("utf-8") color = self.color_good or i3s_config['color_good'] else: output += "OFF" -- cgit v1.3 From 98d149f5c424bff4c03bdd2c9abe7f97d4660555 Mon Sep 17 00:00:00 2001 From: "J.M. Dana" Date: Tue, 24 Feb 2015 21:23:39 +0000 Subject: QA modifications --- py3status/modules/bluetooth.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/py3status/modules/bluetooth.py b/py3status/modules/bluetooth.py index 00fdb84..ee33b09 100644 --- a/py3status/modules/bluetooth.py +++ b/py3status/modules/bluetooth.py @@ -12,9 +12,9 @@ Requires: @license GPLv3 """ -from time import time -import subprocess import shlex +import subprocess +from time import time class Py3status: # configuration parameters @@ -22,16 +22,7 @@ class Py3status: color_good = None color_bad = None - def __init__(self): - pass - - def kill(self, i3s_output_list, i3s_config): - pass - - def on_click(self, i3s_output_list, i3s_config, event): - pass - - def get_status(self, i3s_output_list, i3s_config): + def bluetooth(self, i3s_output_list, i3s_config): # The whole command: # hcitool name `hcitool con | sed -n -r 's/.*([0-9A-F:]{17}).*/\\1/p'` @@ -78,5 +69,5 @@ if __name__ == "__main__": } while True: - print(x.get_status([], config)) + print(x.bluetooth([], config)) sleep(1) -- cgit v1.3 From 602b99f314512552861c7d6f35fe46a5f4776f8e Mon Sep 17 00:00:00 2001 From: "J.M. Dana" Date: Tue, 24 Feb 2015 21:38:44 +0000 Subject: sed dependency removed by using Python regular expressions --- py3status/modules/bluetooth.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/py3status/modules/bluetooth.py b/py3status/modules/bluetooth.py index ee33b09..3585c4d 100644 --- a/py3status/modules/bluetooth.py +++ b/py3status/modules/bluetooth.py @@ -6,16 +6,18 @@ Module for displaying bluetooth status Requires: - hcitool - - sed @author jmdana @license GPLv3 """ +import re import shlex -import subprocess +from subprocess import check_output from time import time +BTMAC_RE = re.compile(r"[0-9A-F:]{17}") + class Py3status: # configuration parameters cache_timeout = 10 @@ -26,22 +28,16 @@ class Py3status: # The whole command: # hcitool name `hcitool con | sed -n -r 's/.*([0-9A-F:]{17}).*/\\1/p'` - cmd1 = shlex.split("hcitool con") - cmd2 = shlex.split("sed -n -r 's/.*([0-9A-F:]{17}).*/\\1/p'") + out = check_output(shlex.split("hcitool con")) + macs = re.findall(BTMAC_RE, out.decode("utf-8")) output = "BT: " color = self.color_bad or i3s_config['color_bad'] - p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE) - p2 = subprocess.Popen(cmd2, stdin=p1.stdout, stdout=subprocess.PIPE) - - macs = p2.communicate()[0].strip().decode("utf-8").split() - if macs != []: names = [] for mac in macs: - cmd3 = shlex.split("hcitool name %s" % mac) - p3 = subprocess.Popen(cmd3, stdout=subprocess.PIPE) - names.append(p3.communicate()[0].strip().decode("utf-8")) + out = check_output(shlex.split("hcitool name %s" % mac)) + names.append(out.strip().decode("utf-8")) output += "|".join(names) -- cgit v1.3