import re
import imaplib
import response_lexer
+from operator import itemgetter
#imaplib.Debug = 5
import imap_utf7
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):
"""
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>.*)\)$')
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.
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)