]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Support date and datetime instances in search criteria
authorMenno Smits <menno@freshfoo.com>
Mon, 14 Sep 2015 22:37:55 +0000 (10:37 +1200)
committerMenno Smits <menno@freshfoo.com>
Mon, 14 Sep 2015 22:37:55 +0000 (10:37 +1200)
These are converted to the correctly formatted date strings.
Closes #170.

doc/src/releases.rst
imapclient/datetime_util.py
imapclient/imapclient.py
imapclient/test/test_datetime_util.py
imapclient/test/test_search.py

index 507992bc9de3f8f60db2a6aab3b6b2f569247bd2..7c220383ad53568c50989dac0394e458f2b57f34 100644 (file)
@@ -69,13 +69,14 @@ search criteria.
 
 Search criteria
 ~~~~~~~~~~~~~~~
-The preferred way to specify criteria is as a list of strings. Valid
-examples are::
+The preferred way to specify criteria is as a list of strings, ints
+and dates (where relevant). Valid examples are::
 
   c.search(['DELETED'])
   c.search(['NOT', 'DELETED'])
   c.search(['FLAGGED', 'SUBJECT', 'foo', 'BODY', 'hello world'])
   c.search(['NOT', 'DELETED', 'SMALLER', 1000])
+  c.search(['SINCE', date(2006, 5, 3)])
 
 IMAPClient will perform all required quoting and encoding. Callers
 should not attempt to do this themselves.
@@ -102,6 +103,7 @@ valid::
   c.search('NOT DELETED')
   c.search('TEXT "foo"')
   c.search('DELETED TEXT "foo" SMALLER 500')
+  c.search('SINCE 03-May-2006')
 
 Search charset
 ~~~~~~~~~~~~~~
index a874fb52ded10748bd9badba1d96fb86d08ff8ff..374d0589c3bff6518fcb3a24467527410487ab04 100644 (file)
@@ -10,6 +10,8 @@ from email.utils import parsedate_tz
 
 from .fixed_offset import FixedOffset
 
+_SHORT_MONTHS = ' Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ')
+
 
 def parse_to_datetime(timestamp, normalise=True):
     """Convert an IMAP datetime string to a datetime.
@@ -61,3 +63,10 @@ def _munge(s):
     if _rfc822_dotted_time.match(s):
         return s.replace(".", ":")
     return s
+
+
+def format_criteria_date(dt):
+    """Format a date or datetime instance for use in IMAP search criteria.
+    """
+    out = '%02d-%s-%d' % (dt.day, _SHORT_MONTHS[dt.month], dt.year)
+    return out.encode('ascii')
index 56e2abd891b62ca6e1a91ed3876163257158c616..7c841af075550bad4a50261af361909f6b9030b1 100644 (file)
@@ -10,7 +10,7 @@ import select
 import socket
 import sys
 import re
-from datetime import datetime
+from datetime import datetime, date
 from operator import itemgetter
 
 from six import moves, iteritems, text_type, integer_types, PY3, binary_type, iterbytes
@@ -24,7 +24,7 @@ except ImportError:
 from . import imap4
 from . import response_lexer
 from . import tls
-from .datetime_util import datetime_to_INTERNALDATE
+from .datetime_util import datetime_to_INTERNALDATE, format_criteria_date
 from .imap_utf7 import encode as encode_utf7, decode as decode_utf7
 from .response_parser import parse_response, parse_message_list, parse_fetch_response
 xrange = moves.xrange
@@ -675,6 +675,7 @@ class IMAPClient(object):
             [u'SMALLER', 500]
             [b'NOT', b'DELETED']
             [u'TEXT', u'foo bar', u'FLAGGED', u'SUBJECT', u'baz']
+            [u'SINCE', date(2005, 4, 3)]
 
         IMAPClient will perform conversion and quoting as
         required. The caller shouldn't do this.
@@ -688,6 +689,7 @@ class IMAPClient(object):
             u'SMALLER 500'
             b'NOT DELETED'
             u'TEXT "foo bar" FLAGGED SUBJECT "baz"'
+            b'SINCE 03-Apr-2005'
 
         *charset* specifies the character set of the criteria. It
         defaults to US-ASCII as this is the only charset that a server
@@ -712,7 +714,6 @@ class IMAPClient(object):
         attribute. This is set if the server included a MODSEQ value
         to the search response (i.e. if a MODSEQ criteria was included
         in the search).
-
         """
         return self._search(criteria, charset)
 
@@ -1283,6 +1284,8 @@ def _normalise_search_criteria(criteria, charset=None):
 def _handle_one_search_criteria(item, charset):
     if isinstance(item, int):
         return str(item).encode('ascii')
+    elif isinstance(item, (datetime, date)):
+        return format_criteria_date(item)
     return _maybe_quote(to_bytes(item, charset))
 
 
index 576225669c80e1695c6f75238e567f16bc63a271..6fa647cfd34998f73cf3707eeed201fadfe4a048 100644 (file)
@@ -4,11 +4,16 @@
 
 from __future__ import unicode_literals
 
-from datetime import datetime
+from datetime import datetime, date
 
 from mock import patch
 
-from ..datetime_util import parse_to_datetime, datetime_to_native, datetime_to_INTERNALDATE
+from ..datetime_util import (
+    datetime_to_INTERNALDATE,
+    datetime_to_native,
+    format_criteria_date,
+    parse_to_datetime,
+)
 from ..fixed_offset import FixedOffset
 from .util import unittest
 
@@ -72,3 +77,12 @@ class TestDatetimeToINTERNALDATE(unittest.TestCase):
         for_system.return_value = FixedOffset(-5 * 60)
 
         self.assertEqual(datetime_to_INTERNALDATE(dt), '02-Jan-2009 03:04:05 -0500')
+
+
+class TestCriteriaDateFormatting(unittest.TestCase):
+
+    def test_basic(self):
+        self.assertEqual(format_criteria_date(date(1996, 2, 22)), b'22-Feb-1996')
+
+    def test_single_digit_day(self):
+        self.assertEqual(format_criteria_date(date(1996, 4, 4)), b'04-Apr-1996')
index d6b81f5c5056110745a2a321486ec0b2e2149d36..5f626057b1b411febbe661e3c04674feecb1c907 100644 (file)
@@ -4,6 +4,8 @@
 
 from __future__ import unicode_literals
 
+from datetime import date, datetime
+
 from mock import Mock
 
 from .imapclient_test import IMAPClientTest
@@ -49,9 +51,16 @@ class TestSearch(TestSearchBase):
         # Default conversion using us-ascii.
         self.check_call([b'CHARSET', b'utf-8', b'FOO', b'\xe2\x98\xb9'])
 
+    def test_with_date(self):
+        self.client.search(['SINCE', date(2005, 4, 3)])
+        self.check_call([b'SINCE', b'03-Apr-2005'])
+
+    def test_with_datetime(self):
+        self.client.search(['SINCE', datetime(2005, 4, 3, 2, 1, 0)])
+        self.check_call([b'SINCE', b'03-Apr-2005'])  # Time part is ignored
+
     def test_quoting(self):
         self.client.search(['TEXT', 'foo bar'])
-
         self.check_call([b'TEXT', b'"foo bar"'])
 
     def test_no_results(self):