From: Menno Smits Date: Thu, 17 Dec 2009 14:02:26 +0000 (+0000) Subject: core parts of fetch parsing done, lots more tests parsing X-Git-Tag: 0.7~59^2~12 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=add84404836a02973affb7c9efde19cd87eb63af;p=imapclient.git core parts of fetch parsing done, lots more tests parsing --- diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index 63b18e4..73f3206 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -32,23 +32,52 @@ def parse_response(text): def parse_fetch_response(text): + #XXX doc response = iter(parse_response(text)) def expect(expected_value): - next_value = response.next() + next_value = response.next().upper() if next_value != expected_value: raise ParseError('expected %r, got %r' % (expected_value, next_value)) - msg_data = {} + parsed_response = {} while True: try: expect('*') except StopIteration: break - msgid = int(response.next()) + + msg_id = _int_or_error(response.next(), 'invalid message ID') expect('FETCH') - msg_data[msgid] = response.next() - return msg_data + + msg_response = response.next() + if not isinstance(msg_response, tuple): + raise ParseError('bad response type: %s' % repr(msg_response)) + if len(msg_response) % 2: + raise ParseError('uneven number of response items: %s' % repr(msg_response)) + + #XXX extract this + msg_data = {} + for i in xrange(0, len(msg_response), 2): + word = msg_response[i].upper() + value = msg_response[i+1] + + if word == 'UID': + msg_id = _int_or_error(value, 'invalid UID') + else: + msg_data[word] = value + + parsed_response[msg_id] = msg_data + + return parsed_response + + +def _int_or_error(value, error_text): + try: + return int(value) + except (TypeError, ValueError): + raise ParseError('%s: %s' % (error_text, repr(value))) + EOF = object() @@ -56,7 +85,7 @@ EOF = object() class ResponseTokeniser(object): CTRL_CHARS = ''.join([chr(ch) for ch in range(32)]) - ATOM_SPECIALS = r'()%*\"]' + CTRL_CHARS + ATOM_SPECIALS = r'()%*"]' + CTRL_CHARS ALL_CHARS = [chr(ch) for ch in range(256)] ATOM_NON_SPECIALS = [ch for ch in ALL_CHARS if ch not in ATOM_SPECIALS] diff --git a/imapclient/test/__init__.py b/imapclient/test/__init__.py index ee8b89b..8f6e7ff 100644 --- a/imapclient/test/__init__.py +++ b/imapclient/test/__init__.py @@ -25,7 +25,7 @@ def load_suite(): def run_suite(): suite = load_suite() - runner = unittest.TextTestRunner(verbosity=2) + runner = unittest.TextTestRunner(verbosity=1) runner.run(suite) diff --git a/imapclient/test/test_response_parser.py b/imapclient/test/test_response_parser.py index 41522f6..0660c23 100644 --- a/imapclient/test/test_response_parser.py +++ b/imapclient/test/test_response_parser.py @@ -37,6 +37,7 @@ class TestParseResponse(unittest.TestCase): def test_unquoted(self): self._test('FOO', 'FOO') self._test('F.O:-O_0;', 'F.O:-O_0;') + self._test(r'\Seen', r'\Seen') def test_string(self): self._test('"TEST"', 'TEST') @@ -128,16 +129,12 @@ system_offset = FixedOffset.for_system() def datetime_to_native(dt): return dt.astimezone(system_offset).replace(tzinfo=None) + class TestParseFetchResponse(unittest.TestCase): def test_basic(self): self.assertEquals(parse_fetch_response('* 4 FETCH ()'), {4: {}}) - - - def test_simple_pair(self): - self.assertEquals(parse_fetch_response('* 23 FETCH (ABC 123 STUFF "hello")'), - {'23': {'ABC': 123, - 'STUFF': 'hello'}}) + self.assertEquals(parse_fetch_response('* 4 fEtCh ()'), {4: {}}) def test_non_fetch(self): @@ -148,14 +145,41 @@ class TestParseFetchResponse(unittest.TestCase): self.assertRaises(ParseError, parse_fetch_response, '* abc FETCH ()') + def test_bad_data(self): + self.assertRaises(ParseError, parse_fetch_response, '* 2 FETCH WHAT') + + + def test_simple_pairs(self): + self.assertEquals(parse_fetch_response('* 23 FETCH (ABC 123 StUfF "hello")'), + {23: {'ABC': 123, + 'STUFF': 'hello'}}) + + + def test_odd_pairs(self): + self.assertRaises(ParseError, parse_fetch_response, '* 2 FETCH (ONE)') + self.assertRaises(ParseError, parse_fetch_response, '* 2 FETCH (ONE TWO THREE)') + + def test_UID(self): self.assertEquals(parse_fetch_response('* 23 FETCH (UID 76)'), - {'76': {}}) + {76: {}}) + self.assertEquals(parse_fetch_response('* 23 FETCH (uiD 76)'), + {76: {}}) + def test_bad_UID(self): + self.assertRaises(ParseError, parse_fetch_response, '* 2 FETCH (UID X)') + + def test_FLAGS(self): + self.assertEquals(parse_fetch_response('* 23 FETCH (FLAGS (\Seen Stuff))'), + {23: {'FLAGS': (r'\Seen', 'Stuff')}}) + + + def test_multiple_messages(self): self.fail() + def test_INTERNALDATE(self): self.fail() @@ -181,34 +205,6 @@ class TestParseFetchResponse(unittest.TestCase): # dt = check(' 9-Dec-2007 17:08:08 +0000', # datetime.datetime(2007, 12, 9, 17, 8, 8, 0, FixedOffset(0))) - def test_multiple_fields(self): - self.fail() - - def test_multiple_messages(self): - self.fail() - - def test_case_handling(self): - self.fail() -# self._parse_test( -# [r'2 (flaGS (\Deleted Foo \Seen))'], -# {2: {'FLAGS': [r'\Deleted', 'Foo', r'\Seen']}} -# ) - - - def test_invalid(self): - self.fail() -# self.assertRaises(ValueError, self.p, [r'2 (FLAGS (\Deleted) MORE)']) -# self.assertRaises(ValueError, -# self.t.process_pairs, 'FOO 123 BAH "abc" WHAT?') -# self.assertRaises(ValueError, -# self.t.process_pairs, 'HMMM FOO 123 BAH "abc"') -# self.assertRaises(ValueError, -# self.t.process_pairs, 'FOO 123 BAD BAH "abc"') - -#TODO convert the following are mainly FETCH specific tests - -# def test_INTERNALDATE(self): - # def testMultipleMessages(self): # '''Test with multple messages in the response