Changeset 226:d5a9a8aa94d5

Show
Ignore:
Timestamp:
31/05/11 13:32:19 (12 months ago)
Author:
Menno Smits <menno@…>
Branch:
default
Message:

Expose debugging functionality (#90)

As part of this, moved IMAP4 creation to a separate method to make
unit testing cleaner and easier.

Files:
4 modified

Legend:

Unmodified
Added
Removed
  • NEWS

    r225 r226  
    2121-------------- 
    2222interact.py can now read livetest.py INI files (#66) 
     23Added "debug" propety and setting of a log file (#90) 
    2324 
    2425Small Bug Fixes 
  • imapclient/imapclient.py

    r223 r226  
    44 
    55import re 
    6 import response_lexer 
    7 from operator import itemgetter 
    86import select 
    97import socket 
     8import sys 
    109import warnings 
     10from datetime import datetime 
     11from operator import itemgetter 
    1112 
    1213import imaplib 
    13 #imaplib.Debug = 5 
     14import response_lexer 
     15 
    1416     
    1517try: 
     
    102104        @param ssl: Make an SSL connection (default is False) 
    103105        """ 
    104         if ssl: 
    105             ImapClass = imaplib.IMAP4_SSL 
    106             default_port = 993 
    107         else: 
    108             ImapClass = imaplib.IMAP4 
    109             default_port = 143 
    110  
    111106        if port is None: 
    112             port = default_port 
    113  
     107            port = ssl and 993 or 143 
     108 
     109        self.host = host 
     110        self.port = port 
     111        self.ssl = ssl 
    114112        self.use_uid = use_uid 
    115         self.ssl = ssl 
    116113        self.folder_encode = True 
    117         self._imap = ImapClass(host, port) 
     114        self.log_file = sys.stderr 
     115 
     116        self._imap = self._create_IMAP4() 
     117        self._imap._mesg = self._log    # patch in custom debug log method 
    118118        self._idle_tag = None 
    119119 
    120     
     120    def _create_IMAP4(self): 
     121        # Create the IMAP instance in a separate method to make unit tests easier 
     122        ImapClass = self.ssl and imaplib.IMAP4_SSL or imaplib.IMAP4 
     123        return ImapClass(self.host, self.port) 
     124 
    121125    def login(self, username, password): 
    122126        """Perform a simple login 
     
    781785        return _quote_arg(name) 
    782786 
     787    def __debug_get(self): 
     788        return self._imap.debug 
     789 
     790    def __debug_set(self, level): 
     791        if level is True: 
     792            level = 4 
     793        elif level is False: 
     794            level = 0 
     795        self._imap.debug = level 
     796 
     797    debug = property(__debug_get, __debug_set, 
     798                     doc="Set debug level. Integers from 0 to 5 or, True and " \ 
     799                         "False can be used. True specifies debug level 4. " \ 
     800                         "Debug output goes to stderr.") 
     801 
     802    def _log(self, text): 
     803        self.log_file.write('%s %s\n' % (datetime.now().strftime('%M:%S.%f'), text)) 
     804        self.log_file.flush() 
     805         
    783806 
    784807def messages_to_str(messages): 
  • imapclient/test/test_IMAPClient.py

    r222 r226  
    55import itertools 
    66import socket 
     7import sys 
    78import time 
    89from datetime import datetime 
     10from StringIO import StringIO 
    911from imapclient.fixed_offset import FixedOffset 
    1012from imapclient.imapclient import datetime_to_imap 
     
    246248        self.assertListEqual([(99, 'EXISTS')], responses) 
    247249 
     250 
     251class TestDebugLogging(IMAPClientTest): 
     252 
     253    def test_default_is_stderr(self): 
     254        self.assertIs(self.client.log_file, sys.stderr) 
     255 
     256    def test_IMAP_is_patched(self): 
     257        log = StringIO() 
     258        self.client.log_file = log 
     259 
     260        self.client._log('one') 
     261        self.client._imap._mesg('two') 
     262 
     263        output = log.getvalue() 
     264        self.assertIn('one', output) 
     265        self.assertIn('two', output) 
     266         
     267 
     268 
    248269if __name__ == '__main__': 
    249270    unittest.main() 
  • imapclient/test/testable_imapclient.py

    r204 r226  
    99 
    1010    def __init__(self): 
    11         self._imap = Mock() 
    12         self.use_uid = True 
    13         self.folder_encode = True 
     11        super(TestableIMAPClient, self).__init__('somehost') 
    1412 
     13    def _create_IMAP4(self): 
     14        return Mock()