From 79dfcfbe92d411b99726878b93b12bec86a2e29c Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Mon, 8 Nov 2010 14:30:42 +0000 Subject: [PATCH] Added is_multipart attribute to returned BODY/BODYSTRUCTURE tuples --- imapclient/response_parser.py | 9 +++++++++ livetest.py | 13 ++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index faef1c9..313dd37 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -79,6 +79,8 @@ def parse_fetch_response(text): msg_id = _int_or_error(value, 'invalid UID') elif word == 'INTERNALDATE': msg_data[word] = _convert_INTERNALDATE(value) + elif word in ('BODY', 'BODYSTRUCTURE'): + msg_data[word] = BodyData(value) else: msg_data[word] = value @@ -94,6 +96,13 @@ def _int_or_error(value, error_text): raise ParseError('%s: %s' % (error_text, repr(value))) +class BodyData(tuple): + + @property + def is_multipart(self): + return isinstance(self[0], list) + + def _convert_INTERNALDATE(date_string): mo = imaplib.InternalDate.match('INTERNALDATE "%s"' % date_string) if not mo: diff --git a/livetest.py b/livetest.py index a841032..44060b7 100755 --- a/livetest.py +++ b/livetest.py @@ -365,19 +365,22 @@ def test_BODYSTRUCTURE(client): # will return. expected = ('text', 'plain', ('charset', 'us-ascii'), None, None, '7bit', 5, 1) - check_BODYSTRUCTURE(expected, fetched[msgs[0]]['BODY']) - check_BODYSTRUCTURE(expected, fetched[msgs[0]]['BODYSTRUCTURE']) + check_BODYSTRUCTURE(expected, fetched[msgs[0]]['BODY'], multipart=False) + check_BODYSTRUCTURE(expected, fetched[msgs[0]]['BODYSTRUCTURE'], multipart=False) expected = ([('text', 'html', ('charset', 'us-ascii'), None, None, 'quoted-printable', 55, 3), ('text', 'plain', ('charset', 'us-ascii'), None, None, '7bit', 26, 1), ], 'mixed', ('boundary', '===============1534046211==')) - check_BODYSTRUCTURE(expected, fetched[msgs[1]]['BODY']) - check_BODYSTRUCTURE(expected, fetched[msgs[1]]['BODYSTRUCTURE']) + check_BODYSTRUCTURE(expected, fetched[msgs[1]]['BODY'], multipart=True) + check_BODYSTRUCTURE(expected, fetched[msgs[1]]['BODYSTRUCTURE'], multipart=True) -def check_BODYSTRUCTURE(expected, actual): +def check_BODYSTRUCTURE(expected, actual, multipart=None): + if multipart is not None: + assert actual.is_multipart == multipart + # BODYSTRUCTURE lengths can various according to the server so # compare up until what is returned for e, a in zip(expected, actual): -- 2.43.0