Changeset 208:3eeca2dda55d
- Timestamp:
- 03/23/11 00:00:36 (14 months ago)
- Author:
- Johannes Heckel <stone.pixel@…>
- Branch:
- idle
- Children:
- 209:80f530da50d7, 212:45fab16c5bf6
- Message:
-
Added an idle function and changed script to use imaplib2 if available.
Added example demonstrating use of idle function.
Switched the tag and keyword args when using imaplib2's _command_complete as
it expects (rqb, kw) rather than (kw, rqb).
imaplib2's untagged_responses seems to be in a list format rather than the
dictionary format imaplib uses. Now if imaplib2 is being used untagged_responses
is automatically converted to the dictionary format using a simple function.
Not sure if this is the ideal solution though.
- Location:
- imapclient
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r204
|
r208
|
|
| 4 | 4 | |
| 5 | 5 | import re |
| 6 | | import imaplib |
| 7 | 6 | import response_lexer |
| 8 | 7 | from operator import itemgetter |
| 9 | 8 | import warnings |
| | 9 | |
| | 10 | try: |
| | 11 | import imaplib2 as imaplib |
| | 12 | imaplib2 = True |
| | 13 | except ImportError: |
| | 14 | imaplib2 = False |
| | 15 | import imaplib |
| | 16 | |
| 10 | 17 | #imaplib.Debug = 5 |
| 11 | | |
| | 18 | |
| 12 | 19 | import imap_utf7 |
| 13 | 20 | from fixed_offset import FixedOffset |
| … |
… |
|
| 263 | 270 | typ, data = self._imap.select(self._encode_folder_name(folder), readonly) |
| 264 | 271 | self._checkok('select', typ, data) |
| 265 | | return self._process_select_response(self._imap.untagged_responses) |
| 266 | | |
| | 272 | if imaplib2: |
| | 273 | untagged_responses = self._response_to_dict(self._imap.untagged_responses) |
| | 274 | else: |
| | 275 | untagged_responses = self._imap.untagged_responses |
| | 276 | return self._process_select_response(untagged_responses) |
| | 277 | |
| | 278 | def _response_to_dict(self, resp): |
| | 279 | dictresp = {} |
| | 280 | |
| | 281 | if len(resp) > 0: |
| | 282 | for item in resp: |
| | 283 | dictresp[item[0]] = item[1] |
| | 284 | |
| | 285 | return dictresp |
| 267 | 286 | |
| 268 | 287 | def _process_select_response(self, resp): |
| … |
… |
|
| 282 | 301 | return out |
| 283 | 302 | |
| 284 | | |
| | 303 | def idle(self, timeout=None, **kwargs): |
| | 304 | """Put server into IDLE mode. |
| | 305 | |
| | 306 | IDLE mode will end when the server notifies of a change, the sever |
| | 307 | reaches the timeout, or another IMAP4 command is scheduled. |
| | 308 | |
| | 309 | @param timeout: timeout for the IDLE command in seconds (default: 29 mins) |
| | 310 | @param callback: Function to be called when IDLE mode ends. If a callback |
| | 311 | is supplied the idle function will be asynchronous, returning immediately. |
| | 312 | @param cb_arg: An optional argument that will be passed to the callback function. |
| | 313 | @return: Server response. (None if callback is specified). |
| | 314 | """ |
| | 315 | if imaplib2: |
| | 316 | if 'callback' in kwargs: |
| | 317 | callback = kwargs.pop('callback') |
| | 318 | def _wrapped_callback(resp): |
| | 319 | typ, data = resp[0] |
| | 320 | self._checkok('idle', typ, data) |
| | 321 | # if there's a cb_arg, pass it along |
| | 322 | cb_arg = resp[1] |
| | 323 | if cb_arg: |
| | 324 | callback(data[0], cb_arg) |
| | 325 | else: |
| | 326 | callback(data[0]) |
| | 327 | self._imap.idle(timeout, callback=_wrapped_callback, **kwargs) |
| | 328 | else: |
| | 329 | typ, data = self._imap.idle(timeout) |
| | 330 | self._checkok('idle', typ, data) |
| | 331 | return data[0] |
| | 332 | else: |
| | 333 | raise self.Error('The imaplib2 module is required to use IDLE') |
| | 334 | |
| 285 | 335 | def folder_status(self, folder, what=None): |
| 286 | 336 | """Requests the status from folder. |
| … |
… |
|
| 510 | 560 | else: |
| 511 | 561 | tag = self._imap._command('FETCH', msg_list, parts_list, modifiers_list) |
| 512 | | typ, data = self._imap._command_complete('FETCH', tag) |
| | 562 | if imaplib2: |
| | 563 | typ, data = self._imap._command_complete(tag, 'FETCH') |
| | 564 | else: |
| | 565 | typ, data = self._imap._command_complete('FETCH', tag) |
| 513 | 566 | self._checkok('fetch', typ, data) |
| 514 | 567 | typ, data = self._imap._untagged_response(typ, data, 'FETCH') |
-
|
r204
|
r208
|
|
| 12 | 12 | #TODO more exact error reporting |
| 13 | 13 | |
| 14 | | import imaplib |
| 15 | 14 | from datetime import datetime |
| 16 | 15 | from fixed_offset import FixedOffset |
| 17 | 16 | from response_lexer import TokenSource |
| 18 | 17 | |
| | 18 | try: |
| | 19 | import imaplib2 as imaplib |
| | 20 | except ImportError: |
| | 21 | imaplib2 = None |
| | 22 | import imaplib |
| 19 | 23 | |
| 20 | 24 | __all__ = ['parse_response', 'ParseError'] |