Move NowPlaying._run_cmd to powerline.lib.shell.run_cmd, make asrun use it
This commit is contained in:
parent
079a750d71
commit
4408358151
|
@ -1,13 +0,0 @@
|
||||||
# vim:fileencoding=utf-8:noet
|
|
||||||
|
|
||||||
def asrun(ascript):
|
|
||||||
'''Run the given AppleScript and return the standard output and error.'''
|
|
||||||
from subprocess import Popen, PIPE
|
|
||||||
osa = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE)
|
|
||||||
return osa.communicate(ascript)[0]
|
|
||||||
|
|
||||||
def asquote(astr):
|
|
||||||
'''Return the AppleScript equivalent of the given string.'''
|
|
||||||
|
|
||||||
astr = astr.replace('"', '" & quote & "')
|
|
||||||
return '"{}"'.format(astr)
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
# vim:fileencoding=utf-8:noet
|
||||||
|
|
||||||
|
from subprocess import Popen, PIPE
|
||||||
|
|
||||||
|
|
||||||
|
def run_cmd(pl, cmd, stdin=None):
|
||||||
|
try:
|
||||||
|
p = Popen(cmd, stdout=PIPE, stdin=PIPE)
|
||||||
|
except OSError as e:
|
||||||
|
pl.exception('Could not execute command ({0}): {1}'.format(e, cmd))
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
stdout, err = p.communicate(stdin)
|
||||||
|
return stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
|
def asrun(pl, ascript):
|
||||||
|
'''Run the given AppleScript and return the standard output and error.'''
|
||||||
|
return run_cmd(pl, ['osascript', '-'], ascript)
|
||||||
|
|
||||||
|
|
||||||
|
def asquote(astr):
|
||||||
|
'''Return the AppleScript equivalent of the given string.'''
|
||||||
|
|
||||||
|
astr = astr.replace('"', '" & quote & "')
|
||||||
|
return '"{}"'.format(astr)
|
|
@ -10,7 +10,7 @@ import socket
|
||||||
from multiprocessing import cpu_count as _cpu_count
|
from multiprocessing import cpu_count as _cpu_count
|
||||||
|
|
||||||
from powerline.lib import add_divider_highlight_group
|
from powerline.lib import add_divider_highlight_group
|
||||||
from powerline.lib.apple_script_runner import asrun, asquote
|
from powerline.lib.shell import asrun, asquote, run_cmd
|
||||||
from powerline.lib.url import urllib_read, urllib_urlencode
|
from powerline.lib.url import urllib_read, urllib_urlencode
|
||||||
from powerline.lib.vcs import guess, tree_status
|
from powerline.lib.vcs import guess, tree_status
|
||||||
from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment, with_docstring
|
from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment, with_docstring
|
||||||
|
@ -889,17 +889,6 @@ class NowPlayingSegment(object):
|
||||||
stats.update(func_stats)
|
stats.update(func_stats)
|
||||||
return format.format(**stats)
|
return format.format(**stats)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _run_cmd(pl, cmd):
|
|
||||||
from subprocess import Popen, PIPE
|
|
||||||
try:
|
|
||||||
p = Popen(cmd, stdout=PIPE)
|
|
||||||
stdout, err = p.communicate()
|
|
||||||
except OSError as e:
|
|
||||||
pl.exception('Could not execute command ({0}): {1}'.format(e, cmd))
|
|
||||||
return None
|
|
||||||
return stdout.strip()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _convert_state(state):
|
def _convert_state(state):
|
||||||
state = state.lower()
|
state = state.lower()
|
||||||
|
@ -934,7 +923,7 @@ class NowPlayingSegment(object):
|
||||||
method takes anything in ignore_levels and brings the key inside that
|
method takes anything in ignore_levels and brings the key inside that
|
||||||
to the first level of the dictionary.
|
to the first level of the dictionary.
|
||||||
'''
|
'''
|
||||||
now_playing_str = self._run_cmd(pl, ['cmus-remote', '-Q'])
|
now_playing_str = run_cmd(pl, ['cmus-remote', '-Q'])
|
||||||
if not now_playing_str:
|
if not now_playing_str:
|
||||||
return
|
return
|
||||||
ignore_levels = ('tag', 'set',)
|
ignore_levels = ('tag', 'set',)
|
||||||
|
@ -973,7 +962,7 @@ class NowPlayingSegment(object):
|
||||||
'total': self._convert_seconds(now_playing.get('time', 0)),
|
'total': self._convert_seconds(now_playing.get('time', 0)),
|
||||||
}
|
}
|
||||||
except ImportError:
|
except ImportError:
|
||||||
now_playing = self._run_cmd(pl, ['mpc', 'current', '-f', '%album%\n%artist%\n%title%\n%time%', '-h', str(host), '-p', str(port)])
|
now_playing = run_cmd(pl, ['mpc', 'current', '-f', '%album%\n%artist%\n%title%\n%time%', '-h', str(host), '-p', str(port)])
|
||||||
if not now_playing:
|
if not now_playing:
|
||||||
return
|
return
|
||||||
now_playing = now_playing.split('\n')
|
now_playing = now_playing.split('\n')
|
||||||
|
@ -1042,7 +1031,7 @@ class NowPlayingSegment(object):
|
||||||
end if
|
end if
|
||||||
'''
|
'''
|
||||||
|
|
||||||
spotify = asrun(ascript)
|
spotify = asrun(pl, ascript)
|
||||||
|
|
||||||
spotify_status = spotify.split(", ")
|
spotify_status = spotify.split(", ")
|
||||||
state = self._convert_state(spotify_status[0])
|
state = self._convert_state(spotify_status[0])
|
||||||
|
@ -1068,7 +1057,7 @@ class NowPlayingSegment(object):
|
||||||
player_spotify = player_spotify_dbus # NOQA
|
player_spotify = player_spotify_dbus # NOQA
|
||||||
|
|
||||||
def player_rhythmbox(self, pl):
|
def player_rhythmbox(self, pl):
|
||||||
now_playing = self._run_cmd(pl, ['rhythmbox-client', '--no-start', '--no-present', '--print-playing-format', '%at\n%aa\n%tt\n%te\n%td'])
|
now_playing = run_cmd(pl, ['rhythmbox-client', '--no-start', '--no-present', '--print-playing-format', '%at\n%aa\n%tt\n%te\n%td'])
|
||||||
if not now_playing:
|
if not now_playing:
|
||||||
return
|
return
|
||||||
now_playing = now_playing.split('\n')
|
now_playing = now_playing.split('\n')
|
||||||
|
|
Loading…
Reference in New Issue