From cfd140652532d9745d05cb77abfaabab02902ea5 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sat, 17 Dec 2016 08:06:58 +0100 Subject: [PATCH] [modules/pulseaudio] (Hopefully) improve parsing logic Improve parsing logic to make code easier to understand and extend. fixes #32 --- bumblebee/modules/pulseaudio.py | 48 ++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/bumblebee/modules/pulseaudio.py b/bumblebee/modules/pulseaudio.py index b13eda3..2252b26 100644 --- a/bumblebee/modules/pulseaudio.py +++ b/bumblebee/modules/pulseaudio.py @@ -26,6 +26,12 @@ class Module(bumblebee.engine.Module): self._mute = False channel = "sink" if self.name == "pasink" else "source" + self._patterns = [ + { "expr": "Name:", "callback": (lambda line: False) }, + { "expr": "Mute:", "callback": (lambda line: self.mute(False if " no" in line.lower() else True)) }, + { "expr": "Volume:", "callback": self.getvolume }, + ] + engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE, cmd="pavucontrol") engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="pactl set-{}-mute @DEFAULT_{}@ toggle".format(channel, channel.upper())) @@ -34,6 +40,21 @@ class Module(bumblebee.engine.Module): engine.input.register_callback(self, button=bumblebee.input.WHEEL_DOWN, cmd="pactl set-{}-volume @DEFAULT_{}@ -2%".format(channel, channel.upper())) + def mute(self, value): + self._mute = value + + def getvolume(self, line): + if "mono" in line: + m = re.search(r'mono:.*\s*\/\s*(\d+)%', line) + if m: + self._mono = m.group(1) + else: + m = re.search(r'left:.*\s*\/\s*(\d+)%.*right:.*\s*\/\s*(\d+)%', line) + if m: + self._left = m.group(1) + self._right = m.group(2) + return True + def _default_device(self): output = bumblebee.util.execute("pactl info") pattern = "Default Sink: " if self.name == "pasink" else "Default Source: " @@ -57,28 +78,17 @@ class Module(bumblebee.engine.Module): result = bumblebee.util.execute("pactl list {}".format(channel)) found = False + for line in result.split("\n"): - if "Name:" in line and found == True: - break if device in line: found = True - - if "Mute:" in line and found == True: - self._mute = False if " no" in line.lower() else True - - if "Volume:" in line and found == True: - m = None - if "mono" in line: - m = re.search(r'mono:.*\s*\/\s*(\d+)%', line) - else: - m = re.search(r'left:.*\s*\/\s*(\d+)%.*right:.*\s*\/\s*(\d+)%', line) - if not m: continue - - if "mono" in line: - self._mono = m.group(1) - else: - self._left = m.group(1) - self._right = m.group(2) + if found == False: + continue + for pattern in self._patterns: + if not pattern["expr"] in line: + continue + if pattern["callback"](line) == False: + break def state(self, widget): if self._mute: