]> khamidou — GitHub mirrors - imapclient.git/commitdiff
select_folder() now returns the full parsed SELECT response (#24)
authorMenno Smits <menno@freshfoo.com>
Mon, 25 Jan 2010 13:26:30 +0000 (13:26 +0000)
committerMenno Smits <menno@freshfoo.com>
Mon, 25 Jan 2010 13:26:30 +0000 (13:26 +0000)
NEWS
imapclient/imapclient.py
imapclient/response_parser.py
livetest.py

diff --git a/NEWS b/NEWS
index 33bdad1c9bc643cef8fa20f21049af4575acdc6b..06d2351656d7edda61c51ce1478a8cc99dfd5241 100644 (file)
--- 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
 -------------
index 8efb1d2edb3482fdc4e18d9e9afcfe4d5db47594..06187342e130d372ef3711c9db1c8d845fca18fd 100644 (file)
@@ -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):
index 13d408c870267bda0d27ae0f5fa17a2c265e3fb0..b164c20453baeb042b7664dffd7122d97db6fdeb 100644 (file)
@@ -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)
index a8ba26aba05980a749609972ea79cb5f75ac850e..2e8335c84d39ecbffe65973a82c1f83eba1ad86c 100755 (executable)
@@ -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],