From 1be3cf6c6118eb39fe65f4e7848f2037f135ff33 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 27 Nov 2010 21:38:16 +0100 Subject: mpclient: add functions for manipulating stickers --- nephilim/mpclient.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/nephilim/mpclient.py b/nephilim/mpclient.py index 6a98a96..104e62c 100644 --- a/nephilim/mpclient.py +++ b/nephilim/mpclient.py @@ -362,6 +362,26 @@ class MPClient(QtCore.QObject): """ self._command('seekid', self.status['songid'], time) + ## stickers ## + def sticker_list(self, song, callback): + """ + Get a list of stickers for specified song. callback is called with an + iterator over (key, value) tuples as argument. + """ + self._command('sticker', 'list', 'song', song, callback = lambda data: callback(self._parse_stickers(data))) + def sticker_set(self, song, key, value): + """ + Set a sticker associated with song. + """ + self._command('sticker', 'set', 'song', song, key, value) + def sticker_get(self, song, key, callback): + """ + Get a sticker with the given key associated with song. + Callback is called with the sticker's value as argument. + """ + self._command('sticker', 'get', 'song', song, key, + callback = lambda data: callback(self._parse_sticker(data)), report_errors = False) + #### PRIVATE #### ## connection functions ## @@ -622,3 +642,25 @@ class MPClient(QtCore.QObject): for song in self._parse_objects(data, ['file', 'directory']): if 'file' in song: yield Song(song) + + def _parse_stickers(self, data): + """ + Parse a list of stickers -- output of sticker list. + Yields (key, value) tuples. + """ + for sticker in self._parse_list(data): + parts = sticker.partition('=') + if not parts[1]: + self._logger.error('Malformed sticker: %s.'%sticker) + continue + yield (parts[0], parts[2]) + def _parse_sticker(self, data): + """ + Parse a single sticker -- output of sticker get. + Returns the sticker's value. + """ + sticker = list(self._parse_list(data)) + try: + return sticker[0].partition('=')[2] + except IndexError: + return "" -- cgit v1.2.3