use py3.command_output helper
@@ -49,10 +49,6 @@ paused
{'color': '#ffff00', 'full_text': 'Music For Programming - Lackluster'}
"""
-from subprocess import check_output
-
-FMT_PARAMETER = ['isplaying']
-FMT_SEPARATOR = u'\u001e'
STRING_NOT_INSTALLED = 'not installed'
@@ -89,18 +85,18 @@ class Py3status:
def post_config_hook(self):
if not self.py3.check_commands('deadbeef'):
raise Exception(STRING_NOT_INSTALLED)
+
+ self.separator = '|SEPARATOR|'
+ self.placeholders = list(
+ set(self.py3.get_placeholders_list(self.format) + ['isplaying'])
+ )
+ self.deadbeef_command = 'deadbeef --nowplaying-tf "{}"'.format(
+ self.separator.join(['%{}%'.format(x) for x in self.placeholders])
+ )
self.color_paused = self.py3.COLOR_PAUSED or self.py3.COLOR_DEGRADED
self.color_playing = self.py3.COLOR_PLAYING or self.py3.COLOR_GOOD
self.color_stopped = self.py3.COLOR_STOPPED or self.py3.COLOR_BAD
- # mix format and necessary placeholders with separator...
- # then we merge together to run deadbeef command only once
- self.placeholders = list(
- set(self.py3.get_placeholders_list(self.format)) |
- set(FMT_PARAMETER))
- fmt = FMT_SEPARATOR.join(['%{}%'.format(x) for x in self.placeholders])
- self.cmd = 'deadbeef --nowplaying-tf "%s"' % fmt
-
def _is_running(self):
try:
self.py3.command_output(['pgrep', 'deadbeef'])
@@ -108,30 +104,30 @@ class Py3status:
except:
return False
+ def _get_deadbeef_data(self):
+ try:
+ self.py3.command_output(self.deadbeef_command)
+ except self.py3.CommandError as ce:
+ return ce.output
+
def deadbeef(self):
- color = self.color_stopped
- status = {}
+ beef_data = {}
cached_until = self.sleep_timeout
+ color = self.color_stopped
if self._is_running():
+ line = self._get_deadbeef_data()
+ beef_data = dict(zip(self.placeholders, line.split(self.separator)))
cached_until = self.cache_timeout
- # Starting deadbeef may generate lot of startup noises either
- # with or without error codes. Running command below may sometimes
- # change how things behaves onscreen. We use subprocess to ignore
- # error codes. We use pgrep and hidden placeholders to dictate
- # how status output and color should look... mainly to stay
- # consistency between versions.
- out = check_output(self.cmd, shell=True).decode('utf-8')
- status = dict(zip(self.placeholders, out.split(FMT_SEPARATOR)))
-
- if status['isplaying']:
+
+ if beef_data['isplaying']:
color = self.color_playing
else:
color = self.color_paused
return {
'cached_until': self.py3.time_in(cached_until),
- 'full_text': self.py3.safe_format(self.format, status),
+ 'full_text': self.py3.safe_format(self.format, beef_data),
'color': color,
}
