From: Johannes Heckel Date: Wed, 23 Mar 2011 00:00:36 +0000 (+0000) Subject: Added an idle function and changed script to use imaplib2 if available. X-Git-Tag: 0.8~34^2~21^2~1 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=94a9110a14dd272069eb683fdd533a453b8274b6;p=imapclient.git 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. --HG-- branch : idle --- diff --git a/imapclient/examples/idle_example.py b/imapclient/examples/idle_example.py new file mode 100644 index 0000000..eeadf35 --- /dev/null +++ b/imapclient/examples/idle_example.py @@ -0,0 +1,38 @@ +from imapclient import IMAPClient +import time + +HOST = 'imap.host.com' +USERNAME = 'someuser' +PASSWORD = 'password' +ssl = True + +server = IMAPClient(HOST, use_uid=True, ssl=ssl) + +server.login(USERNAME, PASSWORD) + +select_info = server.select_folder('INBOX') +print select_info +print + +idling = True + +def callback(resp, arg): + global idling + + print "Something happened, or timeout was reached" + print + print "Callback response:" + print resp + print + print "The callback function received arg: ", arg + print + idling = False + +server.idle(timeout=5, callback=callback, cb_arg="Hello future self!") +print "Began idling" + +while idling: + print "..still idling.." + time.sleep(1) + +print server.logout() \ No newline at end of file diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index b2dd6c6..ef1964b 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -3,12 +3,19 @@ # Please see http://en.wikipedia.org/wiki/BSD_licenses import re -import imaplib import response_lexer from operator import itemgetter import warnings -#imaplib.Debug = 5 +try: + import imaplib2 as imaplib + imaplib2 = True +except ImportError: + imaplib2 = False + import imaplib + +#imaplib.Debug = 5 + import imap_utf7 from fixed_offset import FixedOffset @@ -262,8 +269,20 @@ class IMAPClient(object): """ typ, data = self._imap.select(self._encode_folder_name(folder), readonly) self._checkok('select', typ, data) - return self._process_select_response(self._imap.untagged_responses) + if imaplib2: + untagged_responses = self._response_to_dict(self._imap.untagged_responses) + else: + untagged_responses = self._imap.untagged_responses + return self._process_select_response(untagged_responses) + + def _response_to_dict(self, resp): + dictresp = {} + + if len(resp) > 0: + for item in resp: + dictresp[item[0]] = item[1] + return dictresp def _process_select_response(self, resp): out = {} @@ -281,7 +300,38 @@ class IMAPClient(object): out[key] = value return out + def idle(self, timeout=None, **kwargs): + """Put server into IDLE mode. + IDLE mode will end when the server notifies of a change, the sever + reaches the timeout, or another IMAP4 command is scheduled. + + @param timeout: timeout for the IDLE command in seconds (default: 29 mins) + @param callback: Function to be called when IDLE mode ends. If a callback + 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). + """ + if imaplib2: + if 'callback' in kwargs: + callback = kwargs.pop('callback') + def _wrapped_callback(resp): + typ, data = resp[0] + self._checkok('idle', typ, data) + # if there's a cb_arg, pass it along + 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] + else: + raise self.Error('The imaplib2 module is required to use IDLE') + def folder_status(self, folder, what=None): """Requests the status from folder. @@ -509,7 +559,10 @@ 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) - typ, data = self._imap._command_complete('FETCH', tag) + if imaplib2: + typ, data = self._imap._command_complete(tag, 'FETCH') + else: + typ, data = self._imap._command_complete('FETCH', tag) self._checkok('fetch', typ, data) typ, data = self._imap._untagged_response(typ, data, 'FETCH') return parse_fetch_response(data) diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index 4cacca8..a21586d 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -11,11 +11,15 @@ Intially inspired by http://effbot.org/zone/simple-iterator-parser.htm #TODO more exact error reporting -import imaplib from datetime import datetime from fixed_offset import FixedOffset from response_lexer import TokenSource +try: + import imaplib2 as imaplib +except ImportError: + imaplib2 = None + import imaplib __all__ = ['parse_response', 'ParseError']