Show
Ignore:
Timestamp:
12/17/09 14:02:26 (2 years ago)
Author:
Menno Smits <menno@…>
Branch:
default
Message:

core parts of fetch parsing done, lots more tests parsing

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • imapclient/response_parser.py

    r109 r111  
    3333 
    3434def parse_fetch_response(text): 
     35    #XXX doc 
    3536    response = iter(parse_response(text)) 
    3637 
    3738    def expect(expected_value): 
    38         next_value = response.next() 
     39        next_value = response.next().upper() 
    3940        if next_value != expected_value: 
    4041            raise ParseError('expected %r, got %r' % (expected_value, next_value)) 
    4142 
    42     msg_data = {} 
     43    parsed_response = {} 
    4344    while True: 
    4445        try: 
     
    4647        except StopIteration: 
    4748            break 
    48         msgid = int(response.next()) 
     49 
     50        msg_id = _int_or_error(response.next(), 'invalid message ID') 
    4951        expect('FETCH') 
    50         msg_data[msgid] = response.next() 
    51     return msg_data 
     52 
     53        msg_response = response.next() 
     54        if not isinstance(msg_response, tuple): 
     55            raise ParseError('bad response type: %s' % repr(msg_response)) 
     56        if len(msg_response) % 2: 
     57            raise ParseError('uneven number of response items: %s' % repr(msg_response)) 
     58 
     59        #XXX extract this 
     60        msg_data = {} 
     61        for i in xrange(0, len(msg_response), 2): 
     62            word = msg_response[i].upper() 
     63            value = msg_response[i+1] 
     64 
     65            if word == 'UID': 
     66                msg_id = _int_or_error(value, 'invalid UID') 
     67            else: 
     68                msg_data[word] = value 
     69 
     70        parsed_response[msg_id] = msg_data 
     71 
     72    return parsed_response 
     73 
     74 
     75def _int_or_error(value, error_text): 
     76    try: 
     77        return int(value) 
     78    except (TypeError, ValueError): 
     79        raise ParseError('%s: %s' % (error_text, repr(value))) 
     80 
    5281 
    5382 
     
    5786 
    5887    CTRL_CHARS = ''.join([chr(ch) for ch in range(32)]) 
    59     ATOM_SPECIALS = r'()%*\"]' + CTRL_CHARS 
     88    ATOM_SPECIALS = r'()%*"]' + CTRL_CHARS 
    6089    ALL_CHARS = [chr(ch) for ch in range(256)] 
    6190    ATOM_NON_SPECIALS = [ch for ch in ALL_CHARS if ch not in ATOM_SPECIALS]