Changeset 109:bfb2aabc0b11
- Timestamp:
- 17/12/09 12:00:01 (2 years ago)
- Author:
- Menno Smits <menno@…>
- Branch:
- default
- Message:
-
committing response parser progress so far, lots of failing tests
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r107
|
r109
|
|
| 6 | 6 | |
| 7 | 7 | import shlex |
| | 8 | from cStringIO import StringIO |
| 8 | 9 | |
| 9 | 10 | #XXX higher level response type response type processing |
| … |
… |
|
| 21 | 22 | |
| 22 | 23 | def parse_response(text): |
| 23 | | #XXX |
| | 24 | #XXX doc |
| 24 | 25 | src = ResponseTokeniser(text) |
| 25 | 26 | try: |
| 26 | | return tuple([atom(src, token) for token in src]) |
| | 27 | return tuple(atom(src, token) for token in src) |
| 27 | 28 | except ParseError: |
| 28 | 29 | raise |
| … |
… |
|
| 32 | 33 | |
| 33 | 34 | def parse_fetch_response(text): |
| 34 | | #XXX |
| 35 | | response = parse_response(text) |
| | 35 | response = iter(parse_response(text)) |
| 36 | 36 | |
| 37 | | msgid = response[0] |
| | 37 | def expect(expected_value): |
| | 38 | next_value = response.next() |
| | 39 | if next_value != expected_value: |
| | 40 | raise ParseError('expected %r, got %r' % (expected_value, next_value)) |
| 38 | 41 | |
| 39 | | # Second item should be FETCH |
| 40 | | if response[1] != 'FETCH': |
| 41 | | raise ParseError('not a FETCH response') |
| 42 | | |
| 43 | | |
| | 42 | msg_data = {} |
| | 43 | while True: |
| | 44 | try: |
| | 45 | expect('*') |
| | 46 | except StopIteration: |
| | 47 | break |
| | 48 | msgid = int(response.next()) |
| | 49 | expect('FETCH') |
| | 50 | msg_data[msgid] = response.next() |
| | 51 | return msg_data |
| 44 | 52 | |
| 45 | 53 | |
-
|
r107
|
r109
|
|
| 129 | 129 | return dt.astimezone(system_offset).replace(tzinfo=None) |
| 130 | 130 | |
| 131 | | class TestPatchFetchResponse(object): |
| | 131 | class TestParseFetchResponse(unittest.TestCase): |
| 132 | 132 | |
| 133 | 133 | def test_basic(self): |
| 134 | | parse_fetch_response('4 FETCH ()', {'4': {}}) |
| | 134 | self.assertEquals(parse_fetch_response('* 4 FETCH ()'), {4: {}}) |
| | 135 | |
| | 136 | |
| | 137 | def test_simple_pair(self): |
| | 138 | self.assertEquals(parse_fetch_response('* 23 FETCH (ABC 123 STUFF "hello")'), |
| | 139 | {'23': {'ABC': 123, |
| | 140 | 'STUFF': 'hello'}}) |
| | 141 | |
| 135 | 142 | |
| 136 | 143 | def test_non_fetch(self): |
| 137 | | self.assertRaises(ParseError, parse_fetch_response, '4 OTHER ()') |
| | 144 | self.assertRaises(ParseError, parse_fetch_response, '* 4 OTHER ()') |
| | 145 | |
| | 146 | |
| | 147 | def test_bad_msgid(self): |
| | 148 | self.assertRaises(ParseError, parse_fetch_response, '* abc FETCH ()') |
| | 149 | |
| 138 | 150 | |
| 139 | 151 | def test_UID(self): |
| 140 | | self.fail() |
| 141 | | # '''Test UID handling. The UID is returned instead of the given message |
| 142 | | # ID if present. |
| 143 | | # ''' |
| 144 | | # self._parse_test( |
| 145 | | # ['1 (UID 8)'], |
| 146 | | # {8: {}} |
| 147 | | # ) |
| | 152 | self.assertEquals(parse_fetch_response('* 23 FETCH (UID 76)'), |
| | 153 | {'76': {}}) |
| 148 | 154 | |
| 149 | 155 | |
| … |
… |
|
| 177 | 183 | |
| 178 | 184 | def test_multiple_fields(self): |
| | 185 | self.fail() |
| | 186 | |
| | 187 | def test_multiple_messages(self): |
| 179 | 188 | self.fail() |
| 180 | 189 | |
-
|
r105
|
r109
|
|
| 1 | 1 | from pprint import pprint |
| 2 | 2 | from imapclient import IMAPClient |
| | 3 | from imapclient.response_parser import parse_response |
| 3 | 4 | |
| 4 | 5 | def main(): |
| … |
… |
|
| 8 | 9 | msgs = i.search() |
| 9 | 10 | i._imap.debug = 5 |
| 10 | | lines = i.altfetch(msgs[0], ['RFC822']) |
| | 11 | lines = i.altfetch(msgs, ['ENVELOPE', 'BODYSTRUCTURE']) |
| 11 | 12 | |
| 12 | | print lines[0] |
| 13 | | body = '\r\n'.join(lines[1:-1]) |
| 14 | | body += '\r\n' |
| 15 | | print body |
| 16 | | print len(body) |
| | 13 | body = '\r\n'.join(lines) |
| | 14 | #body += '\r\n' |
| 17 | 15 | |
| | 16 | for x in parse_response(body): |
| | 17 | print x |
| 18 | 18 | |
| 19 | 19 | if __name__ == '__main__': |