[modules/pulseaudio] (Hopefully) improve parsing logic

Improve parsing logic to make code easier to understand and extend.

fixes #32
This commit is contained in:
Tobi-wan Kenobi 2016-12-17 08:06:58 +01:00
parent 220bb9bf60
commit cfd1406525

View file

@ -26,6 +26,12 @@ class Module(bumblebee.engine.Module):
self._mute = False self._mute = False
channel = "sink" if self.name == "pasink" else "source" 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.RIGHT_MOUSE, cmd="pavucontrol")
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
cmd="pactl set-{}-mute @DEFAULT_{}@ toggle".format(channel, channel.upper())) 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, engine.input.register_callback(self, button=bumblebee.input.WHEEL_DOWN,
cmd="pactl set-{}-volume @DEFAULT_{}@ -2%".format(channel, channel.upper())) 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): def _default_device(self):
output = bumblebee.util.execute("pactl info") output = bumblebee.util.execute("pactl info")
pattern = "Default Sink: " if self.name == "pasink" else "Default Source: " 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)) result = bumblebee.util.execute("pactl list {}".format(channel))
found = False found = False
for line in result.split("\n"): for line in result.split("\n"):
if "Name:" in line and found == True:
break
if device in line: if device in line:
found = True found = True
if found == False:
if "Mute:" in line and found == True: continue
self._mute = False if " no" in line.lower() else True for pattern in self._patterns:
if not pattern["expr"] in line:
if "Volume:" in line and found == True: continue
m = None if pattern["callback"](line) == False:
if "mono" in line: break
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)
def state(self, widget): def state(self, widget):
if self._mute: if self._mute: