From: Menno Smits Date: Sat, 16 Apr 2011 16:21:28 +0000 (+0100) Subject: Added livetest for IDLE support and reworked how the IDLE callback function is called X-Git-Tag: 0.8~34^2~16 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=ed25bc4c6a75565567d1ccca56299f1c462ee182;p=imapclient.git Added livetest for IDLE support and reworked how the IDLE callback function is called --- diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 2f130f7..acc286e 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -9,9 +9,9 @@ import warnings try: import imaplib2 as imaplib - imaplib2 = True + using_imaplib2 = True except ImportError: - imaplib2 = False + using_imaplib2 = False import imaplib #imaplib.Debug = 5 @@ -26,7 +26,7 @@ from fixed_offset import FixedOffset __all__ = ['IMAPClient', 'DELETED', 'SEEN', 'ANSWERED', 'FLAGGED', 'DRAFT', - 'RECENT'] + 'RECENT', 'using_imaplib2'] from response_parser import parse_response, parse_fetch_response @@ -294,7 +294,7 @@ class IMAPClient(object): """ typ, data = self._imap.select(self._encode_folder_name(folder), readonly) self._checkok('select', typ, data) - if imaplib2: + if using_imaplib2: untagged_responses = self._response_to_dict(self._imap.untagged_responses) else: untagged_responses = self._imap.untagged_responses @@ -325,10 +325,10 @@ class IMAPClient(object): out[key] = value return out - def idle(self, timeout=None, **kwargs): + def idle(self, timeout=None, callback=None, **kwargs): """Put server into IDLE mode. - IDLE mode will end when the server notifies of a change, the sever + IDLE mode will end when the server notifies of a change, the server reaches the timeout, or another IMAP4 command is scheduled. @param timeout: timeout for the IDLE command in seconds (default: 29 mins) @@ -336,26 +336,33 @@ class IMAPClient(object): is supplied the idle function will be asynchronous, returning immediately. @param cb_arg: An optional argument that will be passed to the callback function. @return: Server response. (None if callback is specified). + + #XXX update documentation + #XXX what about finding out what the IDLE data was? """ - if imaplib2: - if 'callback' in kwargs: - callback = kwargs.pop('callback') - def _wrapped_callback(resp): + if not using_imaplib2: + raise self.Error('The imaplib2 module is required to use IDLE') + + if callback: + cb_arg_was_given = 'cb_arg' in kwargs + def _wrapped_cb(resp): + if resp[0]: + success = True typ, data = resp[0] - self._checkok('idle', typ, data) - # if there's a cb_arg, pass it along + response = (typ, data[0]) + else: + success = False + response = resp[2] + if cb_arg_was_given: cb_arg = resp[1] - if cb_arg: - callback(data[0], cb_arg) - else: - callback(data[0]) - self._imap.idle(timeout, callback=_wrapped_callback, **kwargs) - else: - typ, data = self._imap.idle(timeout) - self._checkok('idle', typ, data) - return data[0] + callback(success, response, cb_arg) + else: + callback(success, response) + self._imap.idle(timeout, callback=_wrapped_cb, cb_arg=kwargs.get('cb_arg')) else: - raise self.Error('The imaplib2 module is required to use IDLE') + typ, data = self._imap.idle(timeout) + self._checkok('idle', typ, data) + return data[0] def folder_status(self, folder, what=None): """Requests the status from folder. @@ -584,7 +591,7 @@ class IMAPClient(object): tag = self._imap._command('UID', 'FETCH', msg_list, parts_list, modifiers_list) else: tag = self._imap._command('FETCH', msg_list, parts_list, modifiers_list) - if imaplib2: + if using_imaplib2: typ, data = self._imap._command_complete(tag, 'FETCH') else: typ, data = self._imap._command_complete('FETCH', tag) diff --git a/livetest.py b/livetest.py index 72bc4ec..b683167 100755 --- a/livetest.py +++ b/livetest.py @@ -8,6 +8,8 @@ import imp import os import sys +import time +import threading from datetime import datetime from ConfigParser import SafeConfigParser, NoOptionError @@ -448,6 +450,39 @@ def createLiveTestClass(conf, use_uid): a = lower_if_str(a) e = lower_if_str(e) self.assertEqual(a, e) + + def test_idle(self): + if not imapclient.using_imaplib2: + return self.skipTest("imaplib2 is not installed") + + idle_event = threading.Event() + cb_data = {} + def cb(success, response, cb_arg): + idle_event.set() + cb_data['success'] = success + cb_data['idle_response'] = response + cb_data['cb_arg'] = cb_arg + + # Start main connection idling + self.client.select_folder('INBOX') + self.client.idle(timeout=20, callback=cb, cb_arg='foo') + + # Start a new connection and upload a new message + client2 = create_client_from_config(conf) + client2.select_folder('INBOX') + client2.append('INBOX', SIMPLE_MESSAGE) + client2.logout() + + idle_event.wait(15) # Wait for IDLE callback to trigger + + self.assertTrue(idle_event.is_set()) + self.assertTrue(cb_data['success']) + idle_status, idle_text = cb_data['idle_response'] + self.assertEqual(idle_status, 'OK') + # Can't be too specific as different servers return different text + self.assertIn('idle', idle_text.lower()) + self.assertEqual(cb_data['cb_arg'], 'foo') + return LiveTest