From 478d22c4d9b8b9e0c7b4d7e651966b2ce0369594 Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Mon, 11 Jan 2010 19:11:54 +0000 Subject: [PATCH] Parse imaplib's fetch data structures instead of doing it all ourselves This makes the code messier but means imaplib does a bit more of the heavy lifting - this is probably safer from a bug standpoint. From Mark Hammond. --- imapclient/imapclient.py | 27 +++--- imapclient/response_parser.py | 92 +++++++++++++----- imapclient/test/test_response_parser.py | 119 +++++++++++++++--------- 3 files changed, 154 insertions(+), 84 deletions(-) diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 791eb52..d43dbfb 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -14,6 +14,8 @@ from fixed_offset import FixedOffset __all__ = ['IMAPClient', 'DELETED', 'SEEN', 'ANSWERED', 'FLAGGED', 'DRAFT', 'RECENT'] +from response_parser import parse_fetch_response + # System flags DELETED = r'\Deleted' SEEN = r'\Seen' @@ -326,6 +328,8 @@ class IMAPClient(object): typ, data = self._imap.search(charset, *crit_list) self._checkok('search', typ, data) + if data == [None]: # no untagged responses... + return [] return [ long(i) for i in data[0].split() ] @@ -435,7 +439,6 @@ class IMAPClient(object): parser = FetchParser() return parser(data) - def altfetch(self, messages, parts): if not messages: return {} @@ -447,21 +450,15 @@ class IMAPClient(object): tag = self._imap._command('UID', 'FETCH', msg_list, parts_list) else: tag = self._imap._command('FETCH', msg_list, parts_list) + typ, data = self._imap._command_complete('FETCH', tag) + self._checkok('fetch', typ, data) + typ, data = self._imap._untagged_response(typ, data, 'FETCH') + # appears to be a special case - no 'untagged' responses (ie, no + # folders) results in [None] + if data == [None]: + return {} - print tag - lines = [] - while True: - line = self._imap._get_line() - if line.startswith(tag): - break - lines.append(line) - return lines - - #self._checkok('fetch', typ, data) - - #parser = FetchParser() - #return parser(data) - + return parse_fetch_response(data) def append(self, folder, msg, flags=(), msg_time=None): '''Append a message to a folder diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index d335dcb..0b26733 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -21,7 +21,6 @@ class ParseError(ValueError): pass - def parse_response(text): #XXX doc src = ResponseTokeniser(text) @@ -37,29 +36,26 @@ def parse_fetch_response(text): #XXX doc response = iter(parse_response(text)) - def expect(expected_value): - next_value = response.next().upper() - if next_value != expected_value: - raise ParseError('expected %r, got %r' % (expected_value, next_value)) - parsed_response = {} while True: try: - expect('*') + msg_id = _int_or_error(response.next(), 'invalid message ID') except StopIteration: break - msg_id = _int_or_error(response.next(), 'invalid message ID') - expect('FETCH') + try: + msg_response = response.next() + except StopIteration: + raise ParseError('unexpected EOF') - 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 = {} + # always return the 'sequence' of the message, so it is available + # even if we return keyed by UID. + msg_data = {'SEQ': msg_id} for i in xrange(0, len(msg_response), 2): word = msg_response[i].upper() value = msg_response[i+1] @@ -80,19 +76,67 @@ def _int_or_error(value, error_text): except (TypeError, ValueError): raise ParseError('%s: %s' % (error_text, repr(value))) +EOF = object() +# imaplib has poor handling of 'literals' - it both fails to remove the +# {size} marker, and fails to keep responses grouped into the same logical +# 'line'. What we end up with is a list of response 'records', where each +# record is either a simple string, or tuple of (str_with_lit, literal) - +# where str_with_lit is a string with the {xxx} marker at its end. Note +# that each elt of this list does *not* correspond 1:1 with the untagged +# responses. +# (http://bugs.python.org/issue5045 also has comments about this) +# So: we have a special file-like object for each of these records. When +# a string literal is finally processed, we peek into this file-like object +# to grab the literal. +class LiteralHandlingReader: + def __init__(self, lexer, resp_record): + self.pushed = None + self.lexer = lexer + if isinstance(resp_record, tuple): + # A 'record' with a string which includes a literal marker, and + # the literal itself. + src_text, self.literal = resp_record + assert src_text.endswith("}"), src_text + # add a token-sep after the text. + self.src = StringIO(src_text + " ") + else: + # just a line with no literals. + self.src = StringIO(resp_record) + self.literal = None + + def read(self, n): + # We also hack into the lexer so we get special treatment for '\\' + # chars - they are only special inside a quoted string. + assert n==1 + if self.pushed is not None: + ret = self.pushed + self.pushed = None + else: + ret = self.src.read(n) + if ret=="\\" and self.lexer.state not in '"\\': + self.pushed = "\\" + return ret + + def close(self): + self.src.close() + self.src = None + self.literal = None -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] - def __init__(self, text): - self.lex = shlex.shlex(text) + def __init__(self, resp_chunks): + # initialize the lexer with all the chunks we read. + self.lex = shlex.shlex('', posix=True) + for chunk in reversed(resp_chunks): + self.lex.push_source(LiteralHandlingReader(self.lex, chunk)) + self.lex.quotes = '"' self.lex.commenters = '' self.lex.wordchars = self.ATOM_NON_SPECIALS @@ -106,9 +150,6 @@ class ResponseTokeniser(object): except StopIteration: return EOF - def read(self, bytes): - return self.lex.instream.read(bytes) - def atom(src, token): if token == "(": @@ -125,15 +166,16 @@ def atom(src, token): return None elif token.startswith('{'): literal_len = int(token[1:-1]) - if src.read(1) != '\n': - raise ParseError('No CRLF after %s' % token) - return src.read(literal_len) + literal_text = src.lex.instream.literal + if literal_text is None: + raise ParseError('No literal corresponds to %r' % token) + if len(literal_text) != literal_len: + raise ParseError('Expecting literal of size %d, got %d' % ( + literal_len, len(literal_text))) + return literal_text elif token.startswith('"'): return token[1:-1] elif token.isdigit(): return int(token) else: return token - - - diff --git a/imapclient/test/test_response_parser.py b/imapclient/test/test_response_parser.py index 19766e3..0bf9979 100644 --- a/imapclient/test/test_response_parser.py +++ b/imapclient/test/test_response_parser.py @@ -73,13 +73,54 @@ class TestParseResponse(unittest.TestCase): ('TEXT', 'PLAIN', ('CHARSET', 'US-ASCII', 'NAME', 'cc.diff'), '', 'foo', 'BASE64', 4554, 73), 'MIXED')) + def test_envelopey(self): + self._test('(UID 5 ENVELOPE ("internal_date" "subject" ' + '(("name" NIL "address1" "domain1.com")) ' + '((NIL NIL "address2" "domain2.com")) ' + '(("name" NIL "address3" "domain3.com")) ' + '((NIL NIL "address4" "domain4.com")) ' + 'NIL NIL "" ""))', + ('UID', + 5, + 'ENVELOPE', + ('internal_date', + 'subject', + (('name', None, 'address1', 'domain1.com'),), + ((None, None, 'address2', 'domain2.com'),), + (('name', None, 'address3', 'domain3.com'),), + ((None, None, 'address4', 'domain4.com'),), + None, + None, + '', + ''))) + + def test_envelopey_quoted(self): + self._test('(UID 5 ENVELOPE ("internal_date" "subject with \\"quotes\\"" ' + '(("name" NIL "address1" "domain1.com")) ' + '((NIL NIL "address2" "domain2.com")) ' + '(("name" NIL "address3" "domain3.com")) ' + '((NIL NIL "address4" "domain4.com")) ' + 'NIL NIL "" ""))', + ('UID', + 5, + 'ENVELOPE', + ('internal_date', + 'subject with "quotes"', + (('name', None, 'address1', 'domain1.com'),), + ((None, None, 'address2', 'domain2.com'),), + (('name', None, 'address3', 'domain3.com'),), + ((None, None, 'address4', 'domain4.com'),), + None, + None, + '', + ''))) def test_literal(self): literal_text = add_crlf(dedent("""\ 012 abc def XYZ """)) - self._test('{18}' + CRLF + literal_text, literal_text) + self._test([('{18}', literal_text)], literal_text) def test_literal_with_more(self): @@ -87,16 +128,13 @@ class TestParseResponse(unittest.TestCase): 012 abc def XYZ """)) - response = add_crlf(dedent("""\ - (12 "foo" {18} - %s) - """) % literal_text) + response = [('(12 "foo" {18}', literal_text), ")"] self._test(response, (12, 'foo', literal_text)) def test_quoted_specials(self): - self._test(r'"foo \"bar\""', ('foo "bar"',)) - self._test(r'"foo\\bar"', (r'foo\bar',)) + self._test(r'"foo \"bar\""', 'foo "bar"') + self._test(r'"foo\\bar"', r'foo\bar') def test_incomplete_tuple(self): @@ -104,19 +142,20 @@ class TestParseResponse(unittest.TestCase): def test_bad_literal(self): - self._test_parse_error('{99} abc', 'No CRLF after {99}') + self._test_parse_error([('{99}', 'abc')], + 'Expecting literal of size 99, got 3') def test_bad_quoting(self): - self._test_parse_error('"abc next', 'No closing quotation: "abc next') - - + self._test_parse_error('"abc next', 'No closing quotation:') def _test(self, to_parse, expected, wrap=True): if wrap: # convenience - expected value should be wrapped in another tuple expected = (expected,) + if not isinstance(to_parse, list): + to_parse = [to_parse] output = parse_response(to_parse) self.assert_( output == expected, @@ -128,7 +167,7 @@ class TestParseResponse(unittest.TestCase): parse_response(to_parse) self.fail("didn't raise an exception") except ParseError, err: - self.assert_(expected_msg == str(err), + self.assert_(expected_msg == str(err)[:len(expected_msg)], 'got ParseError with wrong msg: %r' % str(err)) @@ -140,42 +179,42 @@ def datetime_to_native(dt): class TestParseFetchResponse(unittest.TestCase): def test_basic(self): - self.assertEquals(parse_fetch_response('* 4 FETCH ()'), {4: {}}) - self.assertEquals(parse_fetch_response('* 4 fEtCh ()'), {4: {}}) + self.assertEquals(parse_fetch_response('4 ()'), {4: {'SEQ': 4}}) - def test_non_fetch(self): - self.assertRaises(ParseError, parse_fetch_response, '* 4 OTHER ()') +# def test_non_fetch(self): +# self.assertRaises(ParseError, parse_fetch_response, ['4 ()']) def test_bad_msgid(self): - self.assertRaises(ParseError, parse_fetch_response, '* abc FETCH ()') + self.assertRaises(ParseError, parse_fetch_response, ['abc ()']) def test_bad_data(self): - self.assertRaises(ParseError, parse_fetch_response, '* 2 FETCH WHAT') + self.assertRaises(ParseError, parse_fetch_response, ['2 WHAT']) def test_missing_data(self): - self.assertRaises(ParseError, parse_fetch_response, '* 2 FETCH') + self.assertRaises(ParseError, parse_fetch_response, ['2']) def test_simple_pairs(self): - self.assertEquals(parse_fetch_response('* 23 FETCH (ABC 123 StUfF "hello")'), + self.assertEquals(parse_fetch_response(['23 (ABC 123 StUfF "hello")']), {23: {'ABC': 123, - 'STUFF': 'hello'}}) + 'STUFF': 'hello', + 'SEQ': 23}}) 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)') + 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: {}}) - self.assertEquals(parse_fetch_response('* 23 FETCH (uiD 76)'), - {76: {}}) + self.assertEquals(parse_fetch_response(['23 (UID 76)']), + {76: {'SEQ': 23}}) + self.assertEquals(parse_fetch_response(['23 (uiD 76)']), + {76: {'SEQ': 23}}) def test_bad_UID(self): @@ -183,12 +222,18 @@ class TestParseFetchResponse(unittest.TestCase): def test_FLAGS(self): - self.assertEquals(parse_fetch_response('* 23 FETCH (FLAGS (\Seen Stuff))'), - {23: {'FLAGS': (r'\Seen', 'Stuff')}}) + self.assertEquals(parse_fetch_response(['23 (FLAGS (\Seen Stuff))']), + {23: {'SEQ': 23, 'FLAGS': (r'\Seen', 'Stuff')}}) def test_multiple_messages(self): - self.fail() + self.assertEquals(parse_fetch_response( + ["2 (FLAGS (Foo Bar)) ", + "7 (FLAGS (Baz Sneeve))"]), + { + 2: {'FLAGS': ('Foo', 'Bar'), 'SEQ': 2}, + 7: {'FLAGS': ('Baz', 'Sneeve'), 'SEQ': 7}, + }) def test_INTERNALDATE(self): @@ -217,20 +262,6 @@ class TestParseFetchResponse(unittest.TestCase): # datetime.datetime(2007, 12, 9, 17, 8, 8, 0, FixedOffset(0))) -# def testMultipleMessages(self): -# '''Test with multple messages in the response -# ''' -# self._parse_test( -# [ -# r'2 (FLAGS (Foo Bar))', -# r'7 (FLAGS (Baz Sneeve))', -# ], -# { -# 2: {'FLAGS': ['Foo', 'Bar']}, -# 7: {'FLAGS': ['Baz', 'Sneeve']}, -# } -# ) - # def testLiteral(self): # '''Test literal handling # ''' -- 2.43.0