From: Menno Smits Date: Sun, 31 Oct 2010 16:11:35 +0000 (+0000) Subject: Handling of juxtaposed tuples implemented. X-Git-Tag: 0.7~8^2~4 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=9377601526538cc8824ab2728894c674a4a765a1;p=imapclient.git Handling of juxtaposed tuples implemented. A little messy, needs cleaning up. --- diff --git a/imapclient/response_lexer.py b/imapclient/response_lexer.py index 364acd7..7003481 100644 --- a/imapclient/response_lexer.py +++ b/imapclient/response_lexer.py @@ -94,6 +94,8 @@ class Lexer(object): if token: yield token if nextchar == ')' and stream_i.peek() == '(': + #XXX might be cleaner just to push + stream_i.next() yield ')(' else: yield nextchar # yield the punctuation diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index ebc5213..c730c84 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -120,11 +120,24 @@ def _convert_INTERNALDATE(date_string): def atom(src, token): if token == "(": - out = [] + #XXX names, use separate func? + out = target = [] for token in src: if token == ")": + if target is not out: + out[-1] = tuple(out[-1]) + return out return tuple(out) - out.append(atom(src, token)) + elif token == ')(': + if target is out: + target = [] + out = [tuple(out), target] + else: + out[-1] = tuple(out[-1]) + target = [] + out.append(target) + else: + target.append(atom(src, token)) # oops - no terminator! preceeding = ' '.join(str(val) for val in out) raise ParseError('Tuple incomplete before "(%s"' % preceeding) diff --git a/imapclient/test/test_response_parser.py b/imapclient/test/test_response_parser.py index b70df30..3565fa1 100644 --- a/imapclient/test/test_response_parser.py +++ b/imapclient/test/test_response_parser.py @@ -64,12 +64,18 @@ class TestParseResponse(unittest.TestCase): self._test('(123 "foo" ((0 1 2) "more" NIL) 66)', (123, "foo", ((0, 1, 2), "more", None), 66)) - def test_backed_up_tuples(self): + def test_juxtatposed_tuples(self): + # Tuples with no whitespace between them should be returned as + # a list of tuples self._test('((12 "foo")(34 NIL 0) "foo")', ([(12, "foo"), (34, None, 0)], "foo")) + self._test('((12 "foo")(34 NIL 0)(4 55 6) "foo")', + ([(12, "foo"), (34, None, 0), (4, 55, 6)], "foo")) + self._test('((12 "foo")(34 (NIL 1 2) 0)(4 55 6) "foo")', + ([(12, "foo"), (34, (None, 1, 2), 0), (4, 55, 6)], "foo")) def test_complex_mixed(self): - self._test('((FOO "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 1152 23)' + self._test('((FOO "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 1152 23) ' '("TEXT" "PLAIN" ("CHARSET" "US-ASCII" "NAME" "cc.diff") ' '"" "foo" "BASE64" 4554 73) "MIXED")', (('FOO', 'PLAIN', ('CHARSET', 'US-ASCII'), None, None, '7BIT', 1152, 23),