Changeset 226:d5a9a8aa94d5
- 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:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r225
|
r226
|
|
| 21 | 21 | -------------- |
| 22 | 22 | interact.py can now read livetest.py INI files (#66) |
| | 23 | Added "debug" propety and setting of a log file (#90) |
| 23 | 24 | |
| 24 | 25 | Small Bug Fixes |
-
|
r223
|
r226
|
|
| 4 | 4 | |
| 5 | 5 | import re |
| 6 | | import response_lexer |
| 7 | | from operator import itemgetter |
| 8 | 6 | import select |
| 9 | 7 | import socket |
| | 8 | import sys |
| 10 | 9 | import warnings |
| | 10 | from datetime import datetime |
| | 11 | from operator import itemgetter |
| 11 | 12 | |
| 12 | 13 | import imaplib |
| 13 | | #imaplib.Debug = 5 |
| | 14 | import response_lexer |
| | 15 | |
| 14 | 16 | |
| 15 | 17 | try: |
| … |
… |
|
| 102 | 104 | @param ssl: Make an SSL connection (default is False) |
| 103 | 105 | """ |
| 104 | | if ssl: |
| 105 | | ImapClass = imaplib.IMAP4_SSL |
| 106 | | default_port = 993 |
| 107 | | else: |
| 108 | | ImapClass = imaplib.IMAP4 |
| 109 | | default_port = 143 |
| 110 | | |
| 111 | 106 | 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 |
| 114 | 112 | self.use_uid = use_uid |
| 115 | | self.ssl = ssl |
| 116 | 113 | 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 |
| 118 | 118 | self._idle_tag = None |
| 119 | 119 | |
| 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 | |
| 121 | 125 | def login(self, username, password): |
| 122 | 126 | """Perform a simple login |
| … |
… |
|
| 781 | 785 | return _quote_arg(name) |
| 782 | 786 | |
| | 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 | |
| 783 | 806 | |
| 784 | 807 | def messages_to_str(messages): |
-
|
r222
|
r226
|
|
| 5 | 5 | import itertools |
| 6 | 6 | import socket |
| | 7 | import sys |
| 7 | 8 | import time |
| 8 | 9 | from datetime import datetime |
| | 10 | from StringIO import StringIO |
| 9 | 11 | from imapclient.fixed_offset import FixedOffset |
| 10 | 12 | from imapclient.imapclient import datetime_to_imap |
| … |
… |
|
| 246 | 248 | self.assertListEqual([(99, 'EXISTS')], responses) |
| 247 | 249 | |
| | 250 | |
| | 251 | class 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 | |
| 248 | 269 | if __name__ == '__main__': |
| 249 | 270 | unittest.main() |
-
|
r204
|
r226
|
|
| 9 | 9 | |
| 10 | 10 | def __init__(self): |
| 11 | | self._imap = Mock() |
| 12 | | self.use_uid = True |
| 13 | | self.folder_encode = True |
| | 11 | super(TestableIMAPClient, self).__init__('somehost') |
| 14 | 12 | |
| | 13 | def _create_IMAP4(self): |
| | 14 | return Mock() |