Changeset 109:bfb2aabc0b11

Show
Ignore:
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:
3 modified

Legend:

Unmodified
Added
Removed
  • imapclient/response_parser.py

    r107 r109  
    66 
    77import shlex 
     8from cStringIO import StringIO 
    89 
    910#XXX higher level response type response type processing 
     
    2122 
    2223def parse_response(text): 
    23     #XXX 
     24    #XXX doc 
    2425    src = ResponseTokeniser(text) 
    2526    try: 
    26         return tuple([atom(src, token) for token in src]) 
     27        return tuple(atom(src, token) for token in src) 
    2728    except ParseError: 
    2829        raise 
     
    3233 
    3334def parse_fetch_response(text): 
    34     #XXX 
    35     response = parse_response(text) 
     35    response = iter(parse_response(text)) 
    3636 
    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)) 
    3841 
    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 
    4452 
    4553 
  • imapclient/test/test_response_parser.py

    r107 r109  
    129129    return dt.astimezone(system_offset).replace(tzinfo=None) 
    130130 
    131 class TestPatchFetchResponse(object): 
     131class TestParseFetchResponse(unittest.TestCase): 
    132132 
    133133    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 
    135142 
    136143    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 
    138150 
    139151    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': {}}) 
    148154 
    149155 
     
    177183 
    178184    def test_multiple_fields(self): 
     185        self.fail() 
     186 
     187    def test_multiple_messages(self): 
    179188        self.fail() 
    180189 
  • run_altfetch.py

    r105 r109  
    11from pprint import pprint 
    22from imapclient import IMAPClient 
     3from imapclient.response_parser import parse_response 
    34 
    45def main(): 
     
    89    msgs = i.search() 
    910    i._imap.debug = 5 
    10     lines = i.altfetch(msgs[0], ['RFC822']) 
     11    lines = i.altfetch(msgs, ['ENVELOPE', 'BODYSTRUCTURE']) 
    1112 
    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' 
    1715 
     16    for x in parse_response(body): 
     17        print x 
    1818 
    1919if __name__ == '__main__':