From ff4604f35572192ecadf68984e66110fc9ef2de8 Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Fri, 3 Jun 2011 22:19:09 +0100 Subject: [PATCH] Restructure BODY and BODYSTRUCTURE responses post-parsing to make tuple length predictable. This avoids having to treat )( as special (which is wrong anyway). This closes #91. --- imapclient/response_parser.py | 17 +++++++++++----- imapclient/test/test_response_parser.py | 26 +++++++++++++------------ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index 3158160..73221c9 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -111,11 +111,18 @@ class BodyData(tuple): @classmethod def create(cls, response): # In case of multipart messages we will see at least 2 tuples - # at the start. Restructure these in to a list so that the - # returned response tuple always has a consistent number of - # items regardless of whether the message is multipart or not. - raise NotImplementedError - + # at the start. Nest these in to a list so that the returned + # response tuple always has a consistent number of elements + # regardless of whether the message is multipart or not. + if isinstance(response[0], tuple): + # Multipart, find where the message part tuples stop + for i, part in enumerate(response): + if isinstance(part, basestring): + break + return cls((list(response[:i]),) + response[i:]) + else: + return cls(response) + @property def is_multipart(self): return isinstance(self[0], list) diff --git a/imapclient/test/test_response_parser.py b/imapclient/test/test_response_parser.py index df41d26..b785203 100644 --- a/imapclient/test/test_response_parser.py +++ b/imapclient/test/test_response_parser.py @@ -258,22 +258,24 @@ class TestParseFetchResponse(unittest.TestCase): self.check_BODYish_multipart('BODYSTRUCTURE') def check_BODYish_single_part(self, respType): - resp = '123 (UID 317 %s ("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "7BIT" 16 1))' % respType - self.assertEquals(parse_fetch_response([resp]), - { 317: {respType: ('TEXT', 'PLAIN', ('CHARSET', 'us-ascii'), None, None, '7BIT', 16, 1), - 'SEQ': 123 } - }) + text = '123 (UID 317 %s ("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "7BIT" 16 1))' % respType + parsed = parse_fetch_response([text]) + self.assertEquals(parsed, {317: {respType: ('TEXT', 'PLAIN', ('CHARSET', 'us-ascii'), None, None, '7BIT', 16, 1), + 'SEQ': 123 } + }) + self.assertFalse(parsed[317][respType].is_multipart) def check_BODYish_multipart(self, respType): - resp = '123 (UID 269 %s (("TEXT" "HTML" ("CHARSET" "us-ascii") NIL NIL "QUOTED-PRINTABLE" 55 3)' \ + text = '123 (UID 269 %s (("TEXT" "HTML" ("CHARSET" "us-ascii") NIL NIL "QUOTED-PRINTABLE" 55 3)' \ '("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "7BIT" 26 1) "MIXED"))' \ % respType - self.assertEquals(parse_fetch_response([resp]), - {269: {respType: ([('TEXT', 'HTML', ('CHARSET', 'us-ascii'), None, None, 'QUOTED-PRINTABLE', 55, 3), - ('TEXT', 'PLAIN', ('CHARSET', 'us-ascii'), None, None, '7BIT', 26, 1)], - 'MIXED'), - 'SEQ': 123} - }) + parsed = parse_fetch_response([text]) + self.assertEquals(parsed, {269: {respType: ([('TEXT', 'HTML', ('CHARSET', 'us-ascii'), None, None, 'QUOTED-PRINTABLE', 55, 3), + ('TEXT', 'PLAIN', ('CHARSET', 'us-ascii'), None, None, '7BIT', 26, 1)], + 'MIXED'), + 'SEQ': 123} + }) + self.assertTrue(parsed[269][respType].is_multipart) def test_partial_fetch(self): body = '01234567890123456789' -- 2.43.0