+++ /dev/null
-# Copyright (C) 2001-2006 Python Software Foundation\r
-# Author: Barry Warsaw\r
-# Contact: email-sig@python.org\r
-\r
-"""Various types of useful iterators and generators."""\r
-\r
-__all__ = [\r
- 'body_line_iterator',\r
- 'typed_subpart_iterator',\r
- 'walk',\r
- # Do not include _structure() since it's part of the debugging API.\r
- ]\r
-\r
-import sys\r
-from cStringIO import StringIO\r
-\r
-\r
-\f\r
-# This function will become a method of the Message class\r
-def walk(self):\r
- """Walk over the message tree, yielding each subpart.\r
-\r
- The walk is performed in depth-first order. This method is a\r
- generator.\r
- """\r
- yield self\r
- if self.is_multipart():\r
- for subpart in self.get_payload():\r
- for subsubpart in subpart.walk():\r
- yield subsubpart\r
-\r
-\r
-\f\r
-# These two functions are imported into the Iterators.py interface module.\r
-def body_line_iterator(msg, decode=False):\r
- """Iterate over the parts, returning string payloads line-by-line.\r
-\r
- Optional decode (default False) is passed through to .get_payload().\r
- """\r
- for subpart in msg.walk():\r
- payload = subpart.get_payload(decode=decode)\r
- if isinstance(payload, basestring):\r
- for line in StringIO(payload):\r
- yield line\r
-\r
-\r
-def typed_subpart_iterator(msg, maintype='text', subtype=None):\r
- """Iterate over the subparts with a given MIME type.\r
-\r
- Use `maintype' as the main MIME type to match against; this defaults to\r
- "text". Optional `subtype' is the MIME subtype to match against; if\r
- omitted, only the main type is matched.\r
- """\r
- for subpart in msg.walk():\r
- if subpart.get_content_maintype() == maintype:\r
- if subtype is None or subpart.get_content_subtype() == subtype:\r
- yield subpart\r
-\r
-\r
-\f\r
-def _structure(msg, fp=None, level=0, include_default=False):\r
- """A handy debugging aid"""\r
- if fp is None:\r
- fp = sys.stdout\r
- tab = ' ' * (level * 4)\r
- print >> fp, tab + msg.get_content_type(),\r
- if include_default:\r
- print >> fp, '[%s]' % msg.get_default_type()\r
- else:\r
- print >> fp\r
- if msg.is_multipart():\r
- for subpart in msg.get_payload():\r
- _structure(subpart, fp, level+1, include_default)\r