From: Menno Smits Date: Fri, 17 Aug 2012 20:58:12 +0000 (+0100) Subject: Support connection to the IMAP server using an external command (i.e. IMAP4_stream) X-Git-Tag: 0.9.1~6 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=00b1fa3eb434f0e41b9949b7f580a29d4ed21765;p=imapclient.git Support connection to the IMAP server using an external command (i.e. IMAP4_stream) --- diff --git a/NEWS b/NEWS index d4c07c3..804b4b1 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,18 @@ .. -*-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 diff --git a/THANKS b/THANKS index 0e99097..d92d524 100644 --- a/THANKS +++ b/THANKS @@ -32,6 +32,8 @@ Andrew Scheller Brian Neal Gmail labels methods. +Dave Eckhardt + Stream connection support. Bug Reports ------------ diff --git a/imapclient/config.py b/imapclient/config.py index f710b9c..6c8d12e 100644 --- a/imapclient/config.py +++ b/imapclient/config.py @@ -7,13 +7,16 @@ def parse_config_file(path): 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() @@ -24,11 +27,12 @@ def parse_config_file(path): 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'), @@ -38,12 +42,13 @@ def parse_config_file(path): ) 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 @@ -57,4 +62,3 @@ class Bunch(dict): def __setattr__(self, k, v): self[k] = v - diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 23f26e4..1b64054 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -65,6 +65,11 @@ class IMAPClient(object): 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 @@ -88,13 +93,19 @@ class IMAPClient(object): 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 @@ -106,6 +117,8 @@ class IMAPClient(object): 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) diff --git a/imapclient/test/test_init.py b/imapclient/test/test_init.py new file mode 100644 index 0000000..a88b873 --- /dev/null +++ b/imapclient/test/test_init.py @@ -0,0 +1,59 @@ +# 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)