Show
Ignore:
Timestamp:
16/04/11 17:21:28 (13 months ago)
Author:
Menno Smits <menno@…>
Branch:
default
Message:

Added livetest for IDLE support and reworked how the IDLE callback function is called

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • imapclient/imapclient.py

    r216 r218  
    1010try: 
    1111    import imaplib2 as imaplib 
    12     imaplib2 = True 
     12    using_imaplib2 = True 
    1313except ImportError: 
    14     imaplib2 = False 
     14    using_imaplib2 = False 
    1515    import imaplib 
    1616     
     
    2727 
    2828__all__ = ['IMAPClient', 'DELETED', 'SEEN', 'ANSWERED', 'FLAGGED', 'DRAFT', 
    29     'RECENT'] 
     29    'RECENT', 'using_imaplib2'] 
    3030 
    3131from response_parser import parse_response, parse_fetch_response 
     
    295295        typ, data = self._imap.select(self._encode_folder_name(folder), readonly) 
    296296        self._checkok('select', typ, data) 
    297         if imaplib2: 
     297        if using_imaplib2: 
    298298            untagged_responses = self._response_to_dict(self._imap.untagged_responses) 
    299299        else: 
     
    326326        return out 
    327327 
    328     def idle(self, timeout=None, **kwargs): 
     328    def idle(self, timeout=None, callback=None, **kwargs): 
    329329        """Put server into IDLE mode. 
    330330 
    331         IDLE mode will end when the server notifies of a change, the sever 
     331        IDLE mode will end when the server notifies of a change, the server 
    332332        reaches the timeout, or another IMAP4 command is scheduled. 
    333333         
     
    337337        @param cb_arg: An optional argument that will be passed to the callback function. 
    338338        @return: Server response. (None if callback is specified). 
    339         """ 
    340         if imaplib2: 
    341             if 'callback' in kwargs: 
    342                 callback = kwargs.pop('callback') 
    343                 def _wrapped_callback(resp): 
     339 
     340        #XXX update documentation 
     341        #XXX what about finding out what the IDLE data was? 
     342        """ 
     343        if not using_imaplib2: 
     344            raise self.Error('The imaplib2 module is required to use IDLE') 
     345 
     346        if callback: 
     347            cb_arg_was_given = 'cb_arg' in kwargs 
     348            def _wrapped_cb(resp): 
     349                if resp[0]: 
     350                    success = True 
    344351                    typ, data = resp[0] 
    345                     self._checkok('idle', typ, data) 
    346                     # if there's a cb_arg, pass it along 
     352                    response = (typ, data[0]) 
     353                else: 
     354                    success = False 
     355                    response = resp[2] 
     356                if cb_arg_was_given: 
    347357                    cb_arg = resp[1] 
    348                     if cb_arg: 
    349                         callback(data[0], cb_arg) 
    350                     else: 
    351                         callback(data[0]) 
    352                 self._imap.idle(timeout, callback=_wrapped_callback, **kwargs) 
    353             else: 
    354                 typ, data = self._imap.idle(timeout) 
    355                 self._checkok('idle', typ, data) 
    356                 return data[0] 
    357         else: 
    358             raise self.Error('The imaplib2 module is required to use IDLE') 
     358                    callback(success, response, cb_arg) 
     359                else: 
     360                    callback(success, response) 
     361            self._imap.idle(timeout, callback=_wrapped_cb, cb_arg=kwargs.get('cb_arg')) 
     362        else: 
     363            typ, data = self._imap.idle(timeout) 
     364            self._checkok('idle', typ, data) 
     365            return data[0] 
    359366                 
    360367    def folder_status(self, folder, what=None): 
     
    585592        else: 
    586593            tag = self._imap._command('FETCH', msg_list, parts_list, modifiers_list) 
    587         if imaplib2: 
     594        if using_imaplib2: 
    588595            typ, data = self._imap._command_complete(tag, 'FETCH') 
    589596        else: