--------------
* interact.py can now read livetest.py INI files (#66)
* Added "debug" propety and setting of a log file (#90)
+* "normalise_times" attribute allows caller to select whether
+ datetimes returned by fetch() are native or not (#96) (Thanks Andrew
+ Scheller)
Small Bug Fixes
---------------
FETCH modifiers patch.
Johannes Heckel
- OAUTH and IDLE support.
+ OAUTH and IDLE support.
+
+Andrew Scheller
+ "normalise_times" support.
Bug Reports
------------
If *ssl* is ``True`` an SSL connection will be made (defaults to
``False``).
+ 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
+ that include timezone information (aware). By default
+ *normalise_times* is True (times are normalised to the local
+ system time). This attribute can be changed between ``fetch()``
+ calls if required.
+
The *debug* property can be used to enable debug logging. It can
be set to an integer from 0 to 5 where 0 disables debug output and
5 enables full output with wire logging and parsing logs. ``True``
self.use_uid = use_uid
self.folder_encode = True
self.log_file = sys.stderr
+ self.normalise_times = True
self._imap = self._create_IMAP4()
self._imap._mesg = self._log # patch in custom debug log method
typ, data = self._imap._command_complete('FETCH', tag)
self._checkok('fetch', typ, data)
typ, data = self._imap._untagged_response(typ, data, 'FETCH')
- return parse_fetch_response(data)
+ return parse_fetch_response(data, self.normalise_times)
def append(self, folder, msg, flags=(), msg_time=None):
"""Append a message to *folder*.
raise ParseError("%s: %s" % (str(err), token))
-def parse_fetch_response(text):
+def parse_fetch_response(text, normalise_times=True):
"""Pull apart IMAP FETCH responses as returned by imaplib.
Returns a dictionary, keyed by message ID. Each value a dictionary
if word == 'UID':
msg_id = _int_or_error(value, 'invalid UID')
elif word == 'INTERNALDATE':
- msg_data[word] = _convert_INTERNALDATE(value)
+ msg_data[word] = _convert_INTERNALDATE(value, normalise_times)
elif word in ('BODY', 'BODYSTRUCTURE'):
msg_data[word] = BodyData.create(value)
else:
return isinstance(self[0], list)
-def _convert_INTERNALDATE(date_string):
+def _convert_INTERNALDATE(date_string, normalise_times=True):
mo = imaplib.InternalDate.match('INTERNALDATE "%s"' % date_string)
if not mo:
raise ValueError("couldn't parse date %r" % date_string)
dt = datetime(year, mon, day, hour, min, sec, 0, tz)
- # Normalise to host system's timezone
- return dt.astimezone(FixedOffset.for_system()).replace(tzinfo=None)
+ if normalise_times:
+ # Normalise to host system's timezone
+ return dt.astimezone(FixedOffset.for_system()).replace(tzinfo=None)
+ return dt
def atom(src, token):
import itertools
import socket
import sys
-import time
from datetime import datetime
from StringIO import StringIO
from imapclient.fixed_offset import FixedOffset
output = log.getvalue()
self.assertIn('one', output)
self.assertIn('two', output)
-
+class TestTimeNormalisation(IMAPClientTest):
+
+ def test_default(self):
+ self.assertTrue(self.client.normalise_times)
+
+ @patch('imapclient.imapclient.parse_fetch_response')
+ def test_pass_through(self, parse_fetch_response):
+ self.client._imap._command_complete.return_value = ('OK', sentinel.data)
+ self.client._imap._untagged_response.return_value = ('OK', sentinel.fetch_data)
+
+ def check(expected):
+ self.client.fetch(22, ['SOMETHING'])
+ parse_fetch_response.assert_called_with(sentinel.fetch_data, expected)
+
+ self.client.normalise_times = True
+ check(True)
+
+ self.client.normalise_times = False
+ check(False)
if __name__ == '__main__':
unittest.main()
'SEQ': 123}})
- def test_INTERNALDATE(self):
+ def test_INTERNALDATE_normalised(self):
def check(date_str, expected_dt):
output = parse_fetch_response(['3 (INTERNALDATE "%s")' % date_str])
- self.assertEquals(output.keys(), [3])
- self.assertEquals(set(output[3].keys()), set(['INTERNALDATE', 'SEQ']))
actual_dt = output[3]['INTERNALDATE']
self.assert_(actual_dt.tzinfo is None) # Returned date should be in local timezone
expected_dt = datetime_to_native(expected_dt)
- self.assert_(actual_dt == expected_dt, '%s != %s' % (actual_dt, expected_dt))
+ self.assertEquals(actual_dt, expected_dt)
check(' 9-Feb-2007 17:08:08 -0430',
datetime(2007, 2, 9, 17, 8, 8, 0, FixedOffset(-4*60 - 30)))
check(' 9-Dec-2007 17:08:08 +0000',
datetime(2007, 12, 9, 17, 8, 8, 0, FixedOffset(0)))
+ def test_INTERNALDATE(self):
+ def check(date_str, expected_dt):
+ output = parse_fetch_response(['3 (INTERNALDATE "%s")' % date_str], normalise_times=False)
+ actual_dt = output[3]['INTERNALDATE']
+ self.assertEquals(actual_dt, expected_dt)
+
+ check(' 9-Feb-2007 17:08:08 -0430',
+ datetime(2007, 2, 9, 17, 8, 8, 0, FixedOffset(-4*60 - 30)))
+
+ check('12-Feb-2007 17:08:08 +0200',
+ datetime(2007, 2, 12, 17, 8, 8, 0, FixedOffset(2*60)))
+
+ check(' 9-Dec-2007 17:08:08 +0000',
+ datetime(2007, 12, 9, 17, 8, 8, 0, FixedOffset(0)))
def test_mixed_types(self):
self.assertEquals(parse_fetch_response([('1 (INTERNALDATE " 9-Feb-2007 17:08:08 +0100" RFC822 {21}',