]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Cleaned up documentation and tests relating to ENVELOPE changes (#40)
authorMenno Smits <menno@freshfoo.com>
Wed, 7 May 2014 12:04:52 +0000 (00:04 +1200)
committerMenno Smits <menno@freshfoo.com>
Wed, 7 May 2014 12:04:52 +0000 (00:04 +1200)
NEWS.rst
doc/src/index.rst
imapclient/imapclient.py
imapclient/livetest.py
imapclient/response_parser.py
imapclient/response_types.py
imapclient/test/test_response_parser.py

index d4699449efe984c3cd99f7e7cb494383946dd757..1d2b6ef6eec800cd8f8425939682d1ddb3e485cd 100644 (file)
--- 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
index a604c6259027d203cd490ef22de9943b2747ac9a..478ddfdfb187cf30ee3d63756a8f5ad2ebecd519 100644 (file)
@@ -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
index c68584164f63f042c029ea96320fabe6ae06fdca..0e531cd6ddb377896f3a631ee06c294bd833c68c 100644 (file)
@@ -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 <imapclient.response_types.Envelope>` instances.
 
         In addition to an element for each *data* item, the dict
         returned for each message also contains a *SEQ* key containing
index 909dabaff1ea879b7c5e90674a2529beec67f891..32edc703e3d58c00567a9d7ddb7c9c15b8a903c0 100644 (file)
@@ -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)
index a3294213da57d9976568221bcb209eedf63c669d..52db01e9f58d0974a60c06d7bea56b6f00dd8832 100644 (file)
@@ -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
index d8b01b547a04154e2f47f90a093a63f4049361fe..f299c2e9d3626c2cfe32936dd65b99f03df5ae1c 100644 (file)
@@ -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 <mary@foo.com>
 
-        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))
index c8444929b83282e602d8ddcdaa3307c34e1774a5..bca01a826fe79468f27a757c7083edd6c5fddd7b 100644 (file)
@@ -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 "<reply-to-id>" "<msg_id>"))')
+            'NIL "<reply-to-id>" "<msg_id>"))')
 
         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, "<reply-to-id>", "<msg_id>"
+                None, "<reply-to-id>", "<msg_id>"
             )
         )