]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Expose debugging functionality (#90)
authorMenno Smits <menno@freshfoo.com>
Tue, 31 May 2011 12:32:19 +0000 (13:32 +0100)
committerMenno Smits <menno@freshfoo.com>
Tue, 31 May 2011 12:32:19 +0000 (13:32 +0100)
As part of this, moved IMAP4 creation to a separate method to make
unit testing cleaner and easier.

NEWS
imapclient/imapclient.py
imapclient/test/test_IMAPClient.py
imapclient/test/testable_imapclient.py

diff --git a/NEWS b/NEWS
index 0b4dbf6f545a8418ade421f17388a9f96126f0d1..70869cf64e6380ccb7737341a24f48a8a620b140 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -20,6 +20,7 @@ imapclient/examples/idle_example.py.
 Minor Features
 --------------
 interact.py can now read livetest.py INI files (#66)
+Added "debug" propety and setting of a log file (#90)
 
 Small Bug Fixes
 ---------------
index c1a25d80b6ac71dae241252c0fa6d932c74d1ee7..ab5fd3da36dfdcfc647058d2e91b3b379653cca8 100644 (file)
@@ -3,14 +3,16 @@
 # Please see http://en.wikipedia.org/wiki/BSD_licenses
 
 import re
-import response_lexer
-from operator import itemgetter
 import select
 import socket
+import sys
 import warnings
+from datetime import datetime
+from operator import itemgetter
 
 import imaplib
-#imaplib.Debug = 5
+import response_lexer
+
     
 try:
     import oauth2
@@ -101,23 +103,25 @@ class IMAPClient(object):
         @param use_uid: Should message UIDs be used (default is True).
         @param ssl: Make an SSL connection (default is False)
         """
-        if ssl:
-            ImapClass = imaplib.IMAP4_SSL
-            default_port = 993
-        else:
-            ImapClass = imaplib.IMAP4
-            default_port = 143
-
         if port is None:
-            port = default_port
+            port = ssl and 993 or 143
 
-        self.use_uid = use_uid
+        self.host = host
+        self.port = port
         self.ssl = ssl
+        self.use_uid = use_uid
         self.folder_encode = True
-        self._imap = ImapClass(host, port)
+        self.log_file = sys.stderr
+
+        self._imap = self._create_IMAP4()
+        self._imap._mesg = self._log    # patch in custom debug log method
         self._idle_tag = None
 
-   
+    def _create_IMAP4(self):
+        # Create the IMAP instance in a separate method to make unit tests easier
+        ImapClass = self.ssl and imaplib.IMAP4_SSL or imaplib.IMAP4
+        return ImapClass(self.host, self.port)
+
     def login(self, username, password):
         """Perform a simple login
         """
@@ -780,6 +784,25 @@ class IMAPClient(object):
         # quoting. A hack but it works.
         return _quote_arg(name)
 
+    def __debug_get(self):
+        return self._imap.debug
+
+    def __debug_set(self, level):
+        if level is True:
+            level = 4
+        elif level is False:
+            level = 0
+        self._imap.debug = level
+
+    debug = property(__debug_get, __debug_set,
+                     doc="Set debug level. Integers from 0 to 5 or, True and " \
+                         "False can be used. True specifies debug level 4. " \
+                         "Debug output goes to stderr.")
+
+    def _log(self, text):
+        self.log_file.write('%s %s\n' % (datetime.now().strftime('%M:%S.%f'), text))
+        self.log_file.flush()
+        
 
 def messages_to_str(messages):
     """Convert a sequence of messages ids or a single message id into an
index eb99909a6a5d87978c4cf68d7e124d530953b877..b04dff76b9df5badb675eee9e18f2d88849df870 100644 (file)
@@ -4,8 +4,10 @@
 
 import itertools
 import socket
+import sys
 import time
 from datetime import datetime
+from StringIO import StringIO
 from imapclient.fixed_offset import FixedOffset
 from imapclient.imapclient import datetime_to_imap
 from imapclient.test.mock import patch, sentinel, Mock
@@ -245,5 +247,24 @@ class TestIdle(IMAPClientTest):
         self.assertEqual(text, 'Idle done')
         self.assertListEqual([(99, 'EXISTS')], responses)
 
+
+class TestDebugLogging(IMAPClientTest):
+
+    def test_default_is_stderr(self):
+        self.assertIs(self.client.log_file, sys.stderr)
+
+    def test_IMAP_is_patched(self):
+        log = StringIO()
+        self.client.log_file = log
+
+        self.client._log('one')
+        self.client._imap._mesg('two')
+
+        output = log.getvalue()
+        self.assertIn('one', output)
+        self.assertIn('two', output)
+        
+
+
 if __name__ == '__main__':
     unittest.main()
index 92cd8ecde6decd6272a10f6a0379f819628c6ea7..0108135ef8cb636979eb50963cc1d8213f9c436f 100644 (file)
@@ -8,7 +8,7 @@ from imapclient.test.mock import Mock
 class TestableIMAPClient(IMAPClient):
 
     def __init__(self):
-        self._imap = Mock()
-        self.use_uid = True
-        self.folder_encode = True
+        super(TestableIMAPClient, self).__init__('somehost')
 
+    def _create_IMAP4(self):
+        return Mock()