From e26ecd32f42246759909ef900fb6c0ea5189faf7 Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Mon, 25 Jan 2010 13:26:30 +0000 Subject: [PATCH] select_folder() now returns the full parsed SELECT response (#24) --- NEWS | 3 +++ imapclient/imapclient.py | 34 ++++++++++++++++++++++++++++++---- imapclient/response_parser.py | 6 +++--- livetest.py | 12 +++++++----- 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/NEWS b/NEWS index 33bdad1..06d2351 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,9 @@ various bugs that surfaced when dealing with more complex responses (eg. BODYSTRUCTURE and ENVELOPE). This change also fixes various problems when interacting with Gmail and MS Exchange (#1). +[API CHANGE] select_folder now returns a dictionary with the full +SELECT command response instead of just the message count (#24). + Version 0.5.2 ------------- diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 8efb1d2..0618734 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -14,7 +14,8 @@ from fixed_offset import FixedOffset __all__ = ['IMAPClient', 'DELETED', 'SEEN', 'ANSWERED', 'FLAGGED', 'DRAFT', 'RECENT'] -from response_parser import parse_fetch_response +from response_parser import parse_response, parse_fetch_response + # System flags DELETED = r'\Deleted' @@ -204,12 +205,37 @@ class IMAPClient(object): such as search and fetch will act on the selected folder. @param folder: The folder name. - @return: Number of messages in the folder. - @rtype: long int + @return: A dictionary containing the SELECT response + values. At least the EXISTS, FLAGS and RECENT keys are + guaranteed to exist. Example: + {'EXISTS': 3, + 'FLAGS': ('\\Answered', '\\Flagged', '\\Deleted', ... ), + 'RECENT': 0, + 'PERMANENTFLAGS': ('\\Answered', '\\Flagged', '\\Deleted', ... ), + 'READ-WRITE': True, + 'UIDNEXT': 11, + 'UIDVALIDITY': 1239278212} """ typ, data = self._imap.select(self._encode_folder_name(folder)) self._checkok('select', typ, data) - return long(data[0]) + return self._process_select_response(self._imap.untagged_responses) + + + def _process_select_response(self, resp): + out = {} + for key, value in resp.iteritems(): + key = key.upper() + if key == 'OK': + continue + elif key in ('EXISTS', 'RECENT', 'UIDNEXT', 'UIDVALIDITY'): + value = int(value[0]) + elif key in ('FLAGS', 'PERMANENTFLAGS'): + value = parse_response(value)[0] + elif key == 'READ-WRITE': + value = True + + out[key] = value + return out def folder_status(self, folder, what=None): diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index 13d408c..b164c20 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -161,9 +161,9 @@ class LiteralHandlingReader: class ResponseTokeniser(object): CTRL_CHARS = ''.join([chr(ch) for ch in range(32)]) - ATOM_SPECIALS = r'()%*"' + CTRL_CHARS + SPECIALS = r'()%"' + CTRL_CHARS ALL_CHARS = [chr(ch) for ch in range(256)] - ATOM_NON_SPECIALS = [ch for ch in ALL_CHARS if ch not in ATOM_SPECIALS] + NON_SPECIALS = [ch for ch in ALL_CHARS if ch not in SPECIALS] def __init__(self, resp_chunks): # initialize the lexer with all the chunks we read. @@ -173,7 +173,7 @@ class ResponseTokeniser(object): self.lex.quotes = '"' self.lex.commenters = '' - self.lex.wordchars = self.ATOM_NON_SPECIALS + self.lex.wordchars = self.NON_SPECIALS def __iter__(self): return iter(self.lex) diff --git a/livetest.py b/livetest.py index a8ba26a..2e8335c 100755 --- a/livetest.py +++ b/livetest.py @@ -36,9 +36,12 @@ def test_list_folders(client): def test_select_and_close(client): - num_msgs = client.select_folder('INBOX') - assert isinstance(num_msgs, long) - assert num_msgs >= 0 + resp = client.select_folder('INBOX') + assert isinstance(resp['EXISTS'], int) + assert resp['EXISTS'] > 1 + assert isinstance(resp['RECENT'], int) + assert isinstance(resp['FLAGS'], tuple) + assert len(resp['FLAGS']) > 1 client.close_folder() @@ -140,8 +143,7 @@ def test_append(client): assert isinstance(resp, str) # Retrieve the just added message and check that all looks well - num_msgs = client.select_folder('INBOX') - assert num_msgs == 1 + assert client.select_folder('INBOX')['EXISTS'] == 1 resp = client.fetch( client.search()[0], -- 2.43.0