]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Added THREAD support
authorMenno Smits <menno@freshfoo.com>
Wed, 27 Mar 2013 21:06:32 +0000 (21:06 +0000)
committerMenno Smits <menno@freshfoo.com>
Wed, 27 Mar 2013 21:06:32 +0000 (21:06 +0000)
NEWS
THANKS
imapclient/imapclient.py
imapclient/livetest.py
imapclient/test/test_imapclient.py

diff --git a/NEWS b/NEWS
index 7efd67bfb4b7a0612a7ac66d9d73b51329bec91f..b7850f177ff1ff06256c66277c4734bb31fc7e0c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,11 @@
  0.9.2
 =======
 
+THREAD support [NEW]
+--------------------
+The IMAP THREAD command is now supported. Thanks to Lukasz Mierzwa for
+the patches.
+
 Enhanced capability querying [NEW]
 ----------------------------------
 Previously only the pre-authentication server capabilities were
diff --git a/THANKS b/THANKS
index b1ef28a4ff2539732f2878b85d495537e70e8b09..14b2fc66ba783a67bd3903bf853e179894da83dd 100644 (file)
--- a/THANKS
+++ b/THANKS
@@ -37,6 +37,9 @@ Dave Eckhardt
 
 Zac Witte
     OAUTH2 support.
+
+Lukasz Mierzwa     
+    THREAD support. 
     
 Bug Reports
 ------------
index 49947c25ee558be8a8bda23a01ab03d150383791..12b6e141450742434bd3186dd7b29dd72e4d836d 100644 (file)
@@ -548,6 +548,32 @@ class IMAPClient(object):
             return []
         return [long(i) for i in data.split()]
 
+    def thread(self, algorithm='REFERENCES', criteria='ALL', charset='UTF-8'):
+        """Return a list of messages threads matching *criteria*.
+
+        Each thread is a list of messages ids.
+
+        See `RFC 5256 <https://tools.ietf.org/html/rfc5256>`_ for more details.
+        """
+        if not self.has_capability('THREAD=' + algorithm):
+            raise ValueError('server does not support %s threading algorithm'
+                             % algorithm)
+
+        if not criteria:
+            raise ValueError('no criteria specified')
+
+        if isinstance(criteria, basestring):
+            criteria = (criteria,)
+        crit_list = ['(%s)' % c for c in criteria]
+
+        args = [algorithm]
+        if charset:
+            args.append(charset)
+        args.extend(crit_list)
+
+        data = self._command_and_check('thread', *args, uid=True)
+        return parse_response(data)
+
     def sort(self, sort_criteria, criteria='ALL', charset='UTF-8'):
         """Return a list of message ids sorted by *sort_criteria* and
         optionally filtered by *criteria*.
index 0f7e6b360ed230ef33e4fb0d006d7e0278213b01..8528ae051e027a6b8a2f80fbd06d6ed574b7e246 100644 (file)
@@ -427,6 +427,29 @@ def createLiveTestClass(conf, use_uid):
             expected = [first_id, first_id - 1, first_id - 2]
             self.assertListEqual(messages, expected)
 
+        def test_thread(self):
+            thread_algo = None
+            for cap in self.client.capabilities():
+                if cap.startswith('THREAD='):
+                    thread_algo = cap.split('=')[-1]
+                    break
+            if not thread_algo:
+                return self.skipTest("Server doesn't support THREAD")
+
+            msg_tmpl = 'Subject: %s\r\n\r\nBody'
+            subjects = ('a', 'b', 'c')
+            for subject in subjects:
+                msg = msg_tmpl % subject
+                self.client.append(self.base_folder, msg)
+
+            messages = self.client.thread()
+
+            self.assertEqual(len(messages), 3)
+            self.assertIsInstance(messages[0], tuple)
+            first_id = messages[0][0]
+            expected = ((first_id,), (first_id + 1,), (first_id + 2,))
+            self.assertTupleEqual(messages, expected)
+
         def test_copy(self):
             self.append_msg(SIMPLE_MESSAGE)
             target_folder = self.add_prefix_to_folder('target')
index 1ff737136a68d5dc10a9a8fc700426a3d42c07f3..89bd0cec2fe99ac346b26f5e05251a9f8341cdd8 100644 (file)
@@ -423,5 +423,50 @@ class TestCapabilities(IMAPClientTest):
         self.assertEquals(self.client._cached_capabilities, ('FOO', 'BAR'))
 
 
+class TestThread(IMAPClientTest):
+
+    def test_thread_without_uid(self):
+        self.client._cached_capabilities = ('THREAD=REFERENCES',)
+        self.client.use_uid = False
+        self.client._imap.thread.return_value = ('OK', ['(1 2)(3)(4 5 6)'])
+
+        threads = self.client.thread()
+
+        self.assertSequenceEqual(threads, ((1, 2), (3,), (4, 5, 6)))
+
+    def test_thread_with_uid(self):
+        self.client._cached_capabilities = ('THREAD=REFERENCES',)
+        self.client.use_uid = True
+        self.client._imap.uid.return_value = ('OK', ['(1 2)(3)(4 5 6)'])
+
+        threads = self.client.thread()
+
+        self.assertSequenceEqual(threads, ((1, 2), (3,), (4, 5, 6)))
+
+    def test_no_support(self):
+        self.client._cached_capabilities = ('NOT-THREAD',)
+        self.assertRaises(ValueError, self.client.thread)
+
+    def test_no_support2(self):
+        self.client._cached_capabilities = ('THREAD=FOO',)
+        self.assertRaises(ValueError, self.client.thread)
+
+    def test_all_args_with_uid(self):
+        self.client._cached_capabilities = ('THREAD=FOO',)
+        self.client._imap.uid.return_value = ('OK', [])
+
+        self.client.thread(algorithm='FOO', criteria='STUFF', charset='ASCII')
+
+        self.client._imap.uid.assert_called_once_with('thread', 'FOO', 'ASCII', '(STUFF)')
+
+    def test_all_args_without_uid(self):
+        self.client.use_uid = False
+        self.client._cached_capabilities = ('THREAD=FOO',)
+        self.client._imap.thread.return_value = ('OK', [])
+
+        self.client.thread(algorithm='FOO', criteria='STUFF', charset='ASCII')
+
+        self.client._imap.thread.assert_called_once_with('FOO', 'ASCII','(STUFF)')
+
 if __name__ == '__main__':
     unittest.main()