Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001-2006 Python Software Foundation |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 2 | # Author: Barry Warsaw |
| 3 | # Contact: email-sig@python.org |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 4 | |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 5 | """Various types of useful iterators and generators.""" |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 6 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7 | __all__ = [ |
| 8 | 'body_line_iterator', |
| 9 | 'typed_subpart_iterator', |
| 10 | 'walk', |
| 11 | # Do not include _structure() since it's part of the debugging API. |
| 12 | ] |
| 13 | |
Barry Warsaw | d438574 | 2002-07-19 22:21:47 +0000 | [diff] [blame] | 14 | import sys |
Barry Warsaw | 4c3e33a | 2004-05-09 03:42:37 +0000 | [diff] [blame] | 15 | from cStringIO import StringIO |
Barry Warsaw | 8fa06b5 | 2002-07-09 02:39:07 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | |
Barry Warsaw | 4c3e33a | 2004-05-09 03:42:37 +0000 | [diff] [blame] | 19 | # This function will become a method of the Message class |
| 20 | def walk(self): |
| 21 | """Walk over the message tree, yielding each subpart. |
| 22 | |
| 23 | The walk is performed in depth-first order. This method is a |
| 24 | generator. |
| 25 | """ |
| 26 | yield self |
| 27 | if self.is_multipart(): |
| 28 | for subpart in self.get_payload(): |
| 29 | for subsubpart in subpart.walk(): |
| 30 | yield subsubpart |
| 31 | |
| 32 | |
| 33 | |
| 34 | # These two functions are imported into the Iterators.py interface module. |
Barry Warsaw | 4c3e33a | 2004-05-09 03:42:37 +0000 | [diff] [blame] | 35 | def body_line_iterator(msg, decode=False): |
| 36 | """Iterate over the parts, returning string payloads line-by-line. |
| 37 | |
| 38 | Optional decode (default False) is passed through to .get_payload(). |
| 39 | """ |
| 40 | for subpart in msg.walk(): |
| 41 | payload = subpart.get_payload(decode=decode) |
| 42 | if isinstance(payload, basestring): |
| 43 | for line in StringIO(payload): |
| 44 | yield line |
| 45 | |
| 46 | |
| 47 | def typed_subpart_iterator(msg, maintype='text', subtype=None): |
| 48 | """Iterate over the subparts with a given MIME type. |
| 49 | |
| 50 | Use `maintype' as the main MIME type to match against; this defaults to |
| 51 | "text". Optional `subtype' is the MIME subtype to match against; if |
| 52 | omitted, only the main type is matched. |
| 53 | """ |
| 54 | for subpart in msg.walk(): |
| 55 | if subpart.get_content_maintype() == maintype: |
| 56 | if subtype is None or subpart.get_content_subtype() == subtype: |
| 57 | yield subpart |
| 58 | |
| 59 | |
| 60 | |
| 61 | def _structure(msg, fp=None, level=0, include_default=False): |
Barry Warsaw | 8fa06b5 | 2002-07-09 02:39:07 +0000 | [diff] [blame] | 62 | """A handy debugging aid""" |
Barry Warsaw | d438574 | 2002-07-19 22:21:47 +0000 | [diff] [blame] | 63 | if fp is None: |
| 64 | fp = sys.stdout |
Barry Warsaw | 8fa06b5 | 2002-07-09 02:39:07 +0000 | [diff] [blame] | 65 | tab = ' ' * (level * 4) |
Barry Warsaw | 4c3e33a | 2004-05-09 03:42:37 +0000 | [diff] [blame] | 66 | print >> fp, tab + msg.get_content_type(), |
| 67 | if include_default: |
Barry Warsaw | d49f1d6 | 2004-05-13 20:14:20 +0000 | [diff] [blame] | 68 | print >> fp, '[%s]' % msg.get_default_type() |
Barry Warsaw | 4c3e33a | 2004-05-09 03:42:37 +0000 | [diff] [blame] | 69 | else: |
Barry Warsaw | d49f1d6 | 2004-05-13 20:14:20 +0000 | [diff] [blame] | 70 | print >> fp |
Barry Warsaw | 8fa06b5 | 2002-07-09 02:39:07 +0000 | [diff] [blame] | 71 | if msg.is_multipart(): |
| 72 | for subpart in msg.get_payload(): |
Barry Warsaw | 4c3e33a | 2004-05-09 03:42:37 +0000 | [diff] [blame] | 73 | _structure(subpart, fp, level+1, include_default) |