]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Integrated Gmail labels patch from Brian Neal and added unit tests
authorMenno Smits <menno@freshfoo.com>
Mon, 12 Mar 2012 12:34:50 +0000 (12:34 +0000)
committerMenno Smits <menno@freshfoo.com>
Mon, 12 Mar 2012 12:34:50 +0000 (12:34 +0000)
imapclient/imapclient.py
imapclient/test/test_IMAPClient.py

index 2e9925256b096aa0f2f574883625be5e9f2a7a32..f17d50358a3f27a319c68eab5e11e1b653af3966 100644 (file)
@@ -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.
index 1aa5a1fc1a3e111d1652889f19280e9d3923e2af..a189ae5433f0cca57a98f36fcf01b1b83fa6a3eb 100644 (file)
@@ -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()