From: Menno Smits Date: Wed, 27 Mar 2013 21:06:32 +0000 (+0000) Subject: Added THREAD support X-Git-Tag: 0.9.2^2~3 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=874b4e42395b831dbc6c3f924b6d62de2e354c4e;p=imapclient.git Added THREAD support --- diff --git a/NEWS b/NEWS index 7efd67b..b7850f1 100644 --- 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 b1ef28a..14b2fc6 100644 --- a/THANKS +++ b/THANKS @@ -37,6 +37,9 @@ Dave Eckhardt Zac Witte OAUTH2 support. + +Lukasz Mierzwa + THREAD support. Bug Reports ------------ diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 49947c2..12b6e14 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -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 `_ 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*. diff --git a/imapclient/livetest.py b/imapclient/livetest.py index 0f7e6b3..8528ae0 100644 --- a/imapclient/livetest.py +++ b/imapclient/livetest.py @@ -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') diff --git a/imapclient/test/test_imapclient.py b/imapclient/test/test_imapclient.py index 1ff7371..89bd0ce 100644 --- a/imapclient/test/test_imapclient.py +++ b/imapclient/test/test_imapclient.py @@ -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()