moved error handling to _call()-method

This commit is contained in:
coderkun 2013-03-04 17:55:17 +01:00
commit 687d9cfbd8

65
mcg.py
View file

@ -206,7 +206,16 @@ class MCGClient(MCGBase, mpd.MPDClient):
def _call(self, command, *args):
try:
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):
@ -233,25 +242,14 @@ class MCGClient(MCGBase, mpd.MPDClient):
self._disconnect()
raise e
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:
self._set_connection_status(False, e)
def _disconnect(self):
try:
self._call('noidle')
self._call('disconnect')
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
@ -259,7 +257,6 @@ class MCGClient(MCGBase, mpd.MPDClient):
def _get_status(self):
"""Action: Performs the real status determination
"""
try:
# current status
self._call('noidle')
status = self._call('status')
@ -283,26 +280,14 @@ class MCGClient(MCGBase, mpd.MPDClient):
if '/' in pos:
pos = pos[0: pos.index('/')]
pos = int(pos) - 1
self._state = state
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
def _set_volume(self, volume):
try:
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
@ -310,45 +295,32 @@ class MCGClient(MCGBase, mpd.MPDClient):
def _playpause(self):
"""Action: Performs the real play/pause command.
"""
try:
status = self._call('status')
state = status['state']
if state == 'play':
self._call('pause')
else:
self._call('play')
except mpd.ConnectionError as e:
self._set_connection_status(False, e)
def _play_album(self, album):
if album not in self._albums:
return
try:
track_ids = []
for track in self._albums[album].get_tracks():
track_id = self._call('addid', track.get_file())
track_ids.append(track_id)
if self._state != 'play':
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):
try:
self._call('stop')
except mpd.ConnectionError as e:
self._set_connection_status(False, e)
# Playlist commands
def _load_playlist(self):
try:
playlist = []
for song in self._call('playlistinfo'):
try:
@ -366,18 +338,13 @@ class MCGClient(MCGBase, mpd.MPDClient):
except KeyError:
pass
self._callback(MCGClient.SIGNAL_LOAD_PLAYLIST, playlist, None)
except mpd.ConnectionError as e:
self._set_connection_status(False, e)
def _clear_playlist(self):
"""Action: Performs the real clearing of the current
playlist.
"""
try:
self._call('clear')
except mpd.ConnectionError as e:
self._set_connection_status(False, e)
# Database commands
@ -385,7 +352,6 @@ class MCGClient(MCGBase, mpd.MPDClient):
def _load_albums(self):
"""Action: Performs the real update.
"""
try:
for song in self._call('listallinfo'):
try:
hash = MCGAlbum.hash(song['album'], song['date'])
@ -402,15 +368,10 @@ class MCGClient(MCGBase, mpd.MPDClient):
except KeyError:
pass
self._callback(MCGClient.SIGNAL_LOAD_ALBUMS, self._albums, None)
except mpd.ConnectionError as e:
self._set_connection_status(False, e)
def _update(self):
try:
self._call('update')
except mpd.ConnectionError as e:
self._set_connection_status(False, e)
def _set_connection_status(self, status, error=None):
self._connected = status
@ -422,11 +383,9 @@ class MCGClient(MCGBase, mpd.MPDClient):
def _idle(self):
"""Reacts to idle events from MPD.
"""
try:
modules = self._call('idle')
if not modules:
return
if 'player' in modules:
self.get_status()
if 'mixer' in modules:
@ -441,12 +400,6 @@ class MCGClient(MCGBase, mpd.MPDClient):
self.load_albums()
self.load_playlist()
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)