]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Updated documentation and examples wrt IDLE support
authorMenno Smits <menno@freshfoo.com>
Sun, 17 Apr 2011 10:33:16 +0000 (11:33 +0100)
committerMenno Smits <menno@freshfoo.com>
Sun, 17 Apr 2011 10:33:16 +0000 (11:33 +0100)
NEWS
imapclient/examples/idle_example.py
imapclient/imapclient.py

diff --git a/NEWS b/NEWS
index 80613793995f294a5b134f2776c8a32b761c3cff..019d3dff7ca603ee7344cc51bd2406d903dd0e35 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,12 @@ method. This requires the 3rd party oauth2 package is
 installed. Thanks to Johannes Heckel for contributing the patch to
 this.
 
+IDLE Support (#50) [NEW]
+------------------------
+The IDLE extension is now supported through the new idle(),
+idle_check() and idle_done() methods. See the example in
+imapclient/examples/idle_example.py.
+
 Small Bug Fixes
 ---------------
 getacl() was using wrong lexing class (#85) (Thanks josephhh)
index eeadf35dcac2654d0b779b07fc3f6ffa69372f3a..abb91294abed40c8251cb3348077241d5e570a61 100644 (file)
@@ -1,5 +1,7 @@
+# This example is a lot more interesting if you have an active client\r
+# connected to the same IMAP account!\r
+\r
 from imapclient import IMAPClient\r
-import time\r
 \r
 HOST = 'imap.host.com'\r
 USERNAME = 'someuser'\r
@@ -7,32 +9,19 @@ PASSWORD = 'password'
 ssl = True\r
 \r
 server = IMAPClient(HOST, use_uid=True, ssl=ssl)\r
-\r
 server.login(USERNAME, PASSWORD)\r
+server.select_folder('INBOX')\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
+# Start IDLE mode\r
+server.idle()     \r
 \r
-server.idle(timeout=5, callback=callback, cb_arg="Hello future self!")\r
-print "Began idling"\r
+# Wait for up to 30 seconds for an IDLE response\r
+responses = server.idle_check(timeout=30)\r
+print responses\r
 \r
-while idling:\r
-    print "..still idling.."\r
-    time.sleep(1)\r
+# Come out of IDLE mode\r
+text, responses = server.idle_done()\r
+print 'IDLE done. Server said %r' % text\r
+print 'Final responses: ', responses\r
     \r
-print server.logout()
\ No newline at end of file
+print server.logout()\r
index 526e84603a3c1e433ff9b5c1d7004c29601eba18..174a14b92a9a0a54821d77cb6dbed4fa821639ba 100644 (file)
@@ -315,17 +315,17 @@ class IMAPClient(object):
     def idle(self):
         """Put server into IDLE mode.
 
-        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)
-        @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).
+        In this mode the server will return unsolicited responses
+        about changes to the selected mailbox.
+
+        This method returns immediately. Use idle_check() to check for
+        IDLE responses and idle_done() to stop IDLE mode.
 
-        #XXX update documentation
-        #XXX what about finding out what the IDLE data was?
+        Note: Any other commmands issued while the server is in IDLE
+        mode will fail.
+
+        See RFC 2177 for more information about the IDLE extension.
+        http://tools.ietf.org/html/rfc2177
         """
         self._idle_tag = self._imap._command('IDLE')
         resp = self._imap._get_response()
@@ -333,19 +333,43 @@ class IMAPClient(object):
             raise self.Error('Unexpected IDLE response: %s' % resp)
 
     def idle_check(self, timeout=None):
-        """XXX
+        """Check for any IDLE responses sent by the server.
+
+        This method should only be called if the server is in IDLE
+        mode (see idle()).
+
+        By default, this method will block until an IDLE response is
+        received. If timeout is provided, the call will block for at
+        most this number of seconds (approximately) while waiting for
+        an IDLE response.
+
+        The return value is a list of received IDLE responses. These
+        will be parsed with values converted to appropriate types. For
+        example:
+
+        [(1, 'EXISTS'),
+         ('OK', 'Still here'), 
+         (1, 'FETCH', ('FLAGS', ('\NotJunk',))), 
+         (0, 'EXPUNGE')]
+
+        An attempt is made to group responses that were sent together
+        by the server by waiting for a little longer after the first
+        IDLE response is received.
         """
         timeouts = 0
         resps = []
         start_t = time.time()
-        def done():
-            if timeout is None:
-                if resps and timeouts > 1:
-                    return True
-            else:
-                if time.time() - start_t >= timeout:
-                    return True
-            return False
+
+        def blocking_done():
+            # Wait for one timed out read to try group IDLE responses
+            # that were sent together
+            return resps and timeouts > 1
+
+        if timeout is None:
+            done = blocking_done
+        else:
+            def done():
+                return blocking_done() or (time.time() - start_t >= timeout)
         
         # make the socket non-blocking so the timeout can be
         # implemented for this call
@@ -363,10 +387,19 @@ class IMAPClient(object):
             self._imap.sock.settimeout(None)
 
     def idle_done(self):
-        """XXX
+        """Take the server out of IDLE mode.
+
+        This method should only be called if the server is in IDLE mode.
+
+        The return value is (command_text, idle_responses) where
+        command_text is the text sent by the server when the IDLE
+        command finished (eg. 'Idle terminated') and idle_responses is
+        a list of parsed idle responses received since the last call
+        to idle_check() (if any). These are returned in parsed form as
+        per idle_check().
         """
         self._imap.send('DONE\r\n')
-        # Slurp up any remaining IDLE data until the IDLE is done
+        # Slurp up any remaining IDLE responses until the IDLE is done
         tag = self._idle_tag
         tagged_commands = self._imap.tagged_commands
         resps = []