]> khamidou — GitHub mirrors - imapclient.git/commitdiff
port INTERNALDATE handling from old fetch parser - all tests PASS\!
authorMenno Smits <menno@freshfoo.com>
Sun, 24 Jan 2010 19:02:33 +0000 (19:02 +0000)
committerMenno Smits <menno@freshfoo.com>
Sun, 24 Jan 2010 19:02:33 +0000 (19:02 +0000)
imapclient/response_parser.py
imapclient/test/test_response_parser.py

index 0b267333dd7b6aab2b33ab790fb35b715a80723e..4a908737a5fcaa56733a87852c0f74198c107bd4 100644 (file)
@@ -4,8 +4,11 @@ XXX
 Inspired by: http://effbot.org/zone/simple-iterator-parser.htm
 """
 
+import imaplib
 import shlex
 from cStringIO import StringIO
+from datetime import datetime
+from fixed_offset import FixedOffset
 
 #XXX higher level response type response type processing
 #XXX plug-in this version
@@ -62,6 +65,8 @@ def parse_fetch_response(text):
 
             if word == 'UID':
                 msg_id = _int_or_error(value, 'invalid UID')
+            elif word == 'INTERNALDATE':
+                msg_data[word] = _convert_INTERNALDATE(value)
             else:
                 msg_data[word] = value
 
@@ -76,6 +81,31 @@ def _int_or_error(value, error_text):
     except (TypeError, ValueError):
         raise ParseError('%s: %s' % (error_text, repr(value)))
 
+
+def _convert_INTERNALDATE(date_string):
+    mo = imaplib.InternalDate.match('INTERNALDATE "%s"' % date_string)
+    if not mo:
+        raise ValueError("couldn't parse date %r" % date_string)
+
+    zoneh = int(mo.group('zoneh'))
+    zonem = (zoneh * 60) + int(mo.group('zonem'))
+    if mo.group('zonen') == '-':
+        zonem = -zonem
+    tz = FixedOffset(zonem)
+
+    year = int(mo.group('year'))
+    mon = imaplib.Mon2num[mo.group('mon')]
+    day = int(mo.group('day'))
+    hour = int(mo.group('hour'))
+    min = int(mo.group('min'))
+    sec = int(mo.group('sec'))
+
+    dt = datetime(year, mon, day, hour, min, sec, 0, tz)
+
+    # Normalise to host system's timezone
+    return dt.astimezone(FixedOffset.for_system()).replace(tzinfo=None)
+
+
 EOF = object()
 
 # imaplib has poor handling of 'literals' - it both fails to remove the
index fd0f67b63b83ed5f98fd5dbfe7bf74ba779b7312..ebf84fe87ea666f328f6a101838a0eefd59b0f51 100644 (file)
@@ -18,6 +18,7 @@
 Unit tests for the FetchTokeniser and FetchParser classes
 '''
 
+from datetime import datetime
 from textwrap import dedent
 import unittest
 from imapclient.response_parser import parse_response, parse_fetch_response, ParseError
@@ -26,8 +27,6 @@ from pprint import pformat
 
 #TODO: tokenising tests
 #TODO: test invalid dates and times
-#TODO: more response types
-#TODO: clean up this file
 
 
 CRLF = '\r\n'
@@ -171,10 +170,6 @@ class TestParseResponse(unittest.TestCase):
                          'got ParseError with wrong msg: %r' % str(err))
 
 
-system_offset = FixedOffset.for_system()
-def datetime_to_native(dt):
-    return dt.astimezone(system_offset).replace(tzinfo=None)
-
 
 class TestParseFetchResponse(unittest.TestCase):
 
@@ -248,47 +243,34 @@ class TestParseFetchResponse(unittest.TestCase):
 
 
     def test_INTERNALDATE(self):
-        self.fail()
-
-#         def check(date_str, expected_dt):
-#             output = self.p(['3 (INTERNALDATE "%s")' % date_str])
-#             assert output.keys() == [3]
-#             assert output[3].keys() == ['INTERNALDATE']
-#             actual_dt = output[3]['INTERNALDATE']
-
-#             # Returned date should be in local timezone
-#             self.assert_(actual_dt.tzinfo is None)
-
-#             expected_dt = datetime_to_native(expected_dt)
-#             self.assert_(actual_dt == expected_dt,
-#                          '%s != %s' % (actual_dt, expected_dt))
-
-#         dt = check(' 9-Feb-2007 17:08:08 -0430',
-#                    datetime.datetime(2007, 2, 9, 17, 8, 8, 0, FixedOffset(-4*60 - 30)))
+        def check(date_str, expected_dt):
+            output = parse_fetch_response(['3 (INTERNALDATE "%s")' % date_str])
+            self.assertEquals(output.keys(), [3])
+            self.assertEquals(set(output[3].keys()), set(['INTERNALDATE', 'SEQ']))
+            actual_dt = output[3]['INTERNALDATE']
+            self.assert_(actual_dt.tzinfo is None)   # Returned date should be in local timezone
+            expected_dt = datetime_to_native(expected_dt)
+            self.assert_(actual_dt == expected_dt, '%s != %s' % (actual_dt, expected_dt))
+
+        dt = check(' 9-Feb-2007 17:08:08 -0430',
+                   datetime(2007, 2, 9, 17, 8, 8, 0, FixedOffset(-4*60 - 30)))
  
-#         dt = check('12-Feb-2007 17:08:08 +0200',
-#                    datetime.datetime(2007, 2, 12, 17, 8, 8, 0, FixedOffset(2*60)))
+        dt = check('12-Feb-2007 17:08:08 +0200',
+                   datetime(2007, 2, 12, 17, 8, 8, 0, FixedOffset(2*60)))
  
-#         dt = check(' 9-Dec-2007 17:08:08 +0000',
-#                    datetime.datetime(2007, 12, 9, 17, 8, 8, 0, FixedOffset(0)))
-
+        dt = check(' 9-Dec-2007 17:08:08 +0000',
+                   datetime(2007, 12, 9, 17, 8, 8, 0, FixedOffset(0)))
 
 
-#     def testMultiTypesWithLiteral(self):
-#         self._parse_test(
-#             [
-#                 ('1 (INTERNALDATE " 9-Feb-2007 17:08:08 +0100" RFC822 {21}',
-#                       'Subject: test\r\n\r\nbody'),
-#                 ')'
-#                 ],
-#             {1: {
-#                     'INTERNALDATE': datetime_to_native(datetime.datetime(2007, 2, 9,
-#                                                                          17, 8, 8, 0,
-#                                                                          FixedOffset(60))),
-#                     'RFC822': 'Subject: test\r\n\r\nbody',
-#                     }
-#                 }
-#             )
+    def test_mixed_types(self):
+        self.assertEquals(parse_fetch_response([('1 (INTERNALDATE " 9-Feb-2007 17:08:08 +0100" RFC822 {21}',
+                                                 'Subject: test\r\n\r\nbody'),
+                                                ')']),
+                          {1: {'INTERNALDATE': datetime_to_native(datetime(2007, 2, 9,
+                                                                           17, 8, 8, 0,
+                                                                           FixedOffset(60))),
+                               'RFC822': 'Subject: test\r\n\r\nbody',
+                               'SEQ': 1}})
 
 
 
@@ -296,13 +278,17 @@ def format_error(input_, output, expected):
     return 'failed for:\n%s\ngot:\n%s\nexpected:\n%s' % (
                 pformat(input_),
                 pformat(output),
-                pformat(expected),
-            )
+                pformat(expected))
 
 
 def add_crlf(text):
     return CRLF.join(text.splitlines()) + CRLF
 
 
+system_offset = FixedOffset.for_system()
+def datetime_to_native(dt):
+    return dt.astimezone(system_offset).replace(tzinfo=None)
+
+
 if __name__ == '__main__':
     unittest.main()