From: Menno Smits Date: Fri, 14 Jan 2011 12:54:59 +0000 (+0000) Subject: Added NAMESPACE support (#63) X-Git-Tag: 0.7~3^2~6 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=468982269b59aaf5bf8dc66bf0dcdf1f180e7cc0;p=imapclient.git Added NAMESPACE support (#63) --- diff --git a/NEWS b/NEWS index 47fa4b1..3cfb409 100644 --- 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 =============== diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 16aa8aa..fa239a9 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -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[^"]+)"?\s+' r'\((?P.*)\)$') @@ -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. diff --git a/livetest.py b/livetest.py index 1e373a4..459951a 100755 --- a/livetest.py +++ b/livetest.py @@ -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)