]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Support connection to the IMAP server using an external command (i.e. IMAP4_stream)
authorMenno Smits <menno@freshfoo.com>
Fri, 17 Aug 2012 20:58:12 +0000 (21:58 +0100)
committerMenno Smits <menno@freshfoo.com>
Fri, 17 Aug 2012 20:58:12 +0000 (21:58 +0100)
NEWS
THANKS
imapclient/config.py
imapclient/imapclient.py
imapclient/test/test_init.py [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index d4c07c36430d1dc611aff9d18acd1b167415f779..804b4b1eb0ab1d5344bf7f80117c87b53dc4c8ec 100644 (file)
--- 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 0e99097de9065d7ffb82e3bbc5664b20156aed94..d92d52404f13baab57b809a5a707ee48de17247b 100644 (file)
--- a/THANKS
+++ b/THANKS
@@ -32,6 +32,8 @@ Andrew Scheller
 Brian Neal
     Gmail labels methods.
     
+Dave Eckhardt
+    Stream connection support.
 
 Bug Reports
 ------------
index f710b9c5815cc1159c105846237177dc1abc86b1..6c8d12e02615f87a313ce752dd40bf02bd07e9b7 100644 (file)
@@ -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
-
index 23f26e4c3724fde31db7df4e379aac612274e8b8..1b640540603ab10a5c84eafc19fd3a7b8b6e6bec 100644 (file)
@@ -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 (file)
index 0000000..a88b873
--- /dev/null
@@ -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)