Changeset 126:4319998fc9b5
- Timestamp:
- 25/01/10 13:26:30 (2 years ago)
- Author:
- Menno Smits <menno@…>
- Branch:
- default
- Message:
-
select_folder() now returns the full parsed SELECT response (#24)
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r125
|
r126
|
|
| 5 | 5 | (eg. BODYSTRUCTURE and ENVELOPE). This change also fixes various |
| 6 | 6 | problems when interacting with Gmail and MS Exchange (#1). |
| | 7 | |
| | 8 | [API CHANGE] select_folder now returns a dictionary with the full |
| | 9 | SELECT command response instead of just the message count (#24). |
| 7 | 10 | |
| 8 | 11 | |
-
|
r121
|
r126
|
|
| 15 | 15 | 'RECENT'] |
| 16 | 16 | |
| 17 | | from response_parser import parse_fetch_response |
| | 17 | from response_parser import parse_response, parse_fetch_response |
| | 18 | |
| 18 | 19 | |
| 19 | 20 | # System flags |
| … |
… |
|
| 205 | 206 | |
| 206 | 207 | @param folder: The folder name. |
| 207 | | @return: Number of messages in the folder. |
| 208 | | @rtype: long int |
| | 208 | @return: A dictionary containing the SELECT response |
| | 209 | values. At least the EXISTS, FLAGS and RECENT keys are |
| | 210 | guaranteed to exist. Example: |
| | 211 | {'EXISTS': 3, |
| | 212 | 'FLAGS': ('\\Answered', '\\Flagged', '\\Deleted', ... ), |
| | 213 | 'RECENT': 0, |
| | 214 | 'PERMANENTFLAGS': ('\\Answered', '\\Flagged', '\\Deleted', ... ), |
| | 215 | 'READ-WRITE': True, |
| | 216 | 'UIDNEXT': 11, |
| | 217 | 'UIDVALIDITY': 1239278212} |
| 209 | 218 | """ |
| 210 | 219 | typ, data = self._imap.select(self._encode_folder_name(folder)) |
| 211 | 220 | self._checkok('select', typ, data) |
| 212 | | return long(data[0]) |
| | 221 | return self._process_select_response(self._imap.untagged_responses) |
| | 222 | |
| | 223 | |
| | 224 | def _process_select_response(self, resp): |
| | 225 | out = {} |
| | 226 | for key, value in resp.iteritems(): |
| | 227 | key = key.upper() |
| | 228 | if key == 'OK': |
| | 229 | continue |
| | 230 | elif key in ('EXISTS', 'RECENT', 'UIDNEXT', 'UIDVALIDITY'): |
| | 231 | value = int(value[0]) |
| | 232 | elif key in ('FLAGS', 'PERMANENTFLAGS'): |
| | 233 | value = parse_response(value)[0] |
| | 234 | elif key == 'READ-WRITE': |
| | 235 | value = True |
| | 236 | |
| | 237 | out[key] = value |
| | 238 | return out |
| 213 | 239 | |
| 214 | 240 | |
-
|
r123
|
r126
|
|
| 162 | 162 | |
| 163 | 163 | CTRL_CHARS = ''.join([chr(ch) for ch in range(32)]) |
| 164 | | ATOM_SPECIALS = r'()%*"' + CTRL_CHARS |
| | 164 | SPECIALS = r'()%"' + CTRL_CHARS |
| 165 | 165 | ALL_CHARS = [chr(ch) for ch in range(256)] |
| 166 | | ATOM_NON_SPECIALS = [ch for ch in ALL_CHARS if ch not in ATOM_SPECIALS] |
| | 166 | NON_SPECIALS = [ch for ch in ALL_CHARS if ch not in SPECIALS] |
| 167 | 167 | |
| 168 | 168 | def __init__(self, resp_chunks): |
| … |
… |
|
| 174 | 174 | self.lex.quotes = '"' |
| 175 | 175 | self.lex.commenters = '' |
| 176 | | self.lex.wordchars = self.ATOM_NON_SPECIALS |
| | 176 | self.lex.wordchars = self.NON_SPECIALS |
| 177 | 177 | |
| 178 | 178 | def __iter__(self): |
-
|
r121
|
r126
|
|
| 37 | 37 | |
| 38 | 38 | def test_select_and_close(client): |
| 39 | | num_msgs = client.select_folder('INBOX') |
| 40 | | assert isinstance(num_msgs, long) |
| 41 | | assert num_msgs >= 0 |
| | 39 | resp = client.select_folder('INBOX') |
| | 40 | assert isinstance(resp['EXISTS'], int) |
| | 41 | assert resp['EXISTS'] > 1 |
| | 42 | assert isinstance(resp['RECENT'], int) |
| | 43 | assert isinstance(resp['FLAGS'], tuple) |
| | 44 | assert len(resp['FLAGS']) > 1 |
| 42 | 45 | client.close_folder() |
| 43 | 46 | |
| … |
… |
|
| 141 | 144 | |
| 142 | 145 | # Retrieve the just added message and check that all looks well |
| 143 | | num_msgs = client.select_folder('INBOX') |
| 144 | | assert num_msgs == 1 |
| | 146 | assert client.select_folder('INBOX')['EXISTS'] == 1 |
| 145 | 147 | |
| 146 | 148 | resp = client.fetch( |