From: Menno Smits Date: Mon, 12 Mar 2012 12:34:50 +0000 (+0000) Subject: Integrated Gmail labels patch from Brian Neal and added unit tests X-Git-Tag: 0.9~32 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=12d6231ba8ca9dbb2de0497af6cf428ea6d243a9;p=imapclient.git Integrated Gmail labels patch from Brian Neal and added unit tests --- diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 2e99252..f17d503 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -615,6 +615,61 @@ class IMAPClient(object): return self._store('FLAGS', messages, flags) + def get_gmail_labels(self, messages): + """Return the label set for each message in *messages*. + + The return value is a dictionary structured like this: ``{ + msgid1: [label1, label2, ... ], }``. + + This only works with IMAP servers that support the X-GM-LABELS + attribute (eg. Gmail). + """ + response = self.fetch(messages, ['X-GM-LABELS']) + return self._flatten_dict(response) + + + def add_gmail_labels(self, messages, labels): + """Add *labels* to *messages*. + + *labels* should be a sequence of strings. + + Returns the label set for each modified message (see + *get_gmail_labels*). + + This only works with IMAP servers that support the X-GM-LABELS + attribute (eg. Gmail). + """ + return self._store('+X-GM-LABELS', messages, labels) + + + def remove_gmail_labels(self, messages, labels): + """Remove one or more *labels* from *messages*. + + *labels* should be a sequence of strings. + + Returns the label set for each modified message (see + *get_gmail_labels*). + + This only works with IMAP servers that support the X-GM-LABELS + attribute (eg. Gmail). + """ + return self._store('-X-GM-LABELS', messages, labels) + + + def set_gmail_labels(self, messages, labels): + """Set the *labels* for *messages*. + + *labels* should be a sequence of strings. + + Returns the label set for each modified message (see + *get_gmail_labels*). + + This only works with IMAP servers that support the X-GM-LABELS + attribute (eg. Gmail). + """ + return self._store('X-GM-LABELS', messages, labels) + + def delete_messages(self, messages): """Delete one or more *messages* from the currently selected folder. diff --git a/imapclient/test/test_IMAPClient.py b/imapclient/test/test_IMAPClient.py index 1aa5a1f..a189ae5 100644 --- a/imapclient/test/test_IMAPClient.py +++ b/imapclient/test/test_IMAPClient.py @@ -324,5 +324,35 @@ class TestTimeNormalisation(IMAPClientTest): self.client.normalise_times = False check(False) + +class TestGmailLabels(IMAPClientTest): + + def setUp(self): + super(TestGmailLabels, self).setUp() + self.client._store = Mock(return_value=sentinel.label_set) + + def test_get(self): + self.client.fetch = Mock(return_value={123: {'X-GM-LABELS': ['foo', 'bar']}, + 444: {'X-GM-LABELS': ['foo']}}) + + out = self.client.get_gmail_labels(sentinel.messages) + + self.client.fetch.assert_called_with(sentinel.messages, ['X-GM-LABELS']) + self.assertEquals(out, {123: ['foo', 'bar'], + 444: ['foo']}) + + def test_add(self): + self.client.add_gmail_labels(sentinel.messages, sentinel.labels) + self.client._store.assert_called_with('+X-GM-LABELS', sentinel.messages, sentinel.labels) + + def test_remove(self): + self.client.remove_gmail_labels(sentinel.messages, sentinel.labels) + self.client._store.assert_called_with('-X-GM-LABELS', sentinel.messages, sentinel.labels) + + def test_set(self): + self.client.set_gmail_labels(sentinel.messages, sentinel.labels) + self.client._store.assert_called_with('X-GM-LABELS', sentinel.messages, sentinel.labels) + + if __name__ == '__main__': unittest.main()