From fb168fe66b42193b47d754c0e0e4a38fb73c1370 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Fri, 27 Aug 2010 19:35:13 +0200 Subject: mpclient: group functions by category --- nephilim/mpclient.py | 98 ++++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 46 deletions(-) diff --git a/nephilim/mpclient.py b/nephilim/mpclient.py index 555aada..fd3524a 100644 --- a/nephilim/mpclient.py +++ b/nephilim/mpclient.py @@ -131,6 +131,8 @@ class MPClient(QtCore.QObject): _password = None #### PUBLIC #### + + ## internals ## def __init__(self, parent = None): QtCore.QObject.__init__(self, parent) self._logger = logging.getLogger('%smpclient'%(unicode(parent) + "." if parent else "")) @@ -148,6 +150,7 @@ class MPClient(QtCore.QObject): def __str__(self): return self._logger.name + ## connection functions ## def connect_mpd(self, host = "localhost", port = 6600, password = None): """ Connect to MPD at host:port optionally using a password. A Unix domain @@ -161,7 +164,6 @@ class MPClient(QtCore.QObject): self._socket.connect_changed.connect(lambda val: self._handle_connected() if val else self._handle_disconnected()) self._socket.connect_mpd(host, port) self._password = password - def disconnect_mpd(self): """ Disconnect from MPD. @@ -169,19 +171,64 @@ class MPClient(QtCore.QObject): self._logger.info('Disconnecting from MPD.') if self.is_connected(): self._socket.write_command('close') - def is_connected(self): """ Returns True if connected to MPD, False otherwise. """ return self._socket.state() == QtNetwork.QAbstractSocket.ConnectedState + ## playlists ## def playlist(self, callback): """ Request current playlist from MPD. Callback will be called with an iterator over Songs in current playlist as the argument. """ self._command('playlistinfo', callback = lambda data: callback(self._parse_songs(data))) + def get_plist_song(self, plid): + """ + Get a song with a given playlist id synchronously. + """ + return self._command_sync('playlistid', plid, parse = lambda data: Song(list(self._parse_objects(data, []))[0])) + def delete(self, ids): + """ + Delete songs with specified ids from playlist. + """ + for id in ids: + self._command('deleteid', id) + def clear(self): + """ + Clear current playlist. + """ + self._command('clear') + def add(self, paths, pos = -1): + """ + Add specified songs to specified position in current playlist. + """ + # start playback of the first added song if MPD is stopped + if self.status['state'] == 'stop': + cb = lambda data: [self.play(sid) for sid in self._parse_list(data) ] + else: + cb = None + + args = ['addid', ''] + if pos >= 0: + args.append(pos) + for path in paths: + args[1] = path + if cb: + self._command(*args, callback = cb) + cb = None + else: + self._command(*args) + if pos >= 0: + args[2] += 1 + def move(self, src, dst): + """ + Move a song with given src id to position dst. + """ + self._command('moveid', src, dst) + + ## database ## def database(self, callback): """ Request database information from MPD. Callback will be called with an @@ -189,6 +236,7 @@ class MPClient(QtCore.QObject): """ self._command('listallinfo', callback = lambda data: callback(self._parse_songs(data))) + ## searching ## def find(self, callback, *args): """ Request a search on MPD. Callback will be called with an iterator over @@ -208,11 +256,7 @@ class MPClient(QtCore.QObject): """ self._command('findadd', *args) - def get_plist_song(self, plid): - """ - Get a song with a given playlist id synchronously. - """ - return self._command_sync('playlistid', plid, parse = lambda data: Song(list(self._parse_objects(data, []))[0])) + ## playback options ## def set_volume(self, volume): """ Set MPD volume level. @@ -249,6 +293,7 @@ class MPClient(QtCore.QObject): """ self._command('crossfade', time) + ## controlling playback ## def play(self, id = None): """ Start playback of song with a specified id. If no id is given, then @@ -289,45 +334,6 @@ class MPClient(QtCore.QObject): """ self._command('seekid', self.status['songid'], time) - def delete(self, ids): - """ - Delete songs with specified ids from playlist. - """ - for id in ids: - self._command('deleteid', id) - def clear(self): - """ - Clear current playlist. - """ - self._command('clear') - def add(self, paths, pos = -1): - """ - Add specified songs to specified position in current playlist. - """ - # start playback of the first added song if MPD is stopped - if self.status['state'] == 'stop': - cb = lambda data: [self.play(sid) for sid in self._parse_list(data) ] - else: - cb = None - - args = ['addid', ''] - if pos >= 0: - args.append(pos) - for path in paths: - args[1] = path - if cb: - self._command(*args, callback = cb) - cb = None - else: - self._command(*args) - if pos >= 0: - args[2] += 1 - def move(self, src, dst): - """ - Move a song with given src id to position dst. - """ - self._command('moveid', src, dst) - #### PRIVATE #### ## connection functions ## -- cgit v1.2.3