summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <wyskas@gmail.com>2009-08-23 09:33:47 +0200
committerAnton Khirnov <wyskas@gmail.com>2009-08-23 09:33:47 +0200
commit94b55a0cfc9adb3110ba68e47d4981bea96bd1ed (patch)
tree9d5dcce66212951522b561c3a94826e8cd7d9951
parentd8453209db0c7f3cee4a9fde3df04d3a64ddec52 (diff)
mpclient: don't cache library and playlist
they are rarely needed more than once per update.
-rw-r--r--nephilim/mpclient.py86
1 files changed, 38 insertions, 48 deletions
diff --git a/nephilim/mpclient.py b/nephilim/mpclient.py
index c058ecc..a1298b1 100644
--- a/nephilim/mpclient.py
+++ b/nephilim/mpclient.py
@@ -26,8 +26,6 @@ from song import Song
class MPClient(QtCore.QObject):
"""This class offers another layer above pympd, with usefull events."""
_client = None
- _cur_lib = None
- _cur_playlist = None
_cur_song = None
_status = {'volume' : 0, 'repeat' : 0, 'random' : 0,
'songid' : 0, 'playlist' : 0, 'playlistlength' : 0,
@@ -59,8 +57,6 @@ class MPClient(QtCore.QObject):
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')
@@ -87,12 +83,10 @@ class MPClient(QtCore.QObject):
else:
self._commands = self._retrieve(self._client.commands)
- if not self._check_command_ok('listallinfo'):
+ if not self.__check_command_ok('listallinfo'):
self.logger.error('Don\'t have MPD read permission, diconnecting.')
return self.disconnect_mpd()
- self._update_lib()
- self._update_playlist()
self._update_current_song()
self._db_update = self.stats()['db_update']
@@ -123,8 +117,6 @@ class MPClient(QtCore.QObject):
self._db_timer_id = None
self._status = dict(MPClient._status)
self._cur_song = None
- self._cur_lib = []
- self._cur_playlist = []
self._commands = []
self.emit(QtCore.SIGNAL('disconnected')) #should be removed
self.connect_changed.emit(False)
@@ -132,7 +124,7 @@ class MPClient(QtCore.QObject):
def password(self, password):
"""Use the password to authenticate with MPD."""
self.logger.info('Authenticating with MPD.')
- if not self._check_command_ok('password'):
+ if not self.__check_command_ok('password'):
return
try:
self._client.password(password)
@@ -148,22 +140,32 @@ class MPClient(QtCore.QObject):
"""Get current MPD status."""
return self._status
def playlist(self):
- """Returns the current playlist."""
- return self._cur_playlist
+ """Returns a list of songs in current playlist."""
+ self.logger.info('Listing current playlist.')
+ if not self.__check_command_ok('playlistinfo'):
+ return []
+ return self._array_to_song_array(self._retrieve(self._client.playlistinfo))
def library(self):
- """Returns current library."""
- return self._cur_lib
+ """Returns a list of all songs in library."""
+ self.logger.info('Listing library.')
+ if not self.__check_command_ok('listallinfo'):
+ return []
+ return self._array_to_song_array(self._retrieve(self._client.listallinfo))
def current_song(self):
"""Returns the current playing song."""
return self._cur_song
def is_playing(self):
"""Returns True if MPD is playing, False otherwise."""
return self._status['state'] == 'play'
+ def find(self, path):
+ if not self.__check_command_ok('find'):
+ return []
+ return self._client.find('file', path.encode('utf-8'))
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'):
+ if not self.__check_command_ok('update'):
return
if not paths:
return self._client.update()
@@ -180,7 +182,7 @@ class MPClient(QtCore.QObject):
return []
def set_output(self, output_id, state):
"""Set audio output output_id to state (0/1)."""
- if not self._check_command_ok('enableoutput'):
+ if not self.__check_command_ok('enableoutput'):
return
if state:
self._client.enableoutput(output_id)
@@ -193,7 +195,7 @@ class MPClient(QtCore.QObject):
def set_volume(self, volume):
"""Set volume to volume."""
self.logger.info('Setting volume to %d.'%volume)
- if not self._check_command_ok('setvol'):
+ if not self.__check_command_ok('setvol'):
return
volume = min(100, max(0, volume))
self._client.setvol(volume)
@@ -206,7 +208,7 @@ class MPClient(QtCore.QObject):
return self._client.urlhandlers()
def tagtypes(self):
"""Returns a list of supported tags."""
- if not self._check_command_ok('tagtypes'):
+ if not self.__check_command_ok('tagtypes'):
return []
return self._retrieve(self._client.tagtypes)
@@ -220,7 +222,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'):
+ if not self.__check_command_ok('repeat'):
return
if isinstance(val, bool):
val = 1 if val else 0
@@ -228,7 +230,7 @@ class MPClient(QtCore.QObject):
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'):
+ if not self.__check_command_ok('random'):
return
if isinstance(val, bool):
val = 1 if val else 0
@@ -236,13 +238,13 @@ class MPClient(QtCore.QObject):
def crossfade(self, time):
"""Set crossfading between songs."""
self.logger.info('Setting crossfade to %d'%time)
- if not self._check_command_ok('crossfade'):
+ 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'):
+ if not self.__check_command_ok('single'):
return
if isinstance(val, bool):
val = 1 if val else 0
@@ -250,7 +252,7 @@ class MPClient(QtCore.QObject):
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'):
+ if not self.__check_command_ok('consume'):
return
if isinstance(val, bool):
val = 1 if val else 0
@@ -259,7 +261,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 %s'%(id) if id else ''))
- if not self._check_command_ok('play'):
+ if not self.__check_command_ok('play'):
return
if id:
self._client.playid(id)
@@ -268,44 +270,44 @@ class MPClient(QtCore.QObject):
def pause(self):
"""Pause playing."""
self.logger.info('Pausing playback.')
- if not self._check_command_ok('pause'):
+ 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'):
+ 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'):
+ 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'):
+ 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'):
+ 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'):
+ if not self.__check_command_ok('seekid'):
return
if self._status['songid'] > 0:
self._client.seekid(self._status['songid'], time)
def delete(self, list):
"""Remove all song IDs in list from the playlist."""
- if not self._check_command_ok('deleteid'):
+ if not self.__check_command_ok('deleteid'):
return
self._client.command_list_ok_begin()
try:
@@ -318,12 +320,12 @@ class MPClient(QtCore.QObject):
def clear(self):
"""Clear current playlist."""
self.logger.info('Clearing playlist.')
- if not self._check_command_ok('clear'):
+ if not self.__check_command_ok('clear'):
return
self._client.clear()
def add(self, paths):
"""Add all files in paths to the current playlist."""
- if not self._check_command_ok('addid'):
+ if not self.__check_command_ok('addid'):
return
ret = None
self._client.command_list_ok_begin()
@@ -339,7 +341,7 @@ class MPClient(QtCore.QObject):
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'):
+ if not self.__check_command_ok('moveid'):
return
self._client.command_list_ok_begin()
i = 0
@@ -361,16 +363,6 @@ class MPClient(QtCore.QObject):
self._retr_mutex.unlock()
return ret
- def _update_lib(self):
- """Update the cached library."""
- self._cur_lib = self._array_to_song_array(self._retrieve(self._client.listallinfo))
- id = 0
- for song in self._cur_lib:
- song._data['id'] = id
- id += 1
- def _update_playlist(self):
- """Update the cached playlist."""
- self._cur_playlist = self._array_to_song_array(self._retrieve(self._client.playlistinfo))
def _array_to_song_array(self, array):
"""Convert an array to an array of Songs."""
return map(lambda entry: Song(entry)
@@ -407,9 +399,9 @@ class MPClient(QtCore.QObject):
ret['songid'] = '-1'
return ret
- def _check_command_ok(self, cmd):
+ def __check_command_ok(self, cmd):
if not self._client:
- return self.logger.error('Not connected.')
+ return self.logger.info('Not connected.')
if not cmd in self._commands:
return self.logger.error('Command %s not accessible'%cmd)
return True
@@ -422,7 +414,6 @@ class MPClient(QtCore.QObject):
if db_update > self._db_update:
self.logger.info('Database updated.')
self._db_update = db_update
- self._update_lib()
self.db_updated.emit()
return
@@ -460,5 +451,4 @@ class MPClient(QtCore.QObject):
self.consume_changed.emit(bool(self._status['consume']))
if self._status['playlist'] != old_status['playlist']:
- self._update_playlist()
self.playlist_changed.emit()