]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Restructure BODY and BODYSTRUCTURE responses post-parsing to make tuple length predic...
authorMenno Smits <menno@freshfoo.com>
Fri, 3 Jun 2011 21:19:09 +0000 (22:19 +0100)
committerMenno Smits <menno@freshfoo.com>
Fri, 3 Jun 2011 21:19:09 +0000 (22:19 +0100)
This avoids having to treat )( as special (which is wrong anyway).
This closes #91.

imapclient/response_parser.py
imapclient/test/test_response_parser.py

index 31581609d3036598540fec59a5f8d41f73382175..73221c908a01fce06403a767179170096d9fa52a 100644 (file)
@@ -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)
index df41d265a8bf1b1c53c72ba1eb38d3fdcfa6cbb4..b785203f62adf3ef199ede1083032eb64ed61c4a 100644 (file)
@@ -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'