@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)
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'