blob: 515bac90796bc5011814e914622788f7df042c96 [file] [log] [blame]
Barry Warsaw409a4c02002-04-10 21:01:31 +00001# Copyright (C) 2001,2002 Python Software Foundation
Barry Warsawba925802001-09-23 03:17:28 +00002# Author: barry@zope.com (Barry Warsaw)
3
4"""Various types of useful iterators and generators.
5"""
6
7from __future__ import generators
8from cStringIO import StringIO
9from types import StringType
10
11
Barry Warsawe968ead2001-10-04 17:05:11 +000012
Barry Warsawba925802001-09-23 03:17:28 +000013def body_line_iterator(msg):
Barry Warsaw57758e32001-09-26 05:35:47 +000014 """Iterate over the parts, returning string payloads line-by-line."""
Barry Warsawba925802001-09-23 03:17:28 +000015 for subpart in msg.walk():
16 payload = subpart.get_payload()
17 if type(payload) is StringType:
18 for line in StringIO(payload):
19 yield line
20
21
Barry Warsawe968ead2001-10-04 17:05:11 +000022
Barry Warsaw57758e32001-09-26 05:35:47 +000023def typed_subpart_iterator(msg, maintype='text', subtype=None):
24 """Iterate over the subparts with a given MIME type.
Barry Warsawba925802001-09-23 03:17:28 +000025
Barry Warsaw57758e32001-09-26 05:35:47 +000026 Use `maintype' as the main MIME type to match against; this defaults to
27 "text". Optional `subtype' is the MIME subtype to match against; if
Barry Warsawba925802001-09-23 03:17:28 +000028 omitted, only the main type is matched.
29 """
30 for subpart in msg.walk():
Barry Warsaw0164b6b2001-10-15 04:38:22 +000031 if subpart.get_main_type('text') == maintype:
32 if subtype is None or subpart.get_subtype('plain') == subtype:
Barry Warsawba925802001-09-23 03:17:28 +000033 yield subpart