]> khamidou — GitHub mirrors - imapclient.git/commitdiff
started fetch response work, tests half there
authorMenno Smits <menno@freshfoo.com>
Sun, 15 Nov 2009 23:03:15 +0000 (23:03 +0000)
committerMenno Smits <menno@freshfoo.com>
Sun, 15 Nov 2009 23:03:15 +0000 (23:03 +0000)
imapclient/response_parser.py
imapclient/test/test_response_parser.py

index 002e157d8dca0e5999fcc0e2102aca6f02a567d3..b5e632233e5a441fe1c498e553ed4fc1e8b31d73 100644 (file)
@@ -1,8 +1,14 @@
-# Inspired by: http://effbot.org/zone/simple-iterator-parser.htm
+"""
+XXX
+
+Inspired by: http://effbot.org/zone/simple-iterator-parser.htm
+"""
 
 import shlex
 
 #XXX higher level response type response type processing
+#XXX plug-in this version
+#XXX remove old FetchParser
 #TODO more exact error reporting
 
 __all__ = ['parse_response', 'ParseError']
@@ -12,6 +18,31 @@ class ParseError(ValueError):
     pass
 
 
+
+def parse_response(text):
+    #XXX
+    src = ResponseTokeniser(text)
+    try:
+        return tuple([atom(src, token) for token in src])
+    except ParseError:
+        raise
+    except ValueError, err:
+        raise ParseError("%s: %s" % (str(err), src.lex.token))
+
+
+def parse_fetch_response(text):
+    #XXX
+    response = parse_response(text)
+
+    msgid = response[0]
+
+    # Second item should be FETCH
+    if response[1] != 'FETCH':
+        raise ParseError('not a FETCH response')
+
+
+
+
 EOF = object()
 
 class ResponseTokeniser(object):
@@ -66,12 +97,4 @@ def atom(src, token):
         return token
 
 
-def parse_response(text):
-    src = ResponseTokeniser(text)
-    try:
-        return tuple([atom(src, token) for token in src])
-    except ParseError:
-        raise
-    except ValueError, err:
-        raise ParseError("%s: %s" % (str(err), src.lex.token))
 
index 676be3da067f20f1e5c90167659b707bfc96e98a..acfc9aa35bde4a2bd202457af0faf2a9978b9e18 100644 (file)
@@ -20,7 +20,7 @@ Unit tests for the FetchTokeniser and FetchParser classes
 
 from textwrap import dedent
 import unittest
-from imapclient.response_parser import parse_response, ParseError
+from imapclient.response_parser import parse_response, parse_fetch_response, ParseError
 from imapclient.fixed_offset import FixedOffset
 from pprint import pformat
 
@@ -29,9 +29,6 @@ from pprint import pformat
 #TODO: more response types
 #TODO: clean up this file
 
-system_offset = FixedOffset.for_system()
-def datetime_to_native(dt):
-    return dt.astimezone(system_offset).replace(tzinfo=None)
 
 CRLF = '\r\n'
 
@@ -127,21 +124,34 @@ class TestParseResponse(unittest.TestCase):
                          'got ParseError with wrong msg: %r' % str(err))
 
 
-#TODO convert the following are mainly FETCH specific tests
+system_offset = FixedOffset.for_system()
+def datetime_to_native(dt):
+    return dt.astimezone(system_offset).replace(tzinfo=None)
+
+class TestPatchFetchResponse(object):
+
+    def test_basic(self):
+        parse_fetch_response('4 FETCH ()', {'4': {}})
 
-#     def testCharacterCase(self):
-#         '''Test handling of varied case in the response type name
+    def test_non_fetch(self):
+        self.assertRaises(ParseError, parse_fetch_response, '4 OTHER ()')
+
+    def test_UID(self):
+        self.fail()
+#         '''Test UID handling. The UID is returned instead of the given message
+#         ID if present.
 #         '''
 #         self._parse_test(
-#             [r'2 (flaGS (\Deleted Foo \Seen))'],
-#             {2: {'FLAGS': [r'\Deleted', 'Foo', r'\Seen']}}
+#             ['1 (UID 8)'],
+#             {8: {}}
 #             )
 
-#     def testGarbage(self):
-#         self.assertRaises(ValueError, self.p, [r'2 (FLAGS (\Deleted) MORE)'])
 
+    def test_FLAGS(self):
+        self.fail()
 
-#     def test_INTERNALDATE(self):
+    def test_INTERNALDATE(self):
+        self.fail()
 
 #         def check(date_str, expected_dt):
 #             output = self.p(['3 (INTERNALDATE "%s")' % date_str])
@@ -165,20 +175,32 @@ class TestParseResponse(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 testMultipleTypes(self):
-#         '''Test multiple response types'''
+    def test_case_handling(self):
+        self.fail()
 #         self._parse_test(
-#             [r'2 (FLAGS (\Deleted Foo \Seen) INTERNALDATE " 9-Feb-2007 17:08:08 +0000")'],
-#             {2: {
-#                     'FLAGS': [r'\Deleted', 'Foo', r'\Seen'],
-#                     'INTERNALDATE': datetime_to_native(datetime.datetime(2007, 2, 9,
-#                                                                          17, 8, 8, 0,
-#                                                                          FixedOffset(0)))
-#                     }
-#                 }
+#             [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
 #         '''
@@ -247,14 +269,6 @@ class TestParseResponse(unittest.TestCase):
 #                 }
 #             )
 
-#     def testUID(self):
-#         '''Test UID handling. The UID is returned instead of the given message
-#         ID if present.
-#         '''
-#         self._parse_test(
-#             ['1 (UID 8)'],
-#             {8: {}}
-#             )
 
 
 def format_error(input_, output, expected):
@@ -269,73 +283,5 @@ def add_crlf(text):
     return CRLF.join(text.splitlines()) + CRLF
 
 
-# class TestFetchTokeniser(unittest.TestCase):
-#     def setUp(self):
-#         self.t = FetchTokeniser()
-
-#     def testEmptyList(self):
-#         self._test_list('', [])
-
-#     def testOneItemList(self):
-#         self._test_list('123', [123])
-
-#     def testWordsInList(self):
-#         self._test_list('test stuff', ['test', 'stuff'])
-
-#     def testQuotedStringInList(self):
-#         self._test_list('"test stuff" and more', ['test stuff', 'and', 'more'])
-
-#     def testNILInList(self):
-#         self._test_list('test NIL stuff', ['test', None, 'stuff'])
-
-#     def testEmptyListPair(self):
-#         self._test_pairs('FOO ()', [('FOO', [])])
-
-#     def testIntPair(self):
-#         self._test_pairs('FOO 123', [('FOO', 123)])
-
-#     def testQuotedStringPair(self):
-#         self._test_pairs('FOO "abc def"', [('FOO', "abc def")])
-
-#     def testComplexListPair(self):
-#         self._test_pairs('FOO (123 "a b c" \\XYZ)', [('FOO', [123, "a b c", '\\XYZ'])])
-
-#     def testMultiplePairs(self):
-#         self._test_pairs('FOO 123 bar (def "XYZ") MORE "stuff"', [
-#             ('FOO', 123),
-#             ('bar', ['def', 'XYZ']),
-#             ('MORE', 'stuff'),
-#             ])
-
-#     def testNoPairs(self):
-#         self._test_pairs('', [])
-
-#     def testGarbage(self):
-#         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"')
-
-#     def testLiteral(self):
-#         self._test_pairs('FOO {21}', [('FOO', Literal(21))])
-
-#     def _test_pairs(self, input_, expected):
-#         output = self.t.process_pairs(input_)
-#         self.assert_(
-#                 output == expected,
-#                 format_error(input_, output, expected),
-#             )
-#         return output
-
-#     def _test_list(self, input_, expected):
-#         output = self.t.process_list(input_)
-#         self.assert_(
-#                 output == expected,
-#                 format_error(input_, output, expected),
-#             )
-#         return output
-
 if __name__ == '__main__':
     unittest.main()