]> khamidou — GitHub mirrors - imapclient.git/commitdiff
core parts of fetch parsing done, lots more tests parsing
authorMenno Smits <menno@freshfoo.com>
Thu, 17 Dec 2009 14:02:26 +0000 (14:02 +0000)
committerMenno Smits <menno@freshfoo.com>
Thu, 17 Dec 2009 14:02:26 +0000 (14:02 +0000)
imapclient/response_parser.py
imapclient/test/__init__.py
imapclient/test/test_response_parser.py

index 63b18e4f29310c84d771e3b635f43bf77b63edd9..73f3206d57f9af02bf78a3e976a330a53f6e4412 100644 (file)
@@ -32,23 +32,52 @@ def parse_response(text):
 
 
 def parse_fetch_response(text):
+    #XXX doc
     response = iter(parse_response(text))
 
     def expect(expected_value):
-        next_value = response.next()
+        next_value = response.next().upper()
         if next_value != expected_value:
             raise ParseError('expected %r, got %r' % (expected_value, next_value))
 
-    msg_data = {}
+    parsed_response = {}
     while True:
         try:
             expect('*')
         except StopIteration:
             break
-        msgid = int(response.next())
+
+        msg_id = _int_or_error(response.next(), 'invalid message ID')
         expect('FETCH')
-        msg_data[msgid] = response.next()
-    return msg_data
+
+        msg_response = response.next()
+        if not isinstance(msg_response, tuple):
+            raise ParseError('bad response type: %s' % repr(msg_response))
+        if len(msg_response) % 2:
+            raise ParseError('uneven number of response items: %s' % repr(msg_response))
+
+        #XXX extract this
+        msg_data = {}
+        for i in xrange(0, len(msg_response), 2):
+            word = msg_response[i].upper()
+            value = msg_response[i+1]
+
+            if word == 'UID':
+                msg_id = _int_or_error(value, 'invalid UID')
+            else:
+                msg_data[word] = value
+
+        parsed_response[msg_id] = msg_data
+
+    return parsed_response
+
+
+def _int_or_error(value, error_text):
+    try:
+        return int(value)
+    except (TypeError, ValueError):
+        raise ParseError('%s: %s' % (error_text, repr(value)))
+
 
 
 EOF = object()
@@ -56,7 +85,7 @@ EOF = object()
 class ResponseTokeniser(object):
 
     CTRL_CHARS = ''.join([chr(ch) for ch in range(32)])
-    ATOM_SPECIALS = r'()%*\"]' + CTRL_CHARS
+    ATOM_SPECIALS = r'()%*"]' + CTRL_CHARS
     ALL_CHARS = [chr(ch) for ch in range(256)]
     ATOM_NON_SPECIALS = [ch for ch in ALL_CHARS if ch not in ATOM_SPECIALS]
 
index ee8b89bc3f8d69fc91a0434541d2f28abc355223..8f6e7ffbbdd5a7406db2ba940df3c489d4fe6fe3 100644 (file)
@@ -25,7 +25,7 @@ def load_suite():
 
 def run_suite():
     suite = load_suite()
-    runner = unittest.TextTestRunner(verbosity=2)
+    runner = unittest.TextTestRunner(verbosity=1)
     runner.run(suite)
 
 
index 41522f62390d36b5fceae93d4eca7e21dfefab33..0660c230747d855d0f2af7e14be3d8a7a2beac8a 100644 (file)
@@ -37,6 +37,7 @@ class TestParseResponse(unittest.TestCase):
     def test_unquoted(self):
         self._test('FOO', 'FOO')
         self._test('F.O:-O_0;', 'F.O:-O_0;')
+        self._test(r'\Seen', r'\Seen')
 
     def test_string(self):
         self._test('"TEST"', 'TEST')
@@ -128,16 +129,12 @@ system_offset = FixedOffset.for_system()
 def datetime_to_native(dt):
     return dt.astimezone(system_offset).replace(tzinfo=None)
 
+
 class TestParseFetchResponse(unittest.TestCase):
 
     def test_basic(self):
         self.assertEquals(parse_fetch_response('* 4 FETCH ()'), {4: {}})
-
-
-    def test_simple_pair(self):
-        self.assertEquals(parse_fetch_response('* 23 FETCH (ABC 123 STUFF "hello")'),
-                          {'23': {'ABC': 123,
-                                  'STUFF': 'hello'}})
+        self.assertEquals(parse_fetch_response('* 4 fEtCh ()'), {4: {}})
 
 
     def test_non_fetch(self):
@@ -148,14 +145,41 @@ class TestParseFetchResponse(unittest.TestCase):
         self.assertRaises(ParseError, parse_fetch_response, '* abc FETCH ()')
 
 
+    def test_bad_data(self):
+        self.assertRaises(ParseError, parse_fetch_response, '* 2 FETCH WHAT')
+
+
+    def test_simple_pairs(self):
+        self.assertEquals(parse_fetch_response('* 23 FETCH (ABC 123 StUfF "hello")'),
+                          {23: {'ABC': 123,
+                                'STUFF': 'hello'}})
+
+
+    def test_odd_pairs(self):
+        self.assertRaises(ParseError, parse_fetch_response, '* 2 FETCH (ONE)')
+        self.assertRaises(ParseError, parse_fetch_response, '* 2 FETCH (ONE TWO THREE)')
+
+
     def test_UID(self):
         self.assertEquals(parse_fetch_response('* 23 FETCH (UID 76)'),
-                          {'76': {}})
+                          {76: {}})
+        self.assertEquals(parse_fetch_response('* 23 FETCH (uiD 76)'),
+                          {76: {}})
 
 
+    def test_bad_UID(self):
+        self.assertRaises(ParseError, parse_fetch_response, '* 2 FETCH (UID X)')
+        
+
     def test_FLAGS(self):
+        self.assertEquals(parse_fetch_response('* 23 FETCH (FLAGS (\Seen Stuff))'),
+                          {23: {'FLAGS': (r'\Seen', 'Stuff')}})
+
+
+    def test_multiple_messages(self):
         self.fail()
 
+
     def test_INTERNALDATE(self):
         self.fail()
 
@@ -181,34 +205,6 @@ class TestParseFetchResponse(unittest.TestCase):
 #         dt = check(' 9-Dec-2007 17:08:08 +0000',
 #                    datetime.datetime(2007, 12, 9, 17, 8, 8, 0, FixedOffset(0)))
 
-    def test_multiple_fields(self):
-        self.fail()
-
-    def test_multiple_messages(self):
-        self.fail()
-
-    def test_case_handling(self):
-        self.fail()
-#         self._parse_test(
-#             [r'2 (flaGS (\Deleted Foo \Seen))'],
-#             {2: {'FLAGS': [r'\Deleted', 'Foo', r'\Seen']}}
-#             )
-
-
-    def test_invalid(self):
-        self.fail()
-#         self.assertRaises(ValueError, self.p, [r'2 (FLAGS (\Deleted) MORE)'])
-#         self.assertRaises(ValueError,
-#                 self.t.process_pairs, 'FOO 123 BAH "abc" WHAT?')
-#         self.assertRaises(ValueError,
-#                 self.t.process_pairs, 'HMMM FOO 123 BAH "abc"')
-#         self.assertRaises(ValueError,
-#                 self.t.process_pairs, 'FOO 123 BAD BAH "abc"')
-
-#TODO convert the following are mainly FETCH specific tests
-
-#     def test_INTERNALDATE(self):
-
 
 #     def testMultipleMessages(self):
 #         '''Test with multple messages in the response