email.iterators.rst 2.68 KB
Newer Older
1 2
:mod:`email.iterators`: Iterators
---------------------------------
3 4 5 6 7 8

.. module:: email.iterators
   :synopsis: Iterate over a  message object tree.


Iterating over a message object tree is fairly easy with the
9 10 11
:meth:`Message.walk <email.message.Message.walk>` method.  The
:mod:`email.iterators` module provides some useful higher level iterations over
message object trees.
12 13


14
.. function:: body_line_iterator(msg, decode=False)
15 16 17 18 19

   This iterates over all the payloads in all the subparts of *msg*, returning the
   string payloads line-by-line.  It skips over all the subpart headers, and it
   skips over any subpart with a payload that isn't a Python string.  This is
   somewhat equivalent to reading the flat text representation of the message from
20 21
   a file using :meth:`~io.TextIOBase.readline`, skipping over all the
   intervening headers.
22

23 24
   Optional *decode* is passed through to :meth:`Message.get_payload
   <email.message.Message.get_payload>`.
25 26


27
.. function:: typed_subpart_iterator(msg, maintype='text', subtype=None)
28 29 30 31 32 33 34 35 36 37 38

   This iterates over all the subparts of *msg*, returning only those subparts that
   match the MIME type specified by *maintype* and *subtype*.

   Note that *subtype* is optional; if omitted, then subpart MIME type matching is
   done only with the main type.  *maintype* is optional too; it defaults to
   :mimetype:`text`.

   Thus, by default :func:`typed_subpart_iterator` returns each subpart that has a
   MIME type of :mimetype:`text/\*`.

39

40 41 42
The following function has been added as a useful debugging tool.  It should
*not* be considered part of the supported public interface for the package.

43
.. function:: _structure(msg, fp=None, level=0, include_default=False)
44 45

   Prints an indented representation of the content types of the message object
46 47 48 49 50 51 52 53 54
   structure.  For example:

   .. testsetup::

      >>> import email
      >>> from email.iterators import _structure
      >>> somefile = open('Lib/test/test_email/data/msg_02.txt')

   .. doctest::
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

      >>> msg = email.message_from_file(somefile)
      >>> _structure(msg)
      multipart/mixed
          text/plain
          text/plain
          multipart/digest
              message/rfc822
                  text/plain
              message/rfc822
                  text/plain
              message/rfc822
                  text/plain
              message/rfc822
                  text/plain
              message/rfc822
                  text/plain
          text/plain

74
   .. testsetup::
75 76 77

      >>> somefile.close()

78 79
   Optional *fp* is a file-like object to print the output to.  It must be
   suitable for Python's :func:`print` function.  *level* is used internally.
80
   *include_default*, if true, prints the default type as well.