moved error handling to _call()-method
This commit is contained in:
parent
f557ee8cfb
commit
687d9cfbd8
1 changed files with 104 additions and 151 deletions
65
mcg.py
65
mcg.py
|
|
@ -206,7 +206,16 @@ class MCGClient(MCGBase, mpd.MPDClient):
|
||||||
|
|
||||||
|
|
||||||
def _call(self, command, *args):
|
def _call(self, command, *args):
|
||||||
|
try:
|
||||||
return getattr(super(), command)(*args)
|
return getattr(super(), command)(*args)
|
||||||
|
except mpd.CommandError as e:
|
||||||
|
self._callback(MCGClient.SIGNAL_ERROR, e)
|
||||||
|
except mpd.ConnectionError as e:
|
||||||
|
self._set_connection_status(False, e)
|
||||||
|
except ConnectionResetError as e:
|
||||||
|
self._set_connection_status(False, e)
|
||||||
|
except BrokenPipeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def _run(self):
|
def _run(self):
|
||||||
|
|
@ -233,25 +242,14 @@ class MCGClient(MCGBase, mpd.MPDClient):
|
||||||
self._disconnect()
|
self._disconnect()
|
||||||
raise e
|
raise e
|
||||||
self._set_connection_status(True)
|
self._set_connection_status(True)
|
||||||
except mpd.CommandError as e:
|
|
||||||
self._callback(MCGClient.SIGNAL_ERROR, e)
|
|
||||||
except mpd.ConnectionError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
self._set_connection_status(False, e)
|
self._set_connection_status(False, e)
|
||||||
|
|
||||||
|
|
||||||
def _disconnect(self):
|
def _disconnect(self):
|
||||||
try:
|
|
||||||
self._call('noidle')
|
self._call('noidle')
|
||||||
self._call('disconnect')
|
self._call('disconnect')
|
||||||
self._set_connection_status(False)
|
self._set_connection_status(False)
|
||||||
except BrokenPipeError:
|
|
||||||
pass
|
|
||||||
except ConnectionResetError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
except mpd.ConnectionError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
|
|
||||||
|
|
||||||
# Status commands
|
# Status commands
|
||||||
|
|
@ -259,7 +257,6 @@ class MCGClient(MCGBase, mpd.MPDClient):
|
||||||
def _get_status(self):
|
def _get_status(self):
|
||||||
"""Action: Performs the real status determination
|
"""Action: Performs the real status determination
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
# current status
|
# current status
|
||||||
self._call('noidle')
|
self._call('noidle')
|
||||||
status = self._call('status')
|
status = self._call('status')
|
||||||
|
|
@ -283,26 +280,14 @@ class MCGClient(MCGBase, mpd.MPDClient):
|
||||||
if '/' in pos:
|
if '/' in pos:
|
||||||
pos = pos[0: pos.index('/')]
|
pos = pos[0: pos.index('/')]
|
||||||
pos = int(pos) - 1
|
pos = int(pos) - 1
|
||||||
|
|
||||||
self._state = state
|
self._state = state
|
||||||
self._callback(MCGClient.SIGNAL_STATUS, state, album, pos, volume, error)
|
self._callback(MCGClient.SIGNAL_STATUS, state, album, pos, volume, error)
|
||||||
except BrokenPipeError:
|
|
||||||
pass
|
|
||||||
except ConnectionResetError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
except mpd.ConnectionError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
|
|
||||||
|
|
||||||
# Playback option commants
|
# Playback option commants
|
||||||
|
|
||||||
def _set_volume(self, volume):
|
def _set_volume(self, volume):
|
||||||
try:
|
|
||||||
self._call('setvol', volume)
|
self._call('setvol', volume)
|
||||||
except mpd.CommandError as e:
|
|
||||||
self._callback(MCGClient.SIGNAL_ERROR, e)
|
|
||||||
except mpd.ConnectionError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
|
|
||||||
|
|
||||||
# Playback control commands
|
# Playback control commands
|
||||||
|
|
@ -310,45 +295,32 @@ class MCGClient(MCGBase, mpd.MPDClient):
|
||||||
def _playpause(self):
|
def _playpause(self):
|
||||||
"""Action: Performs the real play/pause command.
|
"""Action: Performs the real play/pause command.
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
status = self._call('status')
|
status = self._call('status')
|
||||||
state = status['state']
|
state = status['state']
|
||||||
|
|
||||||
if state == 'play':
|
if state == 'play':
|
||||||
self._call('pause')
|
self._call('pause')
|
||||||
else:
|
else:
|
||||||
self._call('play')
|
self._call('play')
|
||||||
except mpd.ConnectionError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
|
|
||||||
|
|
||||||
def _play_album(self, album):
|
def _play_album(self, album):
|
||||||
if album not in self._albums:
|
if album not in self._albums:
|
||||||
return
|
return
|
||||||
try:
|
|
||||||
track_ids = []
|
track_ids = []
|
||||||
for track in self._albums[album].get_tracks():
|
for track in self._albums[album].get_tracks():
|
||||||
track_id = self._call('addid', track.get_file())
|
track_id = self._call('addid', track.get_file())
|
||||||
track_ids.append(track_id)
|
track_ids.append(track_id)
|
||||||
if self._state != 'play':
|
if self._state != 'play':
|
||||||
self._call('playid', track_ids[0])
|
self._call('playid', track_ids[0])
|
||||||
except mpd.CommandError as e:
|
|
||||||
self._callback(MCGClient.SIGNAL_ERROR, e)
|
|
||||||
except mpd.ConnectionError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
|
|
||||||
|
|
||||||
def _stop(self):
|
def _stop(self):
|
||||||
try:
|
|
||||||
self._call('stop')
|
self._call('stop')
|
||||||
except mpd.ConnectionError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
|
|
||||||
|
|
||||||
# Playlist commands
|
# Playlist commands
|
||||||
|
|
||||||
def _load_playlist(self):
|
def _load_playlist(self):
|
||||||
try:
|
|
||||||
playlist = []
|
playlist = []
|
||||||
for song in self._call('playlistinfo'):
|
for song in self._call('playlistinfo'):
|
||||||
try:
|
try:
|
||||||
|
|
@ -366,18 +338,13 @@ class MCGClient(MCGBase, mpd.MPDClient):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
self._callback(MCGClient.SIGNAL_LOAD_PLAYLIST, playlist, None)
|
self._callback(MCGClient.SIGNAL_LOAD_PLAYLIST, playlist, None)
|
||||||
except mpd.ConnectionError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
|
|
||||||
|
|
||||||
def _clear_playlist(self):
|
def _clear_playlist(self):
|
||||||
"""Action: Performs the real clearing of the current
|
"""Action: Performs the real clearing of the current
|
||||||
playlist.
|
playlist.
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
self._call('clear')
|
self._call('clear')
|
||||||
except mpd.ConnectionError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
|
|
||||||
|
|
||||||
# Database commands
|
# Database commands
|
||||||
|
|
@ -385,7 +352,6 @@ class MCGClient(MCGBase, mpd.MPDClient):
|
||||||
def _load_albums(self):
|
def _load_albums(self):
|
||||||
"""Action: Performs the real update.
|
"""Action: Performs the real update.
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
for song in self._call('listallinfo'):
|
for song in self._call('listallinfo'):
|
||||||
try:
|
try:
|
||||||
hash = MCGAlbum.hash(song['album'], song['date'])
|
hash = MCGAlbum.hash(song['album'], song['date'])
|
||||||
|
|
@ -402,15 +368,10 @@ class MCGClient(MCGBase, mpd.MPDClient):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
self._callback(MCGClient.SIGNAL_LOAD_ALBUMS, self._albums, None)
|
self._callback(MCGClient.SIGNAL_LOAD_ALBUMS, self._albums, None)
|
||||||
except mpd.ConnectionError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
|
|
||||||
|
|
||||||
def _update(self):
|
def _update(self):
|
||||||
try:
|
|
||||||
self._call('update')
|
self._call('update')
|
||||||
except mpd.ConnectionError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
|
|
||||||
def _set_connection_status(self, status, error=None):
|
def _set_connection_status(self, status, error=None):
|
||||||
self._connected = status
|
self._connected = status
|
||||||
|
|
@ -422,11 +383,9 @@ class MCGClient(MCGBase, mpd.MPDClient):
|
||||||
def _idle(self):
|
def _idle(self):
|
||||||
"""Reacts to idle events from MPD.
|
"""Reacts to idle events from MPD.
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
modules = self._call('idle')
|
modules = self._call('idle')
|
||||||
if not modules:
|
if not modules:
|
||||||
return
|
return
|
||||||
|
|
||||||
if 'player' in modules:
|
if 'player' in modules:
|
||||||
self.get_status()
|
self.get_status()
|
||||||
if 'mixer' in modules:
|
if 'mixer' in modules:
|
||||||
|
|
@ -441,12 +400,6 @@ class MCGClient(MCGBase, mpd.MPDClient):
|
||||||
self.load_albums()
|
self.load_albums()
|
||||||
self.load_playlist()
|
self.load_playlist()
|
||||||
self.get_status()
|
self.get_status()
|
||||||
except BrokenPipeError:
|
|
||||||
pass
|
|
||||||
except ConnectionResetError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
except mpd.ConnectionError as e:
|
|
||||||
self._set_connection_status(False, e)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue