From: Menno Smits Date: Wed, 7 May 2014 12:04:52 +0000 (+1200) Subject: Cleaned up documentation and tests relating to ENVELOPE changes (#40) X-Git-Tag: 0.11~3^2~7 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=149d8bb755006f93ae425f29a53699be0d13e4c6;p=imapclient.git Cleaned up documentation and tests relating to ENVELOPE changes (#40) --- diff --git a/NEWS.rst b/NEWS.rst index d469944..1d2b6ef 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -8,11 +8,21 @@ The new gmail_search methods allows direct Gmail queries using the X-GM-RAW search extension. Thanks to John Louis del Rosario for the patch. +ENVELOPE FETCH response parsing [NEW, API CHANGE] +------------------------------------------------- +ENVELOPE FETCH responses are now returned as Envelope instances. These +objects are namedtuples providing convenient attribute and positional +based access to envelope fields. The Date field is also now converted +to a datetime instance. + +As part of this change various date and time related utilities were +moved to a new module at imapclient.datetime_util. + SELECT response bug fix ----------------------- -Handle square brackets in flags returned in SELECT -response. Previously these would cause parsing errors. Thanks to -Benjamin Morrise for the bug report. +Handle square brackets in flags returned in SELECT response. +Previously these would cause parsing errors. Thanks to Benjamin +Morrise for the bug report. ======== 0.10.2 diff --git a/doc/src/index.rst b/doc/src/index.rst index a604c62..478ddfd 100644 --- a/doc/src/index.rst +++ b/doc/src/index.rst @@ -147,8 +147,11 @@ The IMAP related exceptions that will be raised by this class are: These are aliases for the imaplib.IMAP4 exceptions of the same name. Socket errors may also be raised in the case of network errors. -IMAPClient Class Reference --------------------------- +API Reference +------------- + +IMAPClient Class +~~~~~~~~~~~~~~~~ The primary class used by the imapclient package is the IMAPClient class. All interaction with a remote IMAP server is performed via an IMAPClient instance. @@ -156,6 +159,15 @@ IMAPClient instance. .. autoclass:: imapclient.IMAPClient :members: +Fetch Response Types +~~~~~~~~~~~~~~~~~~~~ +Various types may be used in the data structures returned by +:py:meth:`.IMAPClient.fetch` when certain response types +are encountered during parsing. + +.. automodule:: imapclient.response_types + :members: + Interactive Sessions -------------------- When developing program using IMAPClient is it sometimes useful to diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index c685841..0e531cd 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -768,7 +768,11 @@ class IMAPClient(object): A dictionary is returned, indexed by message number. Each item in this dictionary is also a dictionary, with an entry - corresponding to each item in *data*. + corresponding to each item in *data*. Returned values will be + appropriately typed. For example, integer values will be returned as + Python integers, timestamps will be returned as datetime + instances and ENVELOPE responses will be returned as + :py:class:`Envelope ` instances. In addition to an element for each *data* item, the dict returned for each message also contains a *SEQ* key containing diff --git a/imapclient/livetest.py b/imapclient/livetest.py index 909daba..32edc70 100644 --- a/imapclient/livetest.py +++ b/imapclient/livetest.py @@ -15,7 +15,9 @@ import time from datetime import datetime from email.utils import make_msgid +from .fixed_offset import FixedOffset from .imapclient import IMAPClient, DELETED, to_unicode +from .response_types import Envelope, Address from .six import binary_type, text_type, PY3 from .test.util import unittest from .config import parse_config_file, create_client_from_config @@ -634,6 +636,7 @@ def createUidTestClass(conf, use_uid): self.client.select_folder(self.base_folder) self.append_msg(msg) + self.client.normalise_times = False fields = ['RFC822', b'FLAGS', 'INTERNALDATE', 'ENVELOPE'] msg_id = self.client.search()[0] @@ -650,14 +653,17 @@ def createUidTestClass(conf, use_uid): self.assertMultiLineEqual(msginfo['RFC822'], msg) self.assertIsInstance(msginfo['INTERNALDATE'], datetime) self.assertIsInstance(msginfo['FLAGS'], tuple) - self.assertTupleEqual(msginfo['ENVELOPE'], - ('Tue, 16 Mar 2010 16:45:32 +0000', - 'A multipart message', - (('Bob Smith', None, 'bob', 'smith.com'),), - (('Bob Smith', None, 'bob', 'smith.com'),), - (('Bob Smith', None, 'bob', 'smith.com'),), - (('Some One', None, 'some', 'one.com'), (None, None, 'foo', 'foo.com')), - None, None, None, msg_id_header)) + + self.assertSequenceEqual(msginfo['ENVELOPE'], + Envelope( + datetime(2010, 3, 16, 16, 45, 32, tzinfo=FixedOffset(0)), + 'A multipart message', + (Address('Bob Smith', None, 'bob', 'smith.com'),), + (Address('Bob Smith', None, 'bob', 'smith.com'),), + (Address('Bob Smith', None, 'bob', 'smith.com'),), + (Address('Some One', None, 'some', 'one.com'), + Address(None, None, 'foo', 'foo.com')), + None, None, None, msg_id_header)) def test_partial_fetch(self): self.client.append(self.base_folder, MULTIPART_MESSAGE) diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index a329421..52db01e 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -16,7 +16,6 @@ from __future__ import unicode_literals import sys from collections import defaultdict from datetime import datetime -from email.utils import parsedate_tz from . import six xrange = six.moves.xrange diff --git a/imapclient/response_types.py b/imapclient/response_types.py index d8b01b5..f299c2e 100644 --- a/imapclient/response_types.py +++ b/imapclient/response_types.py @@ -2,70 +2,50 @@ # Released subject to the New BSD License # Please see http://en.wikipedia.org/wiki/BSD_licenses -""" -Structured representation for IMAP response. -Inspired by Ruby Net::IMAP::Envelope and Net::IMAP::Address -(see http://www.ruby-doc.org/stdlib-2.0.0/libdoc/net/imap/rdoc/Net/IMAP.html). -""" - from collections import namedtuple from email.utils import formataddr -# the following hack to add docstrings to namedtuples is courtesy of: -# http://stackoverflow.com/questions/1606436/adding-docstrings-to-namedtuples-in-python - -class Address(namedtuple("Address", "name route mailbox host")): - """ - Represents electronic mail addresses. - - Fields (in-order): - - name -- Returns the phrase from [RFC-822] mailbox. - - route -- Returns the route from [RFC-822] route-addr. - - mailbox -- None indicates end of [RFC-822] group. If not None and host is None, - returns [RFC-822] group name. Otherwise, returns [RFC-822] local-part. - - host -- None indicates [RFC-822] group syntax. Otherwise, returns [RFC-822] domain name. - - """ - - def __str__(self): - return formataddr((self.name, self.mailbox + '@' + self.host)) class Envelope(namedtuple("Envelope", "date subject from_ sender reply_to to " + "cc bcc in_reply_to message_id")): """ + Represents envelope structures of messages. Returned when parsing + ENVELOPE responses. + + :ivar date: A datetime instance that represents the "Date" header. + :ivar subject: A string that contains the "Subject" header. + :ivar from\_: A tuple of Address objects that represent on or more + addresses from the "From" header, or None if header does not exist. + :ivar sender: As for from\_ but represents the "Sender" header. + :ivar reply_to: As for from\_ but represents the "Reply-To" header. + :ivar to: As for from\_ but represents the "To" header. + :ivar cc: As for from\_ but represents the "Cc" header. + :ivar bcc: As for from\_ but represents the "Bcc" recipients. + :ivar in_reply_to: A string that contains the "In-Reply-To" header. + :ivar message_id: A string that contains the "Message-Id" header. + """ - Represents envelope structures of messages. - - Fields (in-order): - - date -- Returns a string that represents the "Date:" header. - - subject -- Returns a string that represents the "Subject:" header. - - from_ -- Returns a tuple sequence of Address structure that represents "From:" header, - or None if header does not exist. - - sender -- Returns a tuple sequence of Address structure that represents "Sender:" header, - or None if header does not exist. - reply_to -- Returns a tuple sequence of Address structure that represents "Reply_To:" header, - or None if header does not exist. +class Address(namedtuple("Address", "name route mailbox host")): + """ + Represents electronic mail addresses. Used to store addresses in + :py:class:`Envelope`. - to -- Returns a tuple sequence of Address structure that represents "To:" header, - or None if header does not exist. + :ivar name: The address "personal name". + :ivar route: SMTP source route (rarely used). + :ivar mailbox: Mailbox name (what comes just before the @ sign). + :ivar host: The host/domain name. - cc -- Returns a tuple sequence of Address structure that represents "Cc:" header, - or None if header does not exist. + As an example, an address header that looks like:: - bcc -- Returns a tuple sequence of Address structure that represents "Bcc:" header, - or None if header does not exist. + Mary Smith - in_reply_to -- Returns a string that represents the "In-Reply-To:" header. + would be represented as:: - message_id -- Returns a string that represents the "Message-Id:" header. + Address(name=u'Mary Smith', route=None, mailbox=u'mary', host=u'foo.com') + See :rfc:`2822` for more. """ + + def __str__(self): + return formataddr((self.name, self.mailbox + '@' + self.host)) diff --git a/imapclient/test/test_response_parser.py b/imapclient/test/test_response_parser.py index c844492..bca01a8 100644 --- a/imapclient/test/test_response_parser.py +++ b/imapclient/test/test_response_parser.py @@ -297,9 +297,10 @@ class TestParseFetchResponse(unittest.TestCase): '(("name" NIL "address1" "domain1.com")) ' '((NIL NIL "address2" "domain2.com")) ' '(("name" NIL "address3" "domain3.com")) ' + 'NIL' '((NIL NIL "address4" "domain4.com") ' '("person" NIL "address4b" "domain4b.com")) ' - 'NIL NIL "" ""))') + 'NIL "" ""))') output = parse_fetch_response([envelope_str], normalise_times=False) @@ -310,9 +311,10 @@ class TestParseFetchResponse(unittest.TestCase): (Address("name", None, "address1", "domain1.com"),), (Address(None, None, "address2", "domain2.com"),), (Address("name", None, "address3", "domain3.com"),), + None, (Address(None, None, "address4", "domain4.com"), Address("person", None, "address4b", "domain4b.com")), - None, None, "", "" + None, "", "" ) )