Refactored plugin so that errors are properly returned up the stack, client connections are lazy and re-initialized upon error
This commit is contained in:
parent
8759dafa0e
commit
f156e9c199
1 changed files with 44 additions and 18 deletions
|
@ -32,12 +32,29 @@ class MusicMpdPlugin(MusicPlugin):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
|
self.client = None
|
||||||
|
|
||||||
|
def _connect(self):
|
||||||
|
if not self.client:
|
||||||
self.client = mpd.MPDClient(use_unicode=True)
|
self.client = mpd.MPDClient(use_unicode=True)
|
||||||
self.client.connect(self.host, self.port)
|
self.client.connect(self.host, self.port)
|
||||||
|
return self.client
|
||||||
|
|
||||||
def _exec(self, method, *args, **kwargs):
|
def _exec(self, method, *args, **kwargs):
|
||||||
getattr(self.client, method)(*args, **kwargs)
|
return_status = kwargs.pop('return_status') \
|
||||||
|
if 'return_status' in kwargs else True
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._connect()
|
||||||
|
response = getattr(self.client, method)(*args, **kwargs)
|
||||||
|
if return_status:
|
||||||
return self.status().output
|
return self.status().output
|
||||||
|
return response
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.warning('Exception while executing MPD method {}: {}'.
|
||||||
|
format(method, str(e)))
|
||||||
|
self.client = None
|
||||||
|
return (None, str(e))
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def play(self, resource=None):
|
def play(self, resource=None):
|
||||||
|
@ -309,7 +326,14 @@ class MusicMpdPlugin(MusicPlugin):
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._connect()
|
||||||
return self.client.status()
|
return self.client.status()
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.warning('Exception while getting MPD status: {}'.
|
||||||
|
format(str(e)))
|
||||||
|
self.client = None
|
||||||
|
return (None, str(e))
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def currentsong(self):
|
def currentsong(self):
|
||||||
|
@ -333,7 +357,7 @@ class MusicMpdPlugin(MusicPlugin):
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
track = self.client.currentsong()
|
track = self._exec('currentsong', return_status=False)
|
||||||
if 'title' in track and ('artist' not in track
|
if 'title' in track and ('artist' not in track
|
||||||
or not track['artist']
|
or not track['artist']
|
||||||
or re.search('^tunein:', track['file'])):
|
or re.search('^tunein:', track['file'])):
|
||||||
|
@ -381,7 +405,7 @@ class MusicMpdPlugin(MusicPlugin):
|
||||||
]
|
]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self.client.playlistinfo()
|
return self._exec('playlistinfo', return_status=False)
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def listplaylists(self):
|
def listplaylists(self):
|
||||||
|
@ -405,7 +429,8 @@ class MusicMpdPlugin(MusicPlugin):
|
||||||
]
|
]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return sorted(self.client.listplaylists(), key=lambda p: p['playlist'])
|
return sorted(self._exec('listplaylists', return_status=False),
|
||||||
|
key=lambda p: p['playlist'])
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def lsinfo(self, uri=None):
|
def lsinfo(self, uri=None):
|
||||||
|
@ -413,7 +438,8 @@ class MusicMpdPlugin(MusicPlugin):
|
||||||
Returns the list of playlists and directories on the server
|
Returns the list of playlists and directories on the server
|
||||||
"""
|
"""
|
||||||
|
|
||||||
output = self.client.lsinfo(uri) if uri else self.client.lsinfo()
|
output = self._exec('lsinfo', uri, return_status=False) \
|
||||||
|
if uri else self._exec('lsinfo', return_status=False)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@action
|
@action
|
||||||
|
@ -428,7 +454,7 @@ class MusicMpdPlugin(MusicPlugin):
|
||||||
:returns: A list of dicts representing the songs being added since the specified version
|
:returns: A list of dicts representing the songs being added since the specified version
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self.client.plchanges(version)
|
return self._exec('plchanges', version, return_status=False)
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def searchaddplaylist(self, name):
|
def searchaddplaylist(self, name):
|
||||||
|
@ -442,12 +468,12 @@ class MusicMpdPlugin(MusicPlugin):
|
||||||
playlists = list(map(lambda _: _['playlist'],
|
playlists = list(map(lambda _: _['playlist'],
|
||||||
filter(lambda playlist:
|
filter(lambda playlist:
|
||||||
name.lower() in playlist['playlist'].lower(),
|
name.lower() in playlist['playlist'].lower(),
|
||||||
self.client.listplaylists())))
|
self._exec('listplaylists', return_status=False))))
|
||||||
|
|
||||||
if len(playlists):
|
if len(playlists):
|
||||||
self.client.clear()
|
self._exec('clear')
|
||||||
self.client.load(playlists[0])
|
self._exec('load', playlists[0])
|
||||||
self.client.play()
|
self._exec('play')
|
||||||
return {'playlist': playlists[0]}
|
return {'playlist': playlists[0]}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
|
@ -460,7 +486,7 @@ class MusicMpdPlugin(MusicPlugin):
|
||||||
:returns: list[dict]
|
:returns: list[dict]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self.client.find(*filter, *args, **kwargs)
|
return self._exec('find', *filter, *args, return_status=False, **kwargs)
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def findadd(self, filter, *args, **kwargs):
|
def findadd(self, filter, *args, **kwargs):
|
||||||
|
@ -472,7 +498,7 @@ class MusicMpdPlugin(MusicPlugin):
|
||||||
:returns: list[dict]
|
:returns: list[dict]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self.client.findadd(*filter, *args, **kwargs)
|
return self._exec('findadd', *filter, *args, return_status=False, **kwargs)
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def search(self, filter, *args, **kwargs):
|
def search(self, filter, *args, **kwargs):
|
||||||
|
@ -484,7 +510,7 @@ class MusicMpdPlugin(MusicPlugin):
|
||||||
:returns: list[dict]
|
:returns: list[dict]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
items = self.client.search(*filter, *args, **kwargs)
|
items = self._exec('search', *filter, *args, return_status=False, **kwargs)
|
||||||
|
|
||||||
# Spotify results first
|
# Spotify results first
|
||||||
items = sorted(items, key=lambda item:
|
items = sorted(items, key=lambda item:
|
||||||
|
@ -502,7 +528,7 @@ class MusicMpdPlugin(MusicPlugin):
|
||||||
:returns: list[dict]
|
:returns: list[dict]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self.client.searchadd(*filter, *args, **kwargs)
|
return self._exec('searchadd', *filter, *args, return_status=False, **kwargs)
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue