summaryrefslogtreecommitdiff
path: root/nephilim/mpclient.py
diff options
context:
space:
mode:
authorAnton Khirnov <wyskas@gmail.com>2009-05-18 19:46:34 +0200
committerAnton Khirnov <wyskas@gmail.com>2009-05-18 19:46:34 +0200
commit4be456af9a3dc999b65b66fb1db925ec169610be (patch)
tree45aab6d064a8694b8cbafbc64b86b9cb82e6efb8 /nephilim/mpclient.py
parent054c89830726858edd40f12cae920921c9b4938f (diff)
mpclient: add some debugging messages.
Diffstat (limited to 'nephilim/mpclient.py')
-rw-r--r--nephilim/mpclient.py62
1 files changed, 40 insertions, 22 deletions
diff --git a/nephilim/mpclient.py b/nephilim/mpclient.py
index 9e9a7bb..9b1a948 100644
--- a/nephilim/mpclient.py
+++ b/nephilim/mpclient.py
@@ -29,7 +29,6 @@ class MPClient(QtCore.QObject):
_cur_lib = None
_cur_playlist = None
_cur_song = None
- _logger = logging.getLogger('mpclient')
_status = {'volume' : 0, 'repeat' : 0, 'random' : 0,
'songid' : 0, 'playlist' : 0, 'playlistlength' : 0,
'time' : 0, 'length' : 0, 'xfade' : 0,
@@ -41,27 +40,30 @@ class MPClient(QtCore.QObject):
_retr_mutex = QtCore.QMutex()
+ logger = None
+
def __init__(self):
QtCore.QObject.__init__(self)
self._cur_lib = []
self._cur_playlist = []
self._commands = []
self._status = dict(MPClient._status)
+ self.logger = logging.getLogger('mpclient')
def connect_mpd(self, host, port, password = None):
"""Connect to MPD@host:port, optionally using password.
Returns True at success, False otherwise."""
- self._logger.info('Connecting to MPD...')
+ self.logger.info('Connecting to MPD...')
if self._client:
- self._logger.warning('Attempted to connect when already connected.')
+ self.logger.warning('Attempted to connect when already connected.')
return True
try:
self._client = mpd.MPDClient()
self._client.connect(host, port)
except socket.error, e:
- self._logger.error('Socket error: %s.'%e)
+ self.logger.error('Socket error: %s.'%e)
self.disconnect_mpd()
return False
@@ -71,7 +73,7 @@ class MPClient(QtCore.QObject):
self._commands = self._retrieve(self._client.commands)
if not self._check_command_ok('listallinfo'):
- self._logger.error('Don\'t have MPD read permission, diconnecting.')
+ self.logger.error('Don\'t have MPD read permission, diconnecting.')
return self.disconnect_mpd()
self._update_lib()
@@ -79,13 +81,13 @@ class MPClient(QtCore.QObject):
self._update_current_song()
self.emit(QtCore.SIGNAL('connected'))
+ self.logger.info('Successfully connected to MPD.')
self.timerEvent(None)
self._timer_id = self.startTimer(500)
- self._logger.info('Successfully connected to MPD.')
return True
def disconnect_mpd(self):
"""Disconnect from MPD."""
- self._logger.info('Disconnecting from MPD...')
+ self.logger.info('Disconnecting from MPD...')
if self._client:
try:
self._client.close()
@@ -94,7 +96,7 @@ class MPClient(QtCore.QObject):
pass
self._client = None
else:
- self._logger.warning('Attempted to disconnect when not connected.')
+ self.logger.warning('Attempted to disconnect when not connected.')
if self._timer_id:
self.killTimer(self._timer_id)
@@ -105,16 +107,18 @@ class MPClient(QtCore.QObject):
self._cur_playlist = []
self._commands = []
self.emit(QtCore.SIGNAL('disconnected'))
+ self.logger.info('Disconnected from MPD.')
def password(self, password):
"""Use the password to authenticate with MPD."""
+ self.logger.info('Authenticating with MPD.')
if not self._check_command_ok('password'):
return
try:
self._client.password(password)
- self._logger.info('Successfully authenticated')
+ self.logger.info('Successfully authenticated')
self._commands = self._retrieve(self._client.commands)
except mpd.CommandError:
- self._logger.error('Incorrect MPD password.')
+ self.logger.error('Incorrect MPD password.')
def is_connected(self):
"""Returns True if connected to MPD, False otherwise."""
return self._client != None
@@ -137,6 +141,7 @@ class MPClient(QtCore.QObject):
def update_db(self, paths = None):
"""Starts MPD database update."""
+ self.logger.info('Updating database %s'%(paths if paths else '.'))
if not self._check_command_ok('update'):
return
if not paths:
@@ -166,14 +171,11 @@ class MPClient(QtCore.QObject):
return int(self._status['volume'])
def set_volume(self, volume):
"""Set volume to volume."""
- if not self._client:
- return self._logger.error('Not connected.')
- try:
- volume = min(100, max(0, volume))
- self._client.setvol(volume)
- except mpd.CommandError, e:
- self._logger.error('Can\'t set volume: %s.' %('don\'t have control permissions' if 'permission' in str(e)
- else 'unknown error'))
+ self.logger.info('Setting volume to %d.'%volume)
+ if not self._check_command_ok('setvol'):
+ return
+ volume = min(100, max(0, volume))
+ self._client.setvol(volume)
def urlhandlers(self):
"""Returns an array of available url handlers."""
@@ -193,6 +195,7 @@ class MPClient(QtCore.QObject):
def repeat(self, val):
"""Set repeat playlist to val (True/False)."""
+ self.logger.info('Setting repeat to %d.'%val)
if not self._check_command_ok('repeat'):
return
if isinstance(val, bool):
@@ -200,6 +203,7 @@ class MPClient(QtCore.QObject):
self._client.repeat(val)
def random(self, val):
"""Set random playback to val (True, False)."""
+ self.logger.info('Setting random to %d.'%val)
if not self._check_command_ok('random'):
return
if isinstance(val, bool):
@@ -207,11 +211,13 @@ class MPClient(QtCore.QObject):
self._client.random(val)
def crossfade(self, time):
"""Set crossfading between songs."""
+ self.logger.info('Setting crossfade to %d'%time)
if not self._check_command_ok('crossfade'):
return
self._client.crossfade(time)
def single(self, val):
"""Set single playback to val (True, False)"""
+ self.logger.info('Setting single to %d.'%val)
if not self._check_command_ok('single'):
return
if isinstance(val, bool):
@@ -219,6 +225,7 @@ class MPClient(QtCore.QObject):
self._client.single(val)
def consume(self, val):
"""Set consume mode to val (True, False)"""
+ self.logger.info('Setting consume to %d.'%val)
if not self._check_command_ok('consume'):
return
if isinstance(val, bool):
@@ -227,6 +234,7 @@ class MPClient(QtCore.QObject):
def play(self, id = None):
"""Play song with ID id or next song if id is None."""
+ self.logger.info('Starting playback %s.'%('of id %d'%id if id else ''))
if not self._check_command_ok('play'):
return
if id:
@@ -235,31 +243,37 @@ class MPClient(QtCore.QObject):
self._client.playid()
def pause(self):
"""Pause playing."""
+ self.logger.info('Pausing playback.')
if not self._check_command_ok('pause'):
return
self._client.pause(1)
def resume(self):
"""Resume playing."""
+ self.logger.info('Resuming playback.')
if not self._check_command_ok('pause'):
return
self._client.pause(0)
def next(self):
"""Move on to the next song in the playlist."""
+ self.logger.info('Skipping to next song.')
if not self._check_command_ok('next'):
return
self._client.next()
def previous(self):
"""Move back to the previous song in the playlist."""
+ self.logger.info('Moving to previous song.')
if not self._check_command_ok('previous'):
return
self._client.previous()
def stop(self):
"""Stop playing."""
+ self.logger.info('Stopping playback.')
if not self._check_command_ok('stop'):
return
self._client.stop()
def seek(self, time):
"""Seek to time (in seconds)."""
+ self.logger.info('Seeking to %d.'%time)
if not self._check_command_ok('seekid'):
return
if self._status['songid'] > 0:
@@ -271,10 +285,12 @@ class MPClient(QtCore.QObject):
return
self._client.command_list_ok_begin()
for id in list:
+ self.logger.info('Deleting id %d from playlist.'%id)
self._client.deleteid(id)
self._client.command_list_end()
def clear(self):
"""Clear current playlist."""
+ self.logger.info('Clearing playlist.')
if not self._check_command_ok('clear'):
return
self._client.clear()
@@ -286,15 +302,17 @@ class MPClient(QtCore.QObject):
self._client.command_list_ok_begin()
try:
for path in paths:
+ self.logger.info('Adding %s to playlist'%path)
self._client.addid(path.encode('utf-8'))
ret = self._client.command_list_end()
except mpd.CommandError, e:
- self._logger.error('Error adding files: %s.'%e)
+ self.logger.error('Error adding files: %s.'%e)
self._update_playlist()
if self._status['state'] == 'stop' and ret:
self.play(ret[0])
def move(self, source, target):
"""Move the songs in playlist. Takes a list of source ids and one target position."""
+ self.logger.info('Moving %d to %d.'%(source, target))
if not self._check_command_ok('moveid'):
return
self._client.command_list_ok_begin()
@@ -310,7 +328,7 @@ class MPClient(QtCore.QObject):
try:
ret = method()
except socket.error:
- self._logger.error('Connection to MPD broken.')
+ self.logger.error('Connection to MPD broken.')
self._retr_mutex.unlock()
self.disconnect_mpd()
return None
@@ -367,9 +385,9 @@ class MPClient(QtCore.QObject):
return ret
def _check_command_ok(self, cmd):
if not self._client:
- return self._logger.error('Not connected.')
+ return self.logger.error('Not connected.')
if not cmd in self._commands:
- return self._logger.error('Command %s not accessible'%cmd)
+ return self.logger.error('Command %s not accessible'%cmd)
return True
def timerEvent(self, event):