From f93dfcde60b845aa7becb8b3ea51f1adfe4fdf2a Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Tue, 18 Aug 2015 18:01:50 +0200 Subject: [PATCH 01/14] cmus - Replace double quote with single quote --- i3pystatus/cmus.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index 60f13ee..0665e5b 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -39,20 +39,20 @@ class Cmus(IntervalModule): 'stream, bitrate'), 'color', ) - color = "#909090" - format = "{status} {song_elapsed}/{song_length} {artist} - {title}" + color = '#909090' + format = '{status} {song_elapsed}/{song_length} {artist} - {title}' status_text = '' interval = 1 status = { - "paused": "▷", - "playing": "▶", - "stopped": "◾", + 'paused': '▷', + 'playing': '▶', + 'stopped': '◾', } - on_leftclick = "playpause" - on_rightclick = "next_song" - on_upscroll = "next_song" - on_downscroll = "previous_song" + on_leftclick = 'playpause' + on_rightclick = 'next_song' + on_upscroll = 'next_song' + on_downscroll = 'previous_song' def _cmus_command(self, command): p = subprocess.Popen('cmus-remote --{command}'.format(command=command), shell=True, @@ -79,13 +79,13 @@ class Cmus(IntervalModule): status = self._query_cmus() if not status: self.output = { - "full_text": 'Not running', - "color": self.color + 'full_text': 'Not running', + 'color': self.color } return fdict = { 'file': status.get('file', ''), - 'status': self.status[status["status"]], + 'status': self.status[status['status']], 'title': status.get('tag_title', ''), 'stream': status.get('stream', ''), 'album': status.get('tag_album', ''), @@ -93,7 +93,7 @@ class Cmus(IntervalModule): 'tracknumber': status.get('tag_tracknumber', 0), 'song_length': TimeWrapper(status.get('duration', 0)), 'song_elapsed': TimeWrapper(status.get('position', 0)), - 'bitrate': int(status.get("bitrate", 0)), + 'bitrate': int(status.get('bitrate', 0)), } if fdict['stream']: @@ -109,8 +109,8 @@ class Cmus(IntervalModule): fdict['artist'] = fdict['artist'].strip() self.output = { - "full_text": formatp(self.format, **fdict), - "color": self.color + 'full_text': formatp(self.format, **fdict), + 'color': self.color } def playpause(self): @@ -123,7 +123,7 @@ class Cmus(IntervalModule): self._cmus_command('play') def next_song(self): - self._cmus_command("next") + self._cmus_command('next') def previous_song(self): - self._cmus_command("prev") + self._cmus_command('prev') From 0daf4ea0c1fa186ac86fb3722428f94b1016ccab Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Tue, 18 Aug 2015 21:12:17 +0200 Subject: [PATCH 02/14] cmus - Remove superflous empty line --- i3pystatus/cmus.py | 1 - 1 file changed, 1 deletion(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index 0665e5b..7ee45ee 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -15,7 +15,6 @@ def _extract_artist_title(input): class Cmus(IntervalModule): - """ Gets the status and current song info using cmus-remote From 5583324c13c39b425bfe5a2fb90df2c1b834fc3a Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Tue, 18 Aug 2015 21:21:16 +0200 Subject: [PATCH 03/14] cmus - Use run_through_shell() --- i3pystatus/cmus.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index 7ee45ee..60e18c8 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -1,8 +1,8 @@ import os from i3pystatus import IntervalModule, formatp +from i3pystatus.core.command import run_through_shell from i3pystatus.core.util import TimeWrapper -import subprocess def _extract_artist_title(input): @@ -54,16 +54,14 @@ class Cmus(IntervalModule): on_downscroll = 'previous_song' def _cmus_command(self, command): - p = subprocess.Popen('cmus-remote --{command}'.format(command=command), shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT) - return p.communicate() + cmdline = 'cmus-remote --{command}'.format(command=command) + return run_through_shell(cmdline, enable_shell=True) def _query_cmus(self): status_dict = {} - status, error = self._cmus_command('query') - if status != b'cmus-remote: cmus is not running\n': - status = status.decode('utf-8').split('\n') + cmd = self._cmus_command('query') + if not cmd.rc: + status = cmd.out.split('\n') for item in status: split_item = item.split(' ') if split_item[0] in ['tag', 'set']: From 502000a4677344a58745820b177b93f1d13120d5 Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Wed, 19 Aug 2015 05:28:56 +0200 Subject: [PATCH 04/14] cmus - Simplify extraction of artist and title information The "fallback" did exactly what the preceeding code did. --- i3pystatus/cmus.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index 60e18c8..6797a14 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -6,12 +6,8 @@ from i3pystatus.core.util import TimeWrapper def _extract_artist_title(input): - for sep in ('-', ' - '): - split = input.split(sep) - if len(split) == 2: - return split[0], split[1] - # fallback - return (input.split('-') + [''] * 2)[:2] + artist, title = (input.split('-') + [''])[:2] + return artist.strip(), title.strip() class Cmus(IntervalModule): From 6996c0b57583b8e7f85f69d8b41f9d70c7e6284f Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Wed, 19 Aug 2015 05:55:11 +0200 Subject: [PATCH 05/14] cmus - Update settings information - Remove duplicate information (available formatters are also listed in docstriong) - Add description for 'color' setting --- i3pystatus/cmus.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index 6797a14..240917e 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -29,10 +29,8 @@ class Cmus(IntervalModule): """ settings = ( - ('format', 'format string, available formatters: status, song_elapsed, ' - 'song_length, artist, title, album, tracknumber, file, ' - 'stream, bitrate'), - 'color', + ('format', 'formatp string'), + ('color', 'The color of the text'), ) color = '#909090' format = '{status} {song_elapsed}/{song_length} {artist} - {title}' From e943831b1200930a296cc2c0e9cf943f4bcdbc58 Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Wed, 19 Aug 2015 06:00:07 +0200 Subject: [PATCH 06/14] cmus - Remove unused class property --- i3pystatus/cmus.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index 240917e..57f10b4 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -32,9 +32,9 @@ class Cmus(IntervalModule): ('format', 'formatp string'), ('color', 'The color of the text'), ) + color = '#909090' format = '{status} {song_elapsed}/{song_length} {artist} - {title}' - status_text = '' interval = 1 status = { 'paused': '▷', From 37c30f645d6492109a58f803ab024f52d24d8e8f Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Wed, 19 Aug 2015 06:15:44 +0200 Subject: [PATCH 07/14] cmus - Make Cmus._query_cmus more pythonic - Use string.partition instead of excessive slicing - Use splitlines instead of split('\n'). This also reduces the times the for-loop is run. --- i3pystatus/cmus.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index 57f10b4..dbd975a 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -52,19 +52,20 @@ class Cmus(IntervalModule): return run_through_shell(cmdline, enable_shell=True) def _query_cmus(self): - status_dict = {} + response = {} cmd = self._cmus_command('query') + if not cmd.rc: - status = cmd.out.split('\n') - for item in status: - split_item = item.split(' ') - if split_item[0] in ['tag', 'set']: - key = '_'.join(split_item[:2]) - val = ' '.join([x for x in split_item[2:]]) - status_dict[key] = val + for line in cmd.out.splitlines(): + category, _, category_value = line.partition(' ') + if category in ('set', 'tag'): + key, _, value = category_value.partition(' ') + key = '_'.join((category, key)) + response[key] = value else: - status_dict[split_item[0]] = ' '.join(split_item[1:]) - return status_dict + response[category] = category_value + + return response def run(self): status = self._query_cmus() From a1f2636c3cea5df1e1ec8d3f65b2ffac1c97837b Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Wed, 19 Aug 2015 06:28:08 +0200 Subject: [PATCH 08/14] cmus - Make Cmus.run() more pythonic - Remove duplicate declaration of self.output - Remove unneccesary stripping of artist and title information. This is take care of in the information gathering functions/methods. - Group code into sections - Make exit path more obvious - Make retrival of filename more obvious - Use more obvious variable names --- i3pystatus/cmus.py | 58 ++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index dbd975a..1035d7f 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -68,42 +68,34 @@ class Cmus(IntervalModule): return response def run(self): - status = self._query_cmus() - if not status: - self.output = { - 'full_text': 'Not running', - 'color': self.color - } - return - fdict = { - 'file': status.get('file', ''), - 'status': self.status[status['status']], - 'title': status.get('tag_title', ''), - 'stream': status.get('stream', ''), - 'album': status.get('tag_album', ''), - 'artist': status.get('tag_artist', ''), - 'tracknumber': status.get('tag_tracknumber', 0), - 'song_length': TimeWrapper(status.get('duration', 0)), - 'song_elapsed': TimeWrapper(status.get('position', 0)), - 'bitrate': int(status.get('bitrate', 0)), - } - - if fdict['stream']: - fdict['artist'], fdict[ - 'title'] = _extract_artist_title(fdict['stream']) - - elif not fdict['title']: - _, filename = os.path.split(fdict['file']) - filebase, _ = os.path.splitext(filename) - fdict['artist'], fdict['title'] = _extract_artist_title(filebase) - - fdict['title'] = fdict['title'].strip() - fdict['artist'] = fdict['artist'].strip() - self.output = { - 'full_text': formatp(self.format, **fdict), + 'full_text': 'Not running', 'color': self.color } + response = self._query_cmus() + + if response: + fdict = { + 'file': response.get('file', ''), + 'status': self.status[response['status']], + 'title': response.get('tag_title', ''), + 'stream': response.get('stream', ''), + 'album': response.get('tag_album', ''), + 'artist': response.get('tag_artist', ''), + 'tracknumber': response.get('tag_tracknumber', 0), + 'song_length': TimeWrapper(response.get('duration', 0)), + 'song_elapsed': TimeWrapper(response.get('position', 0)), + 'bitrate': int(response.get('bitrate', 0)), + } + + if fdict['stream']: + fdict['artist'], fdict['title'] = _extract_artist_title(fdict['stream']) + elif not fdict['title']: + filename = os.path.basename(fdict['file']) + filebase, _ = os.path.splitext(filename) + fdict['artist'], fdict['title'] = _extract_artist_title(filebase) + + self.output['full_text'] = formatp(self.format, **fdict) def playpause(self): status = self._query_cmus().get('status', '') From e979e28aecebc0ce2195407f9e4a9c00ff5f4cee Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Wed, 19 Aug 2015 06:44:38 +0200 Subject: [PATCH 09/14] cmus - One line per from ... import --- i3pystatus/cmus.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index 1035d7f..da486b6 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -1,6 +1,7 @@ import os -from i3pystatus import IntervalModule, formatp +from i3pystatus import formatp +from i3pystatus import IntervalModule from i3pystatus.core.command import run_through_shell from i3pystatus.core.util import TimeWrapper From 2b12fb6fe74adcc4a14fe4338538ac4e2bbee0e9 Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Wed, 19 Aug 2015 07:31:30 +0200 Subject: [PATCH 10/14] cmus - Expose status output mapping as a setting --- i3pystatus/cmus.py | 1 + 1 file changed, 1 insertion(+) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index da486b6..37e3b1c 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -32,6 +32,7 @@ class Cmus(IntervalModule): settings = ( ('format', 'formatp string'), ('color', 'The color of the text'), + ('status', 'Dictionary mapping status to output'), ) color = '#909090' From 1f974087090cc70cdd98dfaab0c0b046356d493f Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Wed, 19 Aug 2015 07:51:52 +0200 Subject: [PATCH 11/14] cmus - Allow changing of output when cmus is not running This closes enkore/i3pystatus#226 --- i3pystatus/cmus.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index 37e3b1c..5474ab6 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -31,12 +31,14 @@ class Cmus(IntervalModule): settings = ( ('format', 'formatp string'), + ('format_not_running', 'Text to show if cmus is not running'), ('color', 'The color of the text'), ('status', 'Dictionary mapping status to output'), ) color = '#909090' format = '{status} {song_elapsed}/{song_length} {artist} - {title}' + format_not_running = 'Not running' interval = 1 status = { 'paused': '▷', @@ -71,7 +73,7 @@ class Cmus(IntervalModule): def run(self): self.output = { - 'full_text': 'Not running', + 'full_text': self.format_not_running, 'color': self.color } response = self._query_cmus() From 2eb0abf5960053a31954b52be2249ac02003adc6 Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Wed, 19 Aug 2015 17:52:32 +0200 Subject: [PATCH 12/14] cmus - Make expected result in Cmus.run clearer --- i3pystatus/cmus.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index 5474ab6..00c1feb 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -73,8 +73,7 @@ class Cmus(IntervalModule): def run(self): self.output = { - 'full_text': self.format_not_running, - 'color': self.color + 'color': self.color, } response = self._query_cmus() @@ -100,6 +99,8 @@ class Cmus(IntervalModule): fdict['artist'], fdict['title'] = _extract_artist_title(filebase) self.output['full_text'] = formatp(self.format, **fdict) + else: + self.output['full_text'] = self.format_not_running def playpause(self): status = self._query_cmus().get('status', '') From aaf8eb38ea1fedf827a7df5c6ca554beaff5fe2b Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Wed, 19 Aug 2015 17:56:37 +0200 Subject: [PATCH 13/14] cmus - Text can now be a different color, when cmus is not running --- i3pystatus/cmus.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index 00c1feb..34e6960 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -33,10 +33,12 @@ class Cmus(IntervalModule): ('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 = '#909090' + color_not_running = '#909090' format = '{status} {song_elapsed}/{song_length} {artist} - {title}' format_not_running = 'Not running' interval = 1 @@ -72,9 +74,7 @@ class Cmus(IntervalModule): return response def run(self): - self.output = { - 'color': self.color, - } + self.output = {} response = self._query_cmus() if response: @@ -99,8 +99,10 @@ class Cmus(IntervalModule): fdict['artist'], fdict['title'] = _extract_artist_title(filebase) self.output['full_text'] = formatp(self.format, **fdict) + self.output['color'] = self.color else: self.output['full_text'] = self.format_not_running + self.output['color'] = self.color_not_running def playpause(self): status = self._query_cmus().get('status', '') From 4823d5653185d4b75b7fbadcf2d0b2a7d6fc53d2 Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Wed, 19 Aug 2015 18:00:59 +0200 Subject: [PATCH 14/14] cmus - Use #fffff as default text color This is the default for most modules. --- i3pystatus/cmus.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index 34e6960..1b8be5b 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -37,8 +37,8 @@ class Cmus(IntervalModule): ('status', 'Dictionary mapping status to output'), ) - color = '#909090' - color_not_running = '#909090' + color = '#ffffff' + color_not_running = '#ffffff' format = '{status} {song_elapsed}/{song_length} {artist} - {title}' format_not_running = 'Not running' interval = 1