/Doc/library/email.iterators.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 65 lines · 46 code · 19 blank · 0 comment · 0 complexity · 3d88b42f677ca7b8c7d1e1a80c5c3ff7 MD5 · raw file

  1. :mod:`email`: Iterators
  2. -----------------------
  3. .. module:: email.iterators
  4. :synopsis: Iterate over a message object tree.
  5. Iterating over a message object tree is fairly easy with the
  6. :meth:`Message.walk` method. The :mod:`email.iterators` module provides some
  7. useful higher level iterations over message object trees.
  8. .. function:: body_line_iterator(msg[, decode])
  9. This iterates over all the payloads in all the subparts of *msg*, returning the
  10. string payloads line-by-line. It skips over all the subpart headers, and it
  11. skips over any subpart with a payload that isn't a Python string. This is
  12. somewhat equivalent to reading the flat text representation of the message from
  13. a file using :meth:`readline`, skipping over all the intervening headers.
  14. Optional *decode* is passed through to :meth:`Message.get_payload`.
  15. .. function:: typed_subpart_iterator(msg[, maintype[, subtype]])
  16. This iterates over all the subparts of *msg*, returning only those subparts that
  17. match the MIME type specified by *maintype* and *subtype*.
  18. Note that *subtype* is optional; if omitted, then subpart MIME type matching is
  19. done only with the main type. *maintype* is optional too; it defaults to
  20. :mimetype:`text`.
  21. Thus, by default :func:`typed_subpart_iterator` returns each subpart that has a
  22. MIME type of :mimetype:`text/\*`.
  23. The following function has been added as a useful debugging tool. It should
  24. *not* be considered part of the supported public interface for the package.
  25. .. function:: _structure(msg[, fp[, level]])
  26. Prints an indented representation of the content types of the message object
  27. structure. For example::
  28. >>> msg = email.message_from_file(somefile)
  29. >>> _structure(msg)
  30. multipart/mixed
  31. text/plain
  32. text/plain
  33. multipart/digest
  34. message/rfc822
  35. text/plain
  36. message/rfc822
  37. text/plain
  38. message/rfc822
  39. text/plain
  40. message/rfc822
  41. text/plain
  42. message/rfc822
  43. text/plain
  44. text/plain
  45. Optional *fp* is a file-like object to print the output to. It must be suitable
  46. for Python's extended print statement. *level* is used internally.