From 81f5a0bc570bde2977878f15eb895810f2e9914a Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Sun, 24 Jan 2010 19:02:33 +0000 Subject: [PATCH] port INTERNALDATE handling from old fetch parser - all tests PASS\! --- imapclient/response_parser.py | 30 ++++++++++ imapclient/test/test_response_parser.py | 76 ++++++++++--------------- 2 files changed, 61 insertions(+), 45 deletions(-) diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index 0b26733..4a90873 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -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 diff --git a/imapclient/test/test_response_parser.py b/imapclient/test/test_response_parser.py index fd0f67b..ebf84fe 100644 --- a/imapclient/test/test_response_parser.py +++ b/imapclient/test/test_response_parser.py @@ -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() -- 2.43.0