moved error handling to _call()-method

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

255
mcg.py
View file

@ -206,7 +206,16 @@ class MCGClient(MCGBase, mpd.MPDClient):
def _call(self, command, *args): def _call(self, command, *args):
return getattr(super(), 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): 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,50 +257,37 @@ 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') state = status['state']
state = status['state'] volume = int(status['volume'])
volume = int(status['volume']) error = None
error = None if 'error' in status:
if 'error' in status: error = status['error']
error = status['error'] # current song
# current song self._call('noidle')
self._call('noidle') song = self._call('currentsong')
song = self._call('currentsong') album = None
album = None pos = None
pos = None if song:
if song: hash = MCGAlbum.hash(song['album'], song['date'])
hash = MCGAlbum.hash(song['album'], song['date']) if hash in self._albums:
if hash in self._albums: album = self._albums[hash]
album = self._albums[hash] pos = song['track']
pos = song['track'] if type(pos) is list:
if type(pos) is list: pos = pos[0]
pos = pos[0] 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._callback(MCGClient.SIGNAL_STATUS, state, album, pos, volume, error)
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 # 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,74 +295,56 @@ 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':
self._call('pause')
if state == 'play': else:
self._call('pause') self._call('play')
else:
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: hash = MCGAlbum.hash(song['album'], song['date'])
hash = MCGAlbum.hash(song['album'], song['date']) if len(playlist) == 0 or playlist[len(playlist)-1].get_hash() != hash:
if len(playlist) == 0 or playlist[len(playlist)-1].get_hash() != hash: date = ""
date = "" if 'date' in song:
if 'date' in song: date = song['date']
date = song['date'] album = MCGAlbum(song['album'], date, self._host, self._image_dir)
album = MCGAlbum(song['album'], date, self._host, self._image_dir) playlist.append(album)
playlist.append(album) else:
else: album = playlist[len(playlist)-1]
album = playlist[len(playlist)-1] track = MCGTrack(song['artist'], song['title'], song['track'], song['time'], song['file'])
track = MCGTrack(song['artist'], song['title'], song['track'], song['time'], song['file']) album.add_track(track)
album.add_track(track) 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,32 +352,26 @@ 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']) if hash in self._albums.keys():
if hash in self._albums.keys(): album = self._albums[hash]
album = self._albums[hash] else:
else: date = ""
date = "" if 'date' in song:
if 'date' in song: date = song['date']
date = song['date'] album = MCGAlbum(song['album'], date, self._host, self._image_dir)
album = MCGAlbum(song['album'], date, self._host, self._image_dir) self._albums[album.get_hash()] = album
self._albums[album.get_hash()] = album track = MCGTrack(song['artist'], song['title'], song['track'], song['time'], song['file'])
track = MCGTrack(song['artist'], song['title'], song['track'], song['time'], song['file']) album.add_track(track)
album.add_track(track) 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,31 +383,23 @@ 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:
self.get_status()
if 'player' in modules: if 'mixer' in modules:
self.get_status() self.get_status()
if 'mixer' in modules: if 'playlist' in modules:
self.get_status() self.load_playlist()
if 'playlist' in modules: if 'database' in modules:
self.load_playlist() self.load_albums()
if 'database' in modules: self.load_playlist()
self.load_albums() self.get_status()
self.load_playlist() if 'update' in modules:
self.get_status() self.load_albums()
if 'update' in modules: self.load_playlist()
self.load_albums() self.get_status()
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)