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.
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()