]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Added an idle function and changed script to use imaplib2 if available.
authorJohannes Heckel <stone.pixel@gmail.com>
Wed, 23 Mar 2011 00:00:36 +0000 (00:00 +0000)
committerJohannes Heckel <stone.pixel@gmail.com>
Wed, 23 Mar 2011 00:00:36 +0000 (00:00 +0000)
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

imapclient/examples/idle_example.py [new file with mode: 0644]
imapclient/imapclient.py
imapclient/response_parser.py

diff --git a/imapclient/examples/idle_example.py b/imapclient/examples/idle_example.py
new file mode 100644 (file)
index 0000000..eeadf35
--- /dev/null
@@ -0,0 +1,38 @@
+from imapclient import IMAPClient\r
+import time\r
+\r
+HOST = 'imap.host.com'\r
+USERNAME = 'someuser'\r
+PASSWORD = 'password'\r
+ssl = True\r
+\r
+server = IMAPClient(HOST, use_uid=True, ssl=ssl)\r
+\r
+server.login(USERNAME, PASSWORD)\r
+\r
+select_info = server.select_folder('INBOX')\r
+print select_info\r
+print\r
+\r
+idling = True\r
+\r
+def callback(resp, arg):\r
+    global idling\r
+    \r
+    print "Something happened, or timeout was reached"\r
+    print\r
+    print "Callback response:"\r
+    print resp\r
+    print\r
+    print "The callback function received arg: ", arg\r
+    print\r
+    idling = False\r
+\r
+server.idle(timeout=5, callback=callback, cb_arg="Hello future self!")\r
+print "Began idling"\r
+\r
+while idling:\r
+    print "..still idling.."\r
+    time.sleep(1)\r
+    \r
+print server.logout()
\ No newline at end of file
index b2dd6c6971d397654be66cee6e83949a5ab81aed..ef1964b7786cdc0ee4a576dc382be27ceb59bd2b 100644 (file)
@@ -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)
index 4cacca8e82af9184db3fcadb1653cac044b02cd4..a21586dc173f86fcf6652729ee9bee555002e8cc 100644 (file)
@@ -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']