error handling corrected and improved

This commit is contained in:
coderkun 2013-03-01 13:52:59 +01:00
commit 3593fcbb9b
2 changed files with 41 additions and 24 deletions

59
mcg.py
View file

@ -65,6 +65,8 @@ class MCGClient(MCGBase, mpd.MPDClient):
SIGNAL_LOAD_ALBUMS = 'load-albums' SIGNAL_LOAD_ALBUMS = 'load-albums'
# Signal: load playlist # Signal: load playlist
SIGNAL_LOAD_PLAYLIST = 'load-playlist' SIGNAL_LOAD_PLAYLIST = 'load-playlist'
# Signal: error
SIGNAL_ERROR = 'error'
def __init__(self): def __init__(self):
@ -210,20 +212,22 @@ class MCGClient(MCGBase, mpd.MPDClient):
self._call('connect', host, port) self._call('connect', host, port)
if password: if password:
self._call('password', password) self._call('password', password)
self._set_connction_status(True, None) self._set_connection_status(True, None)
except mpd.CommandError as e:
self._callback(MCGClient.SIGNAL_ERROR, e)
except mpd.ConnectionError as e: except mpd.ConnectionError as e:
self._set_connction_status(False, e) self._set_connection_status(False, e)
except OSError as e: except OSError as e:
self._set_connction_status(False, e) self._set_connection_status(False, e)
def _disconnect(self): def _disconnect(self):
try: try:
self._call('noidle') self._call('noidle')
self._call('disconnect') self._call('disconnect')
self._set_connction_status(False, None) self._set_connection_status(False, None)
except mpd.ConnectionError as e: except mpd.ConnectionError as e:
self._set_connction_status(False, e) self._set_connection_status(False, e)
# Status commands # Status commands
@ -232,9 +236,14 @@ class MCGClient(MCGBase, mpd.MPDClient):
"""Action: Performs the real status determination """Action: Performs the real status determination
""" """
try: try:
# current status
self._call('noidle') self._call('noidle')
status = self._call('status') status = self._call('status')
state = status['state'] state = status['state']
error = None
if 'error' in status:
error = status['error']
# current song
self._call('noidle') self._call('noidle')
song = self._call('currentsong') song = self._call('currentsong')
album = None album = None
@ -246,9 +255,9 @@ class MCGClient(MCGBase, mpd.MPDClient):
pos = int(song['pos']) pos = int(song['pos'])
self._state = state self._state = state
self._callback(MCGClient.SIGNAL_STATUS, state, album, pos, None) self._callback(MCGClient.SIGNAL_STATUS, state, album, pos, error)
except mpd.ConnectionError as e: except mpd.ConnectionError as e:
self._set_connction_status(False, e) self._set_connection_status(False, e)
# Playback option commants # Playback option commants
@ -259,13 +268,16 @@ 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.
""" """
status = self._call('status') try:
state = status['state'] status = self._call('status')
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):
@ -278,18 +290,17 @@ class MCGClient(MCGBase, mpd.MPDClient):
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])
# TODO CommandError except mpd.CommandError as e:
# except mpd.CommandError as e: self._callback(MCGClient.SIGNAL_ERROR, e)
# _callback(SIGNAL_ERROR)
except mpd.ConnectionError as e: except mpd.ConnectionError as e:
self._set_connction_status(False, e) self._set_connection_status(False, e)
def _stop(self): def _stop(self):
try: try:
self._call('stop') self._call('stop')
except mpd.ConnectionError as e: except mpd.ConnectionError as e:
self._set_connction_status(False, e) self._set_connection_status(False, e)
# Playlist commands # Playlist commands
@ -314,7 +325,7 @@ class MCGClient(MCGBase, mpd.MPDClient):
pass pass
self._callback(MCGClient.SIGNAL_LOAD_PLAYLIST, playlist, None) self._callback(MCGClient.SIGNAL_LOAD_PLAYLIST, playlist, None)
except mpd.ConnectionError as e: except mpd.ConnectionError as e:
self._set_connction_status(False, e) self._set_connection_status(False, e)
# Database commands # Database commands
@ -340,16 +351,16 @@ class MCGClient(MCGBase, mpd.MPDClient):
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: except mpd.ConnectionError as e:
self._set_connction_status(False, e) self._set_connection_status(False, e)
def _update(self): def _update(self):
try: try:
self._call('update') self._call('update')
except mpd.ConnectionError as e: except mpd.ConnectionError as e:
self._set_connction_status(False, e) self._set_connection_status(False, e)
def _set_connction_status(self, status, error): def _set_connection_status(self, status, error):
self._connected = status self._connected = status
self._callback(MCGClient.SIGNAL_CONNECT, status, error) self._callback(MCGClient.SIGNAL_CONNECT, status, error)
if not status: if not status:
@ -380,9 +391,9 @@ class MCGClient(MCGBase, mpd.MPDClient):
self.load_playlist() self.load_playlist()
self.get_status() self.get_status()
except ConnectionResetError as e: except ConnectionResetError as e:
self._set_connction_status(False, e) self._set_connection_status(False, e)
except mpd.ConnectionError as e: except mpd.ConnectionError as e:
self._set_connction_status(False, e) self._set_connection_status(False, e)

View file

@ -62,6 +62,7 @@ class MCGGtk(Gtk.Window):
self._mcg.connect_signal(mcg.MCGClient.SIGNAL_STATUS, self.on_mcg_status) self._mcg.connect_signal(mcg.MCGClient.SIGNAL_STATUS, self.on_mcg_status)
self._mcg.connect_signal(mcg.MCGClient.SIGNAL_LOAD_PLAYLIST, self.on_mcg_load_playlist) self._mcg.connect_signal(mcg.MCGClient.SIGNAL_LOAD_PLAYLIST, self.on_mcg_load_playlist)
self._mcg.connect_signal(mcg.MCGClient.SIGNAL_LOAD_ALBUMS, self.on_mcg_load_albums) self._mcg.connect_signal(mcg.MCGClient.SIGNAL_LOAD_ALBUMS, self.on_mcg_load_albums)
self._mcg.connect_signal(mcg.MCGClient.SIGNAL_ERROR, self.on_mcg_error)
def on_resize(self, widget, event): def on_resize(self, widget, event):
@ -179,6 +180,11 @@ class MCGGtk(Gtk.Window):
self._cover_panel.set_albums(self._connection_panel.get_host(), albums) self._cover_panel.set_albums(self._connection_panel.get_host(), albums)
def on_mcg_error(self, error):
# TODO on_mcg_error()
pass
# Private methods # Private methods
def _connect(self): def _connect(self):