Changeset 126:4319998fc9b5

Show
Ignore:
Timestamp:
25/01/10 13:26:30 (2 years ago)
Author:
Menno Smits <menno@…>
Branch:
default
Message:

select_folder() now returns the full parsed SELECT response (#24)

Files:
4 modified

Legend:

Unmodified
Added
Removed
  • NEWS

    r125 r126  
    55(eg. BODYSTRUCTURE and ENVELOPE). This change also fixes various 
    66problems when interacting with Gmail and MS Exchange (#1). 
     7 
     8[API CHANGE] select_folder now returns a dictionary with the full 
     9SELECT command response instead of just the message count (#24). 
    710 
    811 
  • imapclient/imapclient.py

    r121 r126  
    1515    'RECENT'] 
    1616 
    17 from response_parser import parse_fetch_response 
     17from response_parser import parse_response, parse_fetch_response 
     18 
    1819 
    1920# System flags 
     
    205206 
    206207        @param folder: The folder name. 
    207         @return: Number of messages in the folder. 
    208         @rtype: long int 
     208        @return: A dictionary containing the SELECT response 
     209          values. At least the EXISTS, FLAGS and RECENT keys are 
     210          guaranteed to exist. Example: 
     211            {'EXISTS': 3, 
     212             'FLAGS': ('\\Answered', '\\Flagged', '\\Deleted', ... ), 
     213             'RECENT': 0, 
     214             'PERMANENTFLAGS': ('\\Answered', '\\Flagged', '\\Deleted', ... ), 
     215             'READ-WRITE': True, 
     216             'UIDNEXT': 11, 
     217             'UIDVALIDITY': 1239278212} 
    209218        """ 
    210219        typ, data = self._imap.select(self._encode_folder_name(folder)) 
    211220        self._checkok('select', typ, data) 
    212         return long(data[0]) 
     221        return self._process_select_response(self._imap.untagged_responses) 
     222 
     223 
     224    def _process_select_response(self, resp): 
     225        out = {} 
     226        for key, value in resp.iteritems(): 
     227            key = key.upper() 
     228            if key == 'OK': 
     229                continue 
     230            elif key in ('EXISTS', 'RECENT', 'UIDNEXT', 'UIDVALIDITY'): 
     231                value = int(value[0]) 
     232            elif key in ('FLAGS', 'PERMANENTFLAGS'): 
     233                value = parse_response(value)[0] 
     234            elif key == 'READ-WRITE': 
     235                value = True 
     236                                  
     237            out[key] = value 
     238        return out 
    213239 
    214240 
  • imapclient/response_parser.py

    r123 r126  
    162162 
    163163    CTRL_CHARS = ''.join([chr(ch) for ch in range(32)]) 
    164     ATOM_SPECIALS = r'()%*"' + CTRL_CHARS 
     164    SPECIALS = r'()%"' + CTRL_CHARS 
    165165    ALL_CHARS = [chr(ch) for ch in range(256)] 
    166     ATOM_NON_SPECIALS = [ch for ch in ALL_CHARS if ch not in ATOM_SPECIALS] 
     166    NON_SPECIALS = [ch for ch in ALL_CHARS if ch not in SPECIALS] 
    167167 
    168168    def __init__(self, resp_chunks): 
     
    174174        self.lex.quotes = '"' 
    175175        self.lex.commenters = '' 
    176         self.lex.wordchars = self.ATOM_NON_SPECIALS 
     176        self.lex.wordchars = self.NON_SPECIALS 
    177177 
    178178    def __iter__(self): 
  • livetest.py

    r121 r126  
    3737 
    3838def test_select_and_close(client): 
    39     num_msgs = client.select_folder('INBOX') 
    40     assert isinstance(num_msgs, long) 
    41     assert num_msgs >= 0 
     39    resp = client.select_folder('INBOX') 
     40    assert isinstance(resp['EXISTS'], int) 
     41    assert resp['EXISTS'] > 1 
     42    assert isinstance(resp['RECENT'], int) 
     43    assert isinstance(resp['FLAGS'], tuple) 
     44    assert len(resp['FLAGS']) > 1 
    4245    client.close_folder() 
    4346 
     
    141144 
    142145    # Retrieve the just added message and check that all looks well 
    143     num_msgs = client.select_folder('INBOX') 
    144     assert num_msgs == 1 
     146    assert client.select_folder('INBOX')['EXISTS'] == 1 
    145147 
    146148    resp = client.fetch(