From: Menno Smits Date: Tue, 25 Aug 2015 05:02:41 +0000 (+1200) Subject: Merged the big search criteria handling change into trunk X-Git-Tag: 1.0.0~1^2~39 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=a8e458a9af55c66fa93c5515ab7c5412b02996ef;p=imapclient.git Merged the big search criteria handling change into trunk --- a8e458a9af55c66fa93c5515ab7c5412b02996ef diff --cc NEWS.rst index 816bf4b,82f18da..c383132 --- a/NEWS.rst +++ b/NEWS.rst @@@ -41,17 -19,67 +41,70 @@@ into these TLS improvements STARTTLS support ---------------- -XXX +When the server supports it, IMAPClient can now establish an encrypted +connection after initially connection unencrypted via STARTTLS. The +starttls method takes an SSL context object for controlling the +parameters of the TLS negotiation. -XXX Thank people who contributed (Chris?) +Many thanks to Chris Arndt for his extensive initial work on this. - Fixed charset handling for search, sort and thread - -------------------------------------------------- - The handling of the *charset* argument for the search, sort and thread - methods has been fixed. + More robust criteria handling for search, sort and thread [API] + --------------------------------------------------------------- + The way search criteria are processed by the various methods that + accept them has been changed to allow for easier and more robust + handling. In addition, the way the *charset* argument interacts with + search criteria has been improved. + + Unfortunately these changes will cause compatibility breaks that may + required small changes in code that uses IMAPClient. + + The documentation and tests for search, gmail_search, sort and thread + has updated to account for these changes and have also been generally + improved. + + The details in the following sections apply to all methods that take + search criteria. + + Search criteria + ~~~~~~~~~~~~~~~ + The preferred way to specify criteria is as a list of strings. Valid + examples are:: + + c.search(['DELETED']) + c.search(['NOT', 'DELETED']) + c.search(['FLAGGED', 'SUBJECT', 'foo', 'BODY', 'hello world']) + c.search(['NOT', 'DELETED', 'SMALLER', 1000]) + + IMAPClient will perform all required quoting and encoding. Callers + should not attempt to do this themselves. + + Following are some examples of how to update code written against + older versions of IMAPClient:: + + c.search(['NOT DELETED']) # Before + c.search(['NOT', 'DELETED']) # After + + c.search(['TEXT "foo"']) # Before + c.search(['TEXT', 'foo']) # After + + c.search(['DELETED', 'TEXT "foo"']) # Before + c.search(['DELETED', 'TEXT', 'foo']) # After + + It is possible to pass a single string as the search + criteria. IMAPClient will not attempt quoting in this case, allowing + lower level specification of criteria. Specifying criteria using a + sequence of strings is preferable however. The following examples are + valid:: + + c.search('ALL') + c.search('NOT DELETED') + c.search('TEXT "foo"') + c.search('DELETED TEXT "foo" SMALLER 500') + + Search charset + ~~~~~~~~~~~~~~ + The way that the search *charset* argument is handled has as also + changed. Any unicode criteria given will now be encoded using the supplied charset. The charset must refer to an encoding that is capable of diff --cc imapclient/imapclient.py index b3a21c4,2dfe2f5..7ac6a67 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@@ -13,8 -13,7 +13,8 @@@ import r from datetime import datetime from operator import itemgetter - from six import moves, iteritems, text_type, integer_types, PY3, binary_type ++from six import moves, iteritems, text_type, integer_types, PY3, binary_type, iterbytes + # Confusingly, this module is for OAUTH v1, not v2 try: import oauth2 as oauth_module diff --cc imapclient/test/test_search.py index fd3293f,99d088b..d6b81f5 --- a/imapclient/test/test_search.py +++ b/imapclient/test/test_search.py @@@ -81,62 -70,22 +70,22 @@@ class TestSearch(TestSearchBase) self.assertEqual(result, [1, 2]) self.assertEqual(result.modseq, 51101) - def test_error_from_server(self): - self.client._imap.uid.return_value = ('NO', [b'bad karma']) - - self.assertRaisesRegex(IMAPClient.Error, - 'bad karma', - self.client.search, b'FOO') - - class TestGmailSearch(IMAPClientTest): + class TestGmailSearch(TestSearchBase): - def test_with_uid(self): - self.client.use_uid = True - self.client._imap.uid.return_value = ('OK', [b'1 2 44']) + def test_bytes_query(self): + result = self.client.gmail_search(b'foo bar') - result = self.client.gmail_search('FOO') - - self.client._imap.uid.assert_called_once_with(search_command, b'X-GM-RAW') - self.assertEqual(self.client._imap.literal, b'FOO') - self.assertEqual(result, [1, 2, 44]) - - def test_without_uid(self): - self.client.use_uid = False - self.client._imap.search.return_value = ('OK', [b'1 2 44']) - - result = self.client.gmail_search('FOO') - - self.client._imap.search.assert_called_once_with(None, b'X-GM-RAW') - self.assertEqual(self.client._imap.literal, b'FOO') - self.check_call([b'X-GM-RAW', b'"foo bar"']) ++ self.check_call([b'CHARSET', b'UTF-8', b'X-GM-RAW', b'"foo bar"']) self.assertEqual(result, [1, 2, 44]) - def test_with_uid_with_charset(self): - self.client.use_uid = True - self.client._imap.uid.return_value = ('OK', [b'1 2 44']) - - result = self.client.gmail_search('\u2620', 'UTF-8') - - self.client._imap.uid.assert_called_once_with( - search_command, - b'CHARSET', b'UTF-8', - b'X-GM-RAW') - self.assertEqual(self.client._imap.literal, b'\xe2\x98\xa0') - self.assertEqual(result, [1, 2, 44]) - - def test_without_uid_with_charset(self): - self.client.use_uid = False - self.client._imap.search.return_value = ('OK', [b'1 2 44']) - - result = self.client.gmail_search('\u2620', 'UTF-8') + def test_bytes_query_with_charset(self): + result = self.client.gmail_search(b'foo bar', 'utf-42') - self.client._imap.search.assert_called_once_with(b'UTF-8', b'X-GM-RAW') - self.assertEqual(self.client._imap.literal, b'\xe2\x98\xa0') + self.check_call([b'CHARSET', b'utf-42', b'X-GM-RAW', b'"foo bar"']) self.assertEqual(result, [1, 2, 44]) - def test_error_from_server(self): - self.client._imap.uid.return_value = ('NO', [b'bad karma']) + def test_unicode_criteria_with_charset(self): + self.client.gmail_search('foo \u2639', 'utf-8') - self.assertRaisesRegex(IMAPClient.Error, - 'bad karma', - self.client.gmail_search, b'FOO') + self.check_call([b'CHARSET', b'utf-8', b'X-GM-RAW', b'"foo \xe2\x98\xb9"'])