summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <wyskas@gmail.com>2009-09-08 15:31:38 +0200
committerAnton Khirnov <wyskas@gmail.com>2009-09-08 15:31:38 +0200
commitdeeda78d5ccfb6a33e214f639cebd34dd790accd (patch)
treeb290893394d700574d1f0ec3c169d180a896fedb
parentc632ef7e3a89add73854aef0ae72d7b0d2d4b16c (diff)
mpclient/mpd: always use iterators instead of list.
also get rid of some useless functions like retrieve.
-rw-r--r--nephilim/mpclient.py63
-rw-r--r--nephilim/mpd.py14
2 files changed, 28 insertions, 49 deletions
diff --git a/nephilim/mpclient.py b/nephilim/mpclient.py
index 33b50b1..a7b09de 100644
--- a/nephilim/mpclient.py
+++ b/nephilim/mpclient.py
@@ -43,8 +43,6 @@ class MPClient(QtCore.QObject):
_db_timer_id = None #for querying db updates
_db_update = None #time of last db update
- _retr_mutex = QtCore.QMutex()
-
logger = None
# SIGNALS
@@ -118,7 +116,7 @@ class MPClient(QtCore.QObject):
if password:
self.password(password)
else:
- self._commands = self._retrieve(self._client.commands)
+ self._commands = list(self._client.commands())
if not self.__check_command_ok('listallinfo'):
self.logger.error('Don\'t have MPD read permission, diconnecting.')
@@ -128,7 +126,6 @@ class MPClient(QtCore.QObject):
self.__update_outputs()
self._db_update = self.stats()['db_update']
- self.emit(QtCore.SIGNAL('connected')) #should be removed
self.connect_changed.emit(True)
self.logger.info('Successfully connected to MPD.')
self._timer_id = self.startTimer(500)
@@ -157,7 +154,6 @@ class MPClient(QtCore.QObject):
self._cur_song = None
self._commands = []
self.outputs = []
- self.emit(QtCore.SIGNAL('disconnected')) #should be removed
self.connect_changed.emit(False)
self.logger.info('Disconnected from MPD.')
def password(self, password):
@@ -168,7 +164,7 @@ class MPClient(QtCore.QObject):
try:
self._client.password(password)
self.logger.info('Successfully authenticated')
- self._commands = self._retrieve(self._client.commands)
+ self._commands = list(self._client.commands())
except mpd.CommandError:
self.logger.error('Incorrect MPD password.')
def is_connected(self):
@@ -182,14 +178,20 @@ class MPClient(QtCore.QObject):
"""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))
+ raise StopIteration
+ for song in self._client.playlistinfo():
+ yield Song(song)
+ raise StopIteration
def library(self):
"""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))
+ raise StopIteration
+ for song in self._client.listallinfo():
+ if 'file' in song:
+ yield Song(song)
+
+ raise StopIteration
def current_song(self):
"""Returns the current playing song."""
return self._cur_song
@@ -198,8 +200,10 @@ class MPClient(QtCore.QObject):
return self._status['state'] == 'play'
def find(self, *args):
if not self.__check_command_ok('find'):
- return []
- return self._array_to_song_array(self._client.find(*args))
+ raise StopIteration
+ for song in self._client.find(*args):
+ yield Song(song)
+ raise StopIteration
def findadd(self, *args):
if not self.__check_command_ok('findadd'):
return
@@ -243,13 +247,13 @@ class MPClient(QtCore.QObject):
if not self.__check_command_ok('tagtypes'):
return []
- return map(str.lower, self._retrieve(self._client.tagtypes) + ['file'])
+ return map(str.lower, list(self._client.tagtypes()) + ['file'])
def commands(self):
"""List all currently available MPD commands."""
return self._commands
def stats(self):
"""Get MPD statistics."""
- return self._retrieve(self._client.stats)
+ return self._client.stats()
def repeat(self, val):
"""Set repeat playlist to val (True/False)."""
@@ -365,7 +369,7 @@ class MPClient(QtCore.QObject):
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()
+ ret = list(self._client.command_list_end())
except mpd.CommandError, e:
self.logger.error('Error adding files: %s.'%e)
if self._status['state'] == 'stop' and ret:
@@ -383,27 +387,9 @@ class MPClient(QtCore.QObject):
self._client.command_list_end()
#### private ####
- def _retrieve(self, method):
- """Makes sure only one call is made at a time to MPD."""
- self._retr_mutex.lock()
- try:
- ret = method()
- except socket.error:
- self.logger.error('Connection to MPD broken.')
- self._retr_mutex.unlock()
- self.disconnect_mpd()
- return None
-
- self._retr_mutex.unlock()
- return ret
- def _array_to_song_array(self, array):
- """Convert an array to an array of Songs."""
- return map(lambda entry: Song(entry)
- , filter(lambda entry: not('directory' in entry), array)
- )
def __update_current_song(self):
"""Update the current song."""
- song = self._retrieve(self._client.currentsong)
+ song = self._client.currentsong()
if not song:
self._cur_song = None
else:
@@ -412,7 +398,7 @@ class MPClient(QtCore.QObject):
"""Get current status"""
if not self._client:
return None
- ret = self._retrieve(self._client.status)
+ ret = self._client.status()
if not ret:
return None
@@ -443,7 +429,7 @@ class MPClient(QtCore.QObject):
"""Update the list of MPD audio outputs."""
if self.__check_command_ok('outputs'):
outputs = []
- for output in self._retrieve(self._client.outputs):
+ for output in self._client.outputs():
outputs.append(MPClient.Output(self, output['outputname'], output['outputid'],
bool(output['outputenabled'])))
self.outputs = outputs
@@ -476,9 +462,8 @@ class MPClient(QtCore.QObject):
if not self._status:
return self.disconnect_mpd()
- self.__update_current_song()
-
if self._status['songid'] != old_status['songid']:
+ self.__update_current_song()
self.song_changed.emit(PlaylistEntryRef(self, self._status['songid']))
if self._status['time'] != old_status['time']:
@@ -505,7 +490,7 @@ class MPClient(QtCore.QObject):
if self._status['playlist'] != old_status['playlist']:
self.playlist_changed.emit()
- outputs = self._retrieve(self._client.outputs)
+ outputs = list(self._client.outputs())
for i in range(len(outputs)):
if int(outputs[i]['outputenabled']) != int(self.outputs[i].state):
self.outputs[i].mpd_toggle_state()
diff --git a/nephilim/mpd.py b/nephilim/mpd.py
index 129c82e..cae2bcb 100644
--- a/nephilim/mpd.py
+++ b/nephilim/mpd.py
@@ -48,7 +48,6 @@ class _NotConnected(object):
class MPDClient(object):
def __init__(self):
- self.iterate = False
self._reset()
self._commands = {
# Admin Commands
@@ -228,11 +227,6 @@ class MPDClient(object):
self._getnone()
raise StopIteration
- def _wrapiterator(self, iterator):
- if not self.iterate:
- return list(iterator)
- return iterator
-
def _getnone(self):
line = self._readline()
if line is not None:
@@ -245,10 +239,10 @@ class MPDClient(object):
return items[0][1]
def _getlist(self):
- return self._wrapiterator(self._readlist())
+ return self._readlist()
def _getplaylist(self):
- return self._wrapiterator(self._readplaylist())
+ return self._readplaylist()
def _getobject(self):
objs = list(self._readobjects())
@@ -257,7 +251,7 @@ class MPDClient(object):
return objs[0]
def _getobjects(self, delimiters):
- return self._wrapiterator(self._readobjects(delimiters))
+ return self._readobjects(delimiters)
def _getsongs(self):
return self._getobjects(["file"])
@@ -273,7 +267,7 @@ class MPDClient(object):
def _getcommandlist(self):
try:
- return self._wrapiterator(self._readcommandlist())
+ return self._readcommandlist()
except CommandError:
self._commandlist = None
raise