From a3b26ebba2cfc1f104f7b93c895b2eb2ca86b00c Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Tue, 13 Jan 2015 00:05:00 +1300 Subject: [PATCH] Moved BodyData to response_types module --- imapclient/response_parser.py | 24 +----------------------- imapclient/response_types.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index 87d7593..acbe893 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -23,7 +23,7 @@ xrange = six.moves.xrange from .datetime_util import parse_to_datetime from .fixed_offset import FixedOffset from .response_lexer import TokenSource -from .response_types import Envelope, Address +from .response_types import BodyData, Envelope, Address try: import imaplib2 as imaplib @@ -124,28 +124,6 @@ def _int_or_error(value, error_text): raise ParseError('%s: %s' % (error_text, repr(value))) -class BodyData(tuple): - - @classmethod - def create(cls, response): - # In case of multipart messages we will see at least 2 tuples - # 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, six.binary_type): - break - return cls(([cls.create(part) for part in response[:i]],) + response[i:]) - else: - return cls(response) - - @property - def is_multipart(self): - return isinstance(self[0], list) - - def _convert_INTERNALDATE(date_string, normalise_times=True): date_msg = b'INTERNALDATE "' + date_string + b'"' mo = imaplib.InternalDate.match(date_msg) diff --git a/imapclient/response_types.py b/imapclient/response_types.py index 78365da..5625b58 100644 --- a/imapclient/response_types.py +++ b/imapclient/response_types.py @@ -5,6 +5,8 @@ from collections import namedtuple from email.utils import formataddr +from . import six + class Envelope(namedtuple("Envelope", "date subject from_ sender reply_to to " + "cc bcc in_reply_to message_id")): @@ -50,6 +52,7 @@ class Address(namedtuple("Address", "name route mailbox host")): def __str__(self): return formataddr((self.name, self.mailbox + '@' + self.host)) + class SearchIds(list): """ Contains a list of message ids as returned by IMAPClient.search(). @@ -62,3 +65,28 @@ class SearchIds(list): def __init__(self, *args): list.__init__(self, *args) self.modseq = None + + +class BodyData(tuple): + """ + Returned when parsing BODY and BODYSTRUCTURE responses. + """ + + @classmethod + def create(cls, response): + # In case of multipart messages we will see at least 2 tuples + # 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, six.binary_type): + break + return cls(([cls.create(part) for part in response[:i]],) + response[i:]) + else: + return cls(response) + + @property + def is_multipart(self): + return isinstance(self[0], list) -- 2.43.0