From da8ca234a74e9ba0dbfa7aea2175d7883dc2b529 Mon Sep 17 00:00:00 2001 From: Jan T Date: Tue, 9 Jun 2015 23:39:45 +0300 Subject: Added volume_status.py. --- py3status/modules/volume_status.py | 116 +++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 py3status/modules/volume_status.py (limited to 'py3status/modules/volume_status.py') diff --git a/py3status/modules/volume_status.py b/py3status/modules/volume_status.py new file mode 100644 index 0000000..a395108 --- /dev/null +++ b/py3status/modules/volume_status.py @@ -0,0 +1,116 @@ +""" +Show current volume from amixer. +Reads the current Master volume from amixer. +Configuration parameters: + - format : format the output, available variables: {percentage} + - format_mute : format the output when the volume is muted + - cache_timeout : 0 by default, you usually want continuous monitoring + - threshold_degraded : 50 by default, percentage to change color to color_degraded + - threshold_bad : 20 by default, percentage to change color to color_bad + - color_good : #00FF00 by default, good volume color + - color_degraded : #FFFF00 by default, degraded volume color + - color_bad : #FF0000 by default, bad volume color + - channel : "Master" by default, alsamixer channel to track +@author +@license BSD +""" + +from time import time +import re +from subprocess import check_output + + +class Py3status: + format = "{percentage}%" + format_mute = "mute" + cache_timeout = 0 + + threshold_degraded = 50 + threshold_bad = 20 + + color_good = "#00FF00" + color_degraded = "#FFFF00" + color_bad = "#FF0000" + + channel = "Master" + + def __init__(self): + self.text = "" + + def _perc_to_color(self, string): + try: + value = int(string) + except ValueError: + return self.color_bad + + if value < self.threshold_bad: + return self.color_bad + elif value < self.threshold_degraded: + return self.color_degraded + else: + return self.color_good + + # return the format string formatted with available variables + def _format_output(self, format, percentage): + text = format + text = text.replace("{percentage}", percentage) + return text + + # return the current channel volume value as a string + def _get_percentage(self, output): + prog = re.compile(r"\[\d{1,3}%\]") + text = prog.findall(output)[0][1:-2] + return text + + # returns True if the channel is muted + def _get_muted(self, output): + prog = re.compile(r"\[\w{2,3}\]") + text = prog.findall(output)[0][1:-1] + return text == "off" + + # this method is ran by py3status + # returns a response dict + def current_volume(self, i3s_output_list, i3s_config): + + # call amixer + output = check_output(["amixer", "sget", self.channel]).decode() + + # get the current percentage value + perc = self._get_percentage(output) + + # get info about channel mute status + muted = self._get_muted(output) + + # determine the color based on the current volume level + color = self._perc_to_color(perc) + + # format the output + text = self._format_output(self.format_mute if muted else self.format, perc) + + # if the text has been changed, update the cached text and + # set transformed to True + transformed = text != self.text + self.text = text + + # create response dict + response = { + 'cached_until': time() + self.cache_timeout, + 'full_text': text, + 'transformed': transformed, + 'color': color + } + return response + + # open alsamixer on click + def on_click(self, i3s_output_list, i3s_config, event): + check_output(["i3-sensible-terminal", "-e", "alsamixer"]) + +# test if run directly +if __name__ == "__main__": + from time import sleep + x = Py3status() + config = {} + + while True: + print(x.current_volume([], config)) + sleep(1) -- cgit v1.3 From e4273aa83bd537d1f159f1f0bc68308434e956c4 Mon Sep 17 00:00:00 2001 From: Jan T Date: Wed, 10 Jun 2015 02:17:19 +0300 Subject: Changed standard formats. --- py3status/modules/volume_status.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'py3status/modules/volume_status.py') diff --git a/py3status/modules/volume_status.py b/py3status/modules/volume_status.py index a395108..b436b5c 100644 --- a/py3status/modules/volume_status.py +++ b/py3status/modules/volume_status.py @@ -1,6 +1,6 @@ """ Show current volume from amixer. -Reads the current Master volume from amixer. +Extend on the standard i3status volume module by adding color and threshold settings. Configuration parameters: - format : format the output, available variables: {percentage} - format_mute : format the output when the volume is muted @@ -21,8 +21,8 @@ from subprocess import check_output class Py3status: - format = "{percentage}%" - format_mute = "mute" + format = "♪: {percentage}%" + format_muted = "♪: muted" cache_timeout = 0 threshold_degraded = 50 @@ -74,7 +74,7 @@ class Py3status: # call amixer output = check_output(["amixer", "sget", self.channel]).decode() - + # get the current percentage value perc = self._get_percentage(output) @@ -85,7 +85,7 @@ class Py3status: color = self._perc_to_color(perc) # format the output - text = self._format_output(self.format_mute if muted else self.format, perc) + text = self._format_output(self.format_muted if muted else self.format, perc) # if the text has been changed, update the cached text and # set transformed to True -- cgit v1.3 From 503f2eb95ef73109949a227795a03fee2fad8f72 Mon Sep 17 00:00:00 2001 From: Jan T Date: Wed, 10 Jun 2015 02:30:48 +0300 Subject: Fixed typos and comments. --- py3status/modules/volume_status.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'py3status/modules/volume_status.py') diff --git a/py3status/modules/volume_status.py b/py3status/modules/volume_status.py index b436b5c..90c81a8 100644 --- a/py3status/modules/volume_status.py +++ b/py3status/modules/volume_status.py @@ -1,6 +1,6 @@ """ -Show current volume from amixer. -Extend on the standard i3status volume module by adding color and threshold settings. +Display current sound volume using amixer. +Expands on the standard i3status volume module by adding color and percentage threshold settings. Configuration parameters: - format : format the output, available variables: {percentage} - format_mute : format the output when the volume is muted @@ -34,9 +34,11 @@ class Py3status: channel = "Master" + # constructor def __init__(self): self.text = "" + # compares current volume to the thresholds, returns a color code def _perc_to_color(self, string): try: value = int(string) -- cgit v1.3 From 06f8ed3d9f63c171fdda0205eef5e8d9ee4247f7 Mon Sep 17 00:00:00 2001 From: Jan T Date: Wed, 10 Jun 2015 14:49:41 +0300 Subject: Fixed regexes. Added note about forcing py3status update. --- py3status/modules/volume_status.py | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'py3status/modules/volume_status.py') diff --git a/py3status/modules/volume_status.py b/py3status/modules/volume_status.py index 90c81a8..f61e7df 100644 --- a/py3status/modules/volume_status.py +++ b/py3status/modules/volume_status.py @@ -1,6 +1,11 @@ """ Display current sound volume using amixer. Expands on the standard i3status volume module by adding color and percentage threshold settings. + +NOTE: If you want to refresh the module quicker than the i3status interval, + send a USR1 signal to py3status in the keybinding. + Example: killall -s USR1 py3status + Configuration parameters: - format : format the output, available variables: {percentage} - format_mute : format the output when the volume is muted @@ -60,15 +65,32 @@ class Py3status: # return the current channel volume value as a string def _get_percentage(self, output): - prog = re.compile(r"\[\d{1,3}%\]") - text = prog.findall(output)[0][1:-2] - return text + + # attempt to find a percentage value in square brackets + p = re.compile(r"(?<=\[)\d{1,3}(?=%\])") + text = p.search(output).group() + + # check if the parsed value is sane by checking if it's an integer + try: + int(text) + return text + + # if not, show an error message in output + except ValueError: + return "Error: Can't parse amixer output." # returns True if the channel is muted def _get_muted(self, output): - prog = re.compile(r"\[\w{2,3}\]") - text = prog.findall(output)[0][1:-1] - return text == "off" + p = re.compile(r"(?<=\[)\w{2,3}(?=\])") + text = p.search(output).group() + + # check if the parsed string is either "off" or "on" + if text in ["on", "off"]: + return text == "off" + + # if not, return False + else: + return False # this method is ran by py3status # returns a response dict -- cgit v1.3 From 8299290ce024cae2ece883e77f92ff0487380ef7 Mon Sep 17 00:00:00 2001 From: Jan T Date: Wed, 10 Jun 2015 15:29:14 +0300 Subject: Added hashbang and encoding. Added alsa-utils package as a dependency. --- py3status/modules/volume_status.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'py3status/modules/volume_status.py') diff --git a/py3status/modules/volume_status.py b/py3status/modules/volume_status.py index f61e7df..2ef59fe 100644 --- a/py3status/modules/volume_status.py +++ b/py3status/modules/volume_status.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- """ Display current sound volume using amixer. Expands on the standard i3status volume module by adding color and percentage threshold settings. @@ -6,6 +8,9 @@ NOTE: If you want to refresh the module quicker than the i3status interval, send a USR1 signal to py3status in the keybinding. Example: killall -s USR1 py3status +Dependencies: + alsa-utils (tested with alsa-utils 1.0.29-1) + Configuration parameters: - format : format the output, available variables: {percentage} - format_mute : format the output when the volume is muted -- cgit v1.3 From 3d06da182aa95a561159f42538fa4438604c0314 Mon Sep 17 00:00:00 2001 From: Jan T Date: Sun, 26 Jul 2015 17:33:52 +0300 Subject: Fixed issue where text replacement broke on python2. --- py3status/modules/volume_status.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'py3status/modules/volume_status.py') diff --git a/py3status/modules/volume_status.py b/py3status/modules/volume_status.py index 2ef59fe..14a92a1 100644 --- a/py3status/modules/volume_status.py +++ b/py3status/modules/volume_status.py @@ -64,8 +64,7 @@ class Py3status: # return the format string formatted with available variables def _format_output(self, format, percentage): - text = format - text = text.replace("{percentage}", percentage) + text = format.format(percentage=percentage) return text # return the current channel volume value as a string -- cgit v1.3 From 7d6351ccd71bf305d9108d207b5f9f1af767d0a9 Mon Sep 17 00:00:00 2001 From: Jan T Date: Sun, 26 Jul 2015 17:38:37 +0300 Subject: Removed forced 'on_click' behaviour. --- py3status/modules/volume_status.py | 4 ---- 1 file changed, 4 deletions(-) (limited to 'py3status/modules/volume_status.py') diff --git a/py3status/modules/volume_status.py b/py3status/modules/volume_status.py index 14a92a1..0cad120 100644 --- a/py3status/modules/volume_status.py +++ b/py3status/modules/volume_status.py @@ -129,10 +129,6 @@ class Py3status: } return response - # open alsamixer on click - def on_click(self, i3s_output_list, i3s_config, event): - check_output(["i3-sensible-terminal", "-e", "alsamixer"]) - # test if run directly if __name__ == "__main__": from time import sleep -- cgit v1.3 From f1acc164fe3256427481309ccfbcb09b81aa517c Mon Sep 17 00:00:00 2001 From: Jan T Date: Sun, 26 Jul 2015 17:57:15 +0300 Subject: Calls to amixer now use shlex python module. Added device config option. --- py3status/modules/volume_status.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'py3status/modules/volume_status.py') diff --git a/py3status/modules/volume_status.py b/py3status/modules/volume_status.py index 0cad120..6813cce 100644 --- a/py3status/modules/volume_status.py +++ b/py3status/modules/volume_status.py @@ -21,6 +21,7 @@ Configuration parameters: - color_degraded : #FFFF00 by default, degraded volume color - color_bad : #FF0000 by default, bad volume color - channel : "Master" by default, alsamixer channel to track + - device : "default" by default, alsamixer device to use @author @license BSD """ @@ -28,6 +29,7 @@ Configuration parameters: from time import time import re from subprocess import check_output +import shlex class Py3status: @@ -42,6 +44,7 @@ class Py3status: color_degraded = "#FFFF00" color_bad = "#FF0000" + device = "default" channel = "Master" # constructor @@ -101,7 +104,7 @@ class Py3status: def current_volume(self, i3s_output_list, i3s_config): # call amixer - output = check_output(["amixer", "sget", self.channel]).decode() + output = check_output(shlex.split("amixer -D {} sget {}".format(self.device, self.channel))).decode("utf-8") # get the current percentage value perc = self._get_percentage(output) -- cgit v1.3