.. -*-Mode: rst; -*-
+=============
+ Development
+=============
+
+Stream support [NEW]
+--------------------
+It is now possible to have IMAPClient run an external command to
+establish a connection to the IMAP server via a new *stream* keyword
+argument to the initialiser. This is useful for exotic connection or
+authentication setups. The *host* argument is used as the command to
+run.
+
+Thanks to Dave Eckhardt for the original patch.
+
=====
0.9
Brian Neal
Gmail labels methods.
+Dave Eckhardt
+ Stream connection support.
Bug Reports
------------
Used by livetest.py and interact.py
"""
- parser = SafeConfigParser(dict(ssl='false',
- username=None,
- password=None,
- oauth='false',
- oauth_url=None,
- oauth_token=None,
- oauth_token_secret=None))
+ parser = SafeConfigParser(dict(
+ username=None,
+ password=None,
+ ssl='false',
+ stream='false',
+ oauth='false',
+ oauth_token=None,
+ oauth_token_secret=None,
+ oauth_url=None,
+ ))
fh = file(path)
parser.readfp(fh)
fh.close()
port = parser.getint(section, 'port')
except NoOptionError:
port = None
-
+
return Bunch(
host=parser.get(section, 'host'),
port=port,
ssl=parser.getboolean(section, 'ssl'),
+ stream=parser.getboolean(section, 'stream'),
username=parser.get(section, 'username'),
password=parser.get(section, 'password'),
oauth=parser.getboolean(section, 'oauth'),
)
def create_client_from_config(conf):
- client = imapclient.IMAPClient(conf.host, port=conf.port, ssl=conf.ssl)
+ client = imapclient.IMAPClient(conf.host, port=conf.port,
+ ssl=conf.ssl, stream=conf.stream)
if conf.oauth:
client.oauth_login(conf.oauth_url,
conf.oauth_token,
conf.oauth_token_secret)
- else:
+ elif not conf.stream:
client.login(conf.username, conf.password)
return client
def __setattr__(self, k, v):
self[k] = v
-
If *ssl* is ``True`` an SSL connection will be made (defaults to
``False``).
+ If *stream* is ``True`` then *host* is used as the command to run
+ to establish a connection to the IMAP server (defaults to
+ ``False``). This is useful for exotic connection or authentication
+ setups.
+
The *normalise_times* attribute specifies whether datetimes
returned by ``fetch()`` are normalised to the local system time
and include no timezone information (native), or are datetimes
AbortError = imaplib.IMAP4.abort
ReadOnlyError = imaplib.IMAP4.readonly
- def __init__(self, host, port=None, use_uid=True, ssl=False):
- if port is None:
+ def __init__(self, host, port=None, use_uid=True, ssl=False, stream=False):
+ if stream:
+ if port is not None:
+ raise ValueError("can't set 'port' when 'stream' True")
+ if ssl:
+ raise ValueError("can't use 'ssl' when 'stream' is True")
+ elif port is None:
port = ssl and 993 or 143
self.host = host
self.port = port
self.ssl = ssl
+ self.stream = stream
self.use_uid = use_uid
self.folder_encode = True
self.log_file = sys.stderr
def _create_IMAP4(self):
# Create the IMAP instance in a separate method to make unit tests easier
+ if self.stream:
+ return imaplib.IMAP4_stream(self.host)
ImapClass = self.ssl and imaplib.IMAP4_SSL or imaplib.IMAP4
return ImapClass(self.host, self.port)
--- /dev/null
+# Copyright (c) 2012, Menno Smits
+# Released subject to the New BSD License
+# Please see http://en.wikipedia.org/wiki/BSD_licenses
+
+from imapclient.imapclient import IMAPClient
+from imapclient.test.mock import patch, sentinel
+from imapclient.test.util import unittest
+
+class TestInit(unittest.TestCase):
+
+ def setUp(self):
+ self.patcher = patch('imapclient.imapclient.imaplib')
+ self.imaplib = self.patcher.start()
+
+ def tearDown(self):
+ self.patcher.stop()
+
+ def test_plain(self):
+ self.imaplib.IMAP4.return_value = sentinel.IMAP4
+
+ imap = IMAPClient('1.2.3.4')
+
+ self.assertEqual(imap._imap, sentinel.IMAP4)
+ self.imaplib.IMAP4.assert_called_with('1.2.3.4', 143)
+ self.assertEqual(imap.host, '1.2.3.4')
+ self.assertEqual(imap.port, 143)
+ self.assertEqual(imap.ssl, False)
+ self.assertEqual(imap.stream, False)
+
+ def test_SSL(self):
+ self.imaplib.IMAP4_SSL.return_value = sentinel.IMAP4_SSL
+
+ imap = IMAPClient('1.2.3.4', ssl=True)
+
+ self.assertEqual(imap._imap, sentinel.IMAP4_SSL)
+ self.imaplib.IMAP4_SSL.assert_called_with('1.2.3.4', 993)
+ self.assertEqual(imap.host, '1.2.3.4')
+ self.assertEqual(imap.port, 993)
+ self.assertEqual(imap.ssl, True)
+ self.assertEqual(imap.stream, False)
+
+ def test_stream(self):
+ self.imaplib.IMAP4_stream.return_value = sentinel.IMAP4_stream
+
+ imap = IMAPClient('command', stream=True)
+
+ self.assertEqual(imap._imap, sentinel.IMAP4_stream)
+ self.imaplib.IMAP4_stream.assert_called_with('command')
+
+ self.assertEqual(imap.host, 'command')
+ self.assertEqual(imap.port, None)
+ self.assertEqual(imap.ssl, False)
+ self.assertEqual(imap.stream, True)
+
+ def test_ssl_and_stream_is_error(self):
+ self.assertRaises(ValueError, IMAPClient, 'command', ssl=True, stream=True)
+
+ def test_stream_and_port_is_error(self):
+ self.assertRaises(ValueError, IMAPClient, 'command', stream=True, port=123)