Changeset 208:3eeca2dda55d

Show
Ignore:
Timestamp:
03/23/11 00:00:36 (14 months ago)
Author:
Johannes Heckel <stone.pixel@…>
Branch:
idle
Children:
209:80f530da50d7, 212:45fab16c5bf6
Message:

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.

Location:
imapclient
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • imapclient/imapclient.py

    r204 r208  
    44 
    55import re 
    6 import imaplib 
    76import response_lexer 
    87from operator import itemgetter 
    98import warnings 
     9 
     10try: 
     11    import imaplib2 as imaplib 
     12    imaplib2 = True 
     13except ImportError: 
     14    imaplib2 = False 
     15    import imaplib 
     16     
    1017#imaplib.Debug = 5 
    11  
     18     
    1219import imap_utf7 
    1320from fixed_offset import FixedOffset 
     
    263270        typ, data = self._imap.select(self._encode_folder_name(folder), readonly) 
    264271        self._checkok('select', typ, data) 
    265         return self._process_select_response(self._imap.untagged_responses) 
    266  
     272        if imaplib2: 
     273            untagged_responses = self._response_to_dict(self._imap.untagged_responses) 
     274        else: 
     275            untagged_responses = self._imap.untagged_responses 
     276        return self._process_select_response(untagged_responses) 
     277 
     278    def _response_to_dict(self, resp): 
     279        dictresp = {} 
     280 
     281        if len(resp) > 0: 
     282            for item in resp: 
     283                dictresp[item[0]] = item[1] 
     284 
     285        return dictresp 
    267286 
    268287    def _process_select_response(self, resp): 
     
    282301        return out 
    283302 
    284  
     303    def idle(self, timeout=None, **kwargs): 
     304        """Put server into IDLE mode. 
     305 
     306        IDLE mode will end when the server notifies of a change, the sever 
     307        reaches the timeout, or another IMAP4 command is scheduled. 
     308         
     309        @param timeout: timeout for the IDLE command in seconds (default: 29 mins) 
     310        @param callback: Function to be called when IDLE mode ends. If a callback 
     311            is supplied the idle function will be asynchronous, returning immediately. 
     312        @param cb_arg: An optional argument that will be passed to the callback function. 
     313        @return: Server response. (None if callback is specified). 
     314        """ 
     315        if imaplib2: 
     316            if 'callback' in kwargs: 
     317                callback = kwargs.pop('callback') 
     318                def _wrapped_callback(resp): 
     319                    typ, data = resp[0] 
     320                    self._checkok('idle', typ, data) 
     321                    # if there's a cb_arg, pass it along 
     322                    cb_arg = resp[1] 
     323                    if cb_arg: 
     324                        callback(data[0], cb_arg) 
     325                    else: 
     326                        callback(data[0]) 
     327                self._imap.idle(timeout, callback=_wrapped_callback, **kwargs) 
     328            else: 
     329                typ, data = self._imap.idle(timeout) 
     330                self._checkok('idle', typ, data) 
     331                return data[0] 
     332        else: 
     333            raise self.Error('The imaplib2 module is required to use IDLE') 
     334                 
    285335    def folder_status(self, folder, what=None): 
    286336        """Requests the status from folder. 
     
    510560        else: 
    511561            tag = self._imap._command('FETCH', msg_list, parts_list, modifiers_list) 
    512         typ, data = self._imap._command_complete('FETCH', tag) 
     562        if imaplib2: 
     563            typ, data = self._imap._command_complete(tag, 'FETCH') 
     564        else: 
     565            typ, data = self._imap._command_complete('FETCH', tag) 
    513566        self._checkok('fetch', typ, data) 
    514567        typ, data = self._imap._untagged_response(typ, data, 'FETCH') 
  • imapclient/response_parser.py

    r204 r208  
    1212#TODO more exact error reporting 
    1313 
    14 import imaplib 
    1514from datetime import datetime 
    1615from fixed_offset import FixedOffset 
    1716from response_lexer import TokenSource 
    1817 
     18try: 
     19    import imaplib2 as imaplib 
     20except ImportError: 
     21    imaplib2 = None 
     22    import imaplib 
    1923 
    2024__all__ = ['parse_response', 'ParseError']