Show
Ignore:
Timestamp:
03/06/11 22:19:09 (12 months ago)
Author:
Menno Smits <menno@…>
Branch:
default
Message:

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.

Location:
imapclient
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • imapclient/response_parser.py

    r229 r230  
    112112    def create(cls, response): 
    113113        # In case of multipart messages we will see at least 2 tuples 
    114         # at the start. Restructure these in to a list so that the 
    115         # returned response tuple always has a consistent number of 
    116         # items regardless of whether the message is multipart or not. 
    117         raise NotImplementedError 
    118      
     114        # at the start. Nest these in to a list so that the returned 
     115        # response tuple always has a consistent number of elements 
     116        # regardless of whether the message is multipart or not. 
     117        if isinstance(response[0], tuple): 
     118            # Multipart, find where the message part tuples stop 
     119            for i, part in enumerate(response): 
     120                if isinstance(part, basestring): 
     121                    break 
     122            return cls((list(response[:i]),) + response[i:]) 
     123        else: 
     124            return cls(response) 
     125             
    119126    @property 
    120127    def is_multipart(self): 
  • imapclient/test/test_response_parser.py

    r228 r230  
    259259     
    260260    def check_BODYish_single_part(self, respType): 
    261         resp =  '123 (UID 317 %s ("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "7BIT" 16 1))' % respType 
    262         self.assertEquals(parse_fetch_response([resp]), 
    263             { 317: {respType: ('TEXT', 'PLAIN', ('CHARSET', 'us-ascii'), None, None, '7BIT', 16, 1), 
    264                     'SEQ': 123 } 
    265             }) 
     261        text =  '123 (UID 317 %s ("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "7BIT" 16 1))' % respType 
     262        parsed = parse_fetch_response([text]) 
     263        self.assertEquals(parsed, {317: {respType: ('TEXT', 'PLAIN', ('CHARSET', 'us-ascii'), None, None, '7BIT', 16, 1), 
     264                                         'SEQ': 123 } 
     265                                         }) 
     266        self.assertFalse(parsed[317][respType].is_multipart) 
    266267 
    267268    def check_BODYish_multipart(self, respType): 
    268         resp = '123 (UID 269 %s (("TEXT" "HTML" ("CHARSET" "us-ascii") NIL NIL "QUOTED-PRINTABLE" 55 3)' \ 
     269        text = '123 (UID 269 %s (("TEXT" "HTML" ("CHARSET" "us-ascii") NIL NIL "QUOTED-PRINTABLE" 55 3)' \ 
    269270                                '("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "7BIT" 26 1) "MIXED"))' \ 
    270271                                % respType 
    271         self.assertEquals(parse_fetch_response([resp]), 
    272              {269: {respType: ([('TEXT', 'HTML', ('CHARSET', 'us-ascii'), None, None, 'QUOTED-PRINTABLE', 55, 3), 
    273                                 ('TEXT', 'PLAIN', ('CHARSET', 'us-ascii'), None, None, '7BIT', 26, 1)], 
    274                                 'MIXED'), 
    275                     'SEQ': 123} 
    276             }) 
     272        parsed = parse_fetch_response([text]) 
     273        self.assertEquals(parsed, {269: {respType: ([('TEXT', 'HTML', ('CHARSET', 'us-ascii'), None, None, 'QUOTED-PRINTABLE', 55, 3), 
     274                                                     ('TEXT', 'PLAIN', ('CHARSET', 'us-ascii'), None, None, '7BIT', 26, 1)], 
     275                                                     'MIXED'), 
     276                                        'SEQ': 123} 
     277                                        }) 
     278        self.assertTrue(parsed[269][respType].is_multipart) 
    277279 
    278280    def test_partial_fetch(self):