From d16d66e520c5f80870957c63694708118d6f9f69 Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Fri, 14 Oct 2016 00:46:53 +0200 Subject: [PATCH 1/6] Add module for MOC (music on console) --- i3pystatus/moc.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 i3pystatus/moc.py diff --git a/i3pystatus/moc.py b/i3pystatus/moc.py new file mode 100644 index 0000000..2c8377f --- /dev/null +++ b/i3pystatus/moc.py @@ -0,0 +1,99 @@ +import re + +from i3pystatus import IntervalModule +from i3pystatus import formatp +from i3pystatus.core.command import run_through_shell +from i3pystatus.core.util import TimeWrapper + + +class Moc(IntervalModule): + """ + Display various information from MOC (musci on console) + + .. rubric:: Available formatters + + * `{status}` — current status icon (paused/playing/stopped) + * `{song_elapsed}` — song elapsed time (mm:ss format) + * `{song_length}` — total song duration (mm:ss format) + * `{artist}` — artist + * `{title}` — title + * `{album}` — album + * `{tracknumber}` — tracknumber + * `{file}` — file or url name + """ + + settings = ( + ('format', 'formatp string'), + ('format_not_running', 'Text to show if cmus is not running'), + ('color', 'The color of the text'), + ('color_not_running', 'The color of the text, when cmus is not running'), + ('status', 'Dictionary mapping status to output'), + ) + + color = '#ffffff' + color_not_running = '#ffffff' + format = '{status} {song_elapsed}/{song_length} {artist} - {title}' + format_not_running = 'Not running' + interval = 1 + status = { + 'pause': '▷', + 'play': '▶', + 'stop': '◾', + } + + on_leftclick = 'toggle_pause' + on_rightclick = 'next_song' + on_upscroll = 'next_song' + on_downscroll = 'previous_song' + + def _moc_command(self, command): + cmdline = 'mocp --{command}'.format(command=command) + return run_through_shell(cmdline, enable_shell=True) + + def _query_cmus(self): + response = {} + + # Get raw information + cmd = self._moc_command('info') + + # Now we make it useful + if not cmd.rc: + for line in cmd.out.splitlines(): + key, _, value = line.partition(': ') + response[key] = value + + return response + + def run(self): + response = self._query_cmus() + + if response: + fdict = { + 'album': response.get('Album', ''), + 'artist': response.get('Artist', ''), + 'file': response.get('File', ''), + 'song_elapsed': TimeWrapper(response.get('CurrentSec', 0)), + 'song_length': TimeWrapper(response.get('TotalSec', 0)), + 'status': self.status[response['State'].lower()], + 'title': response.get('SongTitle', ''), + 'tracknumber': re.match(r'(\d*).*', response.get('Title', '')).group(1) or 0, + } + + self.output = { + 'full_text': formatp(self.format, **fdict), + 'color': self.color, + } + else: + self.output = { + 'full_text': self.format_not_running, + 'color': self.color_not_running, + } + + def toggle_pause(self): + self._moc_command('toggle-pause') + + def next_song(self): + self._moc_command('next') + + def previous_song(self): + self._moc_command('previous') From 0df4882ba0c2e93e5565de2f0b9da447447e12be Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Fri, 14 Oct 2016 00:49:52 +0200 Subject: [PATCH 2/6] Add moc to module overview --- docs/i3pystatus.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/i3pystatus.rst b/docs/i3pystatus.rst index c909fa4..a2bf7e2 100644 --- a/docs/i3pystatus.rst +++ b/docs/i3pystatus.rst @@ -11,7 +11,7 @@ Module reference :Audio: `alsa`_ - `pulseaudio`_ :Hardware: `backlight`_ - `battery`_ - `temp`_ :Network: `net_speed`_ - `network`_ - `online`_ - `openstack_vms`_ - `openvpn`_ -:Music: `cmus`_ - `mpd`_ - `now_playing`_ - `pianobar`_ - `spotify`_ +:Music: `cmus`_ - `moc`_ - `mpd`_ - `now_playing`_ - `pianobar`_ - `spotify`_ :Websites: `bitcoin`_ - `dota2wins`_ - `github`_ - `modsde`_ - `parcel`_ - `reddit`_ - `weather`_ - `whosonlocation`_ :Other: `anybar`_ - `mail`_ - `pomodoro`_ - `pyload`_ - `text`_ - `updates`_ From 4ca311233193817b1686085347dbe2ba5c8f4821 Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Fri, 14 Oct 2016 02:41:44 +0200 Subject: [PATCH 3/6] Expose collected data in moc module --- i3pystatus/moc.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/i3pystatus/moc.py b/i3pystatus/moc.py index 2c8377f..32a910f 100644 --- a/i3pystatus/moc.py +++ b/i3pystatus/moc.py @@ -79,11 +79,16 @@ class Moc(IntervalModule): 'tracknumber': re.match(r'(\d*).*', response.get('Title', '')).group(1) or 0, } + self.data = fdict + self.output = { - 'full_text': formatp(self.format, **fdict), + 'full_text': formatp(self.format, **data), 'color': self.color, } else: + if hasattr(self, "data"): + del self.data + self.output = { 'full_text': self.format_not_running, 'color': self.color_not_running, From be22e60433efe7923657c509e6cbe3f2e7dacdc7 Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Fri, 14 Oct 2016 03:40:27 +0200 Subject: [PATCH 4/6] Fix stupid and avoidable error in moc --- i3pystatus/moc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i3pystatus/moc.py b/i3pystatus/moc.py index 32a910f..01466c1 100644 --- a/i3pystatus/moc.py +++ b/i3pystatus/moc.py @@ -82,7 +82,7 @@ class Moc(IntervalModule): self.data = fdict self.output = { - 'full_text': formatp(self.format, **data), + 'full_text': formatp(self.format, **self.data), 'color': self.color, } else: From ab00bf4d2a1a0835b9382a238b959caae3d55989 Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Fri, 14 Oct 2016 15:16:07 +0200 Subject: [PATCH 5/6] moc - Remove all references to cmus --- i3pystatus/moc.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/i3pystatus/moc.py b/i3pystatus/moc.py index 01466c1..3d43964 100644 --- a/i3pystatus/moc.py +++ b/i3pystatus/moc.py @@ -24,9 +24,9 @@ class Moc(IntervalModule): settings = ( ('format', 'formatp string'), - ('format_not_running', 'Text to show if cmus is not running'), + ('format_not_running', 'Text to show if MOC is not running'), ('color', 'The color of the text'), - ('color_not_running', 'The color of the text, when cmus is not running'), + ('color_not_running', 'The color of the text, when MOC is not running'), ('status', 'Dictionary mapping status to output'), ) @@ -50,7 +50,7 @@ class Moc(IntervalModule): cmdline = 'mocp --{command}'.format(command=command) return run_through_shell(cmdline, enable_shell=True) - def _query_cmus(self): + def _query_moc(self): response = {} # Get raw information @@ -65,7 +65,7 @@ class Moc(IntervalModule): return response def run(self): - response = self._query_cmus() + response = self._query_moc() if response: fdict = { From 0fd281d4a0d20a3fcc25673a96312c2576c314d3 Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Fri, 14 Oct 2016 15:16:47 +0200 Subject: [PATCH 6/6] moc - Fix typo --- i3pystatus/moc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i3pystatus/moc.py b/i3pystatus/moc.py index 3d43964..94e4b2f 100644 --- a/i3pystatus/moc.py +++ b/i3pystatus/moc.py @@ -8,7 +8,7 @@ from i3pystatus.core.util import TimeWrapper class Moc(IntervalModule): """ - Display various information from MOC (musci on console) + Display various information from MOC (music on console) .. rubric:: Available formatters