]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Added NAMESPACE support (#63)
authorMenno Smits <menno@freshfoo.com>
Fri, 14 Jan 2011 12:54:59 +0000 (12:54 +0000)
committerMenno Smits <menno@freshfoo.com>
Fri, 14 Jan 2011 12:54:59 +0000 (12:54 +0000)
NEWS
imapclient/imapclient.py
livetest.py

diff --git a/NEWS b/NEWS
index 47fa4b137ceae4c64ba80faa2f2a3d1ea85fb985..3cfb409074094c39c0169220c827ffe32a5619e6 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -50,6 +50,10 @@ IMAP account parameters are now read using a configuration file
 instead of command line arguments. See livetest-sample.ini for an
 example.
 
+Added NAMESPACE support (#63)
+-----------------------------
+See the namespace() method. 
+
 ===============
  Version 0.6.2
 ===============
index 16aa8aa03d1321ad086d663e5a8306ab7cf25c58..fa239a97becd72c51b63d595fd7d90ff36ed4242 100644 (file)
@@ -5,6 +5,7 @@
 import re
 import imaplib
 import response_lexer
+from operator import itemgetter
 #imaplib.Debug = 5
 
 import imap_utf7
@@ -29,6 +30,14 @@ FLAGGED = r'\Flagged'
 DRAFT = r'\Draft'
 RECENT = r'\Recent'         # This flag is read-only
 
+class Namespace(tuple):
+    def __new__(cls, personal, other, shared):
+        return tuple.__new__(cls, (personal, other, shared)) 
+
+    personal = property(itemgetter(0))
+    other = property(itemgetter(1))
+    shared = property(itemgetter(2))
+
 
 class IMAPClient(object):
     """
@@ -69,7 +78,6 @@ class IMAPClient(object):
     AbortError = imaplib.IMAP4.abort
     ReadOnlyError = imaplib.IMAP4.readonly
 
-    re_sep = re.compile('^\(\("[^"]*" "([^"]+)"\)\)')
     re_status = re.compile(r'^\s*"?(?P<folder>[^"]+)"?\s+'
                            r'\((?P<status_items>.*)\)$')
 
@@ -133,6 +141,22 @@ class IMAPClient(object):
         else:
             return False
 
+    def namespace(self):
+        """Return the namespace for the account as a (personal, other, shared) tuple.
+
+        Each element may be None if no namespace of that type exists,
+        or a sequence of (prefix, separator) pairs.
+
+        For convenience the tuple elements may be accessed
+        positionally or attributes named "personal", "other" and
+        "shared".
+
+        See RFC 2342 for more details.
+        """
+        typ, data = self._imap.namespace()
+        self._checkok('namespace', typ, data)
+        return Namespace(*parse_response(data))
+
     def get_folder_delimiter(self):
         """Determine the folder separator used by the IMAP server.
 
index 1e373a48ac14d3d250696c93d51b4605a5ebbed0..459951a16efd1f14d8d66dc9c8fb9e1718072f4a 100755 (executable)
@@ -104,6 +104,23 @@ def createLiveTestClass(host, username, password, port, ssl, use_uid):
                 self.assertTrue(self.client.has_capability(cap))
             self.assertFalse(self.client.has_capability('WONT EXIST'))
 
+        def test_namespace(self):
+            if not self.client.has_capability('NAMESPACE'):
+                return self.skipTest("Server doesn't support NAMESPACE")
+
+            def assertNoneOrTuple(val):
+                assert val is None or isinstance(val, tuple), \
+                       "unexpected namespace value %r" % val
+
+            ns = self.client.namespace()
+            self.assertEqual(len(ns), 3)
+            assertNoneOrTuple(ns.personal)
+            assertNoneOrTuple(ns.other)
+            assertNoneOrTuple(ns.shared)
+            self.assertEqual(ns.personal, ns[0])
+            self.assertEqual(ns.other, ns[1])
+            self.assertEqual(ns.shared, ns[2])
+
         def test_select_and_close(self):
             resp = self.client.select_folder('INBOX')
             self.assertIsInstance(resp['EXISTS'], int)