Changeset 111:a8df0817a791 for imapclient/response_parser.py
- Timestamp:
- 12/17/09 14:02:26 (2 years ago)
- Branch:
- default
- Files:
-
- 1 modified
-
imapclient/response_parser.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
imapclient/response_parser.py
r109 r111 33 33 34 34 def parse_fetch_response(text): 35 #XXX doc 35 36 response = iter(parse_response(text)) 36 37 37 38 def expect(expected_value): 38 next_value = response.next() 39 next_value = response.next().upper() 39 40 if next_value != expected_value: 40 41 raise ParseError('expected %r, got %r' % (expected_value, next_value)) 41 42 42 msg_data= {}43 parsed_response = {} 43 44 while True: 44 45 try: … … 46 47 except StopIteration: 47 48 break 48 msgid = int(response.next()) 49 50 msg_id = _int_or_error(response.next(), 'invalid message ID') 49 51 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 75 def _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 52 81 53 82 … … 57 86 58 87 CTRL_CHARS = ''.join([chr(ch) for ch in range(32)]) 59 ATOM_SPECIALS = r'()%* \"]' + CTRL_CHARS88 ATOM_SPECIALS = r'()%*"]' + CTRL_CHARS 60 89 ALL_CHARS = [chr(ch) for ch in range(256)] 61 90 ATOM_NON_SPECIALS = [ch for ch in ALL_CHARS if ch not in ATOM_SPECIALS]
